util.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package util
  2. import (
  3. "app.yhyue.com/moapp/jybase/common"
  4. "app.yhyue.com/moapp/jybase/encrypt"
  5. "encoding/base64"
  6. "github.com/shopspring/decimal"
  7. "go.mongodb.org/mongo-driver/bson/primitive"
  8. "net/http"
  9. "reflect"
  10. "strconv"
  11. )
  12. type SimpleEncrypt struct {
  13. Key string //加解密用到的key(加密key索引)+
  14. }
  15. var SEPreview = &SimpleEncrypt{Key: "topnet2015topnet2015"}
  16. var SE2 = encrypt.SimpleEncrypt{Key: "topJYBX2019"}
  17. // 数据类型转换
  18. func GetPostForm(r *http.Request) map[string]interface{} {
  19. val := map[string]interface{}{}
  20. for k, _ := range r.Form {
  21. // if len(k) < 2 {
  22. // continue
  23. // }
  24. if k != "_id" {
  25. v := r.FormValue(k)
  26. switch k[:2] {
  27. case "s_": //string型
  28. val[k] = v
  29. case "l_": //int64位
  30. val[k], _ = strconv.ParseInt(v, 10, 64)
  31. case "i_": //int型
  32. val[k], _ = strconv.Atoi(v)
  33. default:
  34. if v == "true" || v == "false" {
  35. b, _ := strconv.ParseBool(v)
  36. val[k] = b
  37. } else {
  38. val[k] = v
  39. }
  40. }
  41. }
  42. }
  43. return val
  44. }
  45. // 从数组中删除元素
  46. func deleteSlice(arr []string, v string) []string {
  47. for k, v1 := range arr {
  48. if v1 == v {
  49. return append(arr[:k], arr[k+1:]...)
  50. }
  51. }
  52. return arr
  53. }
  54. /**
  55. * 前端科学计数法处理
  56. */
  57. func FormatNumber(tmp map[string]interface{}) {
  58. for k, v := range tmp {
  59. if reflect.TypeOf(v).Name() == reflect.Float64.String() {
  60. num, _ := decimal.NewFromString(strconv.FormatFloat(common.Float64All(v), 'e', -1, 64))
  61. tmp[k], _ = num.Float64()
  62. }
  63. }
  64. }
  65. // 获取最新年报
  66. func Sort_year_report(year_report primitive.A) map[string]interface{} {
  67. new_year_report := year_report[0]
  68. for i := 0; i < len(year_report); i++ {
  69. if common.IntAll(new_year_report.(map[string]interface{})["report_year"]) < common.IntAll(year_report[i].(map[string]interface{})["report_year"]) {
  70. new_year_report = year_report[i]
  71. }
  72. }
  73. return new_year_report.(map[string]interface{})
  74. }
  75. // 解密String
  76. func (s *SimpleEncrypt) DecodeString(str string) string {
  77. data, _ := base64.URLEncoding.DecodeString(str)
  78. s.doEncode(data)
  79. return string(data)
  80. }
  81. // 加密String
  82. func (s *SimpleEncrypt) EncodeString(str string) string {
  83. data := []byte(str)
  84. s.doEncode(data)
  85. return base64.URLEncoding.EncodeToString(data)
  86. }
  87. func (s *SimpleEncrypt) doEncode(bs []byte) {
  88. tmp := []byte(s.Key)
  89. THEFOR:
  90. for i := 0; i < len(bs); {
  91. for j := 0; j < len(tmp); j, i = j+1, i+1 {
  92. if i >= len(bs) {
  93. break THEFOR
  94. }
  95. bs[i] = bs[i] ^ tmp[j]
  96. }
  97. }
  98. }