123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450 |
- <?php
- /**
- * Created by PhpStorm.
- * User: wanggb
- * Date: 2018/11/24
- * Time: 18:31
- */
- namespace app\admin\controller\referral;
- use app\common\controller\Backend;
- use app\main\helper\ArrayHelper;
- use app\main\service\ReferralService;
- use app\main\service\VisitLimitService;
- use think\Exception;
- use app\common\library\Ssdb;
- use app\common\library\Redis;
- use app\main\constants\CacheConstants;
- use think\Config;
- use think\Log;
- class Vipagentreferral extends Backend
- {
- /**
- * @var \app\common\model\Referral
- */
- protected $model = null;
- /**
- * @var \app\common\model\VipAdminBind
- */
- protected $vipAdminBindModel = null;
- /**
- * @var \app\common\model\Guide
- */
- protected $guideModel = null;
- /**
- * @var \app\common\model\Entryhost
- */
- protected $entryhostModel = null;
- public function _initialize()
- {
- parent::_initialize();
- $this->model = model('Referral');
- $this->vipAdminBindModel = model('VipAdminBind');
- $this->guideModel = model('Guide');
- $this->entryhostModel = model('Entryhost');
- }
- /**
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- * @author 李聪聪
- */
- public function export()
- {
- if (!in_array($this->group, [7, 8])) {
- $this->error('当前用户不是vip也不是vip运营');
- }
- ini_set('memory_limit', '256M'); //内存限制
- $columns = [
- '渠道商ID',
- '渠道商账号',
- '渠道商昵称',
- '公众号',
- '推广ID',
- '推广链接',
- '入口页面',
- '代理商账号',
- '派单渠道', //new
- '累计阅读人数',
- '今日阅读人数',
- '累计关注人数', //new
- '今日关注人数', //new
- '累计净关注人数', //new
- '今日净关注人数', //new
- '累计充值金额',
- '今日充值金额',
- '推广成本', //new
- '创建时间'
- ];
- $redis = Redis::instance();
- 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();
- }
- //获取所有代理商
- $allAgentIds = $this->vipAdminBindModel->getAllAgentIds($channelIds);
- try {
- $condition['referral.admin_id'] = ['in', $allAgentIds];
- $countFetchObj = $this->getFetchObj($where, $condition);
- $total = $countFetchObj->count();
- if ($total == 0) {
- fclose($fp);
- exit();
- }
- } catch (Exception $exception) {
- Log::info($countFetchObj->getLastSql());
- $exception->getMessage();
- }
- $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();
- $distributeList = $this->filterDataByDistribute($list);
- $migrateCollectList = [];
- if ($migrate) {
- $referralIds = array_column($list, 'id');
- $migrateCollectList = ReferralService::instance()->getReferralCollectFromApi($referralIds)->data;
- }
- foreach ($list as $k => $v) {
- $channelId = $v['qds_id'];
- //获取当前书籍的默认关注章节
- if (empty($v['guide_chapter_idx']) && !empty($v['book_id'])) {
- $list[$k]['guide_chapter_idx'] = $this->guideModel->getGuideChapter($v['admin_id'], $v['book_id'], 0, $channelId);
- }
- if ($v['distribute'] === '0') {
- $urlReferral = Config::get('site.scheme') . '://' . $distributeList[$v['qds_id']]['appid'] . '.' . $distributeList[$v['qds_id']]['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 (!empty($v['book_id'])) {
- $isLimited = model('BookLimit')->backendHasLimit(0, $v['admin_id'], $v['book_id']);
- if ($isLimited) {
- $list[$k]['limited'] = 1;
- }
- }
- $list[$k]['short_url'] = replaceShortDomain($list[$k]['url_referral'], $v['short_id']);
- 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 (isset($list[$k]['guide_chapter_idx']) && !empty($list[$k]['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 (isset($list[$k]['guide_chapter_idx']) && !empty($list[$k]['guide_chapter_idx'])) {
- $entry_page .= '--关注章节:' . $list[$k]['guide_chapter_idx'];
- } else {
- $entry_page .= '--关注章节:默认';
- }
- } else {
- $entry_page .= '书城首页推广';
- }
- } else {
- $entry_page = '未知';
- }
- //获取每列数据,转换处理成需要导出的数据
- $rowData = [
- $list[$k]['qds_id'] ?? 0,
- $list[$k]['qds_username'] ?? '',
- $list[$k]['qds_nickname'] ?? '',
- $list[$k]['wx_nickname'] ?? '',
- $list[$k]['id'] ?? 0,
- $list[$k]['url_referral'] ?? '',
- $entry_page ?? 0,
- $list[$k]['username'] ?? 0,
- $list[$k]['name'] ?? '',
- $list[$k]['uv'] ?? 0,
- $list[$k]['dayuv'] ?? 0,
- $list[$k]['follow'] ?? 0,
- $list[$k]['dayut'] ?? 0,
- $list[$k]['net_follow_num'] ?? '',
- $list[$k]['dayjt'] ?? '',
- $list[$k]['money'] ?? '',
- $list[$k]['daymt'] ?? '',
- $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);
- }
- 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运营下没有绑定渠道商');
- }
- #获取所有代理商
- $allAgentIds = $this->vipAdminBindModel->getAllAgentIds($channelIds);
- try{
- $condition['referral.admin_id'] = ['in', $allAgentIds];
- $countFetchObj = $this->getFetchObj($where, $condition);
- $total = $countFetchObj->count();
- }catch (Exception $exception){
- Log::info($countFetchObj->getLastSql());
- $exception->getMessage();
- }
- try{
- $listFetchObj = $this->getFetchObj($where, $condition);
- $list = $listFetchObj
- ->limit($offset, $limit)
- ->select();
- }catch (Exception $exception){
- Log::info($listFetchObj->getLastSql());
- $exception->getMessage();
- }
- //
- $distributeList = $this->filterDataByDistribute($list);
- $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['qds_id'];
- //获取当前书籍的默认关注章节
- if (empty($v['guide_chapter_idx']) && !empty($v['book_id'])) {
- $list[$k]['guide_chapter_idx'] = $this->guideModel->getGuideChapter($v['admin_id'], $v['book_id'], 0, $channelId);
- }
- if($v['distribute'] === '0'){
- $urlReferral = Config::get('site.scheme') . '://' . $distributeList[$v['qds_id']]['appid'] . '.' . $distributeList[$v['qds_id']]['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);
- }
- if (!empty($v['book_id'])) {
- $isLimited = model('BookLimit')->backendHasLimit(0, $v['admin_id'], $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' => 'dls_admin'], 'admin.id = referral.admin_id')
- ->join('admin_config', 'admin.id = admin_config.admin_id', 'left')
- ->join('book', 'referral.book_id = book.id', 'left')
- ->join('ophost', 'admin_config.ophost_id = ophost.id', 'left')
- ->join('admin_extend', 'admin.id = admin_extend.admin_id', 'INNER')
- ->join(['admin' => 'qds_admin'], 'admin_extend.create_by = qds_admin.id', 'INNER')
- ->field([
- 'admin_extend.create_by' => 'p_admin_id',
- 'qds_admin.username' => 'qds_username',
- 'qds_admin.nickname' => 'qds_nickname',
- 'qds_admin.id' => 'qds_id',
- 'referral.*',
- 'book.name' => 'book_name',
- 'admin.id' => 'dls_id',
- 'admin.username',
- 'admin.nickname',
- 'admin_config.json' => 'wx_json',
- 'admin_config.entryhost_id',
- 'admin_config.appid',
- 'ophost.host',
- 'admin_extend.distribute' => 'distribute'
- ])
- ->where($where)
- ->where($condition)
- ->order('referral.state', 'desc')
- ->order('referral.createtime', 'desc');
- return $fetchObj;
- }
- private function filterDataByDistribute($list){
- $qds_arr = [];
- foreach ($list as $k => $v)
- {
- if($v['distribute'] == '0'){
- $qds_arr[] = $v['qds_id'];
- }
- }
- $qds_str = implode(',', $qds_arr);
- $data = model('admin_config')
- ->join('ophost', 'admin_config.ophost_id = ophost.id', 'left')
- ->field([
- 'admin_config.admin_id',
- 'admin_config.appid',
- 'ophost.host'
- ])
- ->where('admin_id','in', $qds_str)
- ->select();
- $arr = [];
- foreach ($data as $key => $item){
- $arr[$item['admin_id']] = $item;
- }
- return $arr;
- }
- }
|