蛋蛋星球-客户端
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 

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