Vipcustomurl.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use app\common\library\Redis;
  5. use app\main\constants\AdminConstants;
  6. use app\main\constants\CustomConstants;
  7. use app\main\service\CustomService;
  8. /**
  9. * 客服消息分链接统计表,cps系统使用
  10. *
  11. * @icon fa fa-circle-o
  12. */
  13. class Vipcustomurl extends Backend
  14. {
  15. /**
  16. * CustomUrl模型对象
  17. */
  18. protected $model = null;
  19. /**
  20. * 无需鉴权的方法,但需要登录
  21. * @var array
  22. */
  23. protected $noNeedRight = ['export'];
  24. public function _initialize()
  25. {
  26. parent::_initialize();
  27. $this->model = model('CustomUrl');
  28. $this->view->assign("messageTypeList", $this->model->getMessageTypeList());
  29. $this->view->assign("typeList", $this->model->getTypeList());
  30. $this->view->assign("officialAccountTypeList", $this->model->getOfficialAccountTypeList());
  31. $this->view->assign("pushTypeList", $this->model->getPushTypeList());
  32. if (!empty($_GET) && !$this->request->isAjax()) {
  33. $paramsStr = "";
  34. foreach ($_GET as $k => $v) {
  35. if ($k == "addtabs") {
  36. continue;
  37. }
  38. $paramsStr .= "&{$k}={$v}";
  39. }
  40. $this->assignconfig("path", $paramsStr);
  41. }
  42. }
  43. /**
  44. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法
  45. * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑
  46. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  47. */
  48. /**
  49. * 查看
  50. */
  51. public function index($message_type = '0')
  52. {
  53. $redis = Redis::instance();
  54. //设置过滤方法
  55. $this->request->filter(['strip_tags']);
  56. if ($this->request->isAjax()) {
  57. //如果发送的来源是Selectpage,则转发到Selectpage
  58. if ($this->request->request('pkey_name')) {
  59. return $this->selectpage();
  60. }
  61. if ($this->auth->checkGroupId(AdminConstants::ADMIN_GROUP_ID_VIP_OPERATOR) || $this->auth->checkGroupId(AdminConstants::ADMIN_GROUP_ID_VIP)) {
  62. //VIP || VIP运营者
  63. $adminIds = model("VipAdminBind")->getAdminIdsByVip($this->auth->id);//获取服务号id
  64. $adminIds = empty($adminIds) ? $this->auth->id : implode(",", $adminIds);
  65. } else {
  66. $adminIds = $this->auth->id;
  67. }
  68. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  69. $map = [];
  70. $map['custom.statue'] = ['<>', 'delete'];
  71. $map['custom.message_type'] = $this->request->get("message_type");
  72. if ($push_id = $this->request->get("push_id")) {
  73. $map['custom_url.custom_media_push_id'] = $push_id;
  74. unset($map['custom_url.message_type']);
  75. }
  76. $map['custom_url.official_account_id'] = ['in', $adminIds];
  77. $result = CustomService::instance()->getCustomIds($where, $map, $offset, $limit)->data;
  78. if ($result['total']) {
  79. $result['rows'] = CustomService::instance()->getCustomCollectData($result['ids'])->data;
  80. } else {
  81. $result['rows'] = [];
  82. }
  83. // $total = CustomService::instance()->getCustomAnalysis($map, $where, $offset, $limit, $sort, $order)->data;
  84. // $list = CustomService::instance()->getCustomAnalysis($map, $where, $offset, $limit, $sort, $order, false)->data;
  85. return json($result);
  86. }
  87. $this->view->assign("message_type", $message_type);
  88. return $this->view->fetch();
  89. }
  90. /**
  91. * 导出
  92. */
  93. public function export($message_type = '0')
  94. {
  95. ini_set('memory_limit', '256M'); //内存限制
  96. $columns = ['推广ID'];
  97. //VIP显示公众号昵称
  98. if ($this->group == AdminConstants::ADMIN_GROUP_ID_VIP ||
  99. $this->group == AdminConstants::ADMIN_GROUP_ID_VIP_OPERATOR
  100. ) {
  101. array_push($columns, '公众号名称');
  102. }
  103. $columns = array_merge($columns, ['客服消息标题', '推广标题','渠道商名称', '书籍名称', '推广位置', '推广类型', '发送人数', '总UV', '今日UV', '总充值金额', '今日充值金额', '推送时间']);
  104. header('Content-Description: File Transfer');
  105. header('Content-Type: application/vnd.ms-excel');
  106. header('Content-Disposition: attachment; filename="客服消息数据统计导出-' . date('YmdHis', time()) . '.csv"');
  107. header('Expires: 0');
  108. header('Cache-Control: must-revalidate');
  109. header('Pragma: public');
  110. $fp = fopen('php://output', 'a');//打开output流
  111. mb_convert_variables('GBK', 'UTF-8', $columns);
  112. fputcsv($fp, $columns);//将数据格式化为CSV格式并写入到output流中
  113. $redis = Redis::instance();
  114. if ($this->auth->checkGroupId(AdminConstants::ADMIN_GROUP_ID_VIP_OPERATOR) || $this->auth->checkGroupId(AdminConstants::ADMIN_GROUP_ID_VIP)) {
  115. //VIP || VIP运营者
  116. $adminIds = model("VipAdminBind")->getAdminIdsByVip($this->auth->id);//获取服务号id
  117. $adminIds = empty($adminIds) ? $this->auth->id : implode(",", $adminIds);
  118. } else {
  119. $adminIds = $this->auth->id;
  120. }
  121. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  122. $map['custom.statue'] = ['<>', 'delete'];
  123. $map['custom_url.message_type'] = $this->request->get("message_type");
  124. if ($push_id = $this->request->get("push_id")) {
  125. $map['custom_url.custom_media_push_id'] = $push_id;
  126. unset($map['custom_url.message_type']);
  127. }
  128. $map['custom_url.official_account_id'] = ['in', $adminIds];
  129. $total = model('custom_url')
  130. ->join('custom', 'custom.id=custom_url.custom_id')
  131. ->where($where)
  132. ->where($map)
  133. ->count();
  134. $limit = 100;
  135. $pages = ceil($total / $limit);
  136. for($i = 1; $i <= $pages; $i++) {
  137. $offset = ($i - 1) * $limit;
  138. $result = CustomService::instance()->getCustomIds($where, $map, $offset, $limit)->data;
  139. if ($result['total']) {
  140. $list = CustomService::instance()->getCustomCollectData($result['ids'])->data;
  141. } else {
  142. $list = [];
  143. }
  144. $types = ['0' => __('Type 0'),'1' => __('Type 1'),'2' => __('Type 2'),'3' => __('Type 3'),'4' => __('Type 4'),'5' => __('Type 5'),'6' => __('Type 6'),'7' => __('自动签到'),'8' => __('常用链接'),"9"=> "自定义活动"];
  145. foreach ($list as $item) {
  146. foreach ($item['collect'] as $collect) {
  147. //获取每列数据,转换处理成需要导出的数据
  148. $rowData = [];
  149. array_push($rowData, $item['id']);
  150. //VIP显示公众号昵称
  151. if ($this->group == AdminConstants::ADMIN_GROUP_ID_VIP ||
  152. $this->group == AdminConstants::ADMIN_GROUP_ID_VIP_OPERATOR
  153. ) {
  154. array_push($rowData, $collect['official_account_name']);
  155. }
  156. //客服消息标题
  157. array_push($rowData, $item['title']);
  158. //推广标题
  159. array_push($rowData, $collect['title']);
  160. //渠道商账号
  161. array_push($rowData, $item['username']);
  162. //书籍名称
  163. array_push($rowData, $collect['book_name']);
  164. //推广位置
  165. array_push($rowData, $collect['idx']);
  166. //推广类型
  167. array_push($rowData, $types[$collect['type']]);
  168. //发送人数
  169. array_push($rowData, "\t".$item['send_num']);
  170. //总UV
  171. array_push($rowData, "\t".($collect['uv'] ?? '0'));
  172. //今日UV
  173. array_push($rowData, "\t".($collect['day_uv']??'0'));
  174. //总充值金额
  175. array_push($rowData, "\t".($collect['recharge_money'] ?? '0'));
  176. //今日充值金额
  177. array_push($rowData, "\t".($collect['day_recharge_money']??'0'));
  178. //推送时间
  179. array_push($rowData, date("Y-m-d H:i:s", $item['sendtime']));
  180. //需要格式转换,否则会乱码
  181. mb_convert_variables('GBK', 'UTF-8', $rowData);
  182. fputcsv($fp, $rowData);
  183. }
  184. //必须同时使用 ob_flush() 和flush() 函数来刷新输出缓冲。
  185. ob_flush();
  186. flush();
  187. }
  188. }
  189. fclose($fp);
  190. exit();
  191. }
  192. }