Browse Source

add: 小程序消息解密

hsun 3 years ago
parent
commit
45974e3279
3 changed files with 55 additions and 0 deletions
  1. 45 0
      controller/wechat/wechat.go
  2. 1 0
      routers/wechat.go
  3. 9 0
      services/wx_app/wx_app.go

+ 45 - 0
controller/wechat/wechat.go

@@ -65,3 +65,48 @@ func Login(c *gin.Context) {
 		Authorization: token,
 		Authorization: token,
 	}, c)
 	}, c)
 }
 }
+
+// 消息解密请求参数
+type EncryptReq struct {
+	EncryptedData	string
+	Iv				string
+}
+
+// GetEncryptInfo 消息解密
+// @Tags 微信相关接口
+// @Summary  消息解密
+// @Description 消息解密
+// @Accept  json
+// @Product json
+// @Param encryptedData query string true "加密数据"
+// @Param iv query string true "加密算法初始向量"
+// @Success 200 {string} string "获取成功"
+// @Router /wechat/getEncryptInfo [post]
+func GetEncryptInfo(c *gin.Context)  {
+	var req EncryptReq
+	err := c.ShouldBind(&req)
+	if err != nil {
+		response.Fail("参数异常", c)
+		return
+	}
+	if req.EncryptedData == "" {
+		response.Fail("参数异常:encryptedData", c)
+		return
+	}
+	if req.Iv == "" {
+		response.Fail("参数异常:iv", c)
+		return
+	}
+	// 获取登录用户信息
+	userInfo := user.GetInfoByClaims(c)
+	if userInfo.SessionKey == "" {
+		response.Fail("请重新登录", c)
+		return
+	}
+	decryptData, err := wx_app.GetDecryptInfo(userInfo.SessionKey, req.EncryptedData, req.Iv)
+	if err != nil {
+		response.Fail("获取失败,Err:" + err.Error(), c)
+		return
+	}
+	response.OkData("获取成功", decryptData, c)
+}

+ 1 - 0
routers/wechat.go

@@ -13,6 +13,7 @@ func InitWechat(r *gin.Engine) {
 		rGroup.GET("/userInfo", wechat.GetUserInfo)
 		rGroup.GET("/userInfo", wechat.GetUserInfo)
 		rGroup.GET("/session", wechat.GetUserSession)
 		rGroup.GET("/session", wechat.GetUserSession)
 		rGroup.GET("/login", wechat.Login)
 		rGroup.GET("/login", wechat.Login)
+		rGroup.GET("/getEncryptInfo", wechat.GetEncryptInfo)
 
 
 	}
 	}
 }
 }

+ 9 - 0
services/wx_app/wx_app.go

@@ -7,6 +7,7 @@ import (
 	"github.com/silenceper/wechat/v2/miniprogram"
 	"github.com/silenceper/wechat/v2/miniprogram"
 	"github.com/silenceper/wechat/v2/miniprogram/auth"
 	"github.com/silenceper/wechat/v2/miniprogram/auth"
 	"github.com/silenceper/wechat/v2/miniprogram/config"
 	"github.com/silenceper/wechat/v2/miniprogram/config"
+	"github.com/silenceper/wechat/v2/miniprogram/encryptor"
 )
 )
 
 
 //微信小程序配置信息
 //微信小程序配置信息
@@ -55,6 +56,14 @@ func GetUserInfo(code string) (userInfo auth.ResCode2Session, err error) {
 	return
 	return
 }
 }
 
 
+// 获取解密信息 GetDecryptInfo
+func GetDecryptInfo(sessionKey, encryptedData, iv string) (decryptData *encryptor.PlainData, err error) {
+	wechatClient := GetWxApp()
+	encryptorClient := wechatClient.GetEncryptor()
+	decryptData, err = encryptorClient.Decrypt(sessionKey, encryptedData, iv)
+	return
+}
+
 //func GetCode() {
 //func GetCode() {
 //	codeParms := qrcode.QRCoder{
 //	codeParms := qrcode.QRCoder{
 //		Page:      "",
 //		Page:      "",