globalFunc.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. package lib
  2. import (
  3. "jjl-tools/tinodeService/store/types"
  4. "encoding/binary"
  5. "encoding/json"
  6. "fmt"
  7. "golang.org/x/crypto/xtea"
  8. "io/ioutil"
  9. "net/http"
  10. "reflect"
  11. "runtime"
  12. "strconv"
  13. "strings"
  14. "unsafe"
  15. )
  16. func SizeStruct(data interface{}) int {
  17. return sizeof(reflect.ValueOf(data))
  18. }
  19. //uid加密函数 -- 客户端使用,服务端处理后返回给客户端的uid需要做加密处理
  20. func GencodeUid(val int64) string {
  21. var src = make([]byte, 8)
  22. var dst = make([]byte, 8)
  23. binary.LittleEndian.PutUint64(src, uint64(val))
  24. var cipher *xtea.Cipher
  25. cipher, _ = xtea.NewCipher(getUidKey())
  26. cipher.Encrypt(dst, src)
  27. user := types.Uid(binary.LittleEndian.Uint64(dst))
  28. return user.UserId()
  29. }
  30. //字符串 622919054444728320 转成usr这种
  31. func UidSEncodeNoErr(uid string) string {
  32. if uid == "" {
  33. return ""
  34. }
  35. if strings.Contains(uid, "usr") {
  36. return uid
  37. }
  38. uidI, err := strconv.ParseInt(uid, 10, 64)
  39. if err != nil {
  40. //logger.Error(gsfn, "fatal err", err, "uid", uid)
  41. return ""
  42. }
  43. return encodeUid(uidI)
  44. }
  45. //uid加密函数 -- 客户端使用,服务端处理后返回给客户端的uid需要做加密处理
  46. func encodeUid(val int64) string {
  47. var src = make([]byte, 8)
  48. var dst = make([]byte, 8)
  49. binary.LittleEndian.PutUint64(src, uint64(val))
  50. var cipher *xtea.Cipher
  51. cipher, _ = xtea.NewCipher(getUidKey())
  52. cipher.Encrypt(dst, src)
  53. user := types.Uid(binary.LittleEndian.Uint64(dst))
  54. return user.UserId()
  55. }
  56. func sizeof(v reflect.Value) int {
  57. switch v.Kind() {
  58. case reflect.Map:
  59. sum := 0
  60. keys := v.MapKeys()
  61. for i := 0; i < len(keys); i++ {
  62. mapkey := keys[i]
  63. s := sizeof(mapkey)
  64. if s < 0 {
  65. return -1
  66. }
  67. sum += s
  68. s = sizeof(v.MapIndex(mapkey))
  69. if s < 0 {
  70. return -1
  71. }
  72. sum += s
  73. }
  74. return sum
  75. case reflect.Slice, reflect.Array:
  76. sum := 0
  77. for i, n := 0, v.Len(); i < n; i++ {
  78. s := sizeof(v.Index(i))
  79. if s < 0 {
  80. return -1
  81. }
  82. sum += s
  83. }
  84. return sum
  85. case reflect.String:
  86. sum := 0
  87. for i, n := 0, v.Len(); i < n; i++ {
  88. s := sizeof(v.Index(i))
  89. if s < 0 {
  90. return -1
  91. }
  92. sum += s
  93. }
  94. return sum
  95. case reflect.Ptr, reflect.Interface:
  96. p := (*[]byte)(unsafe.Pointer(v.Pointer()))
  97. if p == nil {
  98. return 0
  99. }
  100. return sizeof(v.Elem())
  101. case reflect.Struct:
  102. sum := 0
  103. for i, n := 0, v.NumField(); i < n; i++ {
  104. s := sizeof(v.Field(i))
  105. if s < 0 {
  106. return -1
  107. }
  108. sum += s
  109. }
  110. return sum
  111. case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
  112. reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
  113. reflect.Float32, reflect.Float64, reflect.Complex64, reflect.Complex128,
  114. reflect.Int:
  115. return int(v.Type().Size())
  116. default:
  117. //fmt.Println("t.Kind() no found:", v.Kind())
  118. }
  119. return -1
  120. }
  121. /*
  122. * 返回函数名
  123. */
  124. func GetSysFuncName() string {
  125. pc, _, line, _ := runtime.Caller(1)
  126. f := runtime.FuncForPC(pc)
  127. res := "line:" + strconv.Itoa(line) + "||funcName:" + f.Name() + "||error:"
  128. return res
  129. }
  130. // type Uid uint64
  131. type testJose struct {
  132. UserKey string `json:"uid_key"`
  133. }
  134. type configType struct {
  135. // 16-byte key for XTEA. Used to initialize types.UidGenerator.
  136. UidKey []byte `json:"uid_key"`
  137. }
  138. func getUidKey() []byte {
  139. test := testJose{UserKey: "la6YsO+bNX/+XIkOqc5Svw=="}
  140. jsonStr, err := json.Marshal(test)
  141. var config configType
  142. err = json.Unmarshal([]byte(jsonStr), &config)
  143. if err != nil {
  144. fmt.Println(err)
  145. }
  146. return config.UidKey
  147. }
  148. type AccessTokenResp struct {
  149. Code int `json:"code"`
  150. Errmsg string `json:"errmsg"`
  151. AccessToken string `json:"access_token"`
  152. ExpiresIn string `json:"expires_in"`
  153. }
  154. func RequestOnlineMap(url string) map[string]interface{} {
  155. client := http.Client{}
  156. req, err := http.NewRequest(http.MethodGet, url, nil)
  157. if err != nil {
  158. fmt.Println("err 320 line: ", err)
  159. }
  160. // 添加请求头
  161. req.Header.Add("Content-type", "application/json;charset=utf-8")
  162. // 发送请求
  163. resp, err := client.Do(req)
  164. if err != nil {
  165. fmt.Println("err 327 line: ", err)
  166. }
  167. defer resp.Body.Close()
  168. b, err := ioutil.ReadAll(resp.Body)
  169. if err != nil {
  170. fmt.Println("err 332 line: ", err)
  171. }
  172. //fmt.Println(string(b))
  173. resMap := make(map[string]interface{})
  174. err = json.Unmarshal(b, &resMap)
  175. return resMap
  176. }
  177. func Interface2Int(val interface{}) int {
  178. var res int
  179. res = 0
  180. switch val.(type) {
  181. case string:
  182. res, _ = strconv.Atoi(val.(string))
  183. break
  184. case int:
  185. res = val.(int)
  186. break
  187. case int64:
  188. tmp := strconv.FormatInt(val.(int64), 10)
  189. res, _ = strconv.Atoi(tmp)
  190. break
  191. case uint16:
  192. res = int(val.(uint16))
  193. break
  194. }
  195. return res
  196. }
  197. func Interface2String(val interface{}) string {
  198. var res string
  199. res = ""
  200. switch val.(type) {
  201. case int:
  202. res = strconv.Itoa(val.(int))
  203. break
  204. case string:
  205. res = val.(string)
  206. break
  207. case int64:
  208. res = strconv.FormatInt(val.(int64), 10)
  209. break
  210. }
  211. return res
  212. }