publisher.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. package datacube
  2. import (
  3. "fmt"
  4. "net/url"
  5. "strconv"
  6. "github.com/silenceper/wechat/v2/util"
  7. )
  8. // AdSlot 广告位类型
  9. type AdSlot string
  10. const (
  11. // SlotIDBizBottom 公众号底部广告
  12. SlotIDBizBottom AdSlot = "SLOT_ID_BIZ_BOTTOM"
  13. // SlotIDBizMidContext 公众号文中广告
  14. SlotIDBizMidContext AdSlot = "SLOT_ID_BIZ_MID_CONTEXT"
  15. // SlotIDBizVideoEnd 公众号视频后贴
  16. SlotIDBizVideoEnd AdSlot = "SLOT_ID_BIZ_VIDEO_END"
  17. // SlotIDBizSponsor 公众号互选广告
  18. SlotIDBizSponsor AdSlot = "SLOT_ID_BIZ_SPONSOR"
  19. // SlotIDBizCps 公众号返佣商品
  20. SlotIDBizCps AdSlot = "SLOT_ID_BIZ_CPS"
  21. // SlotIDWeappBanner 小程序banner
  22. SlotIDWeappBanner AdSlot = "SLOT_ID_WEAPP_BANNER"
  23. // SlotIDWeappRewardVideo 小程序激励视频
  24. SlotIDWeappRewardVideo AdSlot = "SLOT_ID_WEAPP_REWARD_VIDEO"
  25. // SlotIDWeappInterstitial 小程序插屏广告
  26. SlotIDWeappInterstitial AdSlot = "SLOT_ID_WEAPP_INTERSTITIAL"
  27. // SlotIDWeappVideoFeeds 小程序视频广告
  28. SlotIDWeappVideoFeeds AdSlot = "SLOT_ID_WEAPP_VIDEO_FEEDS"
  29. // SlotIDWeappVideoBegin 小程序视频前贴
  30. SlotIDWeappVideoBegin AdSlot = "SLOT_ID_WEAPP_VIDEO_BEGIN"
  31. // SlotIDWeappBox 小程序格子广告
  32. SlotIDWeappBox AdSlot = "SLOT_ID_WEAPP_BOX"
  33. )
  34. const (
  35. publisherURL = "https://api.weixin.qq.com/publisher/stat"
  36. )
  37. const (
  38. actionPublisherAdPosGeneral = "publisher_adpos_general"
  39. actionPublisherCpsGeneral = "publisher_cps_general"
  40. actionPublisherSettlement = "publisher_settlement"
  41. )
  42. // BaseResp 错误信息
  43. type BaseResp struct {
  44. ErrMsg string `json:"err_msg"`
  45. Ret int `json:"ret"`
  46. }
  47. // ResPublisherAdPos 公众号分广告位数据响应
  48. type ResPublisherAdPos struct {
  49. util.CommonError
  50. BaseResp BaseResp `json:"base_resp"`
  51. List []ResAdPosList `json:"list"`
  52. Summary ResAdPosSummary `json:"summary"`
  53. TotalNum int `json:"total_num"`
  54. }
  55. // ResAdPosList 公众号分广告位列表
  56. type ResAdPosList struct {
  57. SlotID int64 `json:"slot_id"`
  58. AdSlot string `json:"ad_slot"`
  59. Date string `json:"date"`
  60. ReqSuccCount int `json:"req_succ_count"`
  61. ExposureCount int `json:"exposure_count"`
  62. ExposureRate float64 `json:"exposure_rate"`
  63. ClickCount int `json:"click_count"`
  64. ClickRate float64 `json:"click_rate"`
  65. Income int `json:"income"`
  66. Ecpm float64 `json:"ecpm"`
  67. }
  68. // ResAdPosSummary 公众号分广告位概览
  69. type ResAdPosSummary struct {
  70. ReqSuccCount int `json:"req_succ_count"`
  71. ExposureCount int `json:"exposure_count"`
  72. ExposureRate float64 `json:"exposure_rate"`
  73. ClickCount int `json:"click_count"`
  74. ClickRate float64 `json:"click_rate"`
  75. Income int `json:"income"`
  76. Ecpm float64 `json:"ecpm"`
  77. }
  78. // ResPublisherCps 公众号返佣商品数据响应
  79. type ResPublisherCps struct {
  80. util.CommonError
  81. BaseResp BaseResp `json:"base_resp"`
  82. List []ResCpsList `json:"list"`
  83. Summary ResCpsSummary `json:"summary"`
  84. TotalNum int `json:"total_num"`
  85. }
  86. // ResCpsList 公众号返佣商品列表
  87. type ResCpsList struct {
  88. Date string `json:"date"`
  89. ExposureCount int `json:"exposure_count"`
  90. ClickCount int `json:"click_count"`
  91. ClickRate float64 `json:"click_rate"`
  92. OrderCount int `json:"order_count"`
  93. OrderRate float64 `json:"order_rate"`
  94. TotalFee int `json:"total_fee"`
  95. TotalCommission int `json:"total_commission"`
  96. }
  97. // ResCpsSummary 公众号返佣概览
  98. type ResCpsSummary struct {
  99. ExposureCount int `json:"exposure_count"`
  100. ClickCount int `json:"click_count"`
  101. ClickRate float64 `json:"click_rate"`
  102. OrderCount int `json:"order_count"`
  103. OrderRate float64 `json:"order_rate"`
  104. TotalFee int `json:"total_fee"`
  105. TotalCommission int `json:"total_commission"`
  106. }
  107. // ResPublisherSettlement 公众号结算收入数据及结算主体信息响应
  108. type ResPublisherSettlement struct {
  109. util.CommonError
  110. BaseResp BaseResp `json:"base_resp"`
  111. Body string `json:"body"`
  112. PenaltyAll int `json:"penalty_all"`
  113. RevenueAll int64 `json:"revenue_all"`
  114. SettledRevenueAll int64 `json:"settled_revenue_all"`
  115. SettlementList []SettlementList `json:"settlement_list"`
  116. TotalNum int `json:"total_num"`
  117. }
  118. // SettlementList 结算单列表
  119. type SettlementList struct {
  120. Date string `json:"date"`
  121. Zone string `json:"zone"`
  122. Month string `json:"month"`
  123. Order int `json:"order"`
  124. SettStatus int `json:"sett_status"`
  125. SettledRevenue int `json:"settled_revenue"`
  126. SettNo string `json:"sett_no"`
  127. MailSendCnt string `json:"mail_send_cnt"`
  128. SlotRevenue []SlotRevenue `json:"slot_revenue"`
  129. }
  130. // SlotRevenue 产生收入的广告
  131. type SlotRevenue struct {
  132. SlotID string `json:"slot_id"`
  133. SlotSettledRevenue int `json:"slot_settled_revenue"`
  134. }
  135. // ParamsPublisher 拉取数据参数
  136. type ParamsPublisher struct {
  137. Action string `json:"action"`
  138. StartDate string `json:"start_date"`
  139. EndDate string `json:"end_date"`
  140. Page int `json:"page"`
  141. PageSize int `json:"page_size"`
  142. AdSlot AdSlot `json:"ad_slot"`
  143. }
  144. // fetchData 拉取统计数据
  145. func (cube *DataCube) fetchData(params ParamsPublisher) (response []byte, err error) {
  146. accessToken, err := cube.GetAccessToken()
  147. if err != nil {
  148. return
  149. }
  150. v := url.Values{}
  151. v.Add("action", params.Action)
  152. v.Add("access_token", accessToken)
  153. v.Add("page", strconv.Itoa(params.Page))
  154. v.Add("page_size", strconv.Itoa(params.PageSize))
  155. v.Add("start_date", params.StartDate)
  156. v.Add("end_date", params.EndDate)
  157. if params.AdSlot != "" {
  158. v.Add("ad_slot", string(params.AdSlot))
  159. }
  160. uri := fmt.Sprintf("%s?%s", publisherURL, v.Encode())
  161. response, err = util.HTTPGet(uri)
  162. if err != nil {
  163. return
  164. }
  165. return
  166. }
  167. // GetPublisherAdPosGeneral 获取公众号分广告位数据
  168. func (cube *DataCube) GetPublisherAdPosGeneral(startDate, endDate string, page, pageSize int, adSlot AdSlot) (resPublisherAdPos ResPublisherAdPos, err error) {
  169. params := ParamsPublisher{
  170. Action: actionPublisherAdPosGeneral,
  171. StartDate: startDate,
  172. EndDate: endDate,
  173. Page: page,
  174. PageSize: pageSize,
  175. AdSlot: adSlot,
  176. }
  177. response, err := cube.fetchData(params)
  178. if err != nil {
  179. return
  180. }
  181. err = util.DecodeWithError(response, &resPublisherAdPos, "GetPublisherAdPosGeneral")
  182. if err != nil {
  183. return
  184. }
  185. if resPublisherAdPos.BaseResp.Ret != 0 {
  186. err = fmt.Errorf("GetPublisherAdPosGeneral Error , errcode=%d , errmsg=%s", resPublisherAdPos.BaseResp.Ret, resPublisherAdPos.BaseResp.ErrMsg)
  187. return
  188. }
  189. return
  190. }
  191. // GetPublisherCpsGeneral 获取公众号返佣商品数据
  192. func (cube *DataCube) GetPublisherCpsGeneral(startDate, endDate string, page, pageSize int) (resPublisherCps ResPublisherCps, err error) {
  193. params := ParamsPublisher{
  194. Action: actionPublisherCpsGeneral,
  195. StartDate: startDate,
  196. EndDate: endDate,
  197. Page: page,
  198. PageSize: pageSize,
  199. }
  200. response, err := cube.fetchData(params)
  201. if err != nil {
  202. return
  203. }
  204. err = util.DecodeWithError(response, &resPublisherCps, "GetPublisherCpsGeneral")
  205. if err != nil {
  206. return
  207. }
  208. if resPublisherCps.BaseResp.Ret != 0 {
  209. err = fmt.Errorf("GetPublisherCpsGeneral Error , errcode=%d , errmsg=%s", resPublisherCps.BaseResp.Ret, resPublisherCps.BaseResp.ErrMsg)
  210. return
  211. }
  212. return
  213. }
  214. // GetPublisherSettlement 获取公众号结算收入数据及结算主体信息
  215. func (cube *DataCube) GetPublisherSettlement(startDate, endDate string, page, pageSize int) (resPublisherSettlement ResPublisherSettlement, err error) {
  216. params := ParamsPublisher{
  217. Action: actionPublisherSettlement,
  218. StartDate: startDate,
  219. EndDate: endDate,
  220. Page: page,
  221. PageSize: pageSize,
  222. }
  223. response, err := cube.fetchData(params)
  224. if err != nil {
  225. return
  226. }
  227. err = util.DecodeWithError(response, &resPublisherSettlement, "GetPublisherSettlement")
  228. if err != nil {
  229. return
  230. }
  231. if resPublisherSettlement.BaseResp.Ret != 0 {
  232. err = fmt.Errorf("GetPublisherSettlement Error , errcode=%d , errmsg=%s", resPublisherSettlement.BaseResp.Ret, resPublisherSettlement.BaseResp.ErrMsg)
  233. return
  234. }
  235. return
  236. }