|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322 |
- 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
- }
|