|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- package hdl
-
- import (
- "applet/app/e"
- "applet/app/lib/wechat"
- "applet/app/md"
- "applet/app/utils"
- "applet/app/utils/cache"
- db "code.fnuoos.com/zhimeng/model.git/src"
- "code.fnuoos.com/zhimeng/model.git/src/super/implement"
- "encoding/xml"
- "fmt"
- "github.com/gin-gonic/gin"
- "io/ioutil"
- "net/http"
- "net/url"
- )
-
- type OriginalWxMessage struct {
- AppID string `xml:"AppId"`
- Encrypt string `xml:"Encrypt"`
- }
-
- func SetTicket(c *gin.Context) {
- query := c.Request.URL.Query()
- var params = map[string]string{}
- for key, value := range query {
- fmt.Printf("Key: %s, Value: %s\n", key, value[0])
- params[key] = value[0]
- }
-
- utils.FilePutContents("SetTicket_Get", utils.SerializeStr(params))
-
- var originalWxMessage OriginalWxMessage
- // 读取请求体
- body, err := ioutil.ReadAll(c.Request.Body)
- if err != nil {
- c.JSON(http.StatusBadRequest, gin.H{"error": "failed to read request body"})
- return
- }
-
- utils.FilePutContents("SetTicket_Post", string(body))
- err = xml.Unmarshal(body, &originalWxMessage)
- if err != nil {
- fmt.Println("setTicket>>>>>>>>", err.Error())
- c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
- return
- }
-
- //1、查找对应 wx_open_third_party_app_list 记录
- wxOpenThirdPartyAppListDb := implement.NewWxOpenThirdPartyAppListDb(db.Db)
- wxOpenThirdPartyAppList, err := wxOpenThirdPartyAppListDb.GetWxOpenThirdPartyAppListByAppId(originalWxMessage.AppID)
- if err != nil {
- c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
- return
- }
- if wxOpenThirdPartyAppList == nil {
- c.JSON(http.StatusBadRequest, gin.H{"error": "未查询到对应App记录"})
- return
- }
-
- //2、对消息体进行解密
- instance := wechat.NewWechatMsgCrypt(wxOpenThirdPartyAppList.Token, wxOpenThirdPartyAppList.AesKey, wxOpenThirdPartyAppList.Appid)
- eventRequest := wechat.EventEncryptRequest{
- XMLName: xml.Name{},
- Encrypt: originalWxMessage.Encrypt,
- Appid: originalWxMessage.AppID,
- }
- reqWxMessage := instance.WechatEventDecrypt(eventRequest, params["msg_signature"], params["timestamp"], params["nonce"])
- fmt.Println("解密结果:", reqWxMessage)
- utils.FilePutContents("SetTicket_XML", utils.SerializeStr(reqWxMessage))
- if reqWxMessage.InfoType == "component_verify_ticket" { //TODO::微信公众平台 验证票据
- cacheKey := fmt.Sprintf(md.MasterComponentVerifyTicket, utils.AnyToString(wxOpenThirdPartyAppList.Uuid))
- cacheComponentVerifyTicket, _ := cache.GetString(cacheKey)
- if cacheComponentVerifyTicket == "" || cacheComponentVerifyTicket != reqWxMessage.ComponentVerifyTicket {
- cache.SetEx(cacheKey, reqWxMessage.ComponentVerifyTicket, 43140)
- wxOpenThirdPartyAppList.ComponentVerifyTicket = reqWxMessage.ComponentVerifyTicket
- _, err = wxOpenThirdPartyAppListDb.UpdateWxOpenThirdPartyAppList(wxOpenThirdPartyAppList, "component_verify_ticket")
- if err != nil {
- c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
- return
- }
- }
- }
- if reqWxMessage.InfoType == "unauthorized" { //TODO::微信公众平台 取消授权
- appid := reqWxMessage.AuthorizerAppid
- userWxAppletListDb := implement.NewUserWxAppletListDb(db.Db)
- userWxAppletList, err := userWxAppletListDb.GetUserWxAppletListByAppId(appid)
- if err != nil {
- c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
- return
- }
- if userWxAppletList == nil {
- c.JSON(http.StatusBadRequest, gin.H{"error": "未查询到对应小程序授权记录"})
- return
- }
- userWxAppletList.IsAuth = 0
- _, err = userWxAppletListDb.UpdateUserWxAppletList(userWxAppletList, "is_auth")
- if err != nil {
- c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
- return
- }
- }
- if reqWxMessage.InfoType == "unauthorized" || reqWxMessage.InfoType == "authorized" { //TODO::微信公众平台 授权 || 更新授权
- appid := reqWxMessage.AuthorizerAppid
- userWxAppletListDb := implement.NewUserWxAppletListDb(db.Db)
- userWxAppletList, err := userWxAppletListDb.GetUserWxAppletListByAppId(appid)
- if err != nil {
- c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
- return
- }
- if userWxAppletList == nil {
- c.JSON(http.StatusBadRequest, gin.H{"error": "未查询到对应小程序授权记录"})
- return
- }
-
- userWxAppletList.IsAuth = 1
- _, err = userWxAppletListDb.UpdateUserWxAppletList(userWxAppletList, "is_auth")
- if err != nil {
- c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
- return
- }
- }
-
- c.String(http.StatusOK, "success")
- return
- }
-
- func GetPreAuthCode(c *gin.Context) {
- masterId := c.DefaultQuery("master_id", "")
- wxOpenThirdPartyAppListDb := implement.NewWxOpenThirdPartyAppListDb(db.Db)
- wxOpenThirdPartyAppList, err := wxOpenThirdPartyAppListDb.GetWxOpenThirdPartyAppList(utils.StrToInt(masterId))
- if err != nil {
- return
- }
- if wxOpenThirdPartyAppList == nil {
- e.OutErr(c, e.ERR_NOT_FAN, "未查询到对应三方应用记录")
- return
- }
- wxApiService, err := wechat.NewWxApiService(masterId, wxOpenThirdPartyAppList.Appid, wxOpenThirdPartyAppList.AppSecret)
- if err != nil {
- e.OutErr(c, e.ERR, err.Error())
- return
- }
- preAuthCode, err := wxApiService.GetPreAuthCode()
- if err != nil {
- e.OutErr(c, e.ERR, err.Error())
- return
- }
-
- var w http.ResponseWriter
- w.Header().Set("Access-Control-Allow-Origin", "*")
- redirectURI := "http://super.admin.izhyin.com/Wx/getAuthUrlCallBack"
- // 对redirectURI进行URL编码
- encodedRedirectURI := url.QueryEscape(redirectURI)
-
- // 构造微信登录页面的URL
- baseURL := "https://mp.weixin.qq.com/cgi-bin/componentloginpage"
- query := url.Values{}
- query.Add("component_appid", wxOpenThirdPartyAppList.Appid)
- query.Add("pre_auth_code", preAuthCode)
- query.Add("redirect_uri", encodedRedirectURI)
- query.Add("auth_type", "1")
-
- // 将查询参数附加到基础URL
- urlStr := baseURL + "?" + query.Encode()
- // 设置JavaScript重定向
- fmt.Fprintf(w, `
- <script>
- window.onload = function () {
- window.location.href = '%s';
- };
- </script>`, urlStr)
- }
-
- func WechatMsgRecieve(c *gin.Context) {
-
- return
- }
|