Stat.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com
  8. * =========================================================
  9. */
  10. namespace addon\wechat\model;
  11. use app\model\BaseModel;
  12. use think\facade\Cache;
  13. /**
  14. * 数据统计与分析
  15. */
  16. class Stat extends BaseModel
  17. {
  18. /**
  19. * 获取用户增减数据, 最大时间跨度:7
  20. * @param $begin_date
  21. * @param $end_date
  22. * @return array|\multitype
  23. */
  24. public function userSummary($begin_date, $end_date)
  25. {
  26. $cache = Cache::get("wechat_user_summary_" . "_" . $begin_date . "_" . $end_date);
  27. if (!empty($cache))
  28. return $this->success($cache);
  29. $wechat_model = new Wechat();
  30. $result = $wechat_model->userSummary($begin_date, $end_date);
  31. if ($result["code"] < 0)
  32. return $result;
  33. Cache::tag("wechat_visit")->set("wechat_user_summary_" . "_" . $begin_date . "_" . $end_date);
  34. return $result;
  35. }
  36. /**
  37. * 获取累计用户数据, 最大时间跨度:7;
  38. * @param $begin_date
  39. * @param $end_date
  40. * @return array|\multitype
  41. */
  42. public function userCumulate($begin_date, $end_date)
  43. {
  44. $cache = Cache::get("wechat_user_cumulate_" . "_" . $begin_date . "_" . $end_date);
  45. if (!empty($cache))
  46. return $this->success($cache);
  47. $wechat_model = new Wechat();
  48. $result = $wechat_model->userCumulate($begin_date, $end_date);
  49. if ($result["code"] < 0)
  50. return $result;
  51. Cache::tag("wechat_visit")->set("wechat_user_cumulate_" . "_" . $begin_date . "_" . $end_date);
  52. return $result;
  53. }
  54. /**
  55. * 获取接口分析分时数据, 最大时间跨度:1;
  56. * @param $begin_date
  57. * @param $end_date
  58. * @return array|\multitype
  59. */
  60. public function interfaceSummaryHourly($begin_date, $end_date)
  61. {
  62. $cache = Cache::get("wechat_interface_summary_hourly_" . "_" . $begin_date . "_" . $end_date);
  63. if (!empty($cache))
  64. return $this->success($cache);
  65. $wechat_model = new Wechat();
  66. $result = $wechat_model->interfaceSummaryHourly($begin_date, $end_date);
  67. if ($result["code"] < 0)
  68. return $result;
  69. Cache::tag("wechat_visit")->set("wechat_interface_summary_hourly_" . "_" . $begin_date . "_" . $end_date);
  70. return $result;
  71. }
  72. /**
  73. * 获取接口分析数据, 最大时间跨度:30;
  74. * @param $begin_date
  75. * @param $end_date
  76. * @return array|\multitype
  77. */
  78. public function interfaceSummary($begin_date, $end_date)
  79. {
  80. // $cache = Cache::get("wechat_interface_summary_" . "_" . $begin_date."_" . $end_date);
  81. // if (!empty($cache))
  82. // return $this->success($cache);
  83. $wechat_model = new Wechat();
  84. $result = $wechat_model->interfaceSummary($begin_date, $end_date);
  85. if ($result["code"] < 0)
  86. return $result;
  87. Cache::tag("wechat_visit")->set("wechat_interface_summary_" . "_" . $begin_date . "_" . $end_date);
  88. return $result;
  89. }
  90. /**
  91. * 获取微信粉丝统计(以天为单位)
  92. * @param unknown $param
  93. */
  94. public function fans($begin_date, $end_date)
  95. {
  96. // $cache = Cache::get("wechat_fans_statistics_" . "_" . $begin_date."_" . $end_date);
  97. // if (!empty($cache)) {
  98. // return $this->success($cache);
  99. // }
  100. // $data = [ 'begin_date' => $param['begin_date'], 'end_date' => $param['end_date'] ];
  101. $user_summary_result = $this->userSummary($begin_date, $end_date);
  102. if ($user_summary_result["code"] < 0) {
  103. return $user_summary_result;
  104. }
  105. $user_cumulate_result = $this->userCumulate($begin_date, $end_date);
  106. if ($user_cumulate_result["code"] < 0) {
  107. return $user_cumulate_result;
  108. }
  109. $list = [];
  110. foreach ($user_cumulate_result["data"] as $cumulate_k => $cumulate_v) {
  111. $temp_item = $cumulate_v;
  112. $temp_item['cumulate_user'] = empty($cumulate_v['cumulate_user']) ? 0 : $cumulate_v['cumulate_user'];
  113. $new_user = 0;
  114. $cancel_user = 0;
  115. $net_growth_user = 0;
  116. foreach ($user_summary_result['data'] as $key => $item) {
  117. if ($item["ref_date"] == $cumulate_v["ref_date"]) {
  118. $new_user += $item['new_user'];
  119. $cancel_user += $item['cancel_user'];
  120. $net_growth_user += $item["new_user"] - $item["cancel_user"];
  121. }
  122. }
  123. $temp_item['new_user'] = $new_user;
  124. $temp_item['cancel_user'] = $cancel_user;
  125. $temp_item['net_growth_user'] = $net_growth_user;
  126. $list[] = $temp_item;
  127. }
  128. Cache::tag("wechat_visit")->set("wechat_fans_statistics_" . "_" . $begin_date . "_" . $end_date, $list);
  129. return $this->success($list);
  130. }
  131. /**
  132. * 接口访问数据图
  133. * @param $date_type
  134. * @return array
  135. */
  136. public function interfaceSummaryStatistics($date_type)
  137. {
  138. $date_data = $this->getDaterange($date_type);
  139. $is_error = true;
  140. $callback_count_data = [];
  141. $fail_count_data = [];
  142. $average_time_cost_data = [];
  143. $max_time_cost_data = [];
  144. foreach ($date_data["date_list"] as $k => $v) {
  145. $callback_count = 0;
  146. $fail_count = 0;
  147. $average_time_cost = 0;
  148. $max_time_cost = 0;
  149. if ($is_error) {
  150. $temp_data = $this->interfaceSummary($v, $v);
  151. if (!empty($temp_data["data"])) {
  152. $temp_date_item = $temp_data["data"];
  153. $callback_count = $temp_date_item[0]["callback_count"];
  154. $fail_count = $temp_date_item[0]["fail_count"];
  155. $average_time_cost = $temp_date_item[0]["total_time_cost"] / $temp_date_item[0]["callback_count"];
  156. $max_time_cost = $temp_date_item[0]["max_time_cost"];
  157. } else {
  158. $is_error = false;
  159. }
  160. }
  161. $callback_count_data[] = $callback_count;
  162. $fail_count_data[] = $fail_count;
  163. $average_time_cost_data[] = $average_time_cost;
  164. $max_time_cost_data[] = $max_time_cost;
  165. }
  166. $return_data = array(
  167. "date" => $date_data["date_list"],
  168. "data" => array(
  169. "callback_count_data" => $callback_count_data,
  170. "fail_count_data" => $fail_count_data,
  171. "average_time_cost_data" => $average_time_cost_data,
  172. "max_time_cost_data" => $max_time_cost_data
  173. )
  174. );
  175. return $this->success($return_data);
  176. }
  177. /**
  178. * 用户访问统计图
  179. * @param $date_type
  180. * @return array
  181. */
  182. public function userSummaryStatistics($date_type)
  183. {
  184. $is_error = true;
  185. $date_data = $this->getDaterange($date_type);
  186. $new_user_data = [];
  187. $cancel_user_data = [];
  188. $net_growth_user_data = [];
  189. $cumulate_user_data = [];
  190. foreach ($date_data["date_list"] as $k => $v) {
  191. $new_user = 0;
  192. $cancel_user = 0;
  193. $net_growth_user = 0;
  194. $cumulate_user = 0;
  195. if ($is_error) {
  196. $temp_data = $this->fans($v, $v);
  197. if (!empty($temp_data["data"])) {
  198. $temp_date_item = $temp_data["data"];
  199. $new_user = $temp_date_item[0]["new_user"];
  200. $cancel_user = $temp_date_item[0]["cancel_user"];
  201. $net_growth_user = $temp_date_item[0]["net_growth_user"];
  202. $cumulate_user = $temp_date_item[0]["cumulate_user"];
  203. } else {
  204. $is_error = false;
  205. }
  206. }
  207. $new_user_data[] = $new_user;
  208. $cancel_user_data[] = $cancel_user;
  209. $net_growth_user_data[] = $net_growth_user;
  210. $cumulate_user_data[] = $cumulate_user;
  211. }
  212. $return_data = array(
  213. "date" => $date_data["date_list"],
  214. "data" => array(
  215. "new_user_data" => $new_user_data,
  216. "cancel_user_data" => $cancel_user_data,
  217. "net_growth_user_data" => $net_growth_user_data,
  218. "cumulate_user_data" => $cumulate_user_data
  219. )
  220. );
  221. return $this->success($return_data);
  222. }
  223. /**
  224. * 获取时间间隔
  225. * @param $date_type
  226. * @return array
  227. */
  228. public function getDaterange($date_type)
  229. {
  230. $yesterday = strtotime('-1 days');
  231. switch ($date_type) {
  232. case 'yesterday':
  233. $yesterday = strtotime('-1 days');
  234. $begin_time = $yesterday;
  235. $end_time = $yesterday;
  236. break;
  237. case 'week':
  238. $week = strtotime('-7 days');
  239. $begin_time = $week;
  240. $end_time = $yesterday;
  241. break;
  242. case 'month':
  243. $month = strtotime('-30 days');
  244. $begin_time = $month;
  245. $end_time = $yesterday;
  246. break;
  247. }
  248. $date_x = periodGroup($begin_time, $end_time, "Y-m-d");
  249. $begin_date = date("Ymd", $begin_time);
  250. $end_date = date("Ymd", $end_time);
  251. $data = array(
  252. "begin_date" => $begin_date,
  253. "end_date" => $end_date,
  254. "date_list" => $date_x,
  255. );
  256. return $data;
  257. }
  258. }