Exclusive.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use think\Controller;
  5. use think\Request;
  6. use think\Db;
  7. /**
  8. * 渠道独家管理
  9. *
  10. * @icon fa fa-circle-o
  11. */
  12. class Exclusive extends Backend
  13. {
  14. /**
  15. * @var \app\admin\model\Exclusive
  16. */
  17. protected $model = null;
  18. public function _initialize()
  19. {
  20. parent::_initialize();
  21. $this->model = model('Exclusive');
  22. }
  23. /**
  24. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法
  25. * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑
  26. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  27. */
  28. /**
  29. * 查看
  30. */
  31. public function index()
  32. {
  33. //设置过滤方法
  34. $this->request->filter(['strip_tags']);
  35. if ($this->request->isAjax())
  36. {
  37. //如果发送的来源是Selectpage,则转发到Selectpage
  38. if ($this->request->request('pkey_name'))
  39. {
  40. return $this->selectpage();
  41. }
  42. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  43. $filter = $this->request->get("filter", '');
  44. $filter = json_decode($filter, true);
  45. $where = [];
  46. $wherecnkey = '';
  47. $wherecikey = '';
  48. if ($filter) {
  49. foreach ($filter as $k => $v) {
  50. if($k=='channel_nickname'){
  51. $wherecnkey = $v;
  52. }elseif($k=='channel_id'){
  53. $wherecikey = $v;
  54. }else{
  55. $where[$k] = $v;
  56. }
  57. }
  58. }
  59. //搜索渠道名称
  60. if(!empty($wherecnkey) || !empty($wherecikey)){
  61. $ren = $this->getChannelIdsByNameAndId($wherecnkey,$wherecikey);
  62. if(!empty($ren)){
  63. $wherecn['id'] = ['in',implode(',',$ren)];
  64. }else{
  65. $wherecn['id'] = ['eq',''];
  66. }
  67. }else{
  68. $wherecn = [];
  69. }
  70. $total = $this->model
  71. ->where($where)
  72. ->where($wherecn)
  73. ->order($sort, $order)
  74. ->count();
  75. $list = $this->model
  76. ->where($where)
  77. ->where($wherecn)
  78. ->order($sort, $order)
  79. ->limit($offset, $limit)
  80. ->select();
  81. $result = array("total" => $total, "rows" => $list);
  82. return json($result);
  83. }
  84. return $this->view->fetch();
  85. }
  86. /**
  87. * 删除
  88. */
  89. public function del($ids = "")
  90. {
  91. if ($ids)
  92. {
  93. $pk = $this->model->getPk();
  94. $adminIds = $this->getDataLimitAdminIds();
  95. if (is_array($adminIds))
  96. {
  97. $count = $this->model->where($this->dataLimitField, 'in', $adminIds);
  98. }
  99. $list = $this->model->where($pk, 'in', $ids)->select();
  100. $count = 0;
  101. foreach ($list as $k => $v)
  102. {
  103. $cbwhere['eid'] = ['eq',$v['id']];
  104. model('ExclusiveChannel')->where($cbwhere)->delete();
  105. model('ExclusiveBook')->where($cbwhere)->delete();
  106. $count += $v->delete();
  107. }
  108. if ($count)
  109. {
  110. $this->success();
  111. }
  112. else
  113. {
  114. $this->error(__('No rows were deleted'));
  115. }
  116. }
  117. $this->error(__('Parameter %s can not be empty', 'ids'));
  118. }
  119. //关联渠道
  120. public function bindchannels($ids){
  121. $row = $this->model->get($ids);
  122. if (!$row) {
  123. $this->error(__('No Results were found'));
  124. }
  125. $bindids = '';
  126. $bind_ids = $this->get_bind_channel_ids($ids);
  127. if(!empty($bind_ids)){
  128. $bindids = implode(',',$bind_ids);
  129. }
  130. $this->request->filter(['strip_tags']);
  131. if ($this->request->isAjax())
  132. {
  133. //如果发送的来源是Selectpage,则转发到Selectpage
  134. if ($this->request->request('pkey_name'))
  135. {
  136. return $this->selectpage();
  137. }
  138. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  139. $filter = $this->request->get("filter", '');
  140. $filter = json_decode($filter, true);
  141. $where = [];
  142. $wherekey = '';
  143. $whereukey = '';
  144. if ($filter) {
  145. foreach ($filter as $k => $v) {
  146. if($k=='nickname'){
  147. $wherekey = $v;
  148. }elseif($k=='username'){
  149. $whereukey = $v;
  150. }else{
  151. $where[$k] = $v;
  152. }
  153. }
  154. }
  155. //搜索公众号昵称
  156. if(!empty($wherekey)){
  157. $whereuser['ad.nickname'] = ["like","%{$wherekey}%"];
  158. }else{
  159. $whereuser = [];
  160. }
  161. //搜索用户名
  162. if(!empty($whereukey)){
  163. $wheresub['ad.username'] = ["like","%{$whereukey}%"];
  164. }else{
  165. $wheresub = [];
  166. }
  167. $total = model('admin')->alias('ad')
  168. ->join('admin_config ac','ad.id=ac.admin_id')
  169. ->where($where)
  170. ->where($wheresub)
  171. ->where($whereuser)
  172. ->order($sort, $order)
  173. ->count();
  174. $list = model('admin')->alias('ad')
  175. ->join('admin_config ac','ad.id=ac.admin_id')
  176. ->where($where)
  177. ->where($wheresub)
  178. ->where($whereuser)
  179. ->field(['ad.id,ad.username,ad.nickname'])
  180. ->order($sort, $order)
  181. ->limit($offset, $limit)
  182. ->select();
  183. $result = array("total" => $total, "rows" => $list);
  184. return json($result);
  185. }
  186. $this->view->assign("row", $row);
  187. $this->assignconfig("ids", $ids);
  188. $this->assignconfig("bindids", $bindids);
  189. return $this->view->fetch();
  190. }
  191. //查看渠道
  192. public function showchannels($ids){
  193. $row = $this->model->get($ids);
  194. if (!$row) {
  195. $this->error(__('No Results were found'));
  196. }
  197. $bindids = '';
  198. $bind_ids = $this->get_bind_channel_ids($ids);
  199. if(!empty($bind_ids)){
  200. $bindids = implode(',',$bind_ids);
  201. }
  202. if ($this->request->isAjax())
  203. {
  204. //如果发送的来源是Selectpage,则转发到Selectpage
  205. if ($this->request->request('pkey_name'))
  206. {
  207. return $this->selectpage();
  208. }
  209. $this->model = model('admin');
  210. list($where, $sort, $order, $offset, $limit) = $this->buildparams(null, true);
  211. if(empty($bindids)){
  212. $wherebind['admin.id'] = ['eq',''];
  213. }else{
  214. $wherebind['admin.id'] = ['in',$bindids];
  215. }
  216. $total = model('admin')
  217. ->join('admin_config ac','admin.id=ac.admin_id')
  218. ->where($where)
  219. ->where($wherebind)
  220. ->order($sort, $order)
  221. ->count();
  222. $list = model('admin')
  223. ->join('admin_config ac','admin.id=ac.admin_id')
  224. ->where($where)
  225. ->where($wherebind)
  226. ->field(['admin.id,admin.username,admin.nickname'])
  227. ->order($sort, $order)
  228. ->limit($offset, $limit)
  229. ->select();
  230. $result = array("total" => $total, "rows" => $list);
  231. return json($result);
  232. }
  233. $this->assignconfig("ids", $ids);
  234. $this->assignconfig("bindids", $bindids);
  235. $this->view->assign("row", $row);
  236. return $this->view->fetch();
  237. }
  238. //批量关联渠道
  239. public function batch_bind_channels(){
  240. if ($this->request->isPost())
  241. {
  242. $params = $this->request->post();
  243. if(empty($params['eid']) || empty($params['ids'])){
  244. $this->error('参数错误!');
  245. }
  246. $data = [];
  247. foreach($params['ids'] as $id){
  248. $data[] = [
  249. 'eid' => $params['eid'],
  250. 'cid' => $id
  251. ];
  252. }
  253. $rs = model('ExclusiveChannel')->insertAll($data);
  254. // $sql = 'INSERT INTO `exclusive_channel`(`eid`,`cid`) VALUES ';
  255. // foreach($params['ids'] as $id){
  256. // $sql .= ' ('.$params['eid'].','.$id.'),';
  257. // }
  258. // $sql = rtrim($sql,',');
  259. // $rs = Db::query($sql);
  260. if($rs){
  261. $this->success('操作成功');
  262. }else{
  263. $this->error(json_encode($rs,JSON_UNESCAPED_UNICODE));
  264. }
  265. }
  266. }
  267. //批量取消关联渠道
  268. public function batch_unbind_channels(){
  269. if ($this->request->isPost())
  270. {
  271. $params = $this->request->post();
  272. if(empty($params['eid']) || empty($params['ids'])){
  273. $this->error('参数错误!');
  274. }
  275. $cwhere['eid'] = ['eq',$params['eid']];
  276. $cwhere['cid'] = ['in',implode(',',$params['ids'])];
  277. $rs = model('ExclusiveChannel')->where($cwhere)->delete();
  278. // $sql = 'DELETE FROM `exclusive_channel` where eid='.$params['eid'].' and cid in (';
  279. // foreach($params['ids'] as $id){
  280. // $sql .= $id.',';
  281. // }
  282. // $sql = rtrim($sql,',');
  283. // $sql .= ')';
  284. // $rs = Db::execute($sql);
  285. if($rs){
  286. $this->success('操作成功');
  287. }else{
  288. $this->error(json_encode($rs,JSON_UNESCAPED_UNICODE));
  289. }
  290. }
  291. }
  292. //获取绑定的渠道id
  293. private function get_bind_channel_ids($ids){
  294. $rs = model('ExclusiveChannel')->where('eid','eq',$ids)->column('cid');
  295. return $rs;
  296. }
  297. //获取所有渠道id
  298. private function get_all_channel_ids(){
  299. $rs = model('Admin')->column('id');
  300. // $sql = "SELECT ad.id FROM admin as ad INNER JOIN admin_config as ac ON ad.id=ac.admin_id where ac.json!=''";
  301. // $rs = Db::query($sql);
  302. return $rs;
  303. }
  304. //关联全部渠道
  305. public function bind_all_channels(){
  306. if ($this->request->isPost())
  307. {
  308. $params = $this->request->post();
  309. if(empty($params['ids'])){
  310. $this->error('参数错误!');
  311. }
  312. $all_ids = $this->get_all_channel_ids();
  313. $exist_ids= $this->get_bind_channel_ids($params['ids']);
  314. $diff_ids = array_diff($all_ids,$exist_ids);
  315. if(empty($diff_ids)){
  316. $this->success('操作成功');
  317. }
  318. $data = [];
  319. foreach($diff_ids as $id){
  320. $data[] = [
  321. 'eid' => $params['ids'],
  322. 'cid' => $id
  323. ];
  324. }
  325. $rs = model('ExclusiveChannel')->insertAll($data);
  326. // $sql = 'INSERT INTO `exclusive_channel`(`eid`,`cid`) VALUES ';
  327. // foreach($diff_ids as $id){
  328. // $sql .= ' ('.$params['ids'].','.$id.'),';
  329. // }
  330. // $sql = rtrim($sql,',');
  331. // $rs = Db::query($sql);
  332. if($rs){
  333. $this->success('操作成功');
  334. }else{
  335. $this->error(json_encode($rs,JSON_UNESCAPED_UNICODE));
  336. }
  337. }
  338. }
  339. //取消关联全部渠道
  340. public function unbind_all_channels(){
  341. if ($this->request->isPost())
  342. {
  343. $params = $this->request->post();
  344. if(empty($params['ids'])){
  345. $this->error('参数错误!');
  346. }
  347. $rs = model('ExclusiveChannel')->where('eid','eq',$params['ids'])->delete();
  348. // $sql = 'DELETE FROM `exclusive_channel` where eid='.$params['ids'];
  349. // $rs = Db::execute($sql);
  350. if($rs){
  351. $this->success('操作成功');
  352. }else{
  353. $this->error(json_encode($rs,JSON_UNESCAPED_UNICODE));
  354. }
  355. }
  356. }
  357. //关联书籍
  358. public function bindbooks($ids){
  359. $row = $this->model->get($ids);
  360. if (!$row) {
  361. $this->error(__('No Results were found'));
  362. }
  363. $bindids = '';
  364. $bind_ids = $this->get_bind_book_ids($ids);
  365. if(!empty($bind_ids)){
  366. $bindids = implode(',',$bind_ids);
  367. }
  368. if ($this->request->isAjax())
  369. {
  370. //如果发送的来源是Selectpage,则转发到Selectpage
  371. if ($this->request->request('pkey_name'))
  372. {
  373. return $this->selectpage();
  374. }
  375. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  376. $whereb['state'] = ['eq',1];
  377. $whereb['cansee'] = ['eq',1];
  378. $total = model('book')
  379. ->where($where)
  380. ->where($whereb)
  381. ->order($sort, $order)
  382. ->count();
  383. $list = model('book')
  384. ->where($where)
  385. ->where($whereb)
  386. ->field(['id,name'])
  387. ->order($sort, $order)
  388. ->limit($offset, $limit)
  389. ->select();
  390. $result = array("total" => $total, "rows" => $list);
  391. return json($result);
  392. }
  393. $this->view->assign("row", $row);
  394. $this->assignconfig("ids", $ids);
  395. $this->assignconfig("bindids", $bindids);
  396. return $this->view->fetch();
  397. }
  398. //查看书籍
  399. public function showbooks($ids){
  400. $row = $this->model->get($ids);
  401. if (!$row) {
  402. $this->error(__('No Results were found'));
  403. }
  404. $bindids = '';
  405. $bind_ids = $this->get_bind_book_ids($ids);
  406. if(!empty($bind_ids)){
  407. $bindids = implode(',',$bind_ids);
  408. }
  409. if ($this->request->isAjax())
  410. {
  411. //如果发送的来源是Selectpage,则转发到Selectpage
  412. if ($this->request->request('pkey_name'))
  413. {
  414. return $this->selectpage();
  415. }
  416. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  417. if(empty($bindids)){
  418. $wherebind['id'] = ['eq',''];
  419. }else{
  420. $wherebind['id'] = ['in',$bindids];
  421. }
  422. $total = model('book')
  423. ->where($where)
  424. ->where($wherebind)
  425. ->order($sort, $order)
  426. ->count();
  427. $list = model('book')
  428. ->where($where)
  429. ->where($wherebind)
  430. ->field(['id,name'])
  431. ->order($sort, $order)
  432. ->limit($offset, $limit)
  433. ->select();
  434. $result = array("total" => $total, "rows" => $list);
  435. return json($result);
  436. }
  437. $this->assignconfig("ids", $ids);
  438. $this->assignconfig("bindids", $bindids);
  439. $this->view->assign("row", $row);
  440. return $this->view->fetch();
  441. }
  442. //批量关联书籍
  443. public function batch_bind_books(){
  444. if ($this->request->isPost())
  445. {
  446. $params = $this->request->post();
  447. if(empty($params['eid']) || empty($params['ids'])){
  448. $this->error('参数错误!');
  449. }
  450. $data = [];
  451. foreach($params['ids'] as $id){
  452. $data[] = [
  453. 'eid' => $params['eid'],
  454. 'bid' => $id
  455. ];
  456. }
  457. $rs = model('ExclusiveBook')->insertAll($data);
  458. // $sql = 'INSERT INTO `exclusive_book`(`eid`,`bid`) VALUES ';
  459. // foreach($params['ids'] as $id){
  460. // $sql .= ' ('.$params['eid'].','.$id.'),';
  461. // }
  462. // $sql = rtrim($sql,',');
  463. // $rs = Db::query($sql);
  464. if($rs){
  465. $this->success('操作成功');
  466. }else{
  467. $this->error(json_encode($rs,JSON_UNESCAPED_UNICODE));
  468. }
  469. }
  470. }
  471. //批量取消书籍
  472. public function batch_unbind_books(){
  473. if ($this->request->isPost())
  474. {
  475. $params = $this->request->post();
  476. if(empty($params['eid']) || empty($params['ids'])){
  477. $this->error('参数错误!');
  478. }
  479. $bwhere = [
  480. 'eid' => $params['eid'],
  481. 'bid' => ['in',implode(',',$params['ids'])]
  482. ];
  483. $rs = model('ExclusiveBook')->where($bwhere)->delete();
  484. // $sql = 'DELETE FROM `exclusive_book` where eid='.$params['eid'].' and bid in (';
  485. // foreach($params['ids'] as $id){
  486. // $sql .= $id.',';
  487. // }
  488. // $sql = rtrim($sql,',');
  489. // $sql .= ')';
  490. // $rs = Db::execute($sql);
  491. if($rs){
  492. $this->success('操作成功');
  493. }else{
  494. $this->error(json_encode($rs,JSON_UNESCAPED_UNICODE));
  495. }
  496. }
  497. }
  498. //获取绑定的书籍id
  499. private function get_bind_book_ids($ids){
  500. $rs = model('ExclusiveBook')->where('eid','eq',$ids)->column('bid');
  501. return $rs;
  502. }
  503. //取消关联书籍
  504. public function unbind_all_books(){
  505. if ($this->request->isPost())
  506. {
  507. $params = $this->request->post();
  508. if(empty($params['ids'])){
  509. $this->error('参数错误!');
  510. }
  511. $rs = model('ExclusiveBook')->where('eid','eq',$params['ids'])->delete();
  512. // $sql = 'DELETE FROM `exclusive_book` where eid='.$params['ids'];
  513. // $rs = Db::execute($sql);
  514. if($rs){
  515. $this->success('操作成功');
  516. }else{
  517. $this->error(json_encode($rs,JSON_UNESCAPED_UNICODE));
  518. }
  519. }
  520. }
  521. //搜索渠道名称
  522. private function getChannelIdsByNameAndId($name, $id){
  523. $time = time();
  524. $ers = [];
  525. if(empty($name) && empty($id)){
  526. return $ers;
  527. }
  528. $cwhere = [
  529. 'e.endtime' => ['>',$time],
  530. 'e.status' => ['eq',1]
  531. ];
  532. $whereEq = [];
  533. $whereIn = [];
  534. if(!empty($id)){
  535. $whereEq['ec.cid'] = ['eq', $id];
  536. }
  537. if(!empty($name)){
  538. $rs = model('Admin')->where('nickname','LIKE','%'.$name.'%')->column('id');
  539. if(!empty($rs)){
  540. $whereIn['ec.cid'] = ['in',implode(',',$rs)];
  541. } else {
  542. return $ers;
  543. }
  544. }
  545. $ers = model('ExclusiveChannel')->alias('ec')
  546. ->join('exclusive e',' ec.eid=e.id')
  547. ->where($cwhere)
  548. ->where($whereEq)
  549. ->where($whereIn)
  550. ->column('distinct ec.eid');
  551. return $ers;
  552. }
  553. }