GroupUser.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. declare(strict_types=1);
  3. namespace app\admin\controller\user;
  4. use app\common\controller\Backend;
  5. use app\admin\traits\Actions;
  6. use think\annotation\route\Group;
  7. use think\annotation\route\Route;
  8. use app\common\model\GroupUser as GroupUserModel;
  9. use think\facade\Db;
  10. #[Group("user/group_user")]
  11. class GroupUser extends Backend
  12. {
  13. use Actions;
  14. protected function _initialize()
  15. {
  16. parent::_initialize();
  17. $this->model = new GroupUserModel();
  18. }
  19. /**
  20. * 列表
  21. */
  22. #[Route('GET,POST,JSON', 'index')]
  23. public function index()
  24. {
  25. if (false === $this->request->isAjax()) {
  26. return $this->fetch();
  27. }
  28. if ($this->request->post('selectpage')) {
  29. return $this->selectpage();
  30. }
  31. $type = (string)$this->request->get('type', 2);
  32. [$where, $order, $limit, $with] = $this->buildparams();
  33. $list=[];
  34. if ($type == '1'){
  35. $list = $this->model->alias('g')
  36. ->join("yun_user u", "g.pid = u.id", "LEFT")
  37. ->with($with)
  38. ->where($where)
  39. ->order($order)
  40. ->field(['u.*','u.nickname' => 'manage_nickname'])
  41. ->group('g.pid')
  42. ->paginate($limit)->each(function ($item, $key) {
  43. $item['avatar']=$this->startsWithHttp($item['avatar'])?$item['avatar']:request()->domain().'/' . $item['avatar'];
  44. return $item;
  45. });
  46. }else if($type == '2'){
  47. $list = $this->model->alias('g')
  48. ->join("yun_user u", "g.pid = u.id", "INNER")
  49. ->with($with)
  50. ->where($where)
  51. ->order($order)
  52. ->field(['g.*'])
  53. ->paginate($limit)->each(function ($item, $key) {
  54. $item['avatar']=$this->startsWithHttp($item['avatar'])?$item['avatar']:request()->domain().'/' . $item['avatar'];
  55. return $item;
  56. });
  57. }
  58. $result = ['total' => $list->total(), 'rows' => $list->items(), 'ceshi' => $type];
  59. return json($result);
  60. }
  61. /**
  62. * 添加
  63. */
  64. #[Route('GET,POST', 'add')]
  65. public function add()
  66. {
  67. if (false === $this->request->isPost()) {
  68. return $this->fetch();
  69. }
  70. $params = array_merge($this->request->post("row/a"), $this->postParams);
  71. if (empty($params)) {
  72. $this->error(__('提交的参数不能为空'));
  73. }
  74. if (!$this->request->checkToken('__token__', ['__token__' => $this->request->post('__token__')])) {
  75. $this->error(__('token错误,请刷新页面重试'));
  76. }
  77. foreach ($params as &$value) {
  78. if (is_array($value)) {
  79. $value = implode(',', $value);
  80. }
  81. if ($value === '') {
  82. $value = null;
  83. }
  84. }
  85. $result = false;
  86. Db::startTrans();
  87. try {
  88. $params['salt'] = str_rand(4);
  89. $params['password'] = md5(md5($params['password'] . $params['salt']));
  90. $params['avatar'] = empty($params['avatar']) ? '/assets/img/logo.jpg' : $params['avatar']; //默认头像
  91. $result = $this->model->save($params);
  92. if ($this->callback) {
  93. $callback = $this->callback;
  94. $callback($this->model);
  95. }
  96. Db::commit();
  97. } catch (\Exception $e) {
  98. Db::rollback();
  99. $this->error($e->getMessage());
  100. }
  101. if ($result === false) {
  102. $this->error(__('没有新增任何数据'));
  103. }
  104. $this->success();
  105. }
  106. /**
  107. * 编辑
  108. */
  109. #[Route('GET,POST', 'edit')]
  110. public function edit(mixed $row = null)
  111. {
  112. $ids = $this->request->get('ids');
  113. if (!$row || is_array($row)) {
  114. $row = $this->model->find($ids);
  115. }
  116. if (!$row) {
  117. $this->error(__('没有找到记录'));
  118. }
  119. if (count($this->volidateFields) > 0) {
  120. foreach ($this->volidateFields as $field => $value) {
  121. if ($row[$field] != $value) {
  122. $this->error(__('没有操作权限'));
  123. }
  124. }
  125. }
  126. if (false === $this->request->isPost()) {
  127. $row['password'] = '';
  128. $this->assign('row', $row);
  129. return $this->fetch();
  130. }
  131. $params = array_merge($this->request->post("row/a"), $this->postParams);
  132. if (empty($params)) {
  133. $this->error(__('提交的参数不能为空'));
  134. }
  135. if (!$this->request->checkToken('__token__', ['__token__' => $this->request->post('__token__')])) {
  136. $this->error(__('token错误,请刷新页面重试'));
  137. }
  138. foreach ($params as &$value) {
  139. if (is_array($value)) {
  140. $value = implode(',', $value);
  141. }
  142. if ($value === '') {
  143. $value = null;
  144. }
  145. }
  146. $result = false;
  147. Db::startTrans();
  148. try {
  149. if (!empty($params['password'])) {
  150. $params['salt'] = str_rand(4);
  151. $params['password'] = md5(md5($params['password'] . $params['salt']));
  152. } else {
  153. unset($params['password']);
  154. }
  155. if ($params['type'] == '1' && empty($params['group_name'])) {
  156. $this->error(__('管理身份,必须设置组名'));
  157. }
  158. $params['avatar'] = empty($params['avatar']) ? '/assets/img/logo.jpg' : $params['avatar']; //默认头像
  159. $result = $row->save($params);
  160. if ($this->callback) {
  161. $callback = $this->callback;
  162. $callback($row);
  163. }
  164. Db::commit();
  165. } catch (\Exception $e) {
  166. Db::rollback();
  167. $this->error($e->getMessage());
  168. }
  169. if (false === $result) {
  170. $this->error(__('没有数据被更新'));
  171. }
  172. $this->success();
  173. }
  174. /**
  175. * 列表
  176. */
  177. #[Route('GET,JSON', 'group_list')]
  178. public function group_list()
  179. {
  180. if (false === $this->request->isAjax()) {
  181. return $this->fetch();
  182. }
  183. $pid = (string)$this->request->get('pid', '0');
  184. [$where, $order, $limit, $with] = $this->buildparams();
  185. $where_item = [];
  186. if ($pid != '0') $where_item[] = ['pid', '=', (string)$pid];
  187. $list = $this->model
  188. ->with($with)
  189. ->where($where_item)
  190. ->where($where)
  191. ->order($order)
  192. ->paginate($limit)->each(function ($item, $key) {
  193. $item['avatar']=$this->startsWithHttp($item['avatar'])?$item['avatar']:request()->domain().'/' . $item['avatar'];
  194. return $item;
  195. });
  196. $result = ['total' => $list->total(), 'rows' => $list->items()];
  197. return json($result);
  198. }
  199. // 方法1.1:使用strpos()精准匹配
  200. public function startsWithHttp($url)
  201. {
  202. return strpos($url, 'https://') === 0
  203. || strpos($url, 'http://') === 0;
  204. }
  205. }