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

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