|
- 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{} "token"
- // @Failure 400 {object} md.Response "具体错误"
- // @Router /api/demo [post]
- 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]string{}
- err = json.Unmarshal(body, &bodyParams)
- if err != nil {
- e.OutErr(c, e.ERR, err.Error())
- return
- }
- for key, value := range bodyParams {
- query[key] = 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
- }
|