Guidedomain.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use app\common\library\Redis;
  5. use think\Controller;
  6. use think\Request;
  7. /**
  8. * 倒粉域名池
  9. *
  10. * @icon fa fa-circle-o
  11. */
  12. class Guidedomain extends Backend
  13. {
  14. /**
  15. * GuideDomain模型对象
  16. */
  17. protected $model = null;
  18. /**
  19. * 是否开启Validate验证
  20. */
  21. protected $modelValidate = true;
  22. public function _initialize()
  23. {
  24. parent::_initialize();
  25. $this->model = model('GuideDomain');
  26. $this->view->assign("statusList", $this->model->getStatusList());
  27. }
  28. /**
  29. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法
  30. * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑
  31. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  32. */
  33. /**
  34. * 添加/支持批量
  35. *
  36. * @author liues@dianzhong.com
  37. * @date 2018-08-17 18:43:13
  38. * @return html|json
  39. */
  40. public function add()
  41. {
  42. if ($this->request->isPost())
  43. {
  44. $hosts = trim($this->request->post("row.host"));
  45. $status = $this->request->post("row.status") ?? '1';
  46. if($hosts)
  47. {
  48. $params = array();
  49. $hosts = explode("\r\n", $hosts);
  50. $hosts = array_unique(array_filter($hosts));
  51. if(count($hosts) > 50){
  52. $this->error('单次批量添加上限50条');
  53. }
  54. foreach($hosts as $key => $host)
  55. {
  56. if(!empty($host))
  57. {
  58. if(mb_strlen($host) > 150)
  59. {
  60. $this->error("每条域名长度不能超过150字符\r\n".$host);
  61. }
  62. $exists = model('GuideDomain')->getByHost($host);
  63. if(empty($exists))
  64. {
  65. $params[] = array('host' => $host, 'status' => $status);
  66. }
  67. }
  68. }
  69. try
  70. {
  71. //是否采用模型验证
  72. if ($this->modelValidate)
  73. {
  74. $name = basename(str_replace('\\', '/', get_class($this->model)));
  75. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
  76. $this->model->validate($validate);
  77. }
  78. $result = $this->model->allowField(true)->saveAll($params);
  79. if ($result !== false)
  80. {
  81. $this->success();
  82. }
  83. else
  84. {
  85. $this->error($this->model->getError());
  86. }
  87. }
  88. catch (\think\exception\PDOException $e)
  89. {
  90. $this->error($e->getMessage());
  91. }
  92. $this->error(__('Parameter %s can not be empty', ''));
  93. }
  94. }
  95. return $this->view->fetch();
  96. }
  97. /**
  98. * 编辑
  99. */
  100. public function edit($ids = NULL)
  101. {
  102. $row = $this->model->get($ids);
  103. if (!$row)
  104. $this->error(__('No Results were found'));
  105. $adminIds = $this->getDataLimitAdminIds();
  106. if (is_array($adminIds))
  107. {
  108. if (!in_array($row[$this->dataLimitField], $adminIds))
  109. {
  110. $this->error(__('You have no permission'));
  111. }
  112. }
  113. if ($this->request->isPost())
  114. {
  115. $params = $this->request->post("row/a");
  116. if ($params)
  117. {
  118. /*
  119. * 已经弃用,如果为了兼容老版可取消注释
  120. foreach ($params as $k => &$v)
  121. {
  122. $v = is_array($v) ? implode(',', $v) : $v;
  123. }
  124. */
  125. try
  126. {
  127. //是否采用模型验证
  128. if ($this->modelValidate)
  129. {
  130. $name = basename(str_replace('\\', '/', get_class($this->model)));
  131. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate;
  132. $row->validate($validate);
  133. }
  134. if(!$params['status']){
  135. $this->close($row['id']);
  136. }
  137. $whereMap['host'] = $params['host'];
  138. $whereMap['id'] = ['neq',$row['id']];
  139. $params['updatetime'] = time();
  140. if($this->model->where($whereMap)->find()){
  141. $this->error('域名已存在,请保持域名唯一性');
  142. }
  143. $result = $row->allowField(true)->save($params);
  144. if ($result !== false)
  145. {
  146. $redis = Redis::instance();
  147. if($relations = model('GuideRelation')->where('domain_id',$row['id'])->column('admin_id')){
  148. foreach($relations as $val){
  149. $redis->del('GD-H:'.$val);
  150. }
  151. }
  152. $this->success();
  153. }
  154. else
  155. {
  156. $this->error($row->getError());
  157. }
  158. }
  159. catch (\think\exception\PDOException $e)
  160. {
  161. $this->error($e->getMessage());
  162. }
  163. }
  164. $this->error(__('Parameter %s can not be empty', ''));
  165. }
  166. $this->view->assign("row", $row);
  167. return $this->view->fetch();
  168. }
  169. /**
  170. * 删除
  171. *
  172. * @author liues@dianzhong.com
  173. * @date 2018-08-18 10:16:29
  174. * @param string $ids
  175. * @return json
  176. */
  177. public function del($ids = "")
  178. {
  179. //严禁删除
  180. $this->error(__('No rows were deleted'));
  181. }
  182. /**
  183. * 启用
  184. *
  185. * @author liues@dianzhong.com
  186. * @date 2018-08-17 18:44:02
  187. * @param null $ids
  188. * @return html|json
  189. */
  190. public function open($ids = NULL){
  191. if ($ids)
  192. {
  193. $pk = $this->model->getPk();
  194. // $adminIds = $this->getDataLimitAdminIds();
  195. // if (is_array($adminIds))
  196. // {
  197. // $count = $this->model->where($this->dataLimitField, 'in', $adminIds);
  198. // }
  199. $list = $this->model->where($pk, 'in', $ids)->select();
  200. $count = 0;
  201. foreach ($list as $k => $v)
  202. {
  203. $redis = Redis::instance();
  204. if($relations = model('GuideRelation')->where('domain_id',$v->id)->column('admin_id')){
  205. foreach($relations as $val){
  206. $redis->del('GD-H:'.$val);
  207. }
  208. }
  209. $count += $v->where($pk, $v->id)->update(['status'=>'1','updatetime'=>time()]);
  210. }
  211. if ($count)
  212. {
  213. $this->success();
  214. }
  215. else
  216. {
  217. $this->error(__('No rows were updated'));
  218. }
  219. }
  220. $this->error(__('Parameter %s can not be empty', 'ids'));
  221. }
  222. /**
  223. * 关闭
  224. *
  225. * @author liues@dianzhong.com
  226. * @date 2018-08-17 18:44:02
  227. * @param null $ids
  228. * @return html|json
  229. */
  230. public function close($ids = NULL){
  231. if ($ids)
  232. {
  233. $pk = $this->model->getPk();
  234. $adminIds = $this->getDataLimitAdminIds();
  235. if (is_array($adminIds))
  236. {
  237. $count = $this->model->where($this->dataLimitField, 'in', $adminIds);
  238. }
  239. $list = $this->model->where($pk, 'in', $ids)->select();
  240. $count = 0;
  241. foreach ($list as $k => $v)
  242. {
  243. // $this->model->startTrans();
  244. try{
  245. $count += $v->where($pk, $v->id)->update(['status'=>'0','updatetime'=>time()]);
  246. $relations = model('GuideRelation')->where('domain_id',$v->id)->column('admin_id');
  247. if(!empty($relations)){
  248. foreach ($relations as $val){
  249. if(!model('GuideRelation')->where(['admin_id'=>$val])->where('domain_id','<>',$v['id'])->find()){ //除此短链外,没有任何其他短链了
  250. model('AdminConfig')->where(['admin_id'=>$val])->update(['guide_domain'=>'0']);
  251. model('AdminConfig')->delAdminInfoAllCache($val);
  252. }
  253. }
  254. }
  255. model('GuideRelation')->where('domain_id',$v['id'])->delete(); //禁用后解除绑定关系
  256. }catch (Exception $e){
  257. }
  258. }
  259. if ($count)
  260. {
  261. $this->success();
  262. }
  263. else
  264. {
  265. $this->error(__('No rows were updated'));
  266. }
  267. }
  268. $this->error(__('Parameter %s can not be empty', 'ids'));
  269. }
  270. }