蛋蛋星球-客户端
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 

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