GroupUser.php 7.5 KB

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