123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277 |
- <?php
- namespace app\admin\controller;
- use app\common\controller\Backend;
- use app\main\helper\FileHelper;
- use app\main\service\AdminService;
- use think\Log;
- /**
- * 业务域名
- *
- * @icon fa fa-circle-o
- */
- class Ophost extends Backend
- {
- /**
- * Ophost模型对象
- */
- protected $model = null;
- protected $relationSearch = true;
- protected $noNeedLogin = ['checkhostunique'];
- public function _initialize()
- {
- parent::_initialize();
- $this->model = model('Ophost');
- $this->view->assign("isdefaultList", $this->model->getIsdefaultList());
- $this->view->assign("statusList", $this->model->getStatusList());
- $this->view->assign("allowChangedList", $this->model->getAllowChangedList());
- }
-
- /**
- * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法
- * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑
- * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
- */
- /**
- * 查看
- */
- public function index()
- {
- //设置过滤方法
- $this->request->filter(['strip_tags']);
- if ($this->request->isAjax())
- {
- //如果发送的来源是Selectpage,则转发到Selectpage
- if ($this->request->request('pkey_name'))
- {
- return $this->selectpage();
- }
- list($where, $sort, $order, $offset, $limit) = $this->buildparams();
- $total = $this->model
- ->where($where)
- ->order($sort, $order)
- ->count();
- $list = $this->model
- ->with("platform")
- ->where($where)
- ->order($sort, $order)
- ->limit($offset, $limit)
- ->select();
- foreach($list as $index=>$item){
- $list[$index]['download'] = "/admin/ophost/download?id={$item->id}";
- }
- $result = array("total" => $total, "rows" => $list);
- return json($result);
- }
- return $this->view->fetch();
- }
- /**
- * 添加
- */
- public function add()
- {
- if ($this->request->isPost())
- {
- $params = $this->request->post("row/a");
- if ($params)
- {
- /*
- * 已经弃用,如果为了兼容老版可取消注释
- 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);
- }
- if($params['status'] == 1){
- if($params['isdefault'] == 1){
- $data['isdefault'] = 0;
- $defaultRow = $this->model->where(["isdefault"=>1,"platform_id"=>$params['platform_id']])->find();
- if($defaultRow) {
- $defaultRow->allowField(true)->save($data);
- }
- $this->model->delCacheByPlatformId($params['platform_id']);
- }
- }
- $result = $this->model->allowField(true)->save($params);
- if ($result !== false)
- {
- $this->model->delCacheByHosts();
- $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;
- }
- */
- 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);
- }
- if($params['platform_id'] != $row['platform_id'] && $row['isdefault'] == 1){
- $this->error("默认项不可更改平台");
- }
- $msg = "";
- if($params['status'] != $row['status'] || $params['isdefault']!=$row['isdefault']){
- if($params['status'] == 1 && $params['isdefault'] == 1){
- $data['isdefault'] = 0;
- $defaultRow = $this->model->where(["isdefault"=>1,"platform_id"=>$params['platform_id']])->where("id","not in",$ids)->find();
- if($defaultRow) {
- $defaultRow->allowField(true)->save($data);
- }
- $this->model->delCacheAll($defaultRow['id']);
- $this->model->delCacheByHosts();
- $this->model->delCacheByPlatformId($params['platform_id']);
- }elseif ($params['status'] == 0 && $params['isdefault'] == 1 && $row['isdefault'] == 1){
- //默认可以关闭 $this->error("默认项不可关闭");
- }elseif ($params['isdefault'] == 0 && $row['isdefault'] == 1){
- $this->error("默认项不可更改为非默认");
- }
- if ($params['status'] == 0 && $params['isdefault'] == 1) {
- //默认可以关闭 $this->error("变更默认项时,状态不可关闭");
- }
- if($params['status'] == 0 && $row['status'] == 1){ //关闭业务域名
- //检查是否有渠道正在使用
- if(model('AdminConfig')->where('ophost_id',$row['id'])->find()){
- $this->error('有渠道的业务域名正在使用中,不能关闭');
- }
- if(model('AdminConfig')->where('menuophost_id',$row['id'])->find()){
- $this->error('有渠道的菜单业务域名正在使用中,不能关闭');
- }
- $idlist = model("AdminConfig")->where("menuophost_id",$row['id'])->column("admin_id");
- $idlist = implode(",",$idlist);
- if(!empty($idlist)){
- $msg = "请修改:$idlist 用户的菜单域名";
- }
- }
- }
- $result = $row->allowField(true)->save($params);
- if ($result !== false) {
- if($params['status'] == 1 && $params['isdefault'] == 1){
- $this->model->delCacheByPlatformId($params['platform_id']);
- }
- $this->model->delCacheByHosts();
- $this->model->delCacheAll($row['id']);
- if(!empty($msg)){
- Log::info("CM ID为{$row['id']}的菜单业务域名关闭 {$msg} date:".date("Y-m-d H:i:s",time()));
- }
- $this->success($msg);
- }
- 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();
- }
- /**
- * 检查支付域名是否唯一
- * @return \think\response\Json
- */
- public function checkhostunique(){
- $params = $this->request->param('row/a');
- if(isset($params['host']) && !empty($params['host'])){
- $map['host'] = $params['host'];
- //编辑检查
- if($this->request->has('id')){
- if($id = $this->request->get('id')){
- $map['id'] = ['neq',$id];
- }
- }
- if($this->model->where($map)->find()){
- return json(['code'=>600,'msg'=>'该业务域名已存在,请不要重复使用']);
- }
- return json(['code'=>200,'data'=>['ok'=>'业务域名可以使用']]);
- }
- return json(['code'=>600,'msg'=>'参数错误']);
- }
- public function download($id)
- {
- $head = [
- ['渠道商id', '渠道商用户名', '渠道商昵称', '管理员用户名', '管理员昵称'],
- ];
- $data = AdminService::instance()->getChannelInfoByOphost($id)->data;
- $list = array_merge($head, $data);
- FileHelper::downloadCsv("渠道信息-id_{$id}-".date('Ymd-His'), $list);
- }
- }
|