Book.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <?php
  2. namespace app\admin\controller\booklistch;
  3. use app\common\controller\Backend;
  4. use think\Controller;
  5. use think\Request;
  6. /**
  7. * 书单管理
  8. *
  9. * @icon fa fa-circle-o
  10. */
  11. class Book extends Backend
  12. {
  13. /**
  14. * BookList模型对象
  15. */
  16. protected $model = null;
  17. protected $vipAdminBindModel = null;
  18. public function _initialize()
  19. {
  20. parent::_initialize();
  21. $this->model = model('BookListCh');
  22. $this->vipAdminBindModel = model('VipAdminBind');
  23. $this->view->assign("statusList", $this->model->getStatusList());
  24. }
  25. /**
  26. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法
  27. * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑
  28. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  29. */
  30. /**
  31. * 查看
  32. */
  33. public function index()
  34. {
  35. //设置过滤方法
  36. $this->request->filter(['strip_tags']);
  37. $limitShow = $this->group == 3 ? true : false;
  38. //是否控制显示链接
  39. if ($this->group == 4 && !empty($this->auth->agent_id)) {
  40. //判断是否是配号代理
  41. $limitShow = true;
  42. }
  43. if ($this->request->isAjax())
  44. {
  45. //如果发送的来源是Selectpage,则转发到Selectpage
  46. if ($this->request->request('pkey_name'))
  47. {
  48. return $this->selectpage();
  49. }
  50. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  51. $maps = [];
  52. //判断角色
  53. $maps['admin_id'] = $this->auth->id;
  54. if (in_array($this->group, [7, 8])) {
  55. //vip 或 vip运营只显示自己
  56. $maps['admin_id'] = $this->auth->id;
  57. } else {
  58. //找账号绑定信息
  59. $bindModel = $this->vipAdminBindModel->where('admin_id_slave', 'eq', $this->auth->id)->select();
  60. if ($bindModel) {
  61. $vipAdminIds = array_column($bindModel, 'admin_id_master');
  62. $vipAdminIds[] = $this->auth->id;
  63. $maps['admin_id'] = ['in', $vipAdminIds];
  64. }
  65. }
  66. $total = $this->model
  67. ->where($where)
  68. ->where($maps)
  69. ->order($sort, $order)
  70. ->count();
  71. $list = $this->model
  72. ->where($where)
  73. ->where($maps)
  74. ->order($sort, $order)
  75. ->limit($offset, $limit)
  76. ->select();
  77. if ($limitShow) {
  78. if (!empty($list)) {
  79. foreach ($list as &$row) {
  80. $row['self'] = $row['admin_id'] == $this->auth->id ? 1: 0;
  81. $url = '/index/index/booklistch?id='.$row['id'];
  82. $row['url'] = getCurrentDomain($this->getCurrentAccountChannelId(), $url);
  83. }
  84. }
  85. }
  86. $result = array("total" => $total, "rows" => $list);
  87. return json($result);
  88. }
  89. $this->assignconfig('group', $this->group);
  90. $this->assignconfig('limitShow', $limitShow);
  91. return $this->view->fetch();
  92. }
  93. /**
  94. * 添加
  95. */
  96. public function add()
  97. {
  98. if ($this->request->isPost())
  99. {
  100. $params = $this->request->post("row/a");
  101. if ($params)
  102. {
  103. $params['admin_id'] = $this->auth->id;
  104. /*
  105. * 已经弃用,如果为了兼容老版可取消注释
  106. foreach ($params as $k => &$v)
  107. {
  108. $v = is_array($v) ? implode(',', $v) : $v;
  109. }
  110. */
  111. if ($this->dataLimit)
  112. {
  113. $params[$this->dataLimitField] = $this->auth->id;
  114. }
  115. try
  116. {
  117. //是否采用模型验证
  118. if ($this->modelValidate)
  119. {
  120. $name = basename(str_replace('\\', '/', get_class($this->model)));
  121. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
  122. $this->model->validate($validate);
  123. }
  124. $result = $this->model->allowField(true)->save($params);
  125. if ($result !== false)
  126. {
  127. $this->success();
  128. }
  129. else
  130. {
  131. $this->error($this->model->getError());
  132. }
  133. }
  134. catch (\think\exception\PDOException $e)
  135. {
  136. $this->error($e->getMessage());
  137. }
  138. }
  139. $this->error(__('Parameter %s can not be empty', ''));
  140. }
  141. return $this->view->fetch();
  142. }
  143. /**
  144. * 编辑
  145. */
  146. public function edit($ids = NULL)
  147. {
  148. $row = $this->model
  149. ->where('id', 'eq', $ids)
  150. ->where('admin_id', 'eq', $this->auth->id)
  151. ->find();
  152. if (!$row)
  153. $this->error(__('No Results were found'));
  154. $adminIds = $this->getDataLimitAdminIds();
  155. if (is_array($adminIds))
  156. {
  157. if (!in_array($row[$this->dataLimitField], $adminIds))
  158. {
  159. $this->error(__('You have no permission'));
  160. }
  161. }
  162. if ($this->request->isPost())
  163. {
  164. $params = $this->request->post("row/a");
  165. if ($params)
  166. {
  167. /*
  168. * 已经弃用,如果为了兼容老版可取消注释
  169. foreach ($params as $k => &$v)
  170. {
  171. $v = is_array($v) ? implode(',', $v) : $v;
  172. }
  173. */
  174. try
  175. {
  176. //是否采用模型验证
  177. if ($this->modelValidate)
  178. {
  179. $name = basename(str_replace('\\', '/', get_class($this->model)));
  180. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate;
  181. $row->validate($validate);
  182. }
  183. $result = $row->allowField(true)->save($params);
  184. if ($result !== false)
  185. {
  186. $this->model->rmCache($row['id']);
  187. $this->success();
  188. }
  189. else
  190. {
  191. $this->error($row->getError());
  192. }
  193. }
  194. catch (\think\exception\PDOException $e)
  195. {
  196. $this->error($e->getMessage());
  197. }
  198. }
  199. $this->error(__('Parameter %s can not be empty', ''));
  200. }
  201. $this->view->assign("row", $row);
  202. $this->assignconfig('row', $row);
  203. return $this->view->fetch();
  204. }
  205. /**
  206. * 删除
  207. */
  208. public function del($ids = "")
  209. {
  210. if ($ids)
  211. {
  212. $pk = $this->model->getPk();
  213. $adminIds = $this->getDataLimitAdminIds();
  214. if (is_array($adminIds))
  215. {
  216. $count = $this->model->where($this->dataLimitField, 'in', $adminIds);
  217. }
  218. $list = $this->model->where($pk, 'in', $ids)->select();
  219. $count = 0;
  220. foreach ($list as $k => $v)
  221. {
  222. $this->model->rmCache($v['id']);
  223. $count += $v->delete();
  224. }
  225. if ($count)
  226. {
  227. $this->success();
  228. }
  229. else
  230. {
  231. $this->error(__('No rows were deleted'));
  232. }
  233. }
  234. $this->error(__('Parameter %s can not be empty', 'ids'));
  235. }
  236. /**
  237. * 清除缓存
  238. */
  239. public function clearCache($ids = ''){
  240. if($ids){
  241. $this->model->rmCache($ids);
  242. $this->success('清除成功');
  243. }
  244. $this->error(__('Parameter %s can not be empty', 'ids'));
  245. }
  246. }