Vipreferral.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  1. <?php
  2. namespace app\admin\controller\referral;
  3. use app\admin\controller\auth\Channel;
  4. use app\admin\library\ShortUrl;
  5. use app\common\controller\Backend;
  6. use app\common\library\Redis;
  7. use app\common\service\ReferralService;
  8. use app\main\constants\BookConstants;
  9. use app\main\constants\CacheConstants;
  10. use app\main\helper\ArrayHelper;
  11. use app\main\helper\StringHelper;
  12. use app\main\service\VisitLimitService;
  13. use think\Config;
  14. use think\Controller;
  15. use think\Log;
  16. use think\Request;
  17. /**
  18. * VIP推广链接管理
  19. *
  20. * @icon fa fa-circle-o
  21. */
  22. class Vipreferral extends Backend
  23. {
  24. /**
  25. * ReferralLibrary模型对象
  26. */
  27. protected $model = null;
  28. protected $guideModel = null;
  29. public function _initialize()
  30. {
  31. parent::_initialize();
  32. $this->model = model('ReferralLibrary');
  33. $this->guideModel = model('Guide');
  34. $this->vipAdminBindModel = model('VipAdminBind');
  35. $this->entryhostModel = model('Entryhost');
  36. $this->bookAgentId = 0;
  37. $this->bookChannelId = 0;
  38. }
  39. /**
  40. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法
  41. * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑
  42. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  43. */
  44. /**
  45. * 查看
  46. */
  47. public function index()
  48. {
  49. $state = $this->request->param('state') ?? '1';
  50. //设置过滤方法
  51. $this->request->filter(['strip_tags']);
  52. if ($this->request->isAjax()) {
  53. //如果发送的来源是Selectpage,则转发到Selectpage
  54. if ($this->request->request('pkey_name')) {
  55. return $this->selectpage();
  56. }
  57. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  58. $map = [
  59. 'referral_library.admin_id' => $this->auth->id,
  60. 'referral.state' => $state,
  61. ];
  62. $total = $this->model
  63. ->join('referral', 'referral_library.referral_id = referral.id')
  64. ->join('book', 'referral.book_id = book.id', 'left')
  65. ->where($where)
  66. ->where($map)
  67. ->count();
  68. $list = $this->model
  69. ->join('referral', 'referral_library.referral_id = referral.id')
  70. ->join('book', 'referral.book_id = book.id', 'left')
  71. ->where($where)
  72. ->where($map)
  73. ->field('referral_library.*, referral.name as referral_name, referral.book_id, book.name as book_name, referral.chapter_idx, referral.chapter_id, referral.guide_chapter_idx, referral.state as state, referral.type, referral.chapter_name, referral.cost, referral.state')
  74. ->order($sort, $order)
  75. ->limit($offset, $limit)
  76. ->select();
  77. foreach ($list as $k=>$v){
  78. //获取当前书籍的默认关注章节
  79. if(empty($v['guide_chapter_idx']) && !empty($v['book_id'])){
  80. $default_chapter_idx = 0;
  81. $list[$k]['guide_chapter_idx'] = $default_chapter_idx;
  82. }
  83. //查询所有的关联的referral
  84. $childReferrals = model("referral")->join('referral_slave', 'referral.id = referral_slave.referral_id')->where('referral_slave.library_id', 'eq', $v['id'])->field('referral.*')->select();
  85. $dayuvAll = $dayutAll = $daymtAll= $dayjtAll = $dayqtAll = $daydtAll = $guide_follow_numAll = $net_follow_numAll = $uvnumAll = $follownumAll = $moneyAll = 0;
  86. $childReferralChannelId = 0;
  87. $migrateCollectList = [];
  88. $migrate = VisitLimitService::instance()->checkMigratedV2();
  89. if ($migrate) {
  90. $referralIds = array_column($childReferrals, 'id');
  91. $migrateCollectList = \app\main\service\ReferralService::instance()->getReferralCollectFromApi($referralIds)->data;
  92. }
  93. if (!empty($childReferrals)) {
  94. foreach ($childReferrals as $childReferral) {
  95. $childReferralChannelId = empty($childReferralChannelId) ? $childReferral['admin_id'] : $childReferralChannelId;
  96. if ($migrate) {
  97. if (array_key_exists($childReferral['id'], $migrateCollectList)) {
  98. $dayuvAll += $migrateCollectList[$childReferral['id']]['dayuv'];
  99. $dayutAll += $migrateCollectList[$childReferral['id']]['dayut'];
  100. $daymtAll += $migrateCollectList[$childReferral['id']]['daymt'];
  101. $dayjtAll += $migrateCollectList[$childReferral['id']]['dayjt'];
  102. $dayqtAll += $migrateCollectList[$childReferral['id']]['dayqt'];
  103. $net_follow_numAll += $migrateCollectList[$childReferral['id']]['net_follow_num'];
  104. $uvnumAll += $migrateCollectList[$childReferral['id']]['uv'];
  105. $follownumAll += $migrateCollectList[$childReferral['id']]['follow'];
  106. $moneyAll += $migrateCollectList[$childReferral['id']]['money'];
  107. }
  108. } else {
  109. $dayMTkey = "M-T:".$childReferral['id'].":".date("d"); //今日充值金额key
  110. if (Redis::instance()->get(CacheConstants::getReadOfReferralIdKey($childReferral['id']))) {
  111. $dayuvAll += (int)Redis::instance()->get(CacheConstants::getReadOfReferralIdKey($childReferral['id']));
  112. }
  113. if (Redis::instance()->get(CacheConstants::getSubscribeOfReferralIdKey($childReferral['id']))) {
  114. $dayutAll += (int)Redis::instance()->get(CacheConstants::getSubscribeOfReferralIdKey($childReferral['id']));
  115. }
  116. if (Redis::instance()->get($dayMTkey)) {
  117. $daymtAll += (int)Redis::instance()->get($dayMTkey)? round(Redis::instance()->get($dayMTkey) / 100, 2) :0;
  118. }
  119. if (Redis::instance()->get(CacheConstants::getSubscribeOfPureReferralIdKey($childReferral['id']))) {
  120. $dayjtAll += (int)Redis::instance()->get(CacheConstants::getSubscribeOfPureReferralIdKey($childReferral['id']));
  121. }
  122. if (Redis::instance()->get(CacheConstants::getUnsubscribeOfReferralIdKey($childReferral['id']))) {
  123. $dayqtAll += (int)Redis::instance()->get(CacheConstants::getUnsubscribeOfReferralIdKey($childReferral['id'])); //今日取消关注人数
  124. }
  125. $net_follow_numAll += (int)$childReferral['net_follow_num'];
  126. $uvnumAll += (int)$childReferral['uv'];
  127. $follownumAll += (int)$childReferral['follow'];
  128. $moneyAll += $childReferral['money'];
  129. }
  130. }
  131. }
  132. $list[$k]['dayuv'] = $dayuvAll;
  133. $list[$k]['dayut'] = $dayutAll; //今日关注人数
  134. $list[$k]['daymt'] = StringHelper::moneyFormat($daymtAll); //今日充值金额
  135. $list[$k]['dayjt'] = $dayjtAll; //今日净关注人数
  136. $list[$k]['dayqt'] = $dayqtAll; //今日取消关注人数
  137. $list[$k]['net_follow_num'] = $net_follow_numAll;
  138. $list[$k]['uv'] = $uvnumAll;
  139. $list[$k]['follow'] = $follownumAll;
  140. $list[$k]['money'] = StringHelper::moneyFormat($moneyAll);
  141. if(!empty($v['book_id'])){
  142. $isLimited = model('BookLimit')->backendHasLimit($this->bookAgentId,$this->bookChannelId,$v['book_id']);
  143. if($isLimited){
  144. $list[$k]['limited'] = 1;
  145. }else{
  146. $list[$k]['limited'] = 0;
  147. }
  148. }else{
  149. $list[$k]['limited'] = 0;
  150. }
  151. if ($childReferralChannelId) {
  152. //地址替换 防止切换域名后无法访问
  153. $list[$k]['short_url'] = getCurrentDomain($childReferralChannelId).'v/'.$v['id'];
  154. }
  155. $list[$k]['referral_library'] = [
  156. 'id' => $list[$k]['id'],
  157. 'createtime' => $list[$k]['createtime'],
  158. ];
  159. $list[$k]['referral'] = [
  160. 'name' => $list[$k]['referral_name'],
  161. 'book_id' => $list[$k]['book_id'],
  162. ];
  163. $list[$k]['book'] = [
  164. 'name' => $list[$k]['book_name'],
  165. ];
  166. }
  167. $result = array("total" => $total, "rows" => $list);
  168. return json($result);
  169. }
  170. $this->assign('state', $state);
  171. $this->assignconfig('state', $state);
  172. return $this->view->fetch();
  173. }
  174. /**
  175. * 选择渠道商
  176. */
  177. public function selectchannel()
  178. {
  179. $hasQdsIds = $this->request->request('not_in_qds');
  180. if ($this->request->isAjax())
  181. {
  182. //如果发送的来源是Selectpage,则转发到Selectpage
  183. if ($this->request->request('pkey_name'))
  184. {
  185. return $this->selectpage();
  186. }
  187. $map = [];
  188. $map['vip_admin_bind.flag'] = ['EQ', '1'];
  189. $map['vip_admin_bind.admin_id_master'] = ['EQ', $this->auth->id];
  190. $filter = $this->request->get("filter", '');
  191. $filter = json_decode($filter, true);
  192. if ($hasQdsIds) {
  193. $map['vip_admin_bind.admin_id_slave'] = ['not in', explode(',', $hasQdsIds)];
  194. }
  195. if (!empty($filter)) {
  196. foreach ($filter as $k => $v) {
  197. switch ($k) {
  198. case 'wx_nickname':
  199. $map['JSON_EXTRACT(admin_config.json, "$.authorizer_info.nick_name")'] = ['like', '%' . $v . '%'];
  200. break;
  201. case 'qds_username':
  202. $map["admin.username"] = ['like', "%$v%"];
  203. break;
  204. case 'qds_nickname':
  205. $map["admin.nickname"] = ['like', "%$v%"];
  206. break;
  207. default:
  208. break;
  209. }
  210. }
  211. }
  212. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  213. $total = model('VipAdminBind')
  214. ->field('vip_admin_bind.*')
  215. ->join('admin_config', 'vip_admin_bind.admin_id_slave = admin_config.admin_id')
  216. ->join('admin', 'vip_admin_bind.admin_id_slave = admin.id', 'left')
  217. ->where($map)
  218. ->order($sort, $order)
  219. ->count();
  220. $list = model('VipAdminBind')
  221. ->join('admin_config', 'vip_admin_bind.admin_id_slave = admin_config.admin_id')
  222. ->join('admin', 'vip_admin_bind.admin_id_slave = admin.id', 'left')
  223. ->field("admin_config.json, vip_admin_bind.admin_id_slave as qds_id")
  224. ->where($map)
  225. ->order($sort, $order)
  226. ->limit($offset, $limit)
  227. ->select();
  228. $operatorList = [];
  229. if (!empty($list)) {
  230. foreach ($list as $item) {
  231. $qdsIds[] = $item->qds_id;
  232. }
  233. $operatorList = $this->getOperatorInfoByQdsIds($qdsIds);
  234. }
  235. foreach ($list as $item) {
  236. $adminJson = json_decode($item->json, true);
  237. $item->wx_nickname = $adminJson['authorizer_info']['nick_name'];
  238. unset($item->json);
  239. if (!empty($operatorList[$item->qds_id])) {
  240. $item->qds_nickname = $operatorList[$item->qds_id]['qds_nickname'];
  241. $item->qds_username = $operatorList[$item->qds_id]['qds_username'];
  242. $item->qds_id = $operatorList[$item->qds_id]['qds_id'];
  243. }
  244. }
  245. $result = array("total" => $total, "rows" => $list);
  246. return json($result);
  247. }
  248. $this->assignconfig('not_in_qds', $hasQdsIds);
  249. return $this->view->fetch();
  250. }
  251. private function getFetchObj($where, $order)
  252. {
  253. $fetchObj = model('VipAdminBind')->join(['admin' => 'qds'], 'qds.id = vip_admin_bind.admin_id_slave')
  254. ->join('admin_config', 'qds.id = admin_config.admin_id')
  255. ->join(['auth_group_access' => 'aga_qds'], 'aga_qds.uid = qds.id and aga_qds.group_id=3')
  256. ->where(['vip_admin_bind.admin_id_master' => $this->auth->id])
  257. ->where($where)
  258. ->order('vip_admin_bind.id', $order);
  259. return $fetchObj;
  260. }
  261. /**
  262. * @param $qdsIds
  263. * @return array
  264. * @throws \think\db\exception\DataNotFoundException
  265. * @throws \think\db\exception\ModelNotFoundException
  266. * @throws \think\exception\DbException
  267. */
  268. private function getOperatorInfoByQdsIds($qdsIds)
  269. {
  270. if (empty($qdsIds)) {
  271. return [];
  272. }
  273. $operatorList = model('VipAdminBind')->join(['admin' => 'operator'], 'operator.id = vip_admin_bind.admin_id_slave')
  274. ->where(['vip_admin_bind.admin_id_slave' => ['in', $qdsIds]])
  275. ->field(['vip_admin_bind.admin_id_slave', 'operator.nickname as qds_nickname', 'operator.username as qds_username', 'operator.id' => 'qds_id'])
  276. ->select();
  277. $result = [];
  278. foreach ($operatorList as $operator) {
  279. $result[$operator->admin_id_slave] = [
  280. 'qds_nickname' => $operator->qds_nickname,
  281. 'qds_id' => $operator->qds_id,
  282. 'qds_username' => $operator->qds_username,
  283. ];
  284. }
  285. return $result;
  286. }
  287. /**
  288. * 选择
  289. */
  290. public function doChoose($ids = "", $qdsIds = "")
  291. {
  292. if (empty($qdsIds) || empty($ids)) {
  293. Log::info('缺少参数');
  294. return json_encode(['code' => 200]);
  295. }
  296. $qdsIds = trim($qdsIds, ',');
  297. $qdsIds = explode(',', $qdsIds);
  298. //过滤一下不是该VIP下账号的
  299. $rows = model('VipAdminBind')->where('admin_id_master', 'eq', $this->auth->id)->where('admin_id_slave', 'in', $qdsIds)->where('flag', 'eq', 1)->select();
  300. if (!empty($rows)) {
  301. $library = $this->model->join('referral', 'referral_library.referral_id = referral.id')->where('referral_library.referral_id', 'eq', $ids)->field("referral_library.id as libId, referral_library.short_url as vip_short_url,referral.*")->find();
  302. if ($library) {
  303. foreach ($rows as $row) {
  304. $re = model("ReferralSlave")->join("referral", 'referral_slave.referral_id = referral.id')->where('referral_slave.library_id', 'eq', $library->libId)->where('referral.admin_id', 'eq', $row->admin_id_slave)->find();
  305. if (!empty($re)) continue;
  306. $channelId = $row->admin_id_slave;
  307. //去生成推广链接
  308. $params = [
  309. 'push' => $library->push,
  310. 'manage_title_id' => $library->manage_title_id,
  311. 'manage_cover_id' => $library->manage_cover_id,
  312. 'manage_template_id' => $library-> manage_template_id,
  313. 'manage_preview_id' => $library->manage_preview_id,
  314. 'book_id' => $library->book_id,
  315. 'chapter_id' => $library->chapter_id,
  316. 'chapter_name' => $library->chapter_name,
  317. 'chapter_idx' => $library->chapter_idx,
  318. 'guide_chapter_idx' => $library->guide_chapter_idx,
  319. 'admin_id' => $channelId,
  320. 'name' => $library->name,
  321. //'cost' => $library->cost,
  322. 'wx_type' => $library->wx_type,
  323. 'type' => $library->type,
  324. 'guide_idx' => $library->guide_idx,
  325. 'guide_sex' => $library->guide_sex,
  326. 'guide_title' => $library->guide_title,
  327. 'guide_image' => $library->guide_image,
  328. 'share_image' => $library->share_image,
  329. 'share_title' => $library->share_title,
  330. 'createtime' => time(),
  331. ];
  332. $insertId = model("Referral")->allowField(true)->insertGetId($params);
  333. if($insertId === false){
  334. $this->error($this->model->getError());
  335. }
  336. $source_url = '';
  337. $source_url .= '/index/book/chapter?book_id=' . $params['book_id'] . '&sid=' . $params['chapter_id'] . '&referral_id=' . $insertId;
  338. $updateData['source_url'] =getCurrentDomain($channelId,$source_url);
  339. $updateData['jmp_url'] = getCurrentDomain($channelId,'/t/'.$insertId);
  340. //绑定域名短链ID及重置生成短链接的源地址,代理商使用渠道的短链域名池
  341. $short = model('ShortRelation')->getRandShort($channelId);
  342. if($short){
  343. $updateData['short_id'] = $short->id;
  344. $short_source_url = replaceShortDomain($updateData['jmp_url'], $short->id);
  345. $updateData['jmp_url'] = $short_source_url;
  346. }else{
  347. $short_source_url = $updateData['source_url'];
  348. }
  349. $shoturl = new ShortUrl();
  350. //生成腾讯短链
  351. $updateData['short_url_qq'] = $shoturl->tencent($channelId, $short_source_url);
  352. //生成sina短链
  353. $updateData['short_url_weibo'] = $shoturl->sina($short_source_url);
  354. model("Referral")->save($updateData, ['id' => $insertId]);
  355. //绑定
  356. model("ReferralSlave")->allowField(true)->insertGetId(['library_id' => $library->libId, 'channel_id' => $channelId,'referral_id' => $insertId, 'createtime' => time()]);
  357. //删除Redis缓存
  358. $redis = Redis::instance();
  359. $redis->del('RI:N:'.$insertId);
  360. //增加分流链接的key
  361. $redisKey = BookConstants::VIP_REFERRAL_KEY.$library->libId;
  362. $redis->sadd($redisKey,$insertId);
  363. $redis->sRem($redisKey,0);
  364. }
  365. //如果短链没生成 则取第一个的域名
  366. if (empty($library->vip_short_url)) {
  367. $channelId = reset($qdsIds);
  368. $source_url = '/v/'.$library->libId;
  369. $updateData = [];
  370. $updateData['short_url'] = getCurrentDomain($channelId,$source_url);
  371. $this->model->save($updateData, ['id' => $library->libId]);
  372. }
  373. } else {
  374. Log::error('library 空的');
  375. }
  376. }
  377. return json_encode(['code' => 200]);
  378. }
  379. /**
  380. * 查看分流链接
  381. * @param string $ids
  382. */
  383. public function viewdata($ids ='')
  384. {
  385. if (empty($ids)) {
  386. $this->error('缺少参数');
  387. }
  388. $library = $this->model->get($ids);
  389. //设置过滤方法
  390. $this->request->filter(['strip_tags']);
  391. if ($this->request->isAjax()) {
  392. //如果发送的来源是Selectpage,则转发到Selectpage
  393. if ($this->request->request('pkey_name')) {
  394. return $this->selectpage();
  395. }
  396. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  397. $map = [
  398. 'referral_library.admin_id' => $this->auth->id,
  399. 'referral_slave.library_id' => $ids
  400. ];
  401. $total = model("ReferralSlave")
  402. ->join('referral_library', 'referral_library.id = referral_slave.library_id')
  403. ->join('referral', 'referral.id = referral_slave.referral_id')
  404. ->where($where)
  405. ->where($map)
  406. ->count();
  407. $list = model("ReferralSlave")
  408. ->join('referral_library', 'referral_library.id = referral_slave.library_id')
  409. ->join('referral', 'referral.id = referral_slave.referral_id')
  410. ->join('book', 'book.id = referral.book_id')
  411. ->where($where)
  412. ->where($map)
  413. ->field('referral.*, book.name as book_name')
  414. ->order($sort, $order)
  415. ->limit($offset, $limit)
  416. ->select();
  417. if (!empty($list)) {
  418. $channelids = array_column($list, 'admin_id');
  419. $channelInfos = model("AdminConfig")->alias('ac')->join('admin a', 'ac.admin_id = a.id')->field('ac.admin_id, a.username, a.nickname, ac.json')->where("ac.admin_id", 'in', implode(',', $channelids))->select();
  420. $channelInfos = ArrayHelper::index($channelInfos, 'admin_id');
  421. foreach ($list as $k=>$v){
  422. $ophost_url = $this->getChannelOphost($v['admin_id']);
  423. //获取当前书籍的默认关注章节
  424. $list[$k]['url_referral'] = $ophost_url.'/t/'.$v['id'];
  425. $list[$k]['skin_url'] = ReferralService::instance()->getSkinUrl($v['id'], $v['source_url']);
  426. $dayMTkey = "M-T:".$v['id'].":".date("d"); //今日充值金额key
  427. $list[$k]['dayuv'] = (int)Redis::instance()->get(CacheConstants::getReadOfReferralIdKey($v['id']));
  428. $list[$k]['dayut'] = (int)Redis::instance()->get(CacheConstants::getSubscribeOfReferralIdKey($v['id'])); //今日关注人数
  429. $list[$k]['daymt'] = (int)Redis::instance()->get($dayMTkey)? round(Redis::instance()->get($dayMTkey) / 100, 2) :0; //今日充值金额
  430. $list[$k]['dayjt'] = (int)Redis::instance()->get(CacheConstants::getSubscribeOfPureReferralIdKey($v['id'])); //今日净关注人数
  431. $list[$k]['dayqt'] = (int)Redis::instance()->get(CacheConstants::getUnsubscribeOfReferralIdKey($v['id'])); //今日取消关注人数
  432. $list[$k]['daydt'] = (int)Redis::instance()->get(CacheConstants::getSubscribeOfDimReferralIdKey($v['id'])); //今日导粉号模糊关注人数
  433. $list[$k]['guide_follow_num'] = (int)$list[$k]['guide_follow_num'];
  434. $list[$k]['net_follow_num'] = (int)$list[$k]['net_follow_num'];
  435. $dayMTNkey = "M-T-N:".$v['id'].":".date("d"); //今日充值笔数key
  436. $list[$k]['order_nums'] = (int)$list[$k]['orders_num'];
  437. $list[$k]['day_order_nums'] = (int)Redis::instance()->get($dayMTNkey);
  438. if(!empty($v['book_id'])){
  439. $isLimited = model('BookLimit')->backendHasLimit($this->bookAgentId,$this->bookChannelId,$v['book_id']);
  440. if($isLimited){
  441. $list[$k]['limited'] = 1;
  442. }else{
  443. $list[$k]['limited'] = 0;
  444. }
  445. }else{
  446. $list[$k]['limited'] = 0;
  447. }
  448. if (isset($channelInfos[$v['admin_id']])) {
  449. $list[$k]['username'] = $channelInfos[$v['admin_id']]['username'];
  450. $list[$k]['nickname'] = $channelInfos[$v['admin_id']]['nickname'];
  451. $list[$k]['gzhname'] = $channelInfos[$v['admin_id']]['json']['authorizer_info']['nick_name'];
  452. }
  453. $list[$k]['short_url'] = replaceShortDomain($list[$k]['url_referral'], $v['short_id']);
  454. }
  455. }
  456. $result = array("total" => $total, "rows" => $list);
  457. return json($result);
  458. }
  459. $this->assignconfig('id', $ids);
  460. $this->assignconfig('referral_id', $library->referral_id);
  461. return $this->view->fetch();
  462. }
  463. /**
  464. * 删除
  465. */
  466. public function del($ids = "")
  467. {
  468. if ($ids)
  469. {
  470. $adminIds = $this->getDataLimitAdminIds();
  471. if (is_array($adminIds))
  472. {
  473. $count = $this->model->where($this->dataLimitField, 'in', $adminIds);
  474. }
  475. $list = model("ReferralSlave")->join('referral_library', 'referral_library.id = referral_slave.library_id')->where('referral_library.admin_id', 'eq', $this->auth->id)->where('referral_slave.referral_id', 'in', $ids)->field("referral_slave.*")->select();
  476. $count = 0;
  477. foreach ($list as $k => $v)
  478. {
  479. $count += model("ReferralSlave")->where('id', 'eq', $v['id'])->delete();
  480. $redis = Redis::instance();
  481. //增加分流链接的key
  482. $redisKey = BookConstants::VIP_REFERRAL_KEY.$v['library_id'];
  483. $redis->sRem($redisKey,$v['referral_id']);
  484. }
  485. if ($count)
  486. {
  487. $this->success();
  488. }
  489. else
  490. {
  491. $this->error(__('No rows were deleted'));
  492. }
  493. }
  494. $this->error(__('Parameter %s can not be empty', 'ids'));
  495. }
  496. public function viewdataexport($ids = null)
  497. {
  498. if (!in_array($this->group, [7, 8])) {
  499. $this->error('当前用户不是vip也不是vip运营');
  500. }
  501. if (empty($ids)) {
  502. $this->error('请选择导出的分流链接');
  503. }
  504. ini_set('memory_limit', '256M'); //内存限制
  505. $columns = [
  506. '公众号名称',
  507. '渠道商名称',
  508. '渠道商账号',
  509. '推广链接',
  510. '链接ID',
  511. '书籍ID',
  512. '入口页面(书名、关注章节)',
  513. '名称(渠道名称)',
  514. '创建时间'
  515. ];
  516. $redis = Redis::instance();
  517. header('Content-Description: File Transfer');
  518. header('Content-Type: application/vnd.ms-excel');
  519. header('Content-Disposition: attachment; filename="代理商推广链接统计数据-' . date('YmdHis', time()) . '.csv"');
  520. header('Expires: 0');
  521. header('Cache-Control: must-revalidate');
  522. header('Pragma: public');
  523. $fp = fopen('php://output', 'a');//打开output流
  524. mb_convert_variables('GBK', 'UTF-8', $columns);
  525. fputcsv($fp, $columns);//将数据格式化为CSV格式并写入到output流中
  526. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  527. $channelIds = $this->vipAdminBindModel->getChannelIds($this->auth->id);
  528. if (empty($channelIds)) {
  529. fclose($fp);
  530. exit();
  531. }
  532. //获取所有子链接
  533. $total = model("ReferralSlave")->where('library_id', 'eq', $ids)->count();
  534. if ($total == 0) {
  535. fclose($fp);
  536. exit();
  537. }
  538. $limit = 3000;
  539. $pages = ceil($total / $limit);
  540. for ($i = 1; $i <= $pages; $i++) {
  541. $offset = ($i - 1) * $limit;
  542. $list = model("ReferralSlave")
  543. ->join('referral', 'referral_slave.referral_id = referral.id')->join('admin', 'admin.id = referral.admin_id')->join('admin_config', 'admin.id = admin_config.admin_id')
  544. ->join('book', 'referral.book_id = book.id', 'left')
  545. ->join('ophost', 'admin_config.ophost_id = ophost.id', 'left')
  546. ->field([
  547. 'referral.*',
  548. 'book.name' => 'book_name',
  549. 'admin.username',
  550. 'admin.nickname',
  551. 'admin_config.json' => 'wx_json',
  552. 'admin_config.entryhost_id',
  553. 'admin_config.appid',
  554. 'ophost.host',
  555. ])
  556. ->where('library_id', 'eq', $ids)
  557. ->order('referral_slave.id', 'desc')
  558. ->limit($offset, $limit)
  559. ->select();
  560. foreach ($list as $k => $v) {
  561. $channelId = $v['admin_id'];
  562. //获取当前书籍的默认关注章节
  563. if (empty($v['guide_chapter_idx']) && !empty($v['book_id'])) {
  564. $list[$k]['guide_chapter_idx'] = $this->guideModel->getGuideChapter($v['admin_id'], $v['book_id'], 0, $channelId);
  565. }
  566. if (!empty($v['entryhost_id'])) {
  567. $host = $this->entryhostModel->getInfo($channelId);
  568. $urlReferral = Config::get('site.scheme') . '://' . $host['host'];
  569. } else {
  570. $urlReferral = Config::get('site.scheme') . '://' . $v['appid'] . '.' . $v['host'];
  571. }
  572. $list[$k]['url_referral'] = $urlReferral . '/t/' . $v['id'];
  573. if (!empty($v['book_id'])) {
  574. $isLimited = model('BookLimit')->backendHasLimit(0, $v['admin_id'], $v['book_id']);
  575. if ($isLimited) {
  576. $list[$k]['limited'] = 1;
  577. }
  578. }
  579. $list[$k]['short_url'] = replaceShortDomain($list[$k]['url_referral'], $v['short_id']);
  580. if (isset($v['wx_json'])) {
  581. $adminJson = json_decode($v['wx_json'], true);
  582. if (isset($adminJson['authorizer_info']['nick_name'])) {
  583. $list[$k]['wx_nickname'] = $adminJson['authorizer_info']['nick_name'];
  584. } else {
  585. $list[$k]['wx_nickname'] = '';
  586. }
  587. } else {
  588. $list[$k]['wx_nickname'] = '';
  589. }
  590. $entry_page = '';
  591. if (isset($v['type'])) {
  592. if ($v['type'] == 1) {
  593. if (isset($v['book_name']) && isset($v['chapter_name'])) {
  594. $entry_page .= $v['book_name'];
  595. $entry_page .= '--' . $v['chapter_name'];
  596. if (isset($list[$k]['guide_chapter_idx']) && !empty($list[$k]['guide_chapter_idx'])) {
  597. $entry_page .= '--关注章节:' . $list[$k]['guide_chapter_idx'];
  598. } else {
  599. $entry_page .= '--关注章节:默认';
  600. }
  601. } else {
  602. $entry_page = '未知';
  603. }
  604. } elseif ($v['type'] == 3) {
  605. $entry_page .= '落地页推广';
  606. if (isset($v['book_name']) && isset($v['chapter_name'])) {
  607. $entry_page .= '--' . $v['book_name'];
  608. $entry_page .= '--' . $v['chapter_name'];
  609. }
  610. if (isset($list[$k]['guide_chapter_idx']) && !empty($list[$k]['guide_chapter_idx'])) {
  611. $entry_page .= '--关注章节:' . $list[$k]['guide_chapter_idx'];
  612. } else {
  613. $entry_page .= '--关注章节:默认';
  614. }
  615. } else {
  616. $entry_page .= '书城首页推广';
  617. }
  618. } else {
  619. $entry_page = '未知';
  620. }
  621. //获取每列数据,转换处理成需要导出的数据
  622. $rowData = [
  623. $v['wx_nickname'] ?? '',
  624. $v['nickname'] ?? '',
  625. $v['username'] ?? '',
  626. $v['url_referral'] ?? '',
  627. $v['id'] ?? 0,
  628. empty($v['book_id']) ? 0 : "\t".$v['book_id']."\t",
  629. $entry_page ?? 0,
  630. $v['name'] ?? '',
  631. empty($v['createtime']) ? '无': date('Y-m-d H:i:s', $v['createtime'])
  632. ];
  633. //需要格式转换,否则会乱码
  634. mb_convert_variables('GBK', 'UTF-8', $rowData);
  635. fputcsv($fp, $rowData);
  636. }
  637. fclose($fp);
  638. exit();
  639. }
  640. }
  641. private function getChannelOphost($channel_id){
  642. $adminConfig = model('adminConfig')->getAdminInfoAll($channel_id);
  643. $url_referral = Config::get('site.scheme').'://'.$adminConfig['appid'].'.'.$adminConfig['ophost_host'];
  644. return $url_referral;
  645. }
  646. }