Page.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. namespace app\admin\controller\special;
  3. use app\common\constants\SpecialPageConstants;
  4. use app\common\controller\Backend;
  5. use app\common\library\Redis;
  6. use think\Config;
  7. use app\main\constants\AdminConstants;
  8. use think\Controller;
  9. use think\Request;
  10. /**
  11. * 专题页
  12. *
  13. * @icon fa fa-circle-o
  14. */
  15. class Page extends Backend
  16. {
  17. /**
  18. * SpecialPage模型对象
  19. */
  20. protected $model = null;
  21. public function _initialize()
  22. {
  23. parent::_initialize();
  24. $this->model = model('SpecialPage');
  25. $this->view->assign("typeList", $this->model->getTypeList());
  26. $this->view->assign("statusList", $this->model->getStatusList());
  27. $this->assignconfig("group", $this->group);
  28. }
  29. /**
  30. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法
  31. * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑
  32. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  33. */
  34. /**
  35. * 查看
  36. */
  37. public function index()
  38. {
  39. //设置过滤方法
  40. $this->request->filter(['strip_tags']);
  41. if ($this->request->isAjax())
  42. {
  43. //如果发送的来源是Selectpage,则转发到Selectpage
  44. if ($this->request->request('pkey_name'))
  45. {
  46. return $this->selectpage();
  47. }
  48. $needUrl = false;
  49. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  50. $total = $this->model
  51. ->where($where)
  52. ->order($sort, $order)
  53. ->count();
  54. $list = $this->model
  55. ->where($where)
  56. ->order($sort, $order)
  57. ->limit($offset, $limit)
  58. ->select();
  59. if ($list) {
  60. foreach ($list as &$row) {
  61. $url = '/index/index/specialpage?id='.$row['id'];
  62. if (in_array($this->group, [AdminConstants::ADMIN_GROUP_ID_CHANNEL, AdminConstants::ADMIN_GROUP_ID_AGENT])) {
  63. $url = getCurrentDomain($this->getCurrentAccountChannelId(), $url);
  64. } else {
  65. $channel_id = Config::get('site.test_channel_id');
  66. if ($channel_id) {
  67. $url = getCurrentDomain($channel_id, $url);
  68. }
  69. }
  70. //预览的地址
  71. $row['preview'] = $url.'&view=preview';
  72. $row['url'] = $url;
  73. }
  74. }
  75. $result = array("total" => $total, "rows" => $list);
  76. return json($result);
  77. }
  78. return $this->view->fetch();
  79. }
  80. /**
  81. * 添加
  82. */
  83. public function add()
  84. {
  85. if ($this->request->isPost())
  86. {
  87. $params = $this->request->post("row/a");
  88. if ($params)
  89. {
  90. /*
  91. * 已经弃用,如果为了兼容老版可取消注释
  92. foreach ($params as $k => &$v)
  93. {
  94. $v = is_array($v) ? implode(',', $v) : $v;
  95. }
  96. */
  97. if ($this->dataLimit)
  98. {
  99. $params[$this->dataLimitField] = $this->auth->id;
  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 . '.add' : true) : $this->modelValidate;
  108. $this->model->validate($validate);
  109. }
  110. $params['admin_id'] = $this->auth->id;
  111. $result = $this->model->allowField(true)->save($params);
  112. if ($result !== false)
  113. {
  114. $this->success();
  115. }
  116. else
  117. {
  118. $this->error($this->model->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. return $this->view->fetch();
  129. }
  130. /**
  131. * 编辑
  132. */
  133. public function edit($ids = NULL)
  134. {
  135. $row = $this->model->get($ids);
  136. if (!$row)
  137. $this->error(__('No Results were found'));
  138. $adminIds = $this->getDataLimitAdminIds();
  139. if (is_array($adminIds))
  140. {
  141. if (!in_array($row[$this->dataLimitField], $adminIds))
  142. {
  143. $this->error(__('You have no permission'));
  144. }
  145. }
  146. if ($this->request->isPost())
  147. {
  148. $params = $this->request->post("row/a");
  149. if ($params)
  150. {
  151. /*
  152. * 已经弃用,如果为了兼容老版可取消注释
  153. foreach ($params as $k => &$v)
  154. {
  155. $v = is_array($v) ? implode(',', $v) : $v;
  156. }
  157. */
  158. try
  159. {
  160. if ($params['content'] == '<p><br></p>') {
  161. $params['content'] = '';
  162. }
  163. //是否采用模型验证
  164. if ($this->modelValidate)
  165. {
  166. $name = basename(str_replace('\\', '/', get_class($this->model)));
  167. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate;
  168. $row->validate($validate);
  169. }
  170. $result = $row->allowField(true)->save($params);
  171. if ($result !== false)
  172. {
  173. $redisKey = SpecialPageConstants::REDIS_KEY.$ids;
  174. Redis::instance()->del($redisKey);
  175. $this->success();
  176. }
  177. else
  178. {
  179. $this->error($row->getError());
  180. }
  181. }
  182. catch (\think\exception\PDOException $e)
  183. {
  184. $this->error($e->getMessage());
  185. }
  186. }
  187. $this->error(__('Parameter %s can not be empty', ''));
  188. }
  189. $this->view->assign("row", $row);
  190. return $this->view->fetch();
  191. }
  192. /**
  193. * 清理缓存
  194. */
  195. public function rmcache($ids = null)
  196. {
  197. if (!$ids)
  198. $this->error(__('No Results were found'));
  199. $redisKey = SpecialPageConstants::REDIS_KEY.$ids;
  200. Redis::instance()->del($redisKey);
  201. $this->success('清空缓存成功!');
  202. }
  203. }