|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- package hdl
-
- import (
- "applet/app/e"
- "applet/app/lib/aes"
- "applet/app/lib/aes/md"
- "applet/app/svc"
- "applet/app/utils"
- "encoding/json"
- "github.com/gin-gonic/gin"
- "io/ioutil"
- "strconv"
- "strings"
- "time"
- )
-
- // Demo
- // @Summary Demo测试
- // @Tags Demo
- // @Description Demo样例测试
- // @Accept json
- // @Produce json
- // @Param req body interface{} true "任意参数"
- // @Success 200 {object} map[string]interface{} "返回任意参数"
- // @Failure 400 {object} md.Response "具体错误"
- // @Router /api/v1/test [GET]
- func Demo(c *gin.Context) {
- var args interface{}
- if c.Request.Method == "GET" {
- args = c.Request.URL.Query()
- } else {
- err := c.ShouldBindJSON(&args)
- if err != nil {
- err = svc.HandleValidateErr(err)
- err1 := err.(e.E)
- e.OutErr(c, err1.Code, err1.Error())
- return
- }
- }
-
- e.OutSuc(c, map[string]interface{}{
- "args": args,
- }, nil)
- return
- }
-
- func TestCreateSign(c *gin.Context) {
- var args interface{}
- if c.Request.Method == "GET" {
- args = c.Request.URL.Query()
- } else {
- err := c.ShouldBindJSON(&args)
- if err != nil {
- err = svc.HandleValidateErr(err)
- err1 := err.(e.E)
- e.OutErr(c, err1.Code, err1.Error())
- return
- }
- }
-
- e.OutSuc(c, map[string]interface{}{
- "args": args,
- }, nil)
- return
- }
-
- func CreateSign(c *gin.Context) {
- var query = map[string]string{}
- //1、从请求头中获取必传参数
- query["timestamp"] = c.GetHeader("timestamp")
- query["nonce"] = c.GetHeader("nonce")
- if query["timestamp"] == "" || query["nonce"] == "" {
- e.OutErr(c, e.ERR, "timestamp || nonce 不能为空 ")
- return
- }
- if len(query["nonce"]) != 32 {
- e.OutErr(c, e.ERR, "随机字符串有误 ")
- return
- }
- currentTimestamp := time.Now().Unix()
- storedTimestamp, err := strconv.ParseInt(query["timestamp"], 10, 64)
- if err != nil {
- e.OutErr(c, e.ERR, err.Error())
- return
- }
- if currentTimestamp-storedTimestamp > 300 { // 5分钟
- e.OutErr(c, e.ERR, "时效性过期 ")
- return
- }
-
- //2、判断请求方式,以获取请求参数
- var aesStr string
- if c.Request.Method == "GET" {
- queryParams := c.Request.URL.Query()
- for key, values := range queryParams {
- if len(values) > 0 {
- query[key] = values[0]
- }
- }
- } else {
- body, _ := ioutil.ReadAll(c.Request.Body)
- if string(body) != "" {
- aesStr = aes.AesEncryptByECB(md.AesKey, string(body))
-
- var bodyParams = map[string]interface{}{}
- err = json.Unmarshal(body, &bodyParams)
- if err != nil {
- e.OutErr(c, e.ERR, err.Error())
- return
- }
- for key, value := range bodyParams {
- // 使用类型断言判断是否为 string 类型
- if _, ok := value.(map[string]interface{}); ok {
- query[key] = utils.SerializeStr(value)
- } else {
- query[key] = utils.AnyToString(value)
- }
- }
- }
- }
-
- //3.query参数按照 ASCII 码从小到大排序
- str := utils.JoinStringsInASCII(query, "&", false, false, "")
-
- //4.md5加密 转小写
- sign := strings.ToLower(utils.Md5(str))
-
- e.OutSuc(c, map[string]interface{}{
- "aes": aesStr,
- "sign_str": str,
- "sign": sign,
- }, nil)
- return
- }
-
- func AesDecryptByECB(c *gin.Context) {
- body, _ := ioutil.ReadAll(c.Request.Body)
- strs, err := aes.AesDecryptByECB(md.AesKey, string(body))
- if err != nil {
- e.OutErr(c, e.ERR, err.Error())
- return
- }
-
- e.OutSuc(c, strs, nil)
- return
- }
|