Shortdomain.php 8.8 KB

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