广告平台(站长使用)
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.
 
 
 
 
 

465 lines
14 KiB

  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://ad.zhios.cn"
  28. }
  29. cacheKey := fmt.Sprintf(md.MasterComponentVerifyTicket, wxApiService.MasterId)
  30. cacheComponentVerifyTicket, _ := cache.GetString(cacheKey)
  31. if cacheComponentVerifyTicket == "" {
  32. if !cfg.Local {
  33. return wxApiService, errors.New("微信验证票据ticket未获取到")
  34. }
  35. cacheComponentVerifyTicket = "ticket@@@UFKPikftQ8YmNuw0tGT3LsHrpD08Y3c2XtaImAAs2ID2SPLtq2i1-PDzwOpbCPebRjlC8qyOm-djUOyL-_HXzA"
  36. }
  37. wxApiService.ComponentVerifyTicket = cacheComponentVerifyTicket
  38. return
  39. }
  40. // GetComponentAccessToken 获取接口调用令牌token
  41. func (wxApiService *WxApiService) GetComponentAccessToken() (cacheComponentAccessToken string, err error) { // set方法
  42. cacheKey := fmt.Sprintf(md.MasterComponentAccessToken, wxApiService.MasterId)
  43. cacheComponentAccessToken, _ = cache.GetString(cacheKey)
  44. if cacheComponentAccessToken == "" {
  45. url := "https://api.weixin.qq.com/cgi-bin/component/api_component_token"
  46. params := map[string]string{
  47. "component_appid": wxApiService.ComponentAppid,
  48. "component_appsecret": wxApiService.ComponentAppsecret,
  49. "component_verify_ticket": wxApiService.ComponentVerifyTicket,
  50. }
  51. postBody, err1 := utils.CurlPost(url, utils.SerializeStr(params), nil)
  52. if err1 != nil {
  53. return cacheComponentAccessToken, err1
  54. }
  55. var postData md.GetComponentAccessToken
  56. err = json.Unmarshal(postBody, &postData)
  57. if err != nil {
  58. return
  59. }
  60. if postData.ComponentAccessToken == "" {
  61. err = errors.New("获取接口令牌token失败")
  62. return
  63. }
  64. cacheComponentAccessToken = postData.ComponentAccessToken
  65. cache.SetEx(cacheKey, cacheComponentAccessToken, postData.ExpiresIn-600)
  66. wxOpenThirdPartyAppListDb := implement.NewWxOpenThirdPartyAppListDb(db.Db)
  67. wxOpenThirdPartyAppList, err1 := wxOpenThirdPartyAppListDb.GetWxOpenThirdPartyAppListByAppId(wxApiService.ComponentAppid)
  68. if err1 != nil {
  69. return cacheComponentAccessToken, err1
  70. }
  71. if wxOpenThirdPartyAppList == nil {
  72. err = errors.New("未查询到对应三方应用App记录")
  73. return
  74. }
  75. wxOpenThirdPartyAppList.ComponentAccessToken = cacheComponentAccessToken
  76. _, err = wxOpenThirdPartyAppListDb.UpdateWxOpenThirdPartyAppList(wxOpenThirdPartyAppList, "component_access_token")
  77. if err != nil {
  78. return
  79. }
  80. }
  81. return
  82. }
  83. // GetAuth 获取接口调用令牌
  84. func (wxApiService *WxApiService) GetAuth(appId string) (cacheApiAuthorizerToken string, err error) { // set方法
  85. userWxAppletListDb := implement.NewUserWxAppletListDb(db.Db)
  86. userWxAppletList, err1 := userWxAppletListDb.GetUserWxAppletListByAppId(appId)
  87. if err1 != nil {
  88. return cacheApiAuthorizerToken, err1
  89. }
  90. if userWxAppletList == nil {
  91. err = errors.New("未查询到对应小程序App记录")
  92. return
  93. }
  94. // 获取/刷新接口调用令牌
  95. componentAccessToken, err := wxApiService.GetComponentAccessToken()
  96. if err != nil {
  97. return
  98. }
  99. url := "https://api.weixin.qq.com/cgi-bin/component/api_authorizer_token?component_access_token=" + componentAccessToken
  100. params := map[string]string{
  101. "component_appid": wxApiService.ComponentAppid,
  102. "authorizer_appid": appId,
  103. "authorizer_refresh_token": userWxAppletList.AuthorizerRefreshToken,
  104. }
  105. postBody, err := utils.CurlPost(url, utils.SerializeStr(params), nil)
  106. if err != nil {
  107. return
  108. }
  109. var resp md.ApiAuthorizerToken
  110. err = json.Unmarshal(postBody, &resp)
  111. if err != nil {
  112. return
  113. }
  114. if resp.AuthorizerAccessToken == "" {
  115. err = errors.New("获取/刷新接口调用令牌")
  116. return
  117. }
  118. cacheApiAuthorizerToken = resp.AuthorizerAccessToken
  119. return
  120. }
  121. // GetPreAuthCode 获取预授权码
  122. func (wxApiService *WxApiService) GetPreAuthCode() (preAuthCode string, err error) { // set方法
  123. componentAccessToken, err := wxApiService.GetComponentAccessToken()
  124. if err != nil {
  125. return
  126. }
  127. url := "https://api.weixin.qq.com/cgi-bin/component/api_create_preauthcode?component_access_token=" + componentAccessToken
  128. params := map[string]string{
  129. "component_appid": wxApiService.ComponentAppid,
  130. }
  131. postBody, err := utils.CurlPost(url, utils.SerializeStr(params), nil)
  132. if err != nil {
  133. return
  134. }
  135. var postData md.GetPreAuthCode
  136. err = json.Unmarshal(postBody, &postData)
  137. if err != nil {
  138. return
  139. }
  140. if postData.PreAuthCode == "" {
  141. err = errors.New("获取预授权码失败")
  142. return
  143. }
  144. preAuthCode = postData.PreAuthCode
  145. return
  146. }
  147. // GetAuthorizerAccessTokenByAuthCode 使用授权码获取授权信息
  148. func (wxApiService *WxApiService) GetAuthorizerAccessTokenByAuthCode(authCode string) (resp md.GetAuthorizerAccessTokenByAuthCode, err error) { // set方法
  149. componentAccessToken, err := wxApiService.GetComponentAccessToken()
  150. if err != nil {
  151. return
  152. }
  153. url := "https://api.weixin.qq.com/cgi-bin/component/api_query_auth?component_access_token=" + componentAccessToken
  154. params := map[string]string{
  155. "component_appid": wxApiService.ComponentAppid,
  156. "authorization_code": authCode,
  157. }
  158. postBody, err := utils.CurlPost(url, utils.SerializeStr(params), nil)
  159. if err != nil {
  160. return
  161. }
  162. err = json.Unmarshal(postBody, &resp)
  163. if err != nil {
  164. return
  165. }
  166. if resp.AuthorizationInfo.AuthorizerAppid == "" {
  167. err = errors.New("获取授权信息失败")
  168. return
  169. }
  170. return
  171. }
  172. // DelAuthorize 取消授权
  173. func (wxApiService *WxApiService) DelAuthorize(appId string) (err error) { // set方法
  174. apiAuthorizerToken, err := wxApiService.GetAuth(appId)
  175. if err != nil {
  176. return
  177. }
  178. url := "https://api.weixin.qq.com/wxaapi/wxaembedded/del_authorize?access_token=" + apiAuthorizerToken
  179. params := map[string]string{
  180. "appid": appId,
  181. }
  182. postBody, err := utils.CurlPost(url, utils.SerializeStr(params), nil)
  183. if err != nil {
  184. return
  185. }
  186. var resp md.DelAuthorize
  187. err = json.Unmarshal(postBody, &resp)
  188. if err != nil {
  189. return
  190. }
  191. if resp.ErrCode != 0 {
  192. err = errors.New(resp.ErrMsg)
  193. }
  194. return
  195. }
  196. // AgencyCreateAdunit 创建广告单元
  197. func (wxApiService *WxApiService) AgencyCreateAdunit(appId, name string, adunitType enum.AdunitType) (err error, adUnitId string) { // set方法
  198. apiAuthorizerToken, err := wxApiService.GetAuth(appId)
  199. if err != nil {
  200. return
  201. }
  202. url := "https://api.weixin.qq.com/wxa/operationams?action=agency_create_adunit&access_token=" + apiAuthorizerToken
  203. params := map[string]interface{}{
  204. "name": name,
  205. "type": string(adunitType),
  206. }
  207. if adunitType == enum.AdunitTypeForVideoFeeds {
  208. params["video_duration_min"] = 6
  209. params["video_duration_max"] = 60
  210. }
  211. postBody, err := utils.CurlPost(url, utils.SerializeStr(params), nil)
  212. if err != nil {
  213. return
  214. }
  215. var resp md.AgencyCreateAdunit
  216. err = json.Unmarshal(postBody, &resp)
  217. if err != nil {
  218. return
  219. }
  220. if resp.Ret != 0 {
  221. err = errors.New(resp.ErrMsg)
  222. }
  223. adUnitId = resp.AdUnitId
  224. return
  225. }
  226. // AgencyUpdateAdunit 更新广告单元
  227. func (wxApiService *WxApiService) AgencyUpdateAdunit(appId, adUnitId, name string, adunitType enum.AdunitType, adunitStatus enum.AdunitStatus) (err error) { // set方法
  228. apiAuthorizerToken, err := wxApiService.GetAuth(appId)
  229. if err != nil {
  230. return
  231. }
  232. url := "https://api.weixin.qq.com/wxa/operationams?action=agency_update_adunit&access_token=" + apiAuthorizerToken
  233. params := map[string]interface{}{
  234. "name": name,
  235. "ad_unit_id": adUnitId,
  236. "status": string(adunitStatus),
  237. }
  238. if adunitType == enum.AdunitTypeForVideoFeeds {
  239. params["video_duration_min"] = 6
  240. params["video_duration_max"] = 60
  241. }
  242. postBody, err := utils.CurlPost(url, utils.SerializeStr(params), nil)
  243. if err != nil {
  244. return
  245. }
  246. var resp md.AgencyUpdateAdunit
  247. err = json.Unmarshal(postBody, &resp)
  248. if err != nil {
  249. return
  250. }
  251. if resp.Ret != 0 {
  252. err = errors.New(resp.ErrMsg)
  253. }
  254. return
  255. }
  256. // SetCoverAdposStatus 设置小程序封面广告位开关状态
  257. func (wxApiService *WxApiService) SetCoverAdposStatus(appId string, setCoverAdposStatus enum.SetCoverAdposStatus) (err error) { // set方法
  258. apiAuthorizerToken, err := wxApiService.GetAuth(appId)
  259. if err != nil {
  260. return
  261. }
  262. url := "https://api.weixin.qq.com/wxa/operationams?action=agency_set_cover_adpos_status&access_token=" + apiAuthorizerToken
  263. params := map[string]interface{}{
  264. "status": int32(setCoverAdposStatus),
  265. }
  266. postBody, err := utils.CurlPost(url, utils.SerializeStr(params), nil)
  267. if err != nil {
  268. return
  269. }
  270. var resp md.SetCoverAdposStatus
  271. err = json.Unmarshal(postBody, &resp)
  272. if err != nil {
  273. return
  274. }
  275. if resp.Ret != 0 {
  276. err = errors.New(resp.ErrMsg)
  277. }
  278. return
  279. }
  280. /*
  281. GetAdunitList 获取广告位(除封面广告位)或指定广告单元的信息
  282. @params adSlot 广告位类型名称(非必传)
  283. @params adUnitId 广告单元ID(非必传)
  284. @Remark 返回的广告单元列表按照创建时间倒序,请以分页的形式获取。当需要获取全部广告位的清单时,无需传递广告位类型名称及广告单元ID;当需要获取某类型广告位的清单时,仅需传递广告位类型名称;当需要获取某广告位 id 的数据时,仅需传递广告单元ID。
  285. */
  286. func (wxApiService *WxApiService) GetAdunitList(appId string, page, pageSize int, adSlot enum.AdunitType, adUnitId string) (err error, resp md.GetAdunitList) { // set方法
  287. apiAuthorizerToken, err := wxApiService.GetAuth(appId)
  288. if err != nil {
  289. return
  290. }
  291. url := "https://api.weixin.qq.com/wxa/operationams?action=agency_get_adunit_list&access_token=" + apiAuthorizerToken
  292. params := map[string]interface{}{
  293. "page": page,
  294. "pageSize": pageSize,
  295. "ad_slot": adSlot,
  296. "ad_unit_id": adUnitId,
  297. }
  298. postBody, err := utils.CurlPost(url, utils.SerializeStr(params), nil)
  299. if err != nil {
  300. return
  301. }
  302. err = json.Unmarshal(postBody, &resp)
  303. if err != nil {
  304. return
  305. }
  306. if resp.Ret != 0 {
  307. err = errors.New(resp.ErrMsg)
  308. }
  309. return
  310. }
  311. /*
  312. GetAdposDetail 获取小程序广告细分数据
  313. */
  314. func (wxApiService *WxApiService) GetAdposDetail(appId string, page, pageSize int, startDate, endDate, adUnitId string) (err error, resp md.GetAdposDetail) { // set方法
  315. apiAuthorizerToken, err := wxApiService.GetAuth(appId)
  316. if err != nil {
  317. return
  318. }
  319. url := "https://api.weixin.qq.com/wxa/operationams?action=agency_get_adunit_general&access_token=" + apiAuthorizerToken
  320. params := map[string]interface{}{
  321. "page": page,
  322. "page_size": pageSize,
  323. "ad_unit_id": adUnitId,
  324. "start_date": startDate,
  325. "end_date": endDate,
  326. }
  327. fmt.Println(utils.SerializeStr(params))
  328. postBody, err := utils.CurlPost(url, utils.SerializeStr(params), nil)
  329. if err != nil {
  330. return
  331. }
  332. err = json.Unmarshal(postBody, &resp)
  333. if err != nil {
  334. return
  335. }
  336. if resp.Ret != 0 {
  337. err = errors.New(resp.ErrMsg)
  338. }
  339. return
  340. }
  341. /*
  342. SetBlackList 设置屏蔽的广告主 (op number 是 1:设置屏蔽 2:删除屏蔽)
  343. */
  344. func (wxApiService *WxApiService) SetBlackList(appId string, op int, list string) (err error) { // set方法
  345. apiAuthorizerToken, err := wxApiService.GetAuth(appId)
  346. if err != nil {
  347. return
  348. }
  349. url := "https://api.weixin.qq.com/wxa/operationams?action=agency_set_black_list&access_token=" + apiAuthorizerToken
  350. params := map[string]interface{}{
  351. "op": op,
  352. "list": list,
  353. }
  354. fmt.Println(utils.SerializeStr(params))
  355. postBody, err := utils.CurlPost(url, utils.SerializeStr(params), nil)
  356. if err != nil {
  357. return
  358. }
  359. var resp md.AgencySetBlackListResp
  360. err = json.Unmarshal(postBody, &resp)
  361. if err != nil {
  362. return
  363. }
  364. if resp.Ret != 0 {
  365. err = errors.New(resp.ErrMsg)
  366. }
  367. return
  368. }
  369. /*
  370. GetBlackList 获取屏蔽的广告主
  371. */
  372. func (wxApiService *WxApiService) GetBlackList(appId string) (err error, resp md.AgencyGetBlackListResp) { // set方法
  373. apiAuthorizerToken, err := wxApiService.GetAuth(appId)
  374. if err != nil {
  375. return
  376. }
  377. url := "https://api.weixin.qq.com/wxa/operationams?action=agency_get_black_list&access_token=" + apiAuthorizerToken
  378. postBody, err := utils.CurlPost(url, "", nil)
  379. if err != nil {
  380. return
  381. }
  382. err = json.Unmarshal(postBody, &resp)
  383. if err != nil {
  384. return
  385. }
  386. if resp.Ret != 0 {
  387. err = errors.New(resp.ErrMsg)
  388. }
  389. return
  390. }
  391. /*
  392. SetAmsCategoryBlackList 设置行业屏蔽信息
  393. */
  394. func (wxApiService *WxApiService) SetAmsCategoryBlackList(appId string, amsCategory string) (err error) { // set方法
  395. apiAuthorizerToken, err := wxApiService.GetAuth(appId)
  396. if err != nil {
  397. return
  398. }
  399. url := "https://api.weixin.qq.com/wxa/operationams?action=agency_set_mp_amscategory_blacklist&access_token=" + apiAuthorizerToken
  400. params := map[string]interface{}{
  401. "ams_category": amsCategory,
  402. }
  403. fmt.Println(utils.SerializeStr(params))
  404. postBody, err := utils.CurlPost(url, utils.SerializeStr(params), nil)
  405. if err != nil {
  406. return
  407. }
  408. var resp md.SetAmsCategoryBlackListResp
  409. err = json.Unmarshal(postBody, &resp)
  410. if err != nil {
  411. return
  412. }
  413. if resp.Ret != 0 {
  414. err = errors.New(resp.ErrMsg)
  415. }
  416. return
  417. }
  418. /*
  419. GetAmsCategoryBlackList 获取行业屏蔽信息
  420. */
  421. func (wxApiService *WxApiService) GetAmsCategoryBlackList(appId string) (err error, resp md.GetAmsCategoryBlackListResp) { // set方法
  422. apiAuthorizerToken, err := wxApiService.GetAuth(appId)
  423. if err != nil {
  424. return
  425. }
  426. url := "https://api.weixin.qq.com/wxa/operationams?action=agency_get_mp_amscategory_blacklist&access_token=" + apiAuthorizerToken
  427. postBody, err := utils.CurlPost(url, "", nil)
  428. if err != nil {
  429. return
  430. }
  431. err = json.Unmarshal(postBody, &resp)
  432. if err != nil {
  433. return
  434. }
  435. if resp.Ret != 0 {
  436. err = errors.New(resp.ErrMsg)
  437. }
  438. return
  439. }