model = model('CustomUrl'); $this->view->assign("messageTypeList", $this->model->getMessageTypeList()); $this->view->assign("typeList", $this->model->getTypeList()); $this->view->assign("officialAccountTypeList", $this->model->getOfficialAccountTypeList()); $this->view->assign("pushTypeList", $this->model->getPushTypeList()); if (!empty($_GET) && !$this->request->isAjax()) { $paramsStr = ""; foreach ($_GET as $k => $v) { if ($k == "addtabs") { continue; } $paramsStr .= "&{$k}={$v}"; } $this->assignconfig("path", $paramsStr); } } /** * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法 * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑 * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改 */ /** * 查看 */ public function index($message_type = '0') { $redis = Redis::instance(); //设置过滤方法 $this->request->filter(['strip_tags']); if ($this->request->isAjax()) { //如果发送的来源是Selectpage,则转发到Selectpage if ($this->request->request('pkey_name')) { return $this->selectpage(); } if ($this->auth->checkGroupId(AdminConstants::ADMIN_GROUP_ID_VIP_OPERATOR) || $this->auth->checkGroupId(AdminConstants::ADMIN_GROUP_ID_VIP)) { //VIP || VIP运营者 $adminIds = model("VipAdminBind")->getAdminIdsByVip($this->auth->id);//获取服务号id $adminIds = empty($adminIds) ? $this->auth->id : implode(",", $adminIds); } else { $adminIds = $this->auth->id; } list($where, $sort, $order, $offset, $limit) = $this->buildparams(); $map = []; $map['custom.statue'] = ['<>', 'delete']; $map['custom.message_type'] = $this->request->get("message_type"); if ($push_id = $this->request->get("push_id")) { $map['custom_url.custom_media_push_id'] = $push_id; unset($map['custom_url.message_type']); } $map['custom_url.official_account_id'] = ['in', $adminIds]; $result = CustomService::instance()->getCustomIds($where, $map, $offset, $limit)->data; if ($result['total']) { $result['rows'] = CustomService::instance()->getCustomCollectData($result['ids'])->data; } else { $result['rows'] = []; } // $total = CustomService::instance()->getCustomAnalysis($map, $where, $offset, $limit, $sort, $order)->data; // $list = CustomService::instance()->getCustomAnalysis($map, $where, $offset, $limit, $sort, $order, false)->data; return json($result); } $this->view->assign("message_type", $message_type); return $this->view->fetch(); } /** * 导出 */ public function export($message_type = '0') { ini_set('memory_limit', '256M'); //内存限制 $columns = ['推广ID']; //VIP显示公众号昵称 if ($this->group == AdminConstants::ADMIN_GROUP_ID_VIP || $this->group == AdminConstants::ADMIN_GROUP_ID_VIP_OPERATOR ) { array_push($columns, '公众号名称'); } $columns = array_merge($columns, ['客服消息标题', '推广标题','渠道商名称', '书籍名称', '推广位置', '推广类型', '发送人数', '总UV', '今日UV', '总充值金额', '今日充值金额', '推送时间']); header('Content-Description: File Transfer'); header('Content-Type: application/vnd.ms-excel'); header('Content-Disposition: attachment; filename="客服消息数据统计导出-' . date('YmdHis', time()) . '.csv"'); header('Expires: 0'); header('Cache-Control: must-revalidate'); header('Pragma: public'); $fp = fopen('php://output', 'a');//打开output流 mb_convert_variables('GBK', 'UTF-8', $columns); fputcsv($fp, $columns);//将数据格式化为CSV格式并写入到output流中 $redis = Redis::instance(); if ($this->auth->checkGroupId(AdminConstants::ADMIN_GROUP_ID_VIP_OPERATOR) || $this->auth->checkGroupId(AdminConstants::ADMIN_GROUP_ID_VIP)) { //VIP || VIP运营者 $adminIds = model("VipAdminBind")->getAdminIdsByVip($this->auth->id);//获取服务号id $adminIds = empty($adminIds) ? $this->auth->id : implode(",", $adminIds); } else { $adminIds = $this->auth->id; } list($where, $sort, $order, $offset, $limit) = $this->buildparams(); $map['custom.statue'] = ['<>', 'delete']; $map['custom_url.message_type'] = $this->request->get("message_type"); if ($push_id = $this->request->get("push_id")) { $map['custom_url.custom_media_push_id'] = $push_id; unset($map['custom_url.message_type']); } $map['custom_url.official_account_id'] = ['in', $adminIds]; $total = model('custom_url') ->join('custom', 'custom.id=custom_url.custom_id') ->where($where) ->where($map) ->count(); $limit = 100; $pages = ceil($total / $limit); for($i = 1; $i <= $pages; $i++) { $offset = ($i - 1) * $limit; $result = CustomService::instance()->getCustomIds($where, $map, $offset, $limit)->data; if ($result['total']) { $list = CustomService::instance()->getCustomCollectData($result['ids'])->data; } else { $list = []; } $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"=> "自定义活动"]; foreach ($list as $item) { foreach ($item['collect'] as $collect) { //获取每列数据,转换处理成需要导出的数据 $rowData = []; array_push($rowData, $item['id']); //VIP显示公众号昵称 if ($this->group == AdminConstants::ADMIN_GROUP_ID_VIP || $this->group == AdminConstants::ADMIN_GROUP_ID_VIP_OPERATOR ) { array_push($rowData, $collect['official_account_name']); } //客服消息标题 array_push($rowData, $item['title']); //推广标题 array_push($rowData, $collect['title']); //渠道商账号 array_push($rowData, $item['username']); //书籍名称 array_push($rowData, $collect['book_name']); //推广位置 array_push($rowData, $collect['idx']); //推广类型 array_push($rowData, $types[$collect['type']]); //发送人数 array_push($rowData, "\t".$item['send_num']); //总UV array_push($rowData, "\t".($collect['uv'] ?? '0')); //今日UV array_push($rowData, "\t".($collect['day_uv']??'0')); //总充值金额 array_push($rowData, "\t".($collect['recharge_money'] ?? '0')); //今日充值金额 array_push($rowData, "\t".($collect['day_recharge_money']??'0')); //推送时间 array_push($rowData, date("Y-m-d H:i:s", $item['sendtime'])); //需要格式转换,否则会乱码 mb_convert_variables('GBK', 'UTF-8', $rowData); fputcsv($fp, $rowData); } //必须同时使用 ob_flush() 和flush() 函数来刷新输出缓冲。 ob_flush(); flush(); } } fclose($fp); exit(); } }