Rulemanage.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Elton
  5. * Date: 2019/9/12
  6. * Time: 14:25
  7. */
  8. namespace app\admin\controller\kl;
  9. use app\common\controller\Backend;
  10. use app\common\library\Redis;
  11. use app\main\constants\CacheConstants;
  12. use app\main\service\AdminKlUpdateService;
  13. use function think\__include_file;
  14. class Rulemanage extends Backend
  15. {
  16. protected $noNeedRight = ['citylist'];
  17. public function _initialize()
  18. {
  19. $this->model = model('KlRuleManage');
  20. return parent::_initialize(); // TODO: Change the autogenerated stub
  21. }
  22. /**
  23. * 城市列表
  24. */
  25. public function citylist()
  26. {
  27. //设置过滤方法
  28. $this->request->filter(['strip_tags']);
  29. if ($this->request->isAjax()) {
  30. if ($this->request->get('all')) {
  31. $dbFile = ROOT_PATH . 'public/assets/data/citylist_plus.php';
  32. } else {
  33. $dbFile = ROOT_PATH . 'public/assets/data/citylist.php';
  34. }
  35. $arr = __include_file($dbFile);
  36. //设置过滤方法
  37. $list = $data = [];
  38. $total = 0;
  39. if ($selector = $this->request->param('pkey_value')) {
  40. $ids = explode(",", $selector);
  41. foreach ($arr as $key=>$val) {
  42. if (in_array($key, $ids)) {
  43. $data[$key] = $val;
  44. }
  45. }
  46. $total = count($data);
  47. } else {
  48. $field = $this->request->param('name');
  49. if ($field) {
  50. foreach ($arr as $key=>$val) {
  51. if (strrpos($val, $field) !== false) {
  52. $data[$key] = $val;
  53. }
  54. }
  55. $total = count($data);
  56. } else {
  57. $data = $arr;
  58. $total = count($data);
  59. }
  60. }
  61. if ($total > 0) {
  62. foreach ($data as $index => $item) {
  63. $list[] = [
  64. 'id' => $index,
  65. 'name' => $item
  66. ];
  67. }
  68. }
  69. //这里一定要返回有list这个字段,total是可选的,如果total<=list的数量,则会隐藏分页按钮
  70. return json(['list' => $list, 'total' => $total]);
  71. }
  72. }
  73. /**
  74. * 添加
  75. */
  76. public function add()
  77. {
  78. if ($this->request->isPost())
  79. {
  80. $params = $this->request->post("row/a");
  81. if ($params)
  82. {
  83. $authInfo = $this->auth->getUserInfo($this->auth->id);
  84. $params['opuser_id'] = $this->auth->id;
  85. $params['opuser_name'] = $authInfo['username'];
  86. /*
  87. * 已经弃用,如果为了兼容老版可取消注释
  88. foreach ($params as $k => &$v)
  89. {
  90. $v = is_array($v) ? implode(',', $v) : $v;
  91. }
  92. */
  93. if ($this->dataLimit)
  94. {
  95. $params[$this->dataLimitField] = $this->auth->id;
  96. }
  97. try
  98. {
  99. //是否采用模型验证
  100. if ($this->modelValidate)
  101. {
  102. $name = basename(str_replace('\\', '/', get_class($this->model)));
  103. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
  104. $this->model->validate($validate);
  105. }
  106. $result = $this->model->allowField(true)->save($params);
  107. if ($result !== false)
  108. {
  109. $this->success();
  110. }
  111. else
  112. {
  113. $this->error($this->model->getError());
  114. }
  115. }
  116. catch (\think\exception\PDOException $e)
  117. {
  118. $this->error($e->getMessage());
  119. }
  120. }
  121. $this->error(__('Parameter %s can not be empty', ''));
  122. }
  123. return $this->view->fetch();
  124. }
  125. /**
  126. * 编辑
  127. */
  128. public function edit($ids = NULL)
  129. {
  130. $row = $this->model->get($ids);
  131. if (!$row)
  132. $this->error(__('No Results were found'));
  133. $adminIds = $this->getDataLimitAdminIds();
  134. if (is_array($adminIds))
  135. {
  136. if (!in_array($row[$this->dataLimitField], $adminIds))
  137. {
  138. $this->error(__('You have no permission'));
  139. }
  140. }
  141. if ($this->request->isPost())
  142. {
  143. $params = $this->request->post("row/a");
  144. if ($params)
  145. {
  146. /*
  147. * 已经弃用,如果为了兼容老版可取消注释
  148. foreach ($params as $k => &$v)
  149. {
  150. $v = is_array($v) ? implode(',', $v) : $v;
  151. }
  152. */
  153. $authInfo = $this->auth->getUserInfo($this->auth->id);
  154. $params['opuser_id'] = $this->auth->id;
  155. $params['opuser_name'] = $authInfo['username'];
  156. try
  157. {
  158. //是否采用模型验证
  159. if ($this->modelValidate)
  160. {
  161. $name = basename(str_replace('\\', '/', get_class($this->model)));
  162. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate;
  163. $row->validate($validate);
  164. }
  165. $result = $row->allowField(true)->save($params);
  166. if ($result !== false)
  167. {
  168. AdminKlUpdateService::instance()->updateConfigUserCache($ids, $this->auth->id, '编辑规则');
  169. $this->success();
  170. }
  171. else
  172. {
  173. $this->error($row->getError());
  174. }
  175. }
  176. catch (\think\exception\PDOException $e)
  177. {
  178. $this->error($e->getMessage());
  179. }
  180. }
  181. $this->error(__('Parameter %s can not be empty', ''));
  182. }
  183. $this->view->assign("row", $row);
  184. return $this->view->fetch();
  185. }
  186. /**
  187. * 批量删除关联渠道
  188. *
  189. */
  190. public function batchAssociateDel()
  191. {
  192. if($this->request->isAjax()){
  193. $channel_ids_str = $this->request->post('ids');
  194. $kl_rule_id = $this->request->post('kl_id');
  195. // 获取现有的渠道IDS
  196. $kl_obj = $this->model->get(['id'=>$kl_rule_id]);
  197. $channel_ids_old = trim($kl_obj->channel_ids);
  198. $qdchannel_arr_old = [];
  199. if ($channel_ids_old) {
  200. $qdchannel_arr_old = explode(',', $channel_ids_old);
  201. }
  202. // 合并
  203. $qdchannel_arr_new = explode(',', $channel_ids_str);
  204. $qdchannel_arr = array_diff($qdchannel_arr_old, $qdchannel_arr_new);
  205. // 更新数据库
  206. $this->model->update(
  207. [
  208. 'channel_ids' => implode(',', $qdchannel_arr),
  209. ],
  210. ['id' => $kl_rule_id]
  211. );
  212. AdminKlUpdateService::instance()->updateConfigUserCache($kl_rule_id, $this->auth->id, '批量删除关联');
  213. $this->success();
  214. }
  215. }
  216. /**
  217. * 批量关联
  218. */
  219. public function batchAssociate()
  220. {
  221. if($this->request->isAjax()){
  222. $channel_ids_str = $this->request->post('ids');
  223. $kl_rule_id = $this->request->post('kl_id');
  224. // 获取现有的渠道IDS
  225. $kl_obj = $this->model->get(['id'=>$kl_rule_id]);
  226. $channel_ids_old = trim($kl_obj->channel_ids);
  227. $qdchannel_arr_old = [];
  228. if ($channel_ids_old) {
  229. $qdchannel_arr_old = explode(',', $channel_ids_old);
  230. }
  231. // 合并
  232. $qdchannel_arr_new = explode(',', $channel_ids_str);
  233. $qdchannel_arr = array_unique(array_merge($qdchannel_arr_old, $qdchannel_arr_new));
  234. // 更新数据库
  235. $this->model->update(
  236. [
  237. 'channel_ids' => implode(',', $qdchannel_arr),
  238. ],
  239. ['id' => $kl_rule_id]
  240. );
  241. AdminKlUpdateService::instance()->updateConfigUserCache($kl_rule_id, $this->auth->id, '批量关联');
  242. $this->success();
  243. }
  244. }
  245. /**
  246. * 执行关联操作
  247. */
  248. public function ajaxSingeAssociate(){
  249. if($this->request->isAjax()){
  250. $kl_id = $this->request->post('kl_id');
  251. $channel_id = $this->request->post('channel_id');
  252. $kl_obj = $this->model->get(['id'=>$kl_id]);
  253. $channel_ids = trim($kl_obj->channel_ids);
  254. $qdchannel_arr = [];
  255. if ($channel_ids) {
  256. $qdchannel_arr = explode(',', $channel_ids);
  257. }
  258. if(!in_array($channel_id, $qdchannel_arr)){
  259. array_push($qdchannel_arr, $channel_id);
  260. $this->model->update(
  261. [
  262. 'channel_ids' => implode(',', $qdchannel_arr),
  263. ],
  264. ['id' => $kl_id]
  265. );
  266. }
  267. AdminKlUpdateService::instance()->updateConfigUserCache($kl_id, $this->auth->id, '关联');
  268. $this->success('关联成功');
  269. }
  270. }
  271. /**
  272. * 执行关联操作
  273. * 关联所有渠道
  274. */
  275. public function ajaxSingeAssociateAll(){
  276. if($this->request->isAjax()){
  277. $kl_id = $this->request->param('kl_id');
  278. $this->model->update(
  279. [
  280. 'channel_ids' => '*',
  281. ],
  282. ['id' => $kl_id]
  283. );
  284. AdminKlUpdateService::instance()->updateConfigUserCache($kl_id, $this->auth->id, '关联所有');
  285. $this->success('关联所有渠道成功');
  286. }
  287. }
  288. /**
  289. * 删除关联渠道
  290. */
  291. public function ajaxSingeAssociatedel(){
  292. if($this->request->isAjax()){
  293. $kl_id = $this->request->post('kl_id');
  294. $channel_id = $this->request->post('channel_id');
  295. $kl_obj = $this->model->get(['id'=>$kl_id]);
  296. $channel_ids = trim($kl_obj->channel_ids);
  297. $qdchannel_arr = [];
  298. if ($channel_ids) {
  299. $qdchannel_arr = explode(',', $channel_ids);
  300. }
  301. if(in_array($channel_id, $qdchannel_arr)){
  302. // 删除元素
  303. $tmp_arr = array_flip($qdchannel_arr);
  304. $tmp_key = $tmp_arr[$channel_id];
  305. unset($qdchannel_arr[$tmp_key]);
  306. $this->model->update(
  307. [
  308. 'channel_ids' => implode(',', $qdchannel_arr),
  309. ],
  310. ['id' => $kl_id]
  311. );
  312. AdminKlUpdateService::instance()->updateConfigUserCache($kl_id, $this->auth->id, '删除关联');
  313. }
  314. $this->success('删除成功');
  315. }
  316. }
  317. /**
  318. * 修改状态:打开或隐藏
  319. * @return mixed
  320. */
  321. public function changestatus(){
  322. $id = $this->request->post('id');
  323. $status = $this->request->post('status');
  324. $this->model->update(
  325. [
  326. 'status' => $status,
  327. ],
  328. ['id' => $id]
  329. );
  330. AdminKlUpdateService::instance()->updateConfigUserCache($id, $this->auth->id, '修改规则状态');
  331. return true;
  332. }
  333. /**
  334. * 查看
  335. */
  336. public function index()
  337. {
  338. //设置过滤方法
  339. $this->request->filter(['strip_tags']);
  340. if ($this->request->isAjax())
  341. {
  342. //如果发送的来源是Selectpage,则转发到Selectpage
  343. if ($this->request->request('pkey_name'))
  344. {
  345. return $this->selectpage();
  346. }
  347. $this->relationSearch = true;
  348. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  349. $total = $this->model
  350. ->field("kl_rules_manage.*, kc.id as config_id")
  351. ->join('kl_rule_config kc', 'kc.rule_id = kl_rules_manage.id', 'LEFT')
  352. ->where($where)
  353. ->order('kl_rules_manage.weight', 'desc')
  354. ->order($sort, $order)
  355. ->count();
  356. $list = $this->model
  357. ->field("kl_rules_manage.*, kc.id as config_id")
  358. ->join('kl_rule_config kc', 'kc.rule_id = kl_rules_manage.id', 'LEFT')
  359. ->where($where)
  360. ->order('kl_rules_manage.weight', 'desc')
  361. ->order($sort, $order)
  362. ->limit($offset, $limit)
  363. ->select();
  364. if ($list) {
  365. foreach ($list as &$item) {
  366. $url = '/admin/kl/ruleconfig/add?rule_id='.$item['id'];
  367. $data = Redis::instance()->hGetAll(CacheConstants::getKlRuleModifyInfo($item['id']));
  368. if ($data) {
  369. $item['opuser_name'] = $data['opuser_name'] . '-' . $data['msg'];
  370. $item['updatetime'] = $data['updatetime'];
  371. }
  372. if ($item['config_id']) {
  373. $url = '/admin/kl/ruleconfig/edit?rule_id='.$item['id'].'&ids='.$item['config_id'];
  374. }
  375. $item['config_url'] = $url;
  376. }
  377. }
  378. $result = array("total" => $total, "rows" => $list);
  379. return json($result);
  380. }
  381. return $this->view->fetch();
  382. }
  383. }