package wechat import ( "applet/app/cfg" "applet/app/lib/wechat/enum" "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 } // GetAuth 获取接口调用令牌 func (wxApiService *WxApiService) GetAuth(appId string) (cacheApiAuthorizerToken string, err error) { // set方法 userWxAppletListDb := implement.NewUserWxAppletListDb(db.Db) userWxAppletList, err1 := userWxAppletListDb.GetUserWxAppletListByAppId(appId) if err1 != nil { return cacheApiAuthorizerToken, err1 } if userWxAppletList == nil { err = errors.New("未查询到对应小程序App记录") return } //获取/刷新接口调用令牌 componentAccessToken, err := wxApiService.GetComponentAccessToken() if err != nil { return } url := "https://api.weixin.qq.com/cgi-bin/component/api_authorizer_token?component_access_token=" + componentAccessToken params := map[string]string{ "component_appid": wxApiService.ComponentAppid, "authorizer_appid": appId, "authorizer_refresh_token": userWxAppletList.AuthorizerRefreshToken, } postBody, err := utils.CurlPost(url, utils.SerializeStr(params), nil) if err != nil { return } var resp md.ApiAuthorizerToken err = json.Unmarshal(postBody, &resp) if err != nil { return } if resp.AuthorizerAccessToken == "" { err = errors.New("获取/刷新接口调用令牌") return } cacheApiAuthorizerToken = resp.AuthorizerAccessToken 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 } // DelAuthorize 取消授权 func (wxApiService *WxApiService) DelAuthorize(appId string) (err error) { // set方法 apiAuthorizerToken, err := wxApiService.GetAuth(appId) if err != nil { return } url := "https://api.weixin.qq.com/wxaapi/wxaembedded/del_authorize?access_token=" + apiAuthorizerToken params := map[string]string{ "appid": appId, } postBody, err := utils.CurlPost(url, utils.SerializeStr(params), nil) if err != nil { return } var resp md.DelAuthorize err = json.Unmarshal(postBody, &resp) if err != nil { return } if resp.ErrCode != 0 { err = errors.New(resp.ErrMsg) } return } // AgencyCreateAdunit 创建广告单元 func (wxApiService *WxApiService) AgencyCreateAdunit(appId, name string, adunitType enum.AdunitType) (err error, adUnitId string) { // set方法 apiAuthorizerToken, err := wxApiService.GetAuth(appId) if err != nil { return } url := "https://api.weixin.qq.com/wxa/operationams?action=agency_create_adunit&access_token=" + apiAuthorizerToken params := map[string]interface{}{ "name": name, "type": string(adunitType), } if adunitType == enum.AdunitTypeForVideoFeeds { params["video_duration_min"] = 6 params["video_duration_max"] = 60 } postBody, err := utils.CurlPost(url, utils.SerializeStr(params), nil) if err != nil { return } var resp md.AgencyCreateAdunit err = json.Unmarshal(postBody, &resp) if err != nil { return } if resp.Ret != 0 { err = errors.New(resp.ErrMsg) } adUnitId = resp.AdUnitId return } // AgencyUpdateAdunit 更新广告单元 func (wxApiService *WxApiService) AgencyUpdateAdunit(appId, adUnitId, name string, adunitType enum.AdunitType, adunitStatus enum.AdunitStatus) (err error) { // set方法 apiAuthorizerToken, err := wxApiService.GetAuth(appId) if err != nil { return } url := "https://api.weixin.qq.com/wxa/operationams?action=agency_update_adunit&access_token=" + apiAuthorizerToken params := map[string]interface{}{ "name": name, "ad_unit_id": adUnitId, "status": string(adunitStatus), } if adunitType == enum.AdunitTypeForVideoFeeds { params["video_duration_min"] = 6 params["video_duration_max"] = 60 } postBody, err := utils.CurlPost(url, utils.SerializeStr(params), nil) if err != nil { return } var resp md.AgencyUpdateAdunit err = json.Unmarshal(postBody, &resp) if err != nil { return } if resp.Ret != 0 { err = errors.New(resp.ErrMsg) } return } // SetCoverAdposStatus 设置小程序封面广告位开关状态 func (wxApiService *WxApiService) SetCoverAdposStatus(appId string, setCoverAdposStatus enum.SetCoverAdposStatus) (err error) { // set方法 apiAuthorizerToken, err := wxApiService.GetAuth(appId) if err != nil { return } url := "https://api.weixin.qq.com/wxa/operationams?action=agency_set_cover_adpos_status&access_token=" + apiAuthorizerToken params := map[string]interface{}{ "status": int32(setCoverAdposStatus), } postBody, err := utils.CurlPost(url, utils.SerializeStr(params), nil) if err != nil { return } var resp md.SetCoverAdposStatus err = json.Unmarshal(postBody, &resp) if err != nil { return } if resp.Ret != 0 { err = errors.New(resp.ErrMsg) } return } /* GetAdunitList 获取广告位(除封面广告位)或指定广告单元的信息 @params adSlot 广告位类型名称(非必传) @params adUnitId 广告单元ID(非必传) @Remark 返回的广告单元列表按照创建时间倒序,请以分页的形式获取。当需要获取全部广告位的清单时,无需传递广告位类型名称及广告单元ID;当需要获取某类型广告位的清单时,仅需传递广告位类型名称;当需要获取某广告位 id 的数据时,仅需传递广告单元ID。 */ func (wxApiService *WxApiService) GetAdunitList(appId string, page, pageSize int, adSlot enum.AdunitType, adUnitId string) (err error, resp md.GetAdunitList) { // set方法 apiAuthorizerToken, err := wxApiService.GetAuth(appId) if err != nil { return } url := "https://api.weixin.qq.com/wxa/operationams?action=agency_get_adunit_list&access_token=" + apiAuthorizerToken params := map[string]interface{}{ "page": page, "pageSize": pageSize, "ad_slot": adSlot, "ad_unit_id": adUnitId, } postBody, err := utils.CurlPost(url, utils.SerializeStr(params), nil) if err != nil { return } err = json.Unmarshal(postBody, &resp) if err != nil { return } if resp.Ret != 0 { err = errors.New(resp.ErrMsg) } return } /* GetAdposDetail 获取小程序广告细分数据 */ func (wxApiService *WxApiService) GetAdposDetail(appId string, page, pageSize int, startDate, endDate, adUnitId string) (err error, resp md.GetAdposDetail) { // set方法 apiAuthorizerToken, err := wxApiService.GetAuth(appId) if err != nil { return } url := "https://api.weixin.qq.com/wxa/operationams?action=agency_get_adunit_general&access_token=" + apiAuthorizerToken params := map[string]interface{}{ "page": page, "page_size": pageSize, "ad_unit_id": adUnitId, "start_date": startDate, "end_date": endDate, } fmt.Println(utils.SerializeStr(params)) postBody, err := utils.CurlPost(url, utils.SerializeStr(params), nil) if err != nil { return } err = json.Unmarshal(postBody, &resp) if err != nil { return } if resp.Ret != 0 { err = errors.New(resp.ErrMsg) } return }