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

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