Book.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. namespace app\admin\controller\booklist;
  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. public function _initialize()
  18. {
  19. parent::_initialize();
  20. $this->model = model('BookList');
  21. $this->view->assign("statusList", $this->model->getStatusList());
  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. $limitShow = $this->group == 3 ? true : false;
  36. //是否控制显示链接
  37. if ($this->group == 4 && !empty($this->auth->agent_id)) {
  38. //判断是否是配号代理
  39. $limitShow = true;
  40. }
  41. if ($this->request->isAjax())
  42. {
  43. //如果发送的来源是Selectpage,则转发到Selectpage
  44. if ($this->request->request('pkey_name'))
  45. {
  46. return $this->selectpage();
  47. }
  48. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  49. $total = $this->model
  50. ->where($where)
  51. ->order($sort, $order)
  52. ->count();
  53. $list = $this->model
  54. ->where($where)
  55. ->order($sort, $order)
  56. ->limit($offset, $limit)
  57. ->select();
  58. if ($limitShow) {
  59. if (!empty($list)) {
  60. foreach ($list as &$row) {
  61. $url = '/index/index/booklist?id='.$row['id'];
  62. $row['url'] = getCurrentDomain($this->getCurrentAccountChannelId(), $url);
  63. }
  64. }
  65. }
  66. $result = array("total" => $total, "rows" => $list);
  67. return json($result);
  68. }
  69. $this->assignconfig('group', $this->group);
  70. $this->assignconfig('limitShow', $limitShow);
  71. return $this->view->fetch();
  72. }
  73. /**
  74. * 编辑
  75. */
  76. public function edit($ids = NULL)
  77. {
  78. $row = $this->model->get($ids);
  79. if (!$row)
  80. $this->error(__('No Results were found'));
  81. $adminIds = $this->getDataLimitAdminIds();
  82. if (is_array($adminIds))
  83. {
  84. if (!in_array($row[$this->dataLimitField], $adminIds))
  85. {
  86. $this->error(__('You have no permission'));
  87. }
  88. }
  89. if ($this->request->isPost())
  90. {
  91. $params = $this->request->post("row/a");
  92. if ($params)
  93. {
  94. /*
  95. * 已经弃用,如果为了兼容老版可取消注释
  96. foreach ($params as $k => &$v)
  97. {
  98. $v = is_array($v) ? implode(',', $v) : $v;
  99. }
  100. */
  101. try
  102. {
  103. //是否采用模型验证
  104. if ($this->modelValidate)
  105. {
  106. $name = basename(str_replace('\\', '/', get_class($this->model)));
  107. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate;
  108. $row->validate($validate);
  109. }
  110. $result = $row->allowField(true)->save($params);
  111. if ($result !== false)
  112. {
  113. $this->model->rmCache($row['id']);
  114. $this->success();
  115. }
  116. else
  117. {
  118. $this->error($row->getError());
  119. }
  120. }
  121. catch (\think\exception\PDOException $e)
  122. {
  123. $this->error($e->getMessage());
  124. }
  125. }
  126. $this->error(__('Parameter %s can not be empty', ''));
  127. }
  128. $this->view->assign("row", $row);
  129. $this->assignconfig('row', $row);
  130. return $this->view->fetch();
  131. }
  132. /**
  133. * 删除
  134. */
  135. public function del($ids = "")
  136. {
  137. if ($ids)
  138. {
  139. $pk = $this->model->getPk();
  140. $adminIds = $this->getDataLimitAdminIds();
  141. if (is_array($adminIds))
  142. {
  143. $count = $this->model->where($this->dataLimitField, 'in', $adminIds);
  144. }
  145. $list = $this->model->where($pk, 'in', $ids)->select();
  146. $count = 0;
  147. foreach ($list as $k => $v)
  148. {
  149. $this->model->rmCache($v['id']);
  150. $count += $v->delete();
  151. }
  152. if ($count)
  153. {
  154. $this->success();
  155. }
  156. else
  157. {
  158. $this->error(__('No rows were deleted'));
  159. }
  160. }
  161. $this->error(__('Parameter %s can not be empty', 'ids'));
  162. }
  163. /**
  164. * 清除缓存
  165. */
  166. public function clearCache($ids = ''){
  167. if($ids){
  168. $this->model->rmCache($ids);
  169. $this->success('清除成功');
  170. }
  171. $this->error(__('Parameter %s can not be empty', 'ids'));
  172. }
  173. }