SystemUserLogic.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | saiadmin [ saiadmin快速开发框架 ]
  4. // +----------------------------------------------------------------------
  5. // | Author: sai <1430792918@qq.com>
  6. // +----------------------------------------------------------------------
  7. namespace plugin\saiadmin\app\logic\system;
  8. use plugin\saiadmin\app\cache\UserInfoCache;
  9. use plugin\saiadmin\app\model\system\SystemDept;
  10. use plugin\saiadmin\app\model\system\SystemRole;
  11. use plugin\saiadmin\app\model\system\SystemUser;
  12. use plugin\saiadmin\exception\ApiException;
  13. use plugin\saiadmin\basic\BaseLogic;
  14. use Webman\Event\Event;
  15. use Tinywan\Jwt\JwtToken;
  16. /**
  17. * 用户信息逻辑层
  18. */
  19. class SystemUserLogic extends BaseLogic
  20. {
  21. /**
  22. * 构造函数
  23. */
  24. public function __construct()
  25. {
  26. $this->model = new SystemUser();
  27. }
  28. /**
  29. * 列表
  30. */
  31. public function getList($query): mixed
  32. {
  33. $saiType = request()->input('saiType', 'list');
  34. $page = request()->input('page', 1);
  35. $limit = request()->input('limit', 10);
  36. $orderBy = request()->input('orderBy', '');
  37. $orderType = request()->input('orderType', $this->orderType);
  38. if(empty($orderBy)) {
  39. $orderBy = $this->orderField !== '' ? $this->orderField : $this->model->getPk();
  40. }
  41. // 如果排序字段是id,明确指定表名避免歧义
  42. if($orderBy === 'id') {
  43. $orderBy = 'sa_system_user.id';
  44. }
  45. $query->field([
  46. 'sa_system_user.id' => 'id',
  47. 'sa_system_user.nickname' => 'nickname',
  48. 'sa_system_user.username' => 'username',
  49. 'sa_system_user.phone' => 'phone',
  50. 'sa_system_user.email' => 'email',
  51. 'sa_system_user.status' => 'status',
  52. 'sa_system_user.dept_id' => 'dept_id',
  53. 'sa_system_user.create_time' => 'create_time',
  54. 'sa_system_dept.name' => 'dept_name',
  55. 'GROUP_CONCAT(sa_system_user_role.role_id)' => 'role_id',
  56. 'GROUP_CONCAT(sa_system_role.name)' => 'role_name'
  57. ]);
  58. $query->leftJoin('sa_system_user_role', 'sa_system_user.id = sa_system_user_role.user_id');
  59. $query->leftJoin('sa_system_dept', 'sa_system_user.dept_id = sa_system_dept.id');
  60. $query->leftJoin('sa_system_role', 'sa_system_user_role.role_id = sa_system_role.id');
  61. $query->group('sa_system_user.id');
  62. $query->order($orderBy, $orderType);
  63. if ($saiType === 'all') {
  64. return $query->toArray();
  65. }
  66. return $query->paginate($limit, false, ['page' => $page])->toArray();
  67. }
  68. /**
  69. * 读取数据
  70. * @param $id
  71. * @return array
  72. */
  73. public function read($id): array
  74. {
  75. $admin = $this->model->findOrEmpty($id);
  76. $data = $admin->hidden(['password'])->toArray();
  77. $data['roleList'] = $admin->roles->toArray() ?: [];
  78. $data['postList'] = $admin->posts->toArray() ?: [];
  79. $data['deptList'] = $admin->depts ? $admin->depts->toArray() : [];
  80. if ($this->adminInfo['id'] > 1) {
  81. // 判断部门id是否有操作权限
  82. $dept_ids = SystemDept::whereRaw('FIND_IN_SET("'.$this->adminInfo['dept_id'].'", level) > 0')->column('id');
  83. if (!in_array($admin['dept_id'], $dept_ids)) {
  84. throw new ApiException('没有权限操作该部门数据');
  85. }
  86. }
  87. return $data;
  88. }
  89. /**
  90. * 添加数据
  91. * @param $data
  92. * @return mixed
  93. */
  94. public function add($data): mixed
  95. {
  96. $data['password'] = password_hash($data['password'], PASSWORD_DEFAULT);
  97. return $this->transaction(function () use ($data) {
  98. $role_ids = $data['role_ids'] ?? [];
  99. $post_ids = $data['post_ids'] ?? [];
  100. if ($this->adminInfo['id'] > 1) {
  101. // 1、判断部门id是否有操作权限
  102. $dept_ids = SystemDept::whereRaw('FIND_IN_SET("' . $this->adminInfo['dept_id'] . '", level) > 0')->column('id');
  103. if (!in_array($data['dept_id'], $dept_ids)) {
  104. throw new ApiException('没有权限操作该部门数据');
  105. }
  106. // 2、判断角色id是否有操作权限
  107. $roleIds = [];
  108. foreach ($this->adminInfo['roleList'] as $item) {
  109. $temp = SystemRole::whereRaw('FIND_IN_SET("' . $item['id'] . '", level) > 0')->column('id');
  110. $roleIds = array_merge($roleIds, $temp);
  111. }
  112. if (count(array_diff($role_ids, $roleIds)) > 0) {
  113. throw new ApiException('没有权限操作该角色数据');
  114. }
  115. }
  116. $user = SystemUser::create($data);
  117. $user->roles()->detach();
  118. $user->posts()->detach();
  119. $user->roles()->saveAll($role_ids);
  120. if (!empty($post_ids)) {
  121. $user->posts()->save($post_ids);
  122. }
  123. return $user->getKey();
  124. });
  125. }
  126. /**
  127. * 修改数据
  128. * @param $id
  129. * @param $data
  130. * @return mixed
  131. */
  132. public function edit($id, $data): mixed
  133. {
  134. unset($data['password']);
  135. return $this->transaction(function () use ($data, $id) {
  136. $role_ids = $data['role_ids'] ?? [];
  137. $post_ids = $data['post_ids'] ?? [];
  138. // 1、判断用户是否可以操作
  139. $query = $this->model->where('id', $id);
  140. $query->auth([
  141. 'id' => $this->adminInfo['id'],
  142. 'dept' => $this->adminInfo['deptList']
  143. ]);
  144. $user = $query->findOrEmpty();
  145. if ($user->isEmpty()) {
  146. throw new ApiException('没有权限操作该数据');
  147. }
  148. if ($this->adminInfo['id'] > 1) {
  149. // 2、判断部门id是否有操作权限
  150. $dept_ids = SystemDept::whereRaw('FIND_IN_SET("' . $this->adminInfo['dept_id'] . '", level) > 0')->column('id');
  151. if (!in_array($data['dept_id'], $dept_ids)) {
  152. throw new ApiException('没有权限操作该部门数据');
  153. }
  154. // 3、判断角色id是否有操作权限
  155. $roleIds = [];
  156. foreach ($this->adminInfo['roleList'] as $item) {
  157. $temp = SystemRole::whereRaw('FIND_IN_SET("' . $item['id'] . '", level) > 0')->column('id');
  158. $roleIds = array_merge($roleIds, $temp);
  159. }
  160. if (count(array_diff($role_ids, $roleIds)) > 0) {
  161. throw new ApiException('没有权限操作该角色数据');
  162. }
  163. }
  164. // 如果修改了部门,则清空权限
  165. if ($user->dept_id != $data['dept_id']) {
  166. $data['game_list'] = null;
  167. $data['normal_game_list'] = null;
  168. $data['ad_permission'] = null;
  169. }
  170. $result = parent::edit($id, $data);
  171. if ($result) {
  172. $user->roles()->detach();
  173. $user->posts()->detach();
  174. $user->roles()->saveAll($role_ids);
  175. if (!empty($post_ids)) {
  176. $user->posts()->save($post_ids);
  177. }
  178. $userInfoCache = new UserInfoCache($id);
  179. $userInfoCache->clearUserInfo();
  180. }
  181. return $result;
  182. });
  183. }
  184. /**
  185. * 删除数据
  186. * @param $ids
  187. */
  188. public function destroy($ids)
  189. {
  190. if (is_array($ids)) {
  191. if (count($ids) > 1) {
  192. throw new ApiException('禁止批量删除操作');
  193. }
  194. $ids = $ids[0];
  195. }
  196. if ($ids == 1) {
  197. throw new ApiException('超级管理员禁止删除');
  198. }
  199. $query = $this->model->where('id', $ids);
  200. $query->auth([
  201. 'id' => $this->adminInfo['id'],
  202. 'dept' => $this->adminInfo['deptList']
  203. ]);
  204. $user = $query->findOrEmpty();
  205. if ($user->isEmpty()) {
  206. throw new ApiException('没有权限操作该数据');
  207. }
  208. $userInfoCache = new UserInfoCache($ids);
  209. $userInfoCache->clearUserInfo();
  210. parent::destroy($ids);
  211. }
  212. /**
  213. * 用户登录
  214. * @param string $username
  215. * @param string $password
  216. * @param string $type
  217. * @return array
  218. */
  219. public function login(string $username, string $password, string $type): array
  220. {
  221. $adminInfo = $this->model->where('username', $username)->findOrEmpty();
  222. $status = 1;
  223. $message = '登录成功';
  224. if ($adminInfo->isEmpty()) {
  225. $message = '账号或密码错误,请重新输入!';
  226. throw new ApiException($message);
  227. }
  228. if ($adminInfo->status === 2) {
  229. $status = 0;
  230. $message = '您已被禁止登录!';
  231. }
  232. if (!password_verify($password, $adminInfo->password)) {
  233. $status = 0;
  234. $message = '账号或密码错误,请重新输入!';
  235. }
  236. if ($status === 0) {
  237. // 登录事件
  238. Event::emit('user.login', compact('username','status','message'));
  239. throw new ApiException($message);
  240. }
  241. $adminInfo->login_time = date('Y-m-d H:i:s');
  242. $adminInfo->login_ip = request()->getRealIp();
  243. $adminInfo->save();
  244. $token = JwtToken::generateToken([
  245. 'id' => $adminInfo->id,
  246. 'username' => $adminInfo->username,
  247. 'type' => $type
  248. ]);
  249. // 登录事件
  250. $admin_id = $adminInfo->id;
  251. Event::emit('user.login', compact('username','status','message','admin_id'));
  252. return $token;
  253. }
  254. /**
  255. * 密码修改
  256. * @param $adminId
  257. * @param $oldPassword
  258. * @param $newPassword
  259. * @return bool
  260. */
  261. public function modifyPassword($adminId, $oldPassword, $newPassword): bool
  262. {
  263. $model = $this->model->findOrEmpty($adminId);
  264. if (password_verify($oldPassword, $model->password)) {
  265. $model->password = password_hash($newPassword, PASSWORD_DEFAULT);
  266. return $model->save();
  267. } else {
  268. throw new ApiException('原密码错误');
  269. }
  270. }
  271. /**
  272. * 修改数据
  273. */
  274. public function authEdit($id, $data)
  275. {
  276. if ($this->adminInfo['id'] > 1) {
  277. // 判断用户是否可以操作
  278. $query = SystemUser::where('id', $id);
  279. $query->auth([
  280. 'id' => $this->adminInfo['id'],
  281. 'dept' => $this->adminInfo['deptList']
  282. ]);
  283. $user = $query->findOrEmpty();
  284. if ($user->isEmpty()) {
  285. throw new ApiException('没有权限操作该数据');
  286. }
  287. }
  288. parent::edit($id, $data);
  289. }
  290. }