model = model('KlRuleManage'); return parent::_initialize(); // TODO: Change the autogenerated stub } /** * 城市列表 */ public function citylist() { //设置过滤方法 $this->request->filter(['strip_tags']); if ($this->request->isAjax()) { if ($this->request->get('all')) { $dbFile = ROOT_PATH . 'public/assets/data/citylist_plus.php'; } else { $dbFile = ROOT_PATH . 'public/assets/data/citylist.php'; } $arr = __include_file($dbFile); //设置过滤方法 $list = $data = []; $total = 0; if ($selector = $this->request->param('pkey_value')) { $ids = explode(",", $selector); foreach ($arr as $key=>$val) { if (in_array($key, $ids)) { $data[$key] = $val; } } $total = count($data); } else { $field = $this->request->param('name'); if ($field) { foreach ($arr as $key=>$val) { if (strrpos($val, $field) !== false) { $data[$key] = $val; } } $total = count($data); } else { $data = $arr; $total = count($data); } } if ($total > 0) { foreach ($data as $index => $item) { $list[] = [ 'id' => $index, 'name' => $item ]; } } //这里一定要返回有list这个字段,total是可选的,如果total<=list的数量,则会隐藏分页按钮 return json(['list' => $list, 'total' => $total]); } } /** * 添加 */ public function add() { if ($this->request->isPost()) { $params = $this->request->post("row/a"); if ($params) { $authInfo = $this->auth->getUserInfo($this->auth->id); $params['opuser_id'] = $this->auth->id; $params['opuser_name'] = $authInfo['username']; /* * 已经弃用,如果为了兼容老版可取消注释 foreach ($params as $k => &$v) { $v = is_array($v) ? implode(',', $v) : $v; } */ if ($this->dataLimit) { $params[$this->dataLimitField] = $this->auth->id; } try { //是否采用模型验证 if ($this->modelValidate) { $name = basename(str_replace('\\', '/', get_class($this->model))); $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate; $this->model->validate($validate); } $result = $this->model->allowField(true)->save($params); if ($result !== false) { $this->success(); } else { $this->error($this->model->getError()); } } catch (\think\exception\PDOException $e) { $this->error($e->getMessage()); } } $this->error(__('Parameter %s can not be empty', '')); } return $this->view->fetch(); } /** * 编辑 */ public function edit($ids = NULL) { $row = $this->model->get($ids); if (!$row) $this->error(__('No Results were found')); $adminIds = $this->getDataLimitAdminIds(); if (is_array($adminIds)) { if (!in_array($row[$this->dataLimitField], $adminIds)) { $this->error(__('You have no permission')); } } if ($this->request->isPost()) { $params = $this->request->post("row/a"); if ($params) { /* * 已经弃用,如果为了兼容老版可取消注释 foreach ($params as $k => &$v) { $v = is_array($v) ? implode(',', $v) : $v; } */ $authInfo = $this->auth->getUserInfo($this->auth->id); $params['opuser_id'] = $this->auth->id; $params['opuser_name'] = $authInfo['username']; try { //是否采用模型验证 if ($this->modelValidate) { $name = basename(str_replace('\\', '/', get_class($this->model))); $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate; $row->validate($validate); } $result = $row->allowField(true)->save($params); if ($result !== false) { AdminKlUpdateService::instance()->updateConfigUserCache($ids, $this->auth->id, '编辑规则'); $this->success(); } else { $this->error($row->getError()); } } catch (\think\exception\PDOException $e) { $this->error($e->getMessage()); } } $this->error(__('Parameter %s can not be empty', '')); } $this->view->assign("row", $row); return $this->view->fetch(); } /** * 批量删除关联渠道 * */ public function batchAssociateDel() { if($this->request->isAjax()){ $channel_ids_str = $this->request->post('ids'); $kl_rule_id = $this->request->post('kl_id'); // 获取现有的渠道IDS $kl_obj = $this->model->get(['id'=>$kl_rule_id]); $channel_ids_old = trim($kl_obj->channel_ids); $qdchannel_arr_old = []; if ($channel_ids_old) { $qdchannel_arr_old = explode(',', $channel_ids_old); } // 合并 $qdchannel_arr_new = explode(',', $channel_ids_str); $qdchannel_arr = array_diff($qdchannel_arr_old, $qdchannel_arr_new); // 更新数据库 $this->model->update( [ 'channel_ids' => implode(',', $qdchannel_arr), ], ['id' => $kl_rule_id] ); AdminKlUpdateService::instance()->updateConfigUserCache($kl_rule_id, $this->auth->id, '批量删除关联'); $this->success(); } } /** * 批量关联 */ public function batchAssociate() { if($this->request->isAjax()){ $channel_ids_str = $this->request->post('ids'); $kl_rule_id = $this->request->post('kl_id'); // 获取现有的渠道IDS $kl_obj = $this->model->get(['id'=>$kl_rule_id]); $channel_ids_old = trim($kl_obj->channel_ids); $qdchannel_arr_old = []; if ($channel_ids_old) { $qdchannel_arr_old = explode(',', $channel_ids_old); } // 合并 $qdchannel_arr_new = explode(',', $channel_ids_str); $qdchannel_arr = array_unique(array_merge($qdchannel_arr_old, $qdchannel_arr_new)); // 更新数据库 $this->model->update( [ 'channel_ids' => implode(',', $qdchannel_arr), ], ['id' => $kl_rule_id] ); AdminKlUpdateService::instance()->updateConfigUserCache($kl_rule_id, $this->auth->id, '批量关联'); $this->success(); } } /** * 执行关联操作 */ public function ajaxSingeAssociate(){ if($this->request->isAjax()){ $kl_id = $this->request->post('kl_id'); $channel_id = $this->request->post('channel_id'); $kl_obj = $this->model->get(['id'=>$kl_id]); $channel_ids = trim($kl_obj->channel_ids); $qdchannel_arr = []; if ($channel_ids) { $qdchannel_arr = explode(',', $channel_ids); } if(!in_array($channel_id, $qdchannel_arr)){ array_push($qdchannel_arr, $channel_id); $this->model->update( [ 'channel_ids' => implode(',', $qdchannel_arr), ], ['id' => $kl_id] ); } AdminKlUpdateService::instance()->updateConfigUserCache($kl_id, $this->auth->id, '关联'); $this->success('关联成功'); } } /** * 执行关联操作 * 关联所有渠道 */ public function ajaxSingeAssociateAll(){ if($this->request->isAjax()){ $kl_id = $this->request->param('kl_id'); $this->model->update( [ 'channel_ids' => '*', ], ['id' => $kl_id] ); AdminKlUpdateService::instance()->updateConfigUserCache($kl_id, $this->auth->id, '关联所有'); $this->success('关联所有渠道成功'); } } /** * 删除关联渠道 */ public function ajaxSingeAssociatedel(){ if($this->request->isAjax()){ $kl_id = $this->request->post('kl_id'); $channel_id = $this->request->post('channel_id'); $kl_obj = $this->model->get(['id'=>$kl_id]); $channel_ids = trim($kl_obj->channel_ids); $qdchannel_arr = []; if ($channel_ids) { $qdchannel_arr = explode(',', $channel_ids); } if(in_array($channel_id, $qdchannel_arr)){ // 删除元素 $tmp_arr = array_flip($qdchannel_arr); $tmp_key = $tmp_arr[$channel_id]; unset($qdchannel_arr[$tmp_key]); $this->model->update( [ 'channel_ids' => implode(',', $qdchannel_arr), ], ['id' => $kl_id] ); AdminKlUpdateService::instance()->updateConfigUserCache($kl_id, $this->auth->id, '删除关联'); } $this->success('删除成功'); } } /** * 修改状态:打开或隐藏 * @return mixed */ public function changestatus(){ $id = $this->request->post('id'); $status = $this->request->post('status'); $this->model->update( [ 'status' => $status, ], ['id' => $id] ); AdminKlUpdateService::instance()->updateConfigUserCache($id, $this->auth->id, '修改规则状态'); return true; } /** * 查看 */ public function index() { //设置过滤方法 $this->request->filter(['strip_tags']); if ($this->request->isAjax()) { //如果发送的来源是Selectpage,则转发到Selectpage if ($this->request->request('pkey_name')) { return $this->selectpage(); } $this->relationSearch = true; list($where, $sort, $order, $offset, $limit) = $this->buildparams(); $total = $this->model ->field("kl_rules_manage.*, kc.id as config_id") ->join('kl_rule_config kc', 'kc.rule_id = kl_rules_manage.id', 'LEFT') ->where($where) ->order('kl_rules_manage.weight', 'desc') ->order($sort, $order) ->count(); $list = $this->model ->field("kl_rules_manage.*, kc.id as config_id") ->join('kl_rule_config kc', 'kc.rule_id = kl_rules_manage.id', 'LEFT') ->where($where) ->order('kl_rules_manage.weight', 'desc') ->order($sort, $order) ->limit($offset, $limit) ->select(); if ($list) { foreach ($list as &$item) { $url = '/admin/kl/ruleconfig/add?rule_id='.$item['id']; $data = Redis::instance()->hGetAll(CacheConstants::getKlRuleModifyInfo($item['id'])); if ($data) { $item['opuser_name'] = $data['opuser_name'] . '-' . $data['msg']; $item['updatetime'] = $data['updatetime']; } if ($item['config_id']) { $url = '/admin/kl/ruleconfig/edit?rule_id='.$item['id'].'&ids='.$item['config_id']; } $item['config_url'] = $url; } } $result = array("total" => $total, "rows" => $list); return json($result); } return $this->view->fetch(); } }