广告平台(站长使用)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

wechat_api.go 11 KiB

2 months ago
2 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. package wechat
  2. import (
  3. "applet/app/cfg"
  4. "applet/app/lib/wechat/enum"
  5. "applet/app/lib/wechat/md"
  6. "applet/app/utils"
  7. "applet/app/utils/cache"
  8. db "code.fnuoos.com/zhimeng/model.git/src"
  9. "code.fnuoos.com/zhimeng/model.git/src/super/implement"
  10. "encoding/json"
  11. "errors"
  12. "fmt"
  13. )
  14. type WxApiService struct {
  15. ComponentAppid string `json:"component_appid"`
  16. ComponentAppsecret string `json:"component_appsecret"`
  17. ComponentVerifyTicket string `json:"component_verify_ticket"`
  18. MasterId string `json:"master_id"`
  19. Host string `json:"host"`
  20. }
  21. func NewWxApiService(masterId, componentAppid, componentAppsecret string) (wxApiService WxApiService, err error) { // set方法
  22. wxApiService.MasterId = masterId
  23. wxApiService.ComponentAppid = componentAppid
  24. wxApiService.ComponentAppsecret = componentAppsecret
  25. wxApiService.Host = "http://super.advertisement.dengbiao.top"
  26. if cfg.Prd {
  27. wxApiService.Host = "http://www.baidu.com"
  28. }
  29. cacheKey := fmt.Sprintf(md.MasterComponentVerifyTicket, wxApiService.MasterId)
  30. cacheComponentVerifyTicket, _ := cache.GetString(cacheKey)
  31. if cacheComponentVerifyTicket == "" {
  32. return wxApiService, errors.New("微信验证票据ticket未获取到")
  33. }
  34. wxApiService.ComponentVerifyTicket = cacheComponentVerifyTicket
  35. return
  36. }
  37. // GetComponentAccessToken 获取接口调用令牌token
  38. func (wxApiService *WxApiService) GetComponentAccessToken() (cacheComponentAccessToken string, err error) { // set方法
  39. cacheKey := fmt.Sprintf(md.MasterComponentAccessToken, wxApiService.MasterId)
  40. cacheComponentAccessToken, _ = cache.GetString(cacheKey)
  41. if cacheComponentAccessToken == "" {
  42. url := "https://api.weixin.qq.com/cgi-bin/component/api_component_token"
  43. params := map[string]string{
  44. "component_appid": wxApiService.ComponentAppid,
  45. "component_appsecret": wxApiService.ComponentAppsecret,
  46. "component_verify_ticket": wxApiService.ComponentVerifyTicket,
  47. }
  48. postBody, err1 := utils.CurlPost(url, utils.SerializeStr(params), nil)
  49. if err1 != nil {
  50. return cacheComponentAccessToken, err1
  51. }
  52. var postData md.GetComponentAccessToken
  53. err = json.Unmarshal(postBody, &postData)
  54. if err != nil {
  55. return
  56. }
  57. if postData.ComponentAccessToken == "" {
  58. err = errors.New("获取接口令牌token失败")
  59. return
  60. }
  61. cacheComponentAccessToken = postData.ComponentAccessToken
  62. cache.SetEx(cacheKey, cacheComponentAccessToken, postData.ExpiresIn-600)
  63. wxOpenThirdPartyAppListDb := implement.NewWxOpenThirdPartyAppListDb(db.Db)
  64. wxOpenThirdPartyAppList, err1 := wxOpenThirdPartyAppListDb.GetWxOpenThirdPartyAppListByAppId(wxApiService.ComponentAppid)
  65. if err1 != nil {
  66. return cacheComponentAccessToken, err1
  67. }
  68. if wxOpenThirdPartyAppList == nil {
  69. err = errors.New("未查询到对应三方应用App记录")
  70. return
  71. }
  72. wxOpenThirdPartyAppList.ComponentAccessToken = cacheComponentAccessToken
  73. _, err = wxOpenThirdPartyAppListDb.UpdateWxOpenThirdPartyAppList(wxOpenThirdPartyAppList, "component_access_token")
  74. if err != nil {
  75. return
  76. }
  77. }
  78. return
  79. }
  80. // GetAuth 获取接口调用令牌
  81. func (wxApiService *WxApiService) GetAuth(appId string) (cacheApiAuthorizerToken string, err error) { // set方法
  82. userWxAppletListDb := implement.NewUserWxAppletListDb(db.Db)
  83. userWxAppletList, err1 := userWxAppletListDb.GetUserWxAppletListByAppId(appId)
  84. if err1 != nil {
  85. return cacheApiAuthorizerToken, err1
  86. }
  87. if userWxAppletList == nil {
  88. err = errors.New("未查询到对应小程序App记录")
  89. return
  90. }
  91. //获取/刷新接口调用令牌
  92. componentAccessToken, err := wxApiService.GetComponentAccessToken()
  93. if err != nil {
  94. return
  95. }
  96. url := "https://api.weixin.qq.com/cgi-bin/component/api_authorizer_token?component_access_token=" + componentAccessToken
  97. params := map[string]string{
  98. "component_appid": wxApiService.ComponentAppid,
  99. "authorizer_appid": appId,
  100. "authorizer_refresh_token": userWxAppletList.AuthorizerRefreshToken,
  101. }
  102. postBody, err := utils.CurlPost(url, utils.SerializeStr(params), nil)
  103. if err != nil {
  104. return
  105. }
  106. var resp md.ApiAuthorizerToken
  107. err = json.Unmarshal(postBody, &resp)
  108. if err != nil {
  109. return
  110. }
  111. if resp.AuthorizerAccessToken == "" {
  112. err = errors.New("获取/刷新接口调用令牌")
  113. return
  114. }
  115. cacheApiAuthorizerToken = resp.AuthorizerAccessToken
  116. return
  117. }
  118. // GetPreAuthCode 获取预授权码
  119. func (wxApiService *WxApiService) GetPreAuthCode() (preAuthCode string, err error) { // set方法
  120. componentAccessToken, err := wxApiService.GetComponentAccessToken()
  121. if err != nil {
  122. return
  123. }
  124. url := "https://api.weixin.qq.com/cgi-bin/component/api_create_preauthcode?component_access_token=" + componentAccessToken
  125. params := map[string]string{
  126. "component_appid": wxApiService.ComponentAppid,
  127. }
  128. postBody, err := utils.CurlPost(url, utils.SerializeStr(params), nil)
  129. if err != nil {
  130. return
  131. }
  132. var postData md.GetPreAuthCode
  133. err = json.Unmarshal(postBody, &postData)
  134. if err != nil {
  135. return
  136. }
  137. if postData.PreAuthCode == "" {
  138. err = errors.New("获取预授权码失败")
  139. return
  140. }
  141. preAuthCode = postData.PreAuthCode
  142. return
  143. }
  144. // GetAuthorizerAccessTokenByAuthCode 使用授权码获取授权信息
  145. func (wxApiService *WxApiService) GetAuthorizerAccessTokenByAuthCode(authCode string) (resp md.GetAuthorizerAccessTokenByAuthCode, err error) { // set方法
  146. componentAccessToken, err := wxApiService.GetComponentAccessToken()
  147. if err != nil {
  148. return
  149. }
  150. url := "https://api.weixin.qq.com/cgi-bin/component/api_query_auth?component_access_token=" + componentAccessToken
  151. params := map[string]string{
  152. "component_appid": wxApiService.ComponentAppid,
  153. "authorization_code": authCode,
  154. }
  155. postBody, err := utils.CurlPost(url, utils.SerializeStr(params), nil)
  156. if err != nil {
  157. return
  158. }
  159. err = json.Unmarshal(postBody, &resp)
  160. if err != nil {
  161. return
  162. }
  163. if resp.AuthorizationInfo.AuthorizerAppid == "" {
  164. err = errors.New("获取授权信息失败")
  165. return
  166. }
  167. return
  168. }
  169. // DelAuthorize 取消授权
  170. func (wxApiService *WxApiService) DelAuthorize(appId string) (err error) { // set方法
  171. apiAuthorizerToken, err := wxApiService.GetAuth(appId)
  172. if err != nil {
  173. return
  174. }
  175. url := "https://api.weixin.qq.com/wxaapi/wxaembedded/del_authorize?access_token=" + apiAuthorizerToken
  176. params := map[string]string{
  177. "appid": appId,
  178. }
  179. postBody, err := utils.CurlPost(url, utils.SerializeStr(params), nil)
  180. if err != nil {
  181. return
  182. }
  183. var resp md.DelAuthorize
  184. err = json.Unmarshal(postBody, &resp)
  185. if err != nil {
  186. return
  187. }
  188. if resp.ErrCode != 0 {
  189. err = errors.New(resp.ErrMsg)
  190. }
  191. return
  192. }
  193. // AgencyCreateAdunit 创建广告单元
  194. func (wxApiService *WxApiService) AgencyCreateAdunit(appId, name string, adunitType enum.AdunitType) (err error, adUnitId string) { // set方法
  195. apiAuthorizerToken, err := wxApiService.GetAuth(appId)
  196. if err != nil {
  197. return
  198. }
  199. url := "https://api.weixin.qq.com/wxa/operationams?action=agency_create_adunit&access_token=" + apiAuthorizerToken
  200. params := map[string]interface{}{
  201. "name": name,
  202. "type": string(adunitType),
  203. }
  204. if adunitType == enum.AdunitTypeForVideoFeeds {
  205. params["video_duration_min"] = 6
  206. params["video_duration_max"] = 60
  207. }
  208. postBody, err := utils.CurlPost(url, utils.SerializeStr(params), nil)
  209. if err != nil {
  210. return
  211. }
  212. var resp md.AgencyCreateAdunit
  213. err = json.Unmarshal(postBody, &resp)
  214. if err != nil {
  215. return
  216. }
  217. if resp.Ret != 0 {
  218. err = errors.New(resp.ErrMsg)
  219. }
  220. adUnitId = resp.AdUnitId
  221. return
  222. }
  223. // AgencyUpdateAdunit 更新广告单元
  224. func (wxApiService *WxApiService) AgencyUpdateAdunit(appId, adUnitId, name string, adunitType enum.AdunitType, adunitStatus enum.AdunitStatus) (err error) { // set方法
  225. apiAuthorizerToken, err := wxApiService.GetAuth(appId)
  226. if err != nil {
  227. return
  228. }
  229. url := "https://api.weixin.qq.com/wxa/operationams?action=agency_update_adunit&access_token=" + apiAuthorizerToken
  230. params := map[string]interface{}{
  231. "name": name,
  232. "ad_unit_id": adUnitId,
  233. "status": string(adunitStatus),
  234. }
  235. if adunitType == enum.AdunitTypeForVideoFeeds {
  236. params["video_duration_min"] = 6
  237. params["video_duration_max"] = 60
  238. }
  239. postBody, err := utils.CurlPost(url, utils.SerializeStr(params), nil)
  240. if err != nil {
  241. return
  242. }
  243. var resp md.AgencyUpdateAdunit
  244. err = json.Unmarshal(postBody, &resp)
  245. if err != nil {
  246. return
  247. }
  248. if resp.Ret != 0 {
  249. err = errors.New(resp.ErrMsg)
  250. }
  251. return
  252. }
  253. // SetCoverAdposStatus 设置小程序封面广告位开关状态
  254. func (wxApiService *WxApiService) SetCoverAdposStatus(appId string, setCoverAdposStatus enum.SetCoverAdposStatus) (err error) { // set方法
  255. apiAuthorizerToken, err := wxApiService.GetAuth(appId)
  256. if err != nil {
  257. return
  258. }
  259. url := "https://api.weixin.qq.com/wxa/operationams?action=agency_set_cover_adpos_status&access_token=" + apiAuthorizerToken
  260. params := map[string]interface{}{
  261. "status": int32(setCoverAdposStatus),
  262. }
  263. postBody, err := utils.CurlPost(url, utils.SerializeStr(params), nil)
  264. if err != nil {
  265. return
  266. }
  267. var resp md.SetCoverAdposStatus
  268. err = json.Unmarshal(postBody, &resp)
  269. if err != nil {
  270. return
  271. }
  272. if resp.Ret != 0 {
  273. err = errors.New(resp.ErrMsg)
  274. }
  275. return
  276. }
  277. /*
  278. GetAdunitList 获取广告位(除封面广告位)或指定广告单元的信息
  279. @params adSlot 广告位类型名称(非必传)
  280. @params adUnitId 广告单元ID(非必传)
  281. @Remark 返回的广告单元列表按照创建时间倒序,请以分页的形式获取。当需要获取全部广告位的清单时,无需传递广告位类型名称及广告单元ID;当需要获取某类型广告位的清单时,仅需传递广告位类型名称;当需要获取某广告位 id 的数据时,仅需传递广告单元ID。
  282. */
  283. func (wxApiService *WxApiService) GetAdunitList(appId string, page, pageSize int, adSlot enum.AdunitType, adUnitId string) (err error, resp md.GetAdunitList) { // set方法
  284. apiAuthorizerToken, err := wxApiService.GetAuth(appId)
  285. if err != nil {
  286. return
  287. }
  288. url := "https://api.weixin.qq.com/wxa/operationams?action=agency_get_adunit_list&access_token=" + apiAuthorizerToken
  289. params := map[string]interface{}{
  290. "page": page,
  291. "pageSize": pageSize,
  292. "ad_slot": adSlot,
  293. "ad_unit_id": adUnitId,
  294. }
  295. postBody, err := utils.CurlPost(url, utils.SerializeStr(params), nil)
  296. if err != nil {
  297. return
  298. }
  299. err = json.Unmarshal(postBody, &resp)
  300. if err != nil {
  301. return
  302. }
  303. if resp.Ret != 0 {
  304. err = errors.New(resp.ErrMsg)
  305. }
  306. return
  307. }
  308. /*
  309. GetAdposDetail 获取小程序广告细分数据
  310. */
  311. func (wxApiService *WxApiService) GetAdposDetail(appId string, page, pageSize int, startDate, endDate, adUnitId string) (err error, resp md.GetAdposDetail) { // set方法
  312. apiAuthorizerToken, err := wxApiService.GetAuth(appId)
  313. if err != nil {
  314. return
  315. }
  316. url := "https://api.weixin.qq.com/wxa/operationams?action=agency_get_adunit_general&access_token=" + apiAuthorizerToken
  317. params := map[string]interface{}{
  318. "page": page,
  319. "page_size": pageSize,
  320. "ad_unit_id": adUnitId,
  321. "start_date": startDate,
  322. "end_date": endDate,
  323. }
  324. fmt.Println(utils.SerializeStr(params))
  325. postBody, err := utils.CurlPost(url, utils.SerializeStr(params), nil)
  326. if err != nil {
  327. return
  328. }
  329. err = json.Unmarshal(postBody, &resp)
  330. if err != nil {
  331. return
  332. }
  333. if resp.Ret != 0 {
  334. err = errors.New(resp.ErrMsg)
  335. }
  336. return
  337. }