Entryhost.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 Entryhost extends Backend
  13. {
  14. /**
  15. * Entryhost模型对象
  16. */
  17. protected $model = null;
  18. public function _initialize()
  19. {
  20. parent::_initialize();
  21. $this->model = model('Entryhost');
  22. $this->view->assign("statusList", $this->model->getStatusList());
  23. }
  24. /**
  25. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法
  26. * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑
  27. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  28. */
  29. /**
  30. * 添加
  31. */
  32. public function add()
  33. {
  34. if ($this->request->isPost())
  35. {
  36. $params = $this->request->post("row/a");
  37. if ($params)
  38. {
  39. $redis = Redis::instance();
  40. $redis->del('ENTRYHOST');
  41. /*
  42. * 已经弃用,如果为了兼容老版可取消注释
  43. foreach ($params as $k => &$v)
  44. {
  45. $v = is_array($v) ? implode(',', $v) : $v;
  46. }
  47. */
  48. if ($this->dataLimit)
  49. {
  50. $params[$this->dataLimitField] = $this->auth->id;
  51. }
  52. try
  53. {
  54. //是否采用模型验证
  55. if ($this->modelValidate)
  56. {
  57. $name = basename(str_replace('\\', '/', get_class($this->model)));
  58. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
  59. $this->model->validate($validate);
  60. }
  61. $result = $this->model->allowField(true)->save($params);
  62. if ($result !== false)
  63. {
  64. $this->success();
  65. }
  66. else
  67. {
  68. $this->error($this->model->getError());
  69. }
  70. }
  71. catch (\think\exception\PDOException $e)
  72. {
  73. $this->error($e->getMessage());
  74. }
  75. }
  76. $this->error(__('Parameter %s can not be empty', ''));
  77. }
  78. return $this->view->fetch();
  79. }
  80. /**
  81. * 编辑
  82. */
  83. public function edit($ids = NULL)
  84. {
  85. $row = $this->model->get($ids);
  86. if (!$row)
  87. $this->error(__('No Results were found'));
  88. $adminIds = $this->getDataLimitAdminIds();
  89. if (is_array($adminIds))
  90. {
  91. if (!in_array($row[$this->dataLimitField], $adminIds))
  92. {
  93. $this->error(__('You have no permission'));
  94. }
  95. }
  96. if ($this->request->isPost())
  97. {
  98. $params = $this->request->post("row/a");
  99. if ($params)
  100. {
  101. /*
  102. * 已经弃用,如果为了兼容老版可取消注释
  103. foreach ($params as $k => &$v)
  104. {
  105. $v = is_array($v) ? implode(',', $v) : $v;
  106. }
  107. */
  108. try
  109. {
  110. //是否采用模型验证
  111. if ($this->modelValidate)
  112. {
  113. $name = basename(str_replace('\\', '/', get_class($this->model)));
  114. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate;
  115. $row->validate($validate);
  116. }
  117. $defaultRes = model('Entryhost')->where('id',$row['id'])->find();
  118. if($defaultRes->status != $params['status'] && $params['status']==0){ //如果关闭了该入口域名admin_config表清除
  119. $admin_ids = model('AdminConfig')->where('entryhost_id',$row['id'])->column('admin_id');
  120. $redis = Redis::instance();
  121. $redis->del('ENTRYHOST');
  122. //批量删除admin_config的缓存
  123. foreach($admin_ids as $val){
  124. model('AdminConfig')->delAdminInfoAllCache($val);
  125. }
  126. //更改数据库
  127. model('AdminConfig')->where('entryhost_id',$row['id'])->update(['entryhost_id'=>0]);
  128. }
  129. $result = $row->allowField(true)->save($params);
  130. if ($result !== false)
  131. {
  132. $redis = Redis::instance();
  133. $key = 'EH:'.$row['id'];
  134. $redis->del($key);
  135. $this->success();
  136. }
  137. else
  138. {
  139. $this->error($row->getError());
  140. }
  141. }
  142. catch (\think\exception\PDOException $e)
  143. {
  144. $this->error($e->getMessage());
  145. }
  146. }
  147. $this->error(__('Parameter %s can not be empty', ''));
  148. }
  149. $this->view->assign("row", $row);
  150. return $this->view->fetch();
  151. }
  152. }