蛋蛋星球-客户端
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 

147 satır
3.3 KiB

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