123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Elton
- * Date: 2019/9/12
- * Time: 14:25
- */
- namespace app\admin\controller\kl;
- use app\common\controller\Backend;
- use app\common\library\Redis;
- use app\main\constants\CacheConstants;
- use app\main\service\AdminKlUpdateService;
- use function think\__include_file;
- class Rulemanage extends Backend
- {
- protected $noNeedRight = ['citylist'];
- public function _initialize()
- {
- $this->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();
- }
- }
|