123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- package util
- import (
- "app.yhyue.com/moapp/jybase/common"
- "app.yhyue.com/moapp/jybase/encrypt"
- "encoding/base64"
- "github.com/shopspring/decimal"
- "go.mongodb.org/mongo-driver/bson/primitive"
- "net/http"
- "reflect"
- "strconv"
- )
- type SimpleEncrypt struct {
- Key string //加解密用到的key(加密key索引)+
- }
- var SEPreview = &SimpleEncrypt{Key: "topnet2015topnet2015"}
- var SE2 = encrypt.SimpleEncrypt{Key: "topJYBX2019"}
- // 数据类型转换
- func GetPostForm(r *http.Request) map[string]interface{} {
- val := map[string]interface{}{}
- for k, _ := range r.Form {
- // if len(k) < 2 {
- // continue
- // }
- if k != "_id" {
- v := r.FormValue(k)
- switch k[:2] {
- case "s_": //string型
- val[k] = v
- case "l_": //int64位
- val[k], _ = strconv.ParseInt(v, 10, 64)
- case "i_": //int型
- val[k], _ = strconv.Atoi(v)
- default:
- if v == "true" || v == "false" {
- b, _ := strconv.ParseBool(v)
- val[k] = b
- } else {
- val[k] = v
- }
- }
- }
- }
- return val
- }
- // 从数组中删除元素
- func deleteSlice(arr []string, v string) []string {
- for k, v1 := range arr {
- if v1 == v {
- return append(arr[:k], arr[k+1:]...)
- }
- }
- return arr
- }
- /**
- * 前端科学计数法处理
- */
- func FormatNumber(tmp map[string]interface{}) {
- for k, v := range tmp {
- if reflect.TypeOf(v).Name() == reflect.Float64.String() {
- num, _ := decimal.NewFromString(strconv.FormatFloat(common.Float64All(v), 'e', -1, 64))
- tmp[k], _ = num.Float64()
- }
- }
- }
- // 获取最新年报
- func Sort_year_report(year_report primitive.A) map[string]interface{} {
- new_year_report := year_report[0]
- for i := 0; i < len(year_report); i++ {
- if common.IntAll(new_year_report.(map[string]interface{})["report_year"]) < common.IntAll(year_report[i].(map[string]interface{})["report_year"]) {
- new_year_report = year_report[i]
- }
- }
- return new_year_report.(map[string]interface{})
- }
- // 解密String
- func (s *SimpleEncrypt) DecodeString(str string) string {
- data, _ := base64.URLEncoding.DecodeString(str)
- s.doEncode(data)
- return string(data)
- }
- // 加密String
- func (s *SimpleEncrypt) EncodeString(str string) string {
- data := []byte(str)
- s.doEncode(data)
- return base64.URLEncoding.EncodeToString(data)
- }
- func (s *SimpleEncrypt) doEncode(bs []byte) {
- tmp := []byte(s.Key)
- THEFOR:
- for i := 0; i < len(bs); {
- for j := 0; j < len(tmp); j, i = j+1, i+1 {
- if i >= len(bs) {
- break THEFOR
- }
- bs[i] = bs[i] ^ tmp[j]
- }
- }
- }
|