package wechat import ( "applet/app/cfg" "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/json" "errors" "fmt" ) type WxApiService struct { ComponentAppid string `json:"component_appid"` ComponentAppsecret string `json:"component_appsecret"` ComponentVerifyTicket string `json:"component_verify_ticket"` MasterId string `json:"master_id"` Host string `json:"host"` } func NewWxApiService(masterId, componentAppid, componentAppsecret string) (wxApiService WxApiService, err error) { // set方法 wxApiService.MasterId = masterId wxApiService.ComponentAppid = componentAppid wxApiService.ComponentAppsecret = componentAppsecret wxApiService.Host = "http://super.advertisement.dengbiao.top" if cfg.Prd { wxApiService.Host = "http://www.baidu.com" } cacheKey := fmt.Sprintf(md.MasterComponentVerifyTicket, wxApiService.MasterId) cacheComponentVerifyTicket, _ := cache.GetString(cacheKey) if cacheComponentVerifyTicket == "" { return wxApiService, errors.New("微信验证票据ticket未获取到") } wxApiService.ComponentVerifyTicket = cacheComponentVerifyTicket return } // GetComponentAccessToken 获取接口调用令牌token func (wxApiService *WxApiService) GetComponentAccessToken() (cacheComponentAccessToken string, err error) { // set方法 cacheKey := fmt.Sprintf(md.MasterComponentAccessToken, wxApiService.MasterId) cacheComponentAccessToken, _ = cache.GetString(cacheKey) if cacheComponentAccessToken == "" { url := "https://api.weixin.qq.com/cgi-bin/component/api_component_token" params := map[string]string{ "component_appid": wxApiService.ComponentAppid, "component_appsecret": wxApiService.ComponentAppsecret, "component_verify_ticket": wxApiService.ComponentVerifyTicket, } postBody, err1 := utils.CurlPost(url, utils.SerializeStr(params), nil) if err1 != nil { return cacheComponentAccessToken, err1 } var postData md.GetComponentAccessToken err = json.Unmarshal(postBody, &postData) if err != nil { return } if postData.ComponentAccessToken == "" { err = errors.New("获取接口令牌token失败") return } cacheComponentAccessToken = postData.ComponentAccessToken cache.SetEx(cacheKey, cacheComponentAccessToken, postData.ExpiresIn-600) wxOpenThirdPartyAppListDb := implement.NewWxOpenThirdPartyAppListDb(db.Db) wxOpenThirdPartyAppList, err1 := wxOpenThirdPartyAppListDb.GetWxOpenThirdPartyAppListByAppId(wxApiService.ComponentAppid) if err1 != nil { return cacheComponentAccessToken, err1 } if wxOpenThirdPartyAppList == nil { err = errors.New("未查询到对应App记录") return } wxOpenThirdPartyAppList.ComponentAccessToken = cacheComponentAccessToken _, err = wxOpenThirdPartyAppListDb.UpdateWxOpenThirdPartyAppList(wxOpenThirdPartyAppList, "component_access_token") if err != nil { return } } return } // GetPreAuthCode 获取预授权码 func (wxApiService *WxApiService) GetPreAuthCode() (preAuthCode string, err error) { // set方法 componentAccessToken, err := wxApiService.GetComponentAccessToken() if err != nil { return } url := "https://api.weixin.qq.com/cgi-bin/component/api_create_preauthcode?component_access_token=" + componentAccessToken params := map[string]string{ "component_appid": wxApiService.ComponentAppid, } postBody, err := utils.CurlPost(url, utils.SerializeStr(params), nil) if err != nil { return } var postData md.GetPreAuthCode err = json.Unmarshal(postBody, &postData) if err != nil { return } if postData.PreAuthCode == "" { err = errors.New("获取预授权码失败") return } preAuthCode = postData.PreAuthCode return } // GetAuthorizerAccessTokenByAuthCode 使用授权码获取授权信息 func (wxApiService *WxApiService) GetAuthorizerAccessTokenByAuthCode(authCode string) (resp md.GetAuthorizerAccessTokenByAuthCode, err error) { // set方法 componentAccessToken, err := wxApiService.GetComponentAccessToken() if err != nil { return } url := "https://api.weixin.qq.com/cgi-bin/component/api_query_auth?component_access_token=" + componentAccessToken params := map[string]string{ "component_appid": wxApiService.ComponentAppid, "authorization_code": authCode, } postBody, err := utils.CurlPost(url, utils.SerializeStr(params), nil) if err != nil { return } err = json.Unmarshal(postBody, &resp) if err != nil { return } if resp.AuthorizationInfo.AuthorizerAppid == "" { err = errors.New("获取授权信息失败") return } return }