Ophost.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use app\main\helper\FileHelper;
  5. use app\main\service\AdminService;
  6. use think\Log;
  7. /**
  8. * 业务域名
  9. *
  10. * @icon fa fa-circle-o
  11. */
  12. class Ophost extends Backend
  13. {
  14. /**
  15. * Ophost模型对象
  16. */
  17. protected $model = null;
  18. protected $relationSearch = true;
  19. protected $noNeedLogin = ['checkhostunique'];
  20. public function _initialize()
  21. {
  22. parent::_initialize();
  23. $this->model = model('Ophost');
  24. $this->view->assign("isdefaultList", $this->model->getIsdefaultList());
  25. $this->view->assign("statusList", $this->model->getStatusList());
  26. $this->view->assign("allowChangedList", $this->model->getAllowChangedList());
  27. }
  28. /**
  29. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法
  30. * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑
  31. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  32. */
  33. /**
  34. * 查看
  35. */
  36. public function index()
  37. {
  38. //设置过滤方法
  39. $this->request->filter(['strip_tags']);
  40. if ($this->request->isAjax())
  41. {
  42. //如果发送的来源是Selectpage,则转发到Selectpage
  43. if ($this->request->request('pkey_name'))
  44. {
  45. return $this->selectpage();
  46. }
  47. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  48. $total = $this->model
  49. ->where($where)
  50. ->order($sort, $order)
  51. ->count();
  52. $list = $this->model
  53. ->with("platform")
  54. ->where($where)
  55. ->order($sort, $order)
  56. ->limit($offset, $limit)
  57. ->select();
  58. foreach($list as $index=>$item){
  59. $list[$index]['download'] = "/admin/ophost/download?id={$item->id}";
  60. }
  61. $result = array("total" => $total, "rows" => $list);
  62. return json($result);
  63. }
  64. return $this->view->fetch();
  65. }
  66. /**
  67. * 添加
  68. */
  69. public function add()
  70. {
  71. if ($this->request->isPost())
  72. {
  73. $params = $this->request->post("row/a");
  74. if ($params)
  75. {
  76. /*
  77. * 已经弃用,如果为了兼容老版可取消注释
  78. foreach ($params as $k => &$v)
  79. {
  80. $v = is_array($v) ? implode(',', $v) : $v;
  81. }
  82. */
  83. if ($this->dataLimit)
  84. {
  85. $params[$this->dataLimitField] = $this->auth->id;
  86. }
  87. try
  88. {
  89. //是否采用模型验证
  90. if ($this->modelValidate)
  91. {
  92. $name = basename(str_replace('\\', '/', get_class($this->model)));
  93. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
  94. $this->model->validate($validate);
  95. }
  96. if($params['status'] == 1){
  97. if($params['isdefault'] == 1){
  98. $data['isdefault'] = 0;
  99. $defaultRow = $this->model->where(["isdefault"=>1,"platform_id"=>$params['platform_id']])->find();
  100. if($defaultRow) {
  101. $defaultRow->allowField(true)->save($data);
  102. }
  103. $this->model->delCacheByPlatformId($params['platform_id']);
  104. }
  105. }
  106. $result = $this->model->allowField(true)->save($params);
  107. if ($result !== false)
  108. {
  109. $this->model->delCacheByHosts();
  110. $this->success();
  111. }
  112. else
  113. {
  114. $this->error($this->model->getError());
  115. }
  116. }
  117. catch (\think\exception\PDOException $e)
  118. {
  119. $this->error($e->getMessage());
  120. }
  121. }
  122. $this->error(__('Parameter %s can not be empty', ''));
  123. }
  124. return $this->view->fetch();
  125. }
  126. /**
  127. * 编辑
  128. */
  129. public function edit($ids = NULL)
  130. {
  131. $row = $this->model->get($ids);
  132. if (!$row)
  133. $this->error(__('No Results were found'));
  134. $adminIds = $this->getDataLimitAdminIds();
  135. if (is_array($adminIds))
  136. {
  137. if (!in_array($row[$this->dataLimitField], $adminIds))
  138. {
  139. $this->error(__('You have no permission'));
  140. }
  141. }
  142. if ($this->request->isPost())
  143. {
  144. $params = $this->request->post("row/a");
  145. if ($params)
  146. {
  147. /*
  148. * 已经弃用,如果为了兼容老版可取消注释
  149. foreach ($params as $k => &$v)
  150. {
  151. $v = is_array($v) ? implode(',', $v) : $v;
  152. }
  153. */
  154. try
  155. {
  156. //是否采用模型验证
  157. if ($this->modelValidate)
  158. {
  159. $name = basename(str_replace('\\', '/', get_class($this->model)));
  160. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate;
  161. $row->validate($validate);
  162. }
  163. if($params['platform_id'] != $row['platform_id'] && $row['isdefault'] == 1){
  164. $this->error("默认项不可更改平台");
  165. }
  166. $msg = "";
  167. if($params['status'] != $row['status'] || $params['isdefault']!=$row['isdefault']){
  168. if($params['status'] == 1 && $params['isdefault'] == 1){
  169. $data['isdefault'] = 0;
  170. $defaultRow = $this->model->where(["isdefault"=>1,"platform_id"=>$params['platform_id']])->where("id","not in",$ids)->find();
  171. if($defaultRow) {
  172. $defaultRow->allowField(true)->save($data);
  173. }
  174. $this->model->delCacheAll($defaultRow['id']);
  175. $this->model->delCacheByHosts();
  176. $this->model->delCacheByPlatformId($params['platform_id']);
  177. }elseif ($params['status'] == 0 && $params['isdefault'] == 1 && $row['isdefault'] == 1){
  178. //默认可以关闭 $this->error("默认项不可关闭");
  179. }elseif ($params['isdefault'] == 0 && $row['isdefault'] == 1){
  180. $this->error("默认项不可更改为非默认");
  181. }
  182. if ($params['status'] == 0 && $params['isdefault'] == 1) {
  183. //默认可以关闭 $this->error("变更默认项时,状态不可关闭");
  184. }
  185. if($params['status'] == 0 && $row['status'] == 1){ //关闭业务域名
  186. //检查是否有渠道正在使用
  187. if(model('AdminConfig')->where('ophost_id',$row['id'])->find()){
  188. $this->error('有渠道的业务域名正在使用中,不能关闭');
  189. }
  190. if(model('AdminConfig')->where('menuophost_id',$row['id'])->find()){
  191. $this->error('有渠道的菜单业务域名正在使用中,不能关闭');
  192. }
  193. $idlist = model("AdminConfig")->where("menuophost_id",$row['id'])->column("admin_id");
  194. $idlist = implode(",",$idlist);
  195. if(!empty($idlist)){
  196. $msg = "请修改:$idlist 用户的菜单域名";
  197. }
  198. }
  199. }
  200. $result = $row->allowField(true)->save($params);
  201. if ($result !== false) {
  202. if($params['status'] == 1 && $params['isdefault'] == 1){
  203. $this->model->delCacheByPlatformId($params['platform_id']);
  204. }
  205. $this->model->delCacheByHosts();
  206. $this->model->delCacheAll($row['id']);
  207. if(!empty($msg)){
  208. Log::info("CM ID为{$row['id']}的菜单业务域名关闭 {$msg} date:".date("Y-m-d H:i:s",time()));
  209. }
  210. $this->success($msg);
  211. }
  212. else
  213. {
  214. $this->error($row->getError());
  215. }
  216. }
  217. catch (\think\exception\PDOException $e)
  218. {
  219. $this->error($e->getMessage());
  220. }
  221. }
  222. $this->error(__('Parameter %s can not be empty', ''));
  223. }
  224. $this->view->assign("row", $row);
  225. return $this->view->fetch();
  226. }
  227. /**
  228. * 检查支付域名是否唯一
  229. * @return \think\response\Json
  230. */
  231. public function checkhostunique(){
  232. $params = $this->request->param('row/a');
  233. if(isset($params['host']) && !empty($params['host'])){
  234. $map['host'] = $params['host'];
  235. //编辑检查
  236. if($this->request->has('id')){
  237. if($id = $this->request->get('id')){
  238. $map['id'] = ['neq',$id];
  239. }
  240. }
  241. if($this->model->where($map)->find()){
  242. return json(['code'=>600,'msg'=>'该业务域名已存在,请不要重复使用']);
  243. }
  244. return json(['code'=>200,'data'=>['ok'=>'业务域名可以使用']]);
  245. }
  246. return json(['code'=>600,'msg'=>'参数错误']);
  247. }
  248. public function download($id)
  249. {
  250. $head = [
  251. ['渠道商id', '渠道商用户名', '渠道商昵称', '管理员用户名', '管理员昵称'],
  252. ];
  253. $data = AdminService::instance()->getChannelInfoByOphost($id)->data;
  254. $list = array_merge($head, $data);
  255. FileHelper::downloadCsv("渠道信息-id_{$id}-".date('Ymd-His'), $list);
  256. }
  257. }