Notice.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use think\Controller;
  5. use think\Request;
  6. use think\Session;
  7. use app\main\constants\AdminConstants;
  8. /**
  9. * 公告管理
  10. *
  11. * @icon fa fa-circle-o
  12. */
  13. class Notice extends Backend
  14. {
  15. /**
  16. * Notice模型对象
  17. */
  18. protected $model = null;
  19. public function _initialize()
  20. {
  21. parent::_initialize();
  22. $this->model = model('Notice');
  23. $this->view->assign("showLevelList", $this->model->getShowLevelList());
  24. $this->view->assign("dialogTypeList", $this->model->getDialogTypeList());
  25. $this->view->assign("noticeTypeList", $this->model->getNoticeTypeList());
  26. $this->view->assign("statusList", $this->model->getStatusList());
  27. $this->view->assign("importantTypeList", $this->model->getImportantTypeList());
  28. $groupList = model("AuthGroup")->where("status = 'normal'")->column("id,name");
  29. $this->view->assign("groupsList",$groupList);
  30. $this->assignconfig("groupcount",count($groupList));
  31. }
  32. /**
  33. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法
  34. * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑
  35. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  36. */
  37. /**
  38. * 查看
  39. */
  40. public function index()
  41. {
  42. //设置过滤方法
  43. $this->request->filter(['strip_tags']);
  44. if ($this->request->isAjax())
  45. {
  46. //如果发送的来源是Selectpage,则转发到Selectpage
  47. if ($this->request->request('pkey_name'))
  48. {
  49. return $this->selectpage();
  50. }
  51. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  52. $notice_type = $this->request->get("notice_type", '1');
  53. $whereArr[] = ['status', '=', 'normal'];
  54. $csallarr =
  55. [
  56. AdminConstants::ADMIN_GROUP_ID_SUPER_ADMIN,
  57. AdminConstants::ADMIN_GROUP_ID_ADMIN,
  58. AdminConstants::ADMIN_GROUP_ID_OPERATOR,
  59. AdminConstants::ADMIN_GROUP_ID_CONFIG_MANAGER
  60. ];
  61. if(in_array($this->group,$csallarr)){
  62. $whereArr = [];
  63. }
  64. $whereArr[] = ['notice_type', '=', $notice_type];
  65. $whereArr = function($query) use ($whereArr) {
  66. foreach ($whereArr as $k => $v)
  67. {
  68. if (is_array($v))
  69. {
  70. call_user_func_array([$query, 'where'], $v);
  71. }
  72. else
  73. {
  74. $query->where($v);
  75. }
  76. }
  77. };
  78. $list = $this->model
  79. ->where($where)
  80. ->where($whereArr)
  81. ->order($sort, $order)
  82. ->select();
  83. foreach ($list as$k => &$v){
  84. $v['groupsname'] = implode(",",model("AuthGroup")->where("id","in",$v['groups'])->column("name"));
  85. if(!in_array($this->group,$csallarr) && strpos($v['groups'],"$this->group")===false)
  86. unset($list[$k]);
  87. }
  88. $list = array_values($list);
  89. $result = array("total" => count($list), "rows" => array_slice($list,$offset,$limit));
  90. return json($result);
  91. }
  92. return $this->view->fetch();
  93. }
  94. /**
  95. * 添加
  96. */
  97. public function add()
  98. {
  99. if ($this->request->isPost())
  100. {
  101. $params = $this->request->post("row/a");
  102. if ($params)
  103. {
  104. $params['groups'] = $params['groups']?implode(",",$params['groups']):'';
  105. if(empty($params['groups'])){
  106. $this->error("显示范围必选");
  107. }
  108. if (isset($params['pop_num']) && ($params['pop_num']>10 || $params['pop_num']<0) ){
  109. $this->error("弹出次数不在规定范围内");
  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. $this->view->assign("notice_type",$this->request->get("notice_type"));
  142. return $this->view->fetch();
  143. }
  144. /**
  145. * 编辑
  146. */
  147. public function edit($ids = NULL)
  148. {
  149. $row = $this->model->get($ids);
  150. if (!$row)
  151. $this->error(__('No Results were found'));
  152. $adminIds = $this->getDataLimitAdminIds();
  153. if (is_array($adminIds))
  154. {
  155. if (!in_array($row[$this->dataLimitField], $adminIds))
  156. {
  157. $this->error(__('You have no permission'));
  158. }
  159. }
  160. if ($this->request->isPost())
  161. {
  162. $params = $this->request->post("row/a");
  163. if ($params)
  164. {
  165. $params['groups'] = $params['groups']?implode(",",$params['groups']):'';
  166. if(empty($params['groups'])){
  167. $this->error("显示范围必选");
  168. }
  169. if (isset($params['pop_num']) && ($params['pop_num']>10 || $params['pop_num']<0) ){
  170. $this->error("弹出次数不在规定范围内");
  171. }
  172. /*
  173. * 已经弃用,如果为了兼容老版可取消注释
  174. foreach ($params as $k => &$v)
  175. {
  176. $v = is_array($v) ? implode(',', $v) : $v;
  177. }
  178. */
  179. try
  180. {
  181. //是否采用模型验证
  182. if ($this->modelValidate)
  183. {
  184. $name = basename(str_replace('\\', '/', get_class($this->model)));
  185. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate;
  186. $row->validate($validate);
  187. }
  188. $result = $row->allowField(true)->save($params);
  189. if ($result !== false)
  190. {
  191. $this->success();
  192. }
  193. else
  194. {
  195. $this->error($row->getError());
  196. }
  197. }
  198. catch (\think\exception\PDOException $e)
  199. {
  200. $this->error($e->getMessage());
  201. }
  202. }
  203. $this->error(__('Parameter %s can not be empty', ''));
  204. }
  205. $checkall = 0;
  206. if(implode(",",model("AuthGroup")->where("status","normal")->column("id"))==$row['groups']){
  207. $checkall = 1;
  208. }
  209. $this->view->assign("checkall", $checkall);
  210. $this->view->assign("row", $row);
  211. return $this->view->fetch();
  212. }
  213. /**
  214. * 展示
  215. */
  216. public function show($ids = NULL)
  217. {
  218. $row = $this->model->get($ids);
  219. if (!$row)
  220. $this->error(__('No Results were found'));
  221. $adminIds = $this->getDataLimitAdminIds();
  222. if (is_array($adminIds))
  223. {
  224. if (!in_array($row[$this->dataLimitField], $adminIds))
  225. {
  226. $this->error(__('You have no permission'));
  227. }
  228. }
  229. $isFirst = 0;
  230. // 清除登陆后公告弹窗
  231. if (Session::has('notice_id')) {
  232. $isFirst = 1;
  233. Session::delete('notice_id');
  234. }
  235. // 记录访问log
  236. switch ($row->dialog_type) {
  237. case 1: //只弹一次
  238. $data = [
  239. 'notice_id' => $row->id,
  240. 'admin_id' => $this->auth->id
  241. ];
  242. $noticeLog = model('Notice_log');
  243. $res = $noticeLog->where($data)->find();
  244. if (!$res) {
  245. $noticeLog->save($data);
  246. }
  247. break;
  248. case 2: //每次登陆
  249. break;
  250. case 3: //每天一次
  251. $data = [
  252. 'notice_id' => $row->id,
  253. 'admin_id' => $this->auth->id
  254. ];
  255. $noticeLog = model('Notice_log');
  256. $res = $noticeLog->where($data)->where('createtime', '>', strtotime(date("Y-m-d")))->find();
  257. if (!$res) {
  258. $noticeLog->save($data);
  259. }
  260. break;
  261. case 5:
  262. $data = [
  263. 'notice_id' => $row->id,
  264. 'admin_id' => $this->auth->id
  265. ];
  266. if ($isFirst == 1){
  267. model('Notice_log')->save($data);
  268. }
  269. break;
  270. default:
  271. }
  272. $this->view->assign("row", $row);
  273. return $this->view->fetch();
  274. }
  275. }