model = model('Referral'); $this->vipAdminBindModel = model('VipAdminBind'); $this->guideModel = model('Guide'); $this->entryhostModel = model('Entryhost'); } /** * 导出 * @author 李聪聪 */ public function export() { ini_set('memory_limit', '256M'); //内存限制 if (!in_array($this->group, [7, 8])) { $this->error('当前用户不是vip也不是vip运营'); } $redis = Redis::instance(); ini_set('memory_limit', '256M'); //内存限制 $columns = [ '渠道商ID', '渠道商账号', '渠道商昵称', '公众号', '推广ID', '推广链接', '入口页面', '派单渠道', '累计阅读人数', '今日阅读人数', '累计关注人数', //new '今日关注人数', //new '累计净关注人数', //new '今日净关注人数', //new '累计充值金额', '今日充值金额', //new '推广成本', '创建时间' ]; 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流中 list($where, $sort, $order, $offset, $limit) = $this->buildparams(); $channelIds = $this->vipAdminBindModel->getChannelIds($this->auth->id); if (empty($channelIds)) { fclose($fp); exit(); } $condition['referral.admin_id'] = ['in', $channelIds]; $countFetchObj = $this->getFetchObj($where, $condition); $total = $countFetchObj->count(); if ($total == 0) { fclose($fp); exit(); } $limit = 100; $migrate = VisitLimitService::instance()->checkMigratedV2(); $pages = ceil($total / $limit); for ($i = 1; $i <= $pages; $i++) { $offset = ($i - 1) * $limit; $listFetchObj = $this->getFetchObj($where, $condition); $list = $listFetchObj ->limit($offset, $limit) ->select(); $migrateCollectList = []; if ($migrate) { $referralIds = array_column($list, 'id'); $migrateCollectList = ReferralService::instance()->getReferralCollectFromApi($referralIds)->data; } foreach ($list as $k => $v) { $channelId = $v['admin_id']; //获取当前书籍的默认关注章节 $guide_chapter_idx = 0; if (isset($v['guide_chapter_idx'])){ $guide_chapter_idx = $v['guide_chapter_idx']; } if (empty($v['guide_chapter_idx']) && !empty($v['book_id'])) { $guide_chapter_idx = $list[$k]['guide_chapter_idx'] = $this->guideModel->getGuideChapter($channelId, $v['book_id'], 0, $v['admin_id']); } if (!empty($v['entryhost_id'])) { $host = $this->entryhostModel->getInfo($channelId); $urlReferral = Config::get('site.scheme') . '://' . $host['host']; } else { $urlReferral = Config::get('site.scheme') . '://' . $v['appid'] . '.' . $v['host']; } $list[$k]['url_referral'] = $urlReferral.'/t/'.$v['id']; if ($migrate) { if (array_key_exists($v['id'], $migrateCollectList)) { $list[$k] = array_merge($list[$k]->getData(), $migrateCollectList[$v['id']]); } } else { $dayMTkey = "M-T:".$v['id'].":".date("d"); //今日充值金额key $list[$k]['dayuv'] = (int)Redis::instance()->get(CacheConstants::getReadOfReferralIdKey($v['id'])); $list[$k]['dayut'] = (int)Redis::instance()->get(CacheConstants::getSubscribeOfReferralIdKey($v['id'])); //今日关注人数 $list[$k]['daymt'] = (int)Redis::instance()->get($dayMTkey)? round(Redis::instance()->get($dayMTkey) / 100, 2) :0; //今日充值金额 $list[$k]['dayjt'] = (int)Redis::instance()->get(CacheConstants::getSubscribeOfPureReferralIdKey($v['id'])); //今日净关注人数 $list[$k]['dayqt'] = (int)Redis::instance()->get(CacheConstants::getUnsubscribeOfReferralIdKey($v['id'])); //今日取消关注人数 $list[$k]['net_follow_num'] = (int)$list[$k]['net_follow_num']; if ($list[$k]['uv'] < $list[$k]['dayuv']) { $list[$k]['uv'] = $list[$k]['dayuv']; } if ($list[$k]['follow'] < $list[$k]['dayut']) { $list[$k]['follow'] = $list[$k]['dayut']; } if ($list[$k]['net_follow_num'] < $list[$k]['dayjt']) { $list[$k]['net_follow_num'] = $list[$k]['dayjt']; } } if (isset($v['wx_json'])) { $adminJson = json_decode($v['wx_json'], true); if (isset($adminJson['authorizer_info']['nick_name'])) { $list[$k]['wx_nickname'] = $adminJson['authorizer_info']['nick_name']; } else { $list[$k]['wx_nickname'] = ''; } } else { $list[$k]['wx_nickname'] = ''; } $entry_page = ''; if (isset($v['type'])) { if ($v['type'] == 1) { if (isset($v['book_name']) && isset($v['chapter_name'])) { $entry_page .= $v['book_name']; $entry_page .= '--' . $v['chapter_name']; if (!empty($guide_chapter_idx)) { $entry_page .= '--关注章节:' . $list[$k]['guide_chapter_idx']; } else { $entry_page .= '--关注章节:默认'; } } else { $entry_page = ''; } } elseif ($v['type'] == 3) { $entry_page .= '落地页推广'; if (isset($v['book_name']) && isset($v['chapter_name'])) { $entry_page .= '--' . $v['book_name']; $entry_page .= '--' . $v['chapter_name']; } if (!empty($guide_chapter_idx)) { $entry_page .= '--关注章节:' . $guide_chapter_idx; } else { $entry_page .= '--关注章节:默认'; } } else { $entry_page .= '书城首页推广'; } } else { $entry_page = ''; } //获取每列数据,转换处理成需要导出的数据 $rowData = [ $list[$k]['admin_id'] ?? 0, $list[$k]['username'] ?? '', $list[$k]['nickname'] ?? '', $list[$k]['wx_nickname'] ?? '', $list[$k]['id'] ?? '', $list[$k]['url_referral'] ?? '', $entry_page ?? '', $list[$k]['name'] ?? 0, $list[$k]['uv'] ?? 0, $list[$k]['dayuv'] ?? 0, $list[$k]['follow'] ?? 0, $list[$k]['daydt'] ?? 0, $list[$k]['net_follow_num'] ?? 0, $list[$k]['dayjt'] ?? '', $list[$k]['money'] ?? 0, $list[$k]['daymt'] ?? 0, $list[$k]['cost'] ?? '', empty($list[$k]['createtime']) ? '' : date('Y-m-d H:i:s', $list[$k]['createtime']) ]; //需要格式转换,否则会乱码 mb_convert_variables('GBK', 'UTF-8', $rowData); fputcsv($fp, $rowData); } ob_flush(); flush(); } fclose($fp); exit(); } public function index() { if (!in_array($this->group, [7, 8])) { $this->error('当前用户不是vip也不是vip运营'); } if ($this->request->isAjax()) { //如果发送的来源是Selectpage,则转发到Selectpage if ($this->request->request('pkey_name')) { return $this->selectpage(); } list($where, $sort, $order, $offset, $limit) = $this->buildparams(); $channelIds = $this->vipAdminBindModel->getChannelIds($this->auth->id); if (empty($channelIds)) { $this->error('当前vip或vip运营下没有绑定渠道商'); } $condition['referral.admin_id'] = ['in', $channelIds]; $countFetchObj = $this->getFetchObj($where, $condition); $total = $countFetchObj->count(); $this->model->getLastSql(); $listFetchObj = $this->getFetchObj($where, $condition); $list = $listFetchObj ->limit($offset, $limit) ->select(); $migrate = VisitLimitService::instance()->checkMigratedV2(); $migrateCollectList = []; if ($migrate) { $referralIds = array_column($list, 'id'); $migrateCollectList = ReferralService::instance()->getReferralCollectFromApi($referralIds)->data; } foreach ($list as $k => $v) { $channelId = $v['admin_id']; //获取当前书籍的默认关注章节 if (empty($v['guide_chapter_idx']) && !empty($v['book_id'])) { $list[$k]['guide_chapter_idx'] = $this->guideModel->getGuideChapter($channelId, $v['book_id'], 0, $v['admin_id']); } if (!empty($v['entryhost_id'])) { $host = $this->entryhostModel->getInfo($channelId); $urlReferral = Config::get('site.scheme') . '://' . $host['host']; } else { $urlReferral = Config::get('site.scheme') . '://' . $v['appid'] . '.' . $v['host']; } $list[$k]['url_referral'] = $urlReferral.'/t/'.$v['id']; if ($migrate) { if (array_key_exists($v['id'], $migrateCollectList)) { $list[$k] = array_merge($list[$k]->getData(), $migrateCollectList[$v['id']]); } } else { $dayMTkey = "M-T:".$v['id'].":".date("d"); //今日充值金额key $list[$k]['dayuv'] = (int)Redis::instance()->get(CacheConstants::getReadOfReferralIdKey($v['id'])); $list[$k]['dayut'] = (int)Redis::instance()->get(CacheConstants::getSubscribeOfReferralIdKey($v['id'])); //今日关注人数 $list[$k]['daymt'] = (int)Redis::instance()->get($dayMTkey)? round(Redis::instance()->get($dayMTkey) / 100, 2) :0; //今日充值金额 $list[$k]['dayjt'] = (int)Redis::instance()->get(CacheConstants::getSubscribeOfPureReferralIdKey($v['id'])); //今日净关注人数 $list[$k]['dayqt'] = (int)Redis::instance()->get(CacheConstants::getUnsubscribeOfReferralIdKey($v['id'])); //今日取消关注人数 $list[$k]['net_follow_num'] = (int)$list[$k]['net_follow_num']; if ($list[$k]['uv'] < $list[$k]['dayuv']) { $list[$k]['uv'] = $list[$k]['dayuv']; } if ($list[$k]['follow'] < $list[$k]['dayut']) { $list[$k]['follow'] = $list[$k]['dayut']; } if ($list[$k]['net_follow_num'] < $list[$k]['dayjt']) { $list[$k]['net_follow_num'] = $list[$k]['dayjt']; } $dayMTNkey = "M-T-N:".$v['id'].":".date("d"); //今日充值笔数key $list[$k]['order_nums'] = (int)$list[$k]['orders_num']; $list[$k]['day_order_nums'] = (int)Redis::instance()->get($dayMTNkey); } $list[$k]['limited'] = 0; if (!empty($v['book_id'])) { $isLimited = model('BookLimit')->backendHasLimit(0, $channelId, $v['book_id']); if ($isLimited) { $list[$k]['limited'] = 1; } } $list[$k]['short_url'] = replaceShortDomain($list[$k]['url_referral'], $v['short_id']); $adminJson = json_decode($v['wx_json'], true); $list[$k]['wx_nickname'] = $adminJson['authorizer_info']['nick_name']; unset($v['wx_json']); } $result = array("total" => $total, "rows" => $list); return json($result); } return $this->view->fetch(); } private function getFetchObj($where, $condition) { $fetchObj = $this->model->join('admin', 'admin.id = referral.admin_id') ->join('admin_config', 'admin.id = admin_config.admin_id') ->join('book', 'referral.book_id = book.id', 'left') ->join('ophost', 'admin_config.ophost_id = ophost.id', 'left') ->field([ 'referral.*', 'referral.name'=>'referral_name', 'book.name' => 'book_name', 'admin.username', 'admin.nickname', 'admin_config.json' => 'wx_json', 'admin_config.entryhost_id', 'admin_config.appid', 'ophost.host', ]) ->where($where) ->where($condition) ->order('referral.state', 'desc') ->order('referral.createtime', 'desc'); return $fetchObj; } public function aaa(){ } }