Vipchannelreferral.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. <?php
  2. namespace app\admin\controller\referral;
  3. use app\common\controller\Backend;
  4. use app\common\library\Ssdb;
  5. use app\common\library\Redis;
  6. use app\main\constants\CacheConstants;
  7. use app\main\helper\ArrayHelper;
  8. use app\main\service\ReferralService;
  9. use app\main\service\VisitLimitService;
  10. use think\Config;
  11. class Vipchannelreferral extends Backend
  12. {
  13. /**
  14. * @var \app\common\model\Referral
  15. */
  16. protected $model = null;
  17. /**
  18. * @var \app\common\model\VipAdminBind
  19. */
  20. protected $vipAdminBindModel = null;
  21. /**
  22. * @var \app\common\model\Guide
  23. */
  24. protected $guideModel = null;
  25. /**
  26. * @var \app\common\model\Entryhost
  27. */
  28. protected $entryhostModel = null;
  29. public function _initialize()
  30. {
  31. parent::_initialize();
  32. $this->model = model('Referral');
  33. $this->vipAdminBindModel = model('VipAdminBind');
  34. $this->guideModel = model('Guide');
  35. $this->entryhostModel = model('Entryhost');
  36. }
  37. /**
  38. * 导出
  39. * @author 李聪聪
  40. */
  41. public function export()
  42. {
  43. ini_set('memory_limit', '256M'); //内存限制
  44. if (!in_array($this->group, [7, 8])) {
  45. $this->error('当前用户不是vip也不是vip运营');
  46. }
  47. $redis = Redis::instance();
  48. ini_set('memory_limit', '256M'); //内存限制
  49. $columns = [
  50. '渠道商ID',
  51. '渠道商账号',
  52. '渠道商昵称',
  53. '公众号',
  54. '推广ID',
  55. '推广链接',
  56. '入口页面',
  57. '派单渠道',
  58. '累计阅读人数',
  59. '今日阅读人数',
  60. '累计关注人数', //new
  61. '今日关注人数', //new
  62. '累计净关注人数', //new
  63. '今日净关注人数', //new
  64. '累计充值金额',
  65. '今日充值金额', //new
  66. '推广成本',
  67. '创建时间'
  68. ];
  69. header('Content-Description: File Transfer');
  70. header('Content-Type: application/vnd.ms-excel');
  71. header('Content-Disposition: attachment; filename="渠道商推广链接统计数据-' . date('YmdHis', time()) . '.csv"');
  72. header('Expires: 0');
  73. header('Cache-Control: must-revalidate');
  74. header('Pragma: public');
  75. $fp = fopen('php://output', 'a');//打开output流
  76. mb_convert_variables('GBK', 'UTF-8', $columns);
  77. fputcsv($fp, $columns);//将数据格式化为CSV格式并写入到output流中
  78. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  79. $channelIds = $this->vipAdminBindModel->getChannelIds($this->auth->id);
  80. if (empty($channelIds)) {
  81. fclose($fp);
  82. exit();
  83. }
  84. $condition['referral.admin_id'] = ['in', $channelIds];
  85. $countFetchObj = $this->getFetchObj($where, $condition);
  86. $total = $countFetchObj->count();
  87. if ($total == 0) {
  88. fclose($fp);
  89. exit();
  90. }
  91. $limit = 100;
  92. $migrate = VisitLimitService::instance()->checkMigratedV2();
  93. $pages = ceil($total / $limit);
  94. for ($i = 1; $i <= $pages; $i++) {
  95. $offset = ($i - 1) * $limit;
  96. $listFetchObj = $this->getFetchObj($where, $condition);
  97. $list = $listFetchObj
  98. ->limit($offset, $limit)
  99. ->select();
  100. $migrateCollectList = [];
  101. if ($migrate) {
  102. $referralIds = array_column($list, 'id');
  103. $migrateCollectList = ReferralService::instance()->getReferralCollectFromApi($referralIds)->data;
  104. }
  105. foreach ($list as $k => $v) {
  106. $channelId = $v['admin_id'];
  107. //获取当前书籍的默认关注章节
  108. $guide_chapter_idx = 0;
  109. if (isset($v['guide_chapter_idx'])){
  110. $guide_chapter_idx = $v['guide_chapter_idx'];
  111. }
  112. if (empty($v['guide_chapter_idx']) && !empty($v['book_id'])) {
  113. $guide_chapter_idx = $list[$k]['guide_chapter_idx'] = $this->guideModel->getGuideChapter($channelId, $v['book_id'],
  114. 0, $v['admin_id']);
  115. }
  116. if (!empty($v['entryhost_id'])) {
  117. $host = $this->entryhostModel->getInfo($channelId);
  118. $urlReferral = Config::get('site.scheme') . '://' . $host['host'];
  119. } else {
  120. $urlReferral = Config::get('site.scheme') . '://' . $v['appid'] . '.' . $v['host'];
  121. }
  122. $list[$k]['url_referral'] = $urlReferral.'/t/'.$v['id'];
  123. if ($migrate) {
  124. if (array_key_exists($v['id'], $migrateCollectList)) {
  125. $list[$k] = array_merge($list[$k]->getData(), $migrateCollectList[$v['id']]);
  126. }
  127. } else {
  128. $dayMTkey = "M-T:".$v['id'].":".date("d"); //今日充值金额key
  129. $list[$k]['dayuv'] = (int)Redis::instance()->get(CacheConstants::getReadOfReferralIdKey($v['id']));
  130. $list[$k]['dayut'] = (int)Redis::instance()->get(CacheConstants::getSubscribeOfReferralIdKey($v['id'])); //今日关注人数
  131. $list[$k]['daymt'] = (int)Redis::instance()->get($dayMTkey)? round(Redis::instance()->get($dayMTkey) / 100, 2) :0; //今日充值金额
  132. $list[$k]['dayjt'] = (int)Redis::instance()->get(CacheConstants::getSubscribeOfPureReferralIdKey($v['id'])); //今日净关注人数
  133. $list[$k]['dayqt'] = (int)Redis::instance()->get(CacheConstants::getUnsubscribeOfReferralIdKey($v['id'])); //今日取消关注人数
  134. $list[$k]['net_follow_num'] = (int)$list[$k]['net_follow_num'];
  135. if ($list[$k]['uv'] < $list[$k]['dayuv']) {
  136. $list[$k]['uv'] = $list[$k]['dayuv'];
  137. }
  138. if ($list[$k]['follow'] < $list[$k]['dayut']) {
  139. $list[$k]['follow'] = $list[$k]['dayut'];
  140. }
  141. if ($list[$k]['net_follow_num'] < $list[$k]['dayjt']) {
  142. $list[$k]['net_follow_num'] = $list[$k]['dayjt'];
  143. }
  144. }
  145. if (isset($v['wx_json'])) {
  146. $adminJson = json_decode($v['wx_json'], true);
  147. if (isset($adminJson['authorizer_info']['nick_name'])) {
  148. $list[$k]['wx_nickname'] = $adminJson['authorizer_info']['nick_name'];
  149. } else {
  150. $list[$k]['wx_nickname'] = '';
  151. }
  152. } else {
  153. $list[$k]['wx_nickname'] = '';
  154. }
  155. $entry_page = '';
  156. if (isset($v['type'])) {
  157. if ($v['type'] == 1) {
  158. if (isset($v['book_name']) && isset($v['chapter_name'])) {
  159. $entry_page .= $v['book_name'];
  160. $entry_page .= '--' . $v['chapter_name'];
  161. if (!empty($guide_chapter_idx)) {
  162. $entry_page .= '--关注章节:' . $list[$k]['guide_chapter_idx'];
  163. } else {
  164. $entry_page .= '--关注章节:默认';
  165. }
  166. } else {
  167. $entry_page = '';
  168. }
  169. } elseif ($v['type'] == 3) {
  170. $entry_page .= '落地页推广';
  171. if (isset($v['book_name']) && isset($v['chapter_name'])) {
  172. $entry_page .= '--' . $v['book_name'];
  173. $entry_page .= '--' . $v['chapter_name'];
  174. }
  175. if (!empty($guide_chapter_idx)) {
  176. $entry_page .= '--关注章节:' . $guide_chapter_idx;
  177. } else {
  178. $entry_page .= '--关注章节:默认';
  179. }
  180. } else {
  181. $entry_page .= '书城首页推广';
  182. }
  183. } else {
  184. $entry_page = '';
  185. }
  186. //获取每列数据,转换处理成需要导出的数据
  187. $rowData = [
  188. $list[$k]['admin_id'] ?? 0,
  189. $list[$k]['username'] ?? '',
  190. $list[$k]['nickname'] ?? '',
  191. $list[$k]['wx_nickname'] ?? '',
  192. $list[$k]['id'] ?? '',
  193. $list[$k]['url_referral'] ?? '',
  194. $entry_page ?? '',
  195. $list[$k]['name'] ?? 0,
  196. $list[$k]['uv'] ?? 0,
  197. $list[$k]['dayuv'] ?? 0,
  198. $list[$k]['follow'] ?? 0,
  199. $list[$k]['daydt'] ?? 0,
  200. $list[$k]['net_follow_num'] ?? 0,
  201. $list[$k]['dayjt'] ?? '',
  202. $list[$k]['money'] ?? 0,
  203. $list[$k]['daymt'] ?? 0,
  204. $list[$k]['cost'] ?? '',
  205. empty($list[$k]['createtime']) ? '' : date('Y-m-d H:i:s', $list[$k]['createtime'])
  206. ];
  207. //需要格式转换,否则会乱码
  208. mb_convert_variables('GBK', 'UTF-8', $rowData);
  209. fputcsv($fp, $rowData);
  210. }
  211. ob_flush();
  212. flush();
  213. }
  214. fclose($fp);
  215. exit();
  216. }
  217. public function index()
  218. {
  219. if (!in_array($this->group, [7, 8])) {
  220. $this->error('当前用户不是vip也不是vip运营');
  221. }
  222. if ($this->request->isAjax()) {
  223. //如果发送的来源是Selectpage,则转发到Selectpage
  224. if ($this->request->request('pkey_name')) {
  225. return $this->selectpage();
  226. }
  227. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  228. $channelIds = $this->vipAdminBindModel->getChannelIds($this->auth->id);
  229. if (empty($channelIds)) {
  230. $this->error('当前vip或vip运营下没有绑定渠道商');
  231. }
  232. $condition['referral.admin_id'] = ['in', $channelIds];
  233. $countFetchObj = $this->getFetchObj($where, $condition);
  234. $total = $countFetchObj->count();
  235. $this->model->getLastSql();
  236. $listFetchObj = $this->getFetchObj($where, $condition);
  237. $list = $listFetchObj
  238. ->limit($offset, $limit)
  239. ->select();
  240. $migrate = VisitLimitService::instance()->checkMigratedV2();
  241. $migrateCollectList = [];
  242. if ($migrate) {
  243. $referralIds = array_column($list, 'id');
  244. $migrateCollectList = ReferralService::instance()->getReferralCollectFromApi($referralIds)->data;
  245. }
  246. foreach ($list as $k => $v) {
  247. $channelId = $v['admin_id'];
  248. //获取当前书籍的默认关注章节
  249. if (empty($v['guide_chapter_idx']) && !empty($v['book_id'])) {
  250. $list[$k]['guide_chapter_idx'] = $this->guideModel->getGuideChapter($channelId, $v['book_id'],
  251. 0, $v['admin_id']);
  252. }
  253. if (!empty($v['entryhost_id'])) {
  254. $host = $this->entryhostModel->getInfo($channelId);
  255. $urlReferral = Config::get('site.scheme') . '://' . $host['host'];
  256. } else {
  257. $urlReferral = Config::get('site.scheme') . '://' . $v['appid'] . '.' . $v['host'];
  258. }
  259. $list[$k]['url_referral'] = $urlReferral.'/t/'.$v['id'];
  260. if ($migrate) {
  261. if (array_key_exists($v['id'], $migrateCollectList)) {
  262. $list[$k] = array_merge($list[$k]->getData(), $migrateCollectList[$v['id']]);
  263. }
  264. } else {
  265. $dayMTkey = "M-T:".$v['id'].":".date("d"); //今日充值金额key
  266. $list[$k]['dayuv'] = (int)Redis::instance()->get(CacheConstants::getReadOfReferralIdKey($v['id']));
  267. $list[$k]['dayut'] = (int)Redis::instance()->get(CacheConstants::getSubscribeOfReferralIdKey($v['id'])); //今日关注人数
  268. $list[$k]['daymt'] = (int)Redis::instance()->get($dayMTkey)? round(Redis::instance()->get($dayMTkey) / 100, 2) :0; //今日充值金额
  269. $list[$k]['dayjt'] = (int)Redis::instance()->get(CacheConstants::getSubscribeOfPureReferralIdKey($v['id'])); //今日净关注人数
  270. $list[$k]['dayqt'] = (int)Redis::instance()->get(CacheConstants::getUnsubscribeOfReferralIdKey($v['id'])); //今日取消关注人数
  271. $list[$k]['net_follow_num'] = (int)$list[$k]['net_follow_num'];
  272. if ($list[$k]['uv'] < $list[$k]['dayuv']) {
  273. $list[$k]['uv'] = $list[$k]['dayuv'];
  274. }
  275. if ($list[$k]['follow'] < $list[$k]['dayut']) {
  276. $list[$k]['follow'] = $list[$k]['dayut'];
  277. }
  278. if ($list[$k]['net_follow_num'] < $list[$k]['dayjt']) {
  279. $list[$k]['net_follow_num'] = $list[$k]['dayjt'];
  280. }
  281. $dayMTNkey = "M-T-N:".$v['id'].":".date("d"); //今日充值笔数key
  282. $list[$k]['order_nums'] = (int)$list[$k]['orders_num'];
  283. $list[$k]['day_order_nums'] = (int)Redis::instance()->get($dayMTNkey);
  284. }
  285. $list[$k]['limited'] = 0;
  286. if (!empty($v['book_id'])) {
  287. $isLimited = model('BookLimit')->backendHasLimit(0, $channelId, $v['book_id']);
  288. if ($isLimited) {
  289. $list[$k]['limited'] = 1;
  290. }
  291. }
  292. $list[$k]['short_url'] = replaceShortDomain($list[$k]['url_referral'], $v['short_id']);
  293. $adminJson = json_decode($v['wx_json'], true);
  294. $list[$k]['wx_nickname'] = $adminJson['authorizer_info']['nick_name'];
  295. unset($v['wx_json']);
  296. }
  297. $result = array("total" => $total, "rows" => $list);
  298. return json($result);
  299. }
  300. return $this->view->fetch();
  301. }
  302. private function getFetchObj($where, $condition)
  303. {
  304. $fetchObj = $this->model->join('admin', 'admin.id = referral.admin_id')
  305. ->join('admin_config', 'admin.id = admin_config.admin_id')
  306. ->join('book', 'referral.book_id = book.id', 'left')
  307. ->join('ophost', 'admin_config.ophost_id = ophost.id', 'left')
  308. ->field([
  309. 'referral.*',
  310. 'referral.name'=>'referral_name',
  311. 'book.name' => 'book_name',
  312. 'admin.username',
  313. 'admin.nickname',
  314. 'admin_config.json' => 'wx_json',
  315. 'admin_config.entryhost_id',
  316. 'admin_config.appid',
  317. 'ophost.host',
  318. ])
  319. ->where($where)
  320. ->where($condition)
  321. ->order('referral.state', 'desc')
  322. ->order('referral.createtime', 'desc');
  323. return $fetchObj;
  324. }
  325. public function aaa(){
  326. }
  327. }