蛋蛋星球-客户端
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.
 
 
 
 
 

162 lines
3.8 KiB

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