Privatebookpage.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. /**
  3. * Created by: PhpStorm
  4. * User: lytian
  5. * Date: 2020/3/25
  6. * Time: 18:11
  7. */
  8. namespace app\admin\controller;
  9. use app\common\controller\Backend;
  10. use app\common\library\Redis;
  11. use app\main\constants\CacheConstants;
  12. class Privatebookpage extends Backend
  13. {
  14. protected $model = null;
  15. /**
  16. * 无需鉴权的方法,但需要登录
  17. * @var array
  18. */
  19. protected $noNeedRight = ['cleancache'];
  20. public function _initialize()
  21. {
  22. parent::_initialize(); // TODO: Change the autogenerated stub
  23. $this->model = model("PrivateBookPage");
  24. $this->view->assign("statusList", $this->model->getStatusList());
  25. }
  26. /**
  27. * 查看
  28. */
  29. public function index()
  30. {
  31. //设置过滤方法
  32. $this->request->filter(['strip_tags']);
  33. if ($this->request->isAjax()) {
  34. //如果发送的来源是Selectpage,则转发到Selectpage
  35. if ($this->request->request('pkey_name')) {
  36. return $this->selectpage();
  37. }
  38. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  39. //if (in_array($this->group, [3, 4, 7, 8])) {
  40. $maps = [
  41. 'admin_id' => $this->auth->id,
  42. ];
  43. //}
  44. $total = $this->model
  45. ->where($where)
  46. ->where($maps)
  47. ->order($sort, $order)
  48. ->count();
  49. $list = $this->model
  50. ->where($where)
  51. ->where($maps)
  52. ->order($sort, $order)
  53. ->limit($offset, $limit)
  54. ->select();
  55. $result = array("total" => $total, "rows" => $list);
  56. return json($result);
  57. }
  58. return $this->view->fetch();
  59. }
  60. /**
  61. * 添加
  62. */
  63. public function add()
  64. {
  65. if ($this->request->isPost()) {
  66. $params = $this->request->post("row/a");
  67. if ($params) {
  68. /*
  69. * 已经弃用,如果为了兼容老版可取消注释
  70. foreach ($params as $k => &$v)
  71. {
  72. $v = is_array($v) ? implode(',', $v) : $v;
  73. }
  74. */
  75. if ($this->dataLimit) {
  76. $params[$this->dataLimitField] = $this->auth->id;
  77. }
  78. try {
  79. //是否采用模型验证
  80. if ($this->modelValidate) {
  81. $name = basename(str_replace('\\', '/', get_class($this->model)));
  82. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
  83. $this->model->validate($validate);
  84. }
  85. $params['admin_id'] = $this->auth->id;
  86. if ($params['end_time']) {
  87. $params['end_time'] = strtotime($params['end_time']);
  88. }
  89. $result = $this->model->allowField(true)->save($params);
  90. if ($result !== false) {
  91. //刷入缓存
  92. $this->model->getResourceList($this->model->id, true);
  93. $this->success();
  94. } else {
  95. $this->error($this->model->getError());
  96. }
  97. } catch (\think\exception\PDOException $e) {
  98. $this->error($e->getMessage());
  99. }
  100. }
  101. $this->error(__('Parameter %s can not be empty', ''));
  102. }
  103. return $this->view->fetch();
  104. }
  105. /**
  106. * 编辑
  107. */
  108. public function edit($ids = null)
  109. {
  110. $row = $this->model->get($ids);
  111. if (!$row) {
  112. $this->error(__('No Results were found'));
  113. }
  114. $adminIds = $this->getDataLimitAdminIds();
  115. if (is_array($adminIds)) {
  116. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  117. $this->error(__('You have no permission'));
  118. }
  119. }
  120. if ($this->request->isPost()) {
  121. $params = $this->request->post("row/a");
  122. if ($params) {
  123. /*
  124. * 已经弃用,如果为了兼容老版可取消注释
  125. foreach ($params as $k => &$v)
  126. {
  127. $v = is_array($v) ? implode(',', $v) : $v;
  128. }
  129. */
  130. try {
  131. //是否采用模型验证
  132. if ($this->modelValidate) {
  133. $name = basename(str_replace('\\', '/', get_class($this->model)));
  134. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate;
  135. $row->validate($validate);
  136. }
  137. if ($params['end_time']) {
  138. $params['end_time'] = strtotime($params['end_time']);
  139. }
  140. $result = $row->allowField(true)->save($params);
  141. if ($result !== false) {
  142. $this->model->getResourceList($row->id, true);
  143. $this->success();
  144. } else {
  145. $this->error($row->getError());
  146. }
  147. } catch (\think\exception\PDOException $e) {
  148. $this->error($e->getMessage());
  149. }
  150. }
  151. $this->error(__('Parameter %s can not be empty', ''));
  152. }
  153. $this->view->assign("row", $row);
  154. return $this->view->fetch();
  155. }
  156. /**
  157. * 删除
  158. */
  159. public function del($ids = "")
  160. {
  161. if ($ids) {
  162. $pk = $this->model->getPk();
  163. $adminIds = $this->getDataLimitAdminIds();
  164. if (is_array($adminIds)) {
  165. $count = $this->model->where($this->dataLimitField, 'in', $adminIds);
  166. }
  167. $list = $this->model->where($pk, 'in', $ids)->select();
  168. $count = 0;
  169. foreach ($list as $k => $v) {
  170. $count += $v->delete();
  171. $this->model->getResourceList($v->id, true);
  172. }
  173. if ($count) {
  174. $this->success();
  175. } else {
  176. $this->error(__('No rows were deleted'));
  177. }
  178. }
  179. $this->error(__('Parameter %s can not be empty', 'ids'));
  180. }
  181. public function cleancache()
  182. {
  183. $id = $this->request->param('id',0);
  184. if (!$id){
  185. $this->error('活动不存在');
  186. }
  187. $redisKey = CacheConstants::getSinglePushResourceCache($id);
  188. $re = Redis::instance()->del($redisKey);
  189. $this->success($re);
  190. }
  191. }