package hdl import ( "applet/app/cfg" "applet/app/e" "applet/app/lib/wechat" md2 "applet/app/lib/wechat/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(md2.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 { e.OutErr(c, e.ERR, err.Error()) 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 } c.Writer.Header().Set("Access-Control-Allow-Origin", "*") redirectURI := "http://super.advertisement.dengbiao.top/api/wxOpen/getAuthUrlCallBack?masterId=" + masterId if cfg.Prd { redirectURI = "http://ad.zhios.cn/api/wxOpen/getAuthUrlCallBack?masterId=" + masterId } // 对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", redirectURI) query.Add("auth_type", "2") // 将查询参数附加到基础URL authURL := baseURL + "?" + query.Encode() // 设置JavaScript重定向 c.Header("Content-Type", "text/html") //TODO::必须指定相应内容为 text/html, 否则浏览器不能正确解析 c.String(200, ` `, authURL) } func GetAuthUrlCallBack(c *gin.Context) { authCode := c.DefaultQuery("auth_code", "") if authCode == "" { e.OutErr(c, e.ERR_NOT_FAN, "auth_code获取失败") return } fmt.Println("auth_code>>>>>>>>", authCode) masterId := c.DefaultQuery("masterId", "") 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 } resp, err := wxApiService.GetAuthorizerAccessTokenByAuthCode(authCode) if err != nil { e.OutErr(c, e.ERR, err.Error()) return } userWxAppletListDb := implement.NewUserWxAppletListDb(db.Db) userWxAppletList, err := userWxAppletListDb.GetUserWxAppletListByAppId(resp.AuthorizationInfo.AuthorizerAppid) if err != nil { return } if userWxAppletList == nil { e.OutErr(c, e.ERR_NOT_FAN, "未查询到小程序应用记录") return } userWxAppletList.IsAuth = 1 userWxAppletList.AuthorizerRefreshToken = resp.AuthorizationInfo.AuthorizerRefreshToken _, err = userWxAppletListDb.UpdateUserWxAppletList(userWxAppletList, "authorizer_refresh_token", "is_auth") if err != nil { e.OutErr(c, e.ERR_DB_ORM, err.Error()) return } c.HTML(http.StatusOK, "success.html", gin.H{ //"applet_name": "激活鸟", }) return } func WechatMsgRecieve(c *gin.Context) { return }