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, ` `, urlStr) } func WechatMsgRecieve(c *gin.Context) { return }