蛋蛋星球-客户端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

160 lines
3.7 KiB

  1. package hdl
  2. import (
  3. "applet/app/e"
  4. "applet/app/lib/aes"
  5. "applet/app/lib/aes/md"
  6. md2 "applet/app/md"
  7. "applet/app/svc"
  8. "applet/app/utils"
  9. "code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git/rabbit"
  10. "encoding/json"
  11. "github.com/gin-gonic/gin"
  12. "io/ioutil"
  13. "strconv"
  14. "strings"
  15. "time"
  16. )
  17. // Demo
  18. // @Summary Demo测试
  19. // @Tags Demo
  20. // @Description Demo样例测试
  21. // @Accept json
  22. // @Produce json
  23. // @Param req body interface{} true "任意参数"
  24. // @Success 200 {object} map[string]interface{} "返回任意参数"
  25. // @Failure 400 {object} md.Response "具体错误"
  26. // @Router /api/v1/test [GET]
  27. func Demo(c *gin.Context) {
  28. var args interface{}
  29. if c.Request.Method == "GET" {
  30. args = c.Request.URL.Query()
  31. } else {
  32. err := c.ShouldBindJSON(&args)
  33. if err != nil {
  34. err = svc.HandleValidateErr(err)
  35. err1 := err.(e.E)
  36. e.OutErr(c, err1.Code, err1.Error())
  37. return
  38. }
  39. }
  40. e.OutSuc(c, map[string]interface{}{
  41. "args": args,
  42. }, nil)
  43. return
  44. }
  45. func Demo1(c *gin.Context) {
  46. ch, err := rabbit.Cfg.Pool.GetChannel()
  47. if err == nil {
  48. defer ch.Release()
  49. }
  50. arg := md2.AdvertisingWatch{Id: "3"}
  51. err = ch.PublishV2(md2.EggAdvertisingQueueExchange, arg, md2.EggAdvertisingSmash)
  52. if err != nil {
  53. ch.PublishV2(md2.EggAdvertisingQueueExchange, arg, md2.EggAdvertisingSmash)
  54. }
  55. }
  56. func TestCreateSign(c *gin.Context) {
  57. var args interface{}
  58. if c.Request.Method == "GET" {
  59. args = c.Request.URL.Query()
  60. } else {
  61. err := c.ShouldBindJSON(&args)
  62. if err != nil {
  63. err = svc.HandleValidateErr(err)
  64. err1 := err.(e.E)
  65. e.OutErr(c, err1.Code, err1.Error())
  66. return
  67. }
  68. }
  69. e.OutSuc(c, map[string]interface{}{
  70. "args": args,
  71. }, nil)
  72. return
  73. }
  74. func CreateSign(c *gin.Context) {
  75. var query = map[string]string{}
  76. //1、从请求头中获取必传参数
  77. query["timestamp"] = c.GetHeader("timestamp")
  78. query["nonce"] = c.GetHeader("nonce")
  79. if query["timestamp"] == "" || query["nonce"] == "" {
  80. e.OutErr(c, e.ERR, "timestamp || nonce 不能为空 ")
  81. return
  82. }
  83. if len(query["nonce"]) != 32 {
  84. e.OutErr(c, e.ERR, "随机字符串有误 ")
  85. return
  86. }
  87. currentTimestamp := time.Now().Unix()
  88. storedTimestamp, err := strconv.ParseInt(query["timestamp"], 10, 64)
  89. if err != nil {
  90. e.OutErr(c, e.ERR, err.Error())
  91. return
  92. }
  93. if currentTimestamp-storedTimestamp > 300 { // 5分钟
  94. e.OutErr(c, e.ERR, "时效性过期 ")
  95. return
  96. }
  97. //2、判断请求方式,以获取请求参数
  98. var aesStr string
  99. if c.Request.Method == "GET" {
  100. queryParams := c.Request.URL.Query()
  101. for key, values := range queryParams {
  102. if len(values) > 0 {
  103. query[key] = values[0]
  104. }
  105. }
  106. } else {
  107. body, _ := ioutil.ReadAll(c.Request.Body)
  108. if string(body) != "" {
  109. aesStr = aes.AesEncryptByECB(md.AesKey, string(body))
  110. var bodyParams = map[string]interface{}{}
  111. err = json.Unmarshal(body, &bodyParams)
  112. if err != nil {
  113. e.OutErr(c, e.ERR, err.Error())
  114. return
  115. }
  116. for key, value := range bodyParams {
  117. // 使用类型断言判断是否为 string 类型
  118. if _, ok := value.(map[string]interface{}); ok {
  119. query[key] = utils.SerializeStr(value)
  120. } else {
  121. query[key] = utils.AnyToString(value)
  122. }
  123. }
  124. }
  125. }
  126. //3.query参数按照 ASCII 码从小到大排序
  127. str := utils.JoinStringsInASCII(query, "&", false, false, "")
  128. //4.md5加密 转小写
  129. sign := strings.ToLower(utils.Md5(str))
  130. e.OutSuc(c, map[string]interface{}{
  131. "aes": aesStr,
  132. "sign_str": str,
  133. "sign": sign,
  134. }, nil)
  135. return
  136. }
  137. func AesDecryptByECB(c *gin.Context) {
  138. body, _ := ioutil.ReadAll(c.Request.Body)
  139. strs, err := aes.AesDecryptByECB(md.AesKey, string(body))
  140. if err != nil {
  141. e.OutErr(c, e.ERR, err.Error())
  142. return
  143. }
  144. e.OutSuc(c, strs, nil)
  145. return
  146. }