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

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