Lightrule.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <?php
  2. namespace app\admin\controller\manage;
  3. use app\common\library\Redis;
  4. use app\common\controller\Backend;
  5. use app\main\constants\CacheConstants;
  6. /**
  7. * 清水配置管理
  8. *
  9. * @icon fa fa-circle-o
  10. */
  11. class Lightrule extends Backend
  12. {
  13. /**
  14. * Configlightrule模型对象
  15. */
  16. protected $model = null;
  17. public function _initialize()
  18. {
  19. parent::_initialize();
  20. $this->model = model('ConfigLightRule');
  21. }
  22. /**
  23. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法
  24. * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑
  25. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  26. */
  27. /**
  28. * 添加
  29. */
  30. public function add()
  31. {
  32. if ($this->request->isPost())
  33. {
  34. $params = $this->request->post("row/a");
  35. if ($params)
  36. {
  37. /*
  38. * 已经弃用,如果为了兼容老版可取消注释
  39. foreach ($params as $k => &$v)
  40. {
  41. $v = is_array($v) ? implode(',', $v) : $v;
  42. }
  43. */
  44. if ($this->dataLimit)
  45. {
  46. $params[$this->dataLimitField] = $this->auth->id;
  47. }
  48. try
  49. {
  50. //是否采用模型验证
  51. if ($this->modelValidate)
  52. {
  53. $name = basename(str_replace('\\', '/', get_class($this->model)));
  54. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
  55. $this->model->validate($validate);
  56. }
  57. // 增加校验,全部城市
  58. $city_ids_arr = array_filter(explode(',', $params['city_ids']));
  59. if(count($city_ids_arr) > 1 && in_array('*', $city_ids_arr)){
  60. $this->error('选择全部城市后,不能再选择单个城市');
  61. }
  62. $result = $this->model->allowField(true)->save($params);
  63. if ($result !== false)
  64. {
  65. $this->success();
  66. }
  67. else
  68. {
  69. $this->error($this->model->getError());
  70. }
  71. }
  72. catch (\think\exception\PDOException $e)
  73. {
  74. $this->error($e->getMessage());
  75. }
  76. }
  77. $this->error(__('Parameter %s can not be empty', ''));
  78. }
  79. return $this->view->fetch();
  80. }
  81. /**
  82. * 编辑
  83. */
  84. public function edit($ids = NULL)
  85. {
  86. $row = $this->model->get($ids);
  87. if (!$row)
  88. $this->error(__('No Results were found'));
  89. $adminIds = $this->getDataLimitAdminIds();
  90. if (is_array($adminIds))
  91. {
  92. if (!in_array($row[$this->dataLimitField], $adminIds))
  93. {
  94. $this->error(__('You have no permission'));
  95. }
  96. }
  97. if ($this->request->isPost())
  98. {
  99. $params = $this->request->post("row/a");
  100. if ($params)
  101. {
  102. /*
  103. * 已经弃用,如果为了兼容老版可取消注释
  104. foreach ($params as $k => &$v)
  105. {
  106. $v = is_array($v) ? implode(',', $v) : $v;
  107. }
  108. */
  109. try
  110. {
  111. //是否采用模型验证
  112. if ($this->modelValidate)
  113. {
  114. $name = basename(str_replace('\\', '/', get_class($this->model)));
  115. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate;
  116. $row->validate($validate);
  117. }
  118. // 增加校验,全部城市
  119. $city_ids_arr = array_filter(explode(',', $params['city_ids']));
  120. if(count($city_ids_arr) > 1 && in_array('*', $city_ids_arr)){
  121. $this->error('选择全部城市后,不能再选择单个城市');
  122. }
  123. $result = $row->allowField(true)->save($params);
  124. if ($result !== false)
  125. {
  126. $this->success();
  127. }
  128. else
  129. {
  130. $this->error($row->getError());
  131. }
  132. }
  133. catch (\think\exception\PDOException $e)
  134. {
  135. $this->error($e->getMessage());
  136. }
  137. }
  138. $this->error(__('Parameter %s can not be empty', ''));
  139. }
  140. $this->view->assign("row", $row);
  141. return $this->view->fetch();
  142. }
  143. /**
  144. * 关联渠道
  145. */
  146. public function operate()
  147. {
  148. if ($this->request->isAjax()) {
  149. $id = $this->request->param('id');
  150. $channelIds = $this->request->param('channel_ids');
  151. if (!preg_match('/^(\*)?[-,0-9]*$/', $channelIds)) {
  152. $this->error('渠道id输入有误,渠道id使用逗号分隔,第一个字符可以为*');
  153. }
  154. $operate = $this->request->param('method');
  155. $row = $this->model->get($id);
  156. if ($row) {
  157. if ($operate == 'add') {
  158. if ($row['channel_ids'] == '*') {
  159. $this->error('已添加所有渠道配置,不能重复添加');
  160. }
  161. if ($channelIds == '*') {
  162. $rule = $this->model->where('channel_ids', $channelIds)->find();
  163. if ($rule) {
  164. $this->error('已添加所有渠道配置,不能重复添加');
  165. }
  166. } else {
  167. // 一个渠道只允许配置一条
  168. $channelIdArr = array_filter(explode(',', $channelIds));
  169. if (empty($channelIdArr)) {
  170. $this->error('渠道id输入有误');
  171. }
  172. foreach ($channelIdArr as $item) {
  173. $rule = $this->model->where("FIND_IN_SET({$item}, channel_ids)")->find();
  174. if ($rule) {
  175. $this->error('有渠道添加过配置,不能重复添加');
  176. }
  177. }
  178. $channelIds = trim(implode(',', $channelIdArr) . ',' . trim($row['channel_ids'], ','), ',');
  179. }
  180. } else if ($operate == 'remove') {
  181. if ($channelIds == '*') {
  182. $channelIds = '';
  183. } else {
  184. $channelIds = array_filter(explode(',', $channelIds));
  185. $oldChannelIds = array_filter(explode(',', $row['channel_ids']));
  186. $channelIds = implode(',', array_diff($oldChannelIds, $channelIds));
  187. }
  188. }
  189. $this->model->where('id', $id)->update(['channel_ids' => $channelIds, 'updatetime' => time()]);
  190. } else {
  191. $this->error('记录不存在');
  192. }
  193. }
  194. $this->success();
  195. }
  196. /**
  197. * 刷新缓存
  198. */
  199. public function refreshCache()
  200. {
  201. $data = $this->model->select();
  202. $list = [];
  203. foreach ($data as $item) {
  204. if (empty($item['channel_ids']) || empty($item['city_ids'])) {
  205. continue;
  206. }
  207. if (strpos($item['channel_ids'], ',') !== false) {
  208. $channelIdArr = explode(',', $item['channel_ids']);
  209. foreach ($channelIdArr as $channelId) {
  210. if (empty($channelId)) {
  211. continue;
  212. }
  213. $list[$channelId] = json_encode([
  214. 'c' => $item['city_ids'],
  215. 't' => $item['push_type'],
  216. ], JSON_UNESCAPED_UNICODE);
  217. }
  218. } else {
  219. $list[$item['channel_ids']] = json_encode([
  220. 'c' => $item['city_ids'],
  221. 't' => $item['push_type'],
  222. ], JSON_UNESCAPED_UNICODE);
  223. }
  224. }
  225. // 删除旧缓存
  226. Redis::instance()->del(CacheConstants::LIGHT_RULE);
  227. if (!empty($list)) {
  228. if (!Redis::instance()->hMSet(CacheConstants::LIGHT_RULE, $list)) {
  229. $this->error('刷新缓存失败');
  230. }
  231. }
  232. $this->success();
  233. }
  234. }