Stats.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. <?php
  2. /*
  3. * This file is part of the overtrue/wechat.
  4. *
  5. * (c) overtrue <i@overtrue.me>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. /**
  11. * Stats.php.
  12. *
  13. * @author overtrue <i@overtrue.me>
  14. * @copyright 2015 overtrue <i@overtrue.me>
  15. *
  16. * @see https://github.com/overtrue
  17. * @see http://overtrue.me
  18. */
  19. namespace EasyWeChat\Stats;
  20. use EasyWeChat\Core\AbstractAPI;
  21. /**
  22. * Class stats.
  23. */
  24. class Stats extends AbstractAPI
  25. {
  26. // 获取用户增减数据
  27. const API_USER_SUMMARY = 'https://api.weixin.qq.com/datacube/getusersummary';
  28. // 获取累计用户数据
  29. const API_USER_CUMULATE = 'https://api.weixin.qq.com/datacube/getusercumulate';
  30. // 获取图文群发每日数据
  31. const API_ARTICLE_SUMMARY = 'https://api.weixin.qq.com/datacube/getarticlesummary';
  32. // 获取图文群发总数据
  33. const API_ARTICLE_TOTAL = 'https://api.weixin.qq.com/datacube/getarticletotal';
  34. // 获取图文统计数据
  35. const API_USER_READ_SUMMARY = 'https://api.weixin.qq.com/datacube/getuserread';
  36. // 获取图文统计分时数据
  37. const API_USER_READ_HOURLY = 'https://api.weixin.qq.com/datacube/getuserreadhour';
  38. // 获取图文分享转发数据
  39. const API_USER_SHARE_SUMMARY = 'https://api.weixin.qq.com/datacube/getusershare';
  40. // 获取图文分享转发分时数据
  41. const API_USER_SHARE_HOURLY = 'https://api.weixin.qq.com/datacube/getusersharehour';
  42. // 获取消息发送概况数据
  43. const API_UPSTREAM_MSG_SUMMARY = 'https://api.weixin.qq.com/datacube/getupstreammsg';
  44. // 获取消息分送分时数据
  45. const API_UPSTREAM_MSG_HOURLY = 'https://api.weixin.qq.com/datacube/getupstreammsghour';
  46. // 获取消息发送周数据
  47. const API_UPSTREAM_MSG_WEEKLY = 'https://api.weixin.qq.com/datacube/getupstreammsgweek';
  48. // 获取消息发送月数据
  49. const API_UPSTREAM_MSG_MONTHLY = 'https://api.weixin.qq.com/datacube/getupstreammsgmonth';
  50. // 获取消息发送分布数据
  51. const API_UPSTREAM_MSG_DIST_SUMMARY = 'https://api.weixin.qq.com/datacube/getupstreammsgdist';
  52. // 获取消息发送分布周数据
  53. const API_UPSTREAM_MSG_DIST_WEEKLY = 'https://api.weixin.qq.com/datacube/getupstreammsgdistweek';
  54. // 获取消息发送分布月数据
  55. const API_UPSTREAM_MSG_DIST_MONTHLY = 'https://api.weixin.qq.com/datacube/getupstreammsgdistmonth?';
  56. // 获取接口分析数据
  57. const API_INTERFACE_SUMMARY = 'https://api.weixin.qq.com/datacube/getinterfacesummary';
  58. // 获取接口分析分时数据
  59. const API_INTERFACE_SUMMARY_HOURLY = 'https://api.weixin.qq.com/datacube/getinterfacesummaryhour';
  60. // 拉取卡券概况数据接口
  61. const API_CARD_SUMMARY = 'https://api.weixin.qq.com/datacube/getcardbizuininfo';
  62. // 获取免费券数据接口
  63. const API_FREE_CARD_SUMMARY = 'https://api.weixin.qq.com/datacube/getcardcardinfo';
  64. // 拉取会员卡数据接口
  65. const API_MEMBER_CARD_SUMMARY = 'https://api.weixin.qq.com/datacube/getcardmembercardinfo';
  66. /**
  67. * 获取用户增减数据.
  68. *
  69. * @param string $from
  70. * @param string $to
  71. *
  72. * @return \EasyWeChat\Support\Collection
  73. */
  74. public function userSummary($from, $to)
  75. {
  76. return $this->query(self::API_USER_SUMMARY, $from, $to);
  77. }
  78. /**
  79. * 获取累计用户数据.
  80. *
  81. * @param string $from
  82. * @param string $to
  83. *
  84. * @return \EasyWeChat\Support\Collection
  85. */
  86. public function userCumulate($from, $to)
  87. {
  88. return $this->query(self::API_USER_CUMULATE, $from, $to);
  89. }
  90. /**
  91. * 获取图文群发每日数据.
  92. *
  93. * @param string $from
  94. * @param string $to
  95. *
  96. * @return \EasyWeChat\Support\Collection
  97. */
  98. public function articleSummary($from, $to)
  99. {
  100. return $this->query(self::API_ARTICLE_SUMMARY, $from, $to);
  101. }
  102. /**
  103. * 获取图文群发总数据.
  104. *
  105. * @param string $from
  106. * @param string $to
  107. *
  108. * @return \EasyWeChat\Support\Collection
  109. */
  110. public function articleTotal($from, $to)
  111. {
  112. return $this->query(self::API_ARTICLE_TOTAL, $from, $to);
  113. }
  114. /**
  115. * 获取图文统计数据.
  116. *
  117. * @param string $from
  118. * @param string $to
  119. *
  120. * @return \EasyWeChat\Support\Collection
  121. */
  122. public function userReadSummary($from, $to)
  123. {
  124. return $this->query(self::API_USER_READ_SUMMARY, $from, $to);
  125. }
  126. /**
  127. * 获取图文统计分时数据.
  128. *
  129. * @param string $from
  130. * @param string $to
  131. *
  132. * @return \EasyWeChat\Support\Collection
  133. */
  134. public function userReadHourly($from, $to)
  135. {
  136. return $this->query(self::API_USER_READ_HOURLY, $from, $to);
  137. }
  138. /**
  139. * 获取图文分享转发数据.
  140. *
  141. * @param string $from
  142. * @param string $to
  143. *
  144. * @return \EasyWeChat\Support\Collection
  145. */
  146. public function userShareSummary($from, $to)
  147. {
  148. return $this->query(self::API_USER_SHARE_SUMMARY, $from, $to);
  149. }
  150. /**
  151. * 获取图文分享转发分时数据.
  152. *
  153. * @param string $from
  154. * @param string $to
  155. *
  156. * @return \EasyWeChat\Support\Collection
  157. */
  158. public function userShareHourly($from, $to)
  159. {
  160. return $this->query(self::API_USER_SHARE_HOURLY, $from, $to);
  161. }
  162. /**
  163. * 获取消息发送概况数据.
  164. *
  165. * @param string $from
  166. * @param string $to
  167. *
  168. * @return \EasyWeChat\Support\Collection
  169. */
  170. public function upstreamMessageSummary($from, $to)
  171. {
  172. return $this->query(self::API_UPSTREAM_MSG_SUMMARY, $from, $to);
  173. }
  174. /**
  175. * 获取消息分送分时数据.
  176. *
  177. * @param string $from
  178. * @param string $to
  179. *
  180. * @return \EasyWeChat\Support\Collection
  181. */
  182. public function upstreamMessageHourly($from, $to)
  183. {
  184. return $this->query(self::API_UPSTREAM_MSG_HOURLY, $from, $to);
  185. }
  186. /**
  187. * 获取消息发送周数据.
  188. *
  189. * @param string $from
  190. * @param string $to
  191. *
  192. * @return \EasyWeChat\Support\Collection
  193. */
  194. public function upstreamMessageWeekly($from, $to)
  195. {
  196. return $this->query(self::API_UPSTREAM_MSG_WEEKLY, $from, $to);
  197. }
  198. /**
  199. * 获取消息发送月数据.
  200. *
  201. * @param string $from
  202. * @param string $to
  203. *
  204. * @return \EasyWeChat\Support\Collection
  205. */
  206. public function upstreamMessageMonthly($from, $to)
  207. {
  208. return $this->query(self::API_UPSTREAM_MSG_MONTHLY, $from, $to);
  209. }
  210. /**
  211. * 获取消息发送分布数据.
  212. *
  213. * @param string $from
  214. * @param string $to
  215. *
  216. * @return \EasyWeChat\Support\Collection
  217. */
  218. public function upstreamMessageDistSummary($from, $to)
  219. {
  220. return $this->query(self::API_UPSTREAM_MSG_DIST_SUMMARY, $from, $to);
  221. }
  222. /**
  223. * 获取消息发送分布周数据.
  224. *
  225. * @param string $from
  226. * @param string $to
  227. *
  228. * @return \EasyWeChat\Support\Collection
  229. */
  230. public function upstreamMessageDistWeekly($from, $to)
  231. {
  232. return $this->query(self::API_UPSTREAM_MSG_DIST_WEEKLY, $from, $to);
  233. }
  234. /**
  235. * 获取消息发送分布月数据.
  236. *
  237. * @param string $from
  238. * @param string $to
  239. *
  240. * @return \EasyWeChat\Support\Collection
  241. */
  242. public function upstreamMessageDistMonthly($from, $to)
  243. {
  244. return $this->query(self::API_UPSTREAM_MSG_DIST_MONTHLY, $from, $to);
  245. }
  246. /**
  247. * 获取接口分析数据.
  248. *
  249. * @param string $from
  250. * @param string $to
  251. *
  252. * @return \EasyWeChat\Support\Collection
  253. */
  254. public function interfaceSummary($from, $to)
  255. {
  256. return $this->query(self::API_INTERFACE_SUMMARY, $from, $to);
  257. }
  258. /**
  259. * 获取接口分析分时数据.
  260. *
  261. * @param string $from
  262. * @param string $to
  263. *
  264. * @return \EasyWeChat\Support\Collection
  265. */
  266. public function interfaceSummaryHourly($from, $to)
  267. {
  268. return $this->query(self::API_INTERFACE_SUMMARY_HOURLY, $from, $to);
  269. }
  270. /**
  271. * 拉取卡券概况数据接口.
  272. *
  273. * @param string $from
  274. * @param string $to
  275. * @param int $condSource
  276. *
  277. * @return \EasyWeChat\Support\Collection
  278. */
  279. public function cardSummary($from, $to, $condSource = 0)
  280. {
  281. $ext = [
  282. 'cond_source' => intval($condSource),
  283. ];
  284. return $this->query(self::API_CARD_SUMMARY, $from, $to, $ext);
  285. }
  286. /**
  287. * 获取免费券数据接口.
  288. *
  289. * @param string $from
  290. * @param string $to
  291. * @param int $condSource
  292. * @param string $cardId
  293. *
  294. * @return \EasyWeChat\Support\Collection
  295. */
  296. public function freeCardSummary($from, $to, $condSource = 0, $cardId = '')
  297. {
  298. $ext = [
  299. 'cond_source' => intval($condSource),
  300. 'card_id' => $cardId,
  301. ];
  302. return $this->query(self::API_FREE_CARD_SUMMARY, $from, $to, $ext);
  303. }
  304. /**
  305. * 拉取会员卡数据接口.
  306. *
  307. * @param string $from
  308. * @param string $to
  309. * @param int $condSource
  310. *
  311. * @return \EasyWeChat\Support\Collection
  312. */
  313. public function memberCardSummary($from, $to, $condSource = 0)
  314. {
  315. $ext = [
  316. 'cond_source' => intval($condSource),
  317. ];
  318. return $this->query(self::API_MEMBER_CARD_SUMMARY, $from, $to, $ext);
  319. }
  320. /**
  321. * 查询数据.
  322. *
  323. * @param string $api
  324. * @param string $from
  325. * @param string $to
  326. *
  327. * @return \EasyWeChat\Support\Collection
  328. */
  329. protected function query($api, $from, $to, array $ext = [])
  330. {
  331. $params = [
  332. 'begin_date' => $from,
  333. 'end_date' => $to,
  334. ];
  335. if (!empty($ext)) {
  336. $params = array_merge($params, $ext);
  337. }
  338. return $this->parseJSON('json', [$api, $params]);
  339. }
  340. }