SystemUserLogic.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. * @param $id
  31. * @return array
  32. */
  33. public function read($id): array
  34. {
  35. $admin = $this->model->findOrEmpty($id);
  36. $data = $admin->hidden(['password'])->toArray();
  37. $data['roleList'] = $admin->roles->toArray() ?: [];
  38. $data['postList'] = $admin->posts->toArray() ?: [];
  39. $data['deptList'] = $admin->depts ? $admin->depts->toArray() : [];
  40. if ($this->adminInfo['id'] > 1) {
  41. // 判断部门id是否有操作权限
  42. $dept_ids = SystemDept::whereRaw('FIND_IN_SET("'.$this->adminInfo['dept_id'].'", level) > 0')->column('id');
  43. if (!in_array($admin['dept_id'], $dept_ids)) {
  44. throw new ApiException('没有权限操作该部门数据');
  45. }
  46. }
  47. return $data;
  48. }
  49. /**
  50. * 添加数据
  51. * @param $data
  52. * @return mixed
  53. */
  54. public function add($data): mixed
  55. {
  56. $data['password'] = password_hash($data['password'], PASSWORD_DEFAULT);
  57. return $this->transaction(function () use ($data) {
  58. $role_ids = $data['role_ids'] ?? [];
  59. $post_ids = $data['post_ids'] ?? [];
  60. if ($this->adminInfo['id'] > 1) {
  61. // 1、判断部门id是否有操作权限
  62. $dept_ids = SystemDept::whereRaw('FIND_IN_SET("' . $this->adminInfo['dept_id'] . '", level) > 0')->column('id');
  63. if (!in_array($data['dept_id'], $dept_ids)) {
  64. throw new ApiException('没有权限操作该部门数据');
  65. }
  66. // 2、判断角色id是否有操作权限
  67. $roleIds = [];
  68. foreach ($this->adminInfo['roleList'] as $item) {
  69. $temp = SystemRole::whereRaw('FIND_IN_SET("' . $item['id'] . '", level) > 0')->column('id');
  70. $roleIds = array_merge($roleIds, $temp);
  71. }
  72. if (count(array_diff($role_ids, $roleIds)) > 0) {
  73. throw new ApiException('没有权限操作该角色数据');
  74. }
  75. }
  76. $user = SystemUser::create($data);
  77. $user->roles()->detach();
  78. $user->posts()->detach();
  79. $user->roles()->saveAll($role_ids);
  80. if (!empty($post_ids)) {
  81. $user->posts()->save($post_ids);
  82. }
  83. return $user->getKey();
  84. });
  85. }
  86. /**
  87. * 修改数据
  88. * @param $id
  89. * @param $data
  90. * @return mixed
  91. */
  92. public function edit($id, $data): mixed
  93. {
  94. unset($data['password']);
  95. return $this->transaction(function () use ($data, $id) {
  96. $role_ids = $data['role_ids'] ?? [];
  97. $post_ids = $data['post_ids'] ?? [];
  98. // 1、判断用户是否可以操作
  99. $query = $this->model->where('id', $id);
  100. $query->auth([
  101. 'id' => $this->adminInfo['id'],
  102. 'dept' => $this->adminInfo['deptList']
  103. ]);
  104. $user = $query->findOrEmpty();
  105. if ($user->isEmpty()) {
  106. throw new ApiException('没有权限操作该数据');
  107. }
  108. if ($this->adminInfo['id'] > 1) {
  109. // 2、判断部门id是否有操作权限
  110. $dept_ids = SystemDept::whereRaw('FIND_IN_SET("' . $this->adminInfo['dept_id'] . '", level) > 0')->column('id');
  111. if (!in_array($data['dept_id'], $dept_ids)) {
  112. throw new ApiException('没有权限操作该部门数据');
  113. }
  114. // 3、判断角色id是否有操作权限
  115. $roleIds = [];
  116. foreach ($this->adminInfo['roleList'] as $item) {
  117. $temp = SystemRole::whereRaw('FIND_IN_SET("' . $item['id'] . '", level) > 0')->column('id');
  118. $roleIds = array_merge($roleIds, $temp);
  119. }
  120. if (count(array_diff($role_ids, $roleIds)) > 0) {
  121. throw new ApiException('没有权限操作该角色数据');
  122. }
  123. }
  124. // 如果修改了部门,则清空权限
  125. if ($user->dept_id != $data['dept_id']) {
  126. $data['game_list'] = null;
  127. $data['normal_game_list'] = null;
  128. $data['ad_permission'] = null;
  129. }
  130. $result = parent::edit($id, $data);
  131. if ($result) {
  132. $user->roles()->detach();
  133. $user->posts()->detach();
  134. $user->roles()->saveAll($role_ids);
  135. if (!empty($post_ids)) {
  136. $user->posts()->save($post_ids);
  137. }
  138. $userInfoCache = new UserInfoCache($id);
  139. $userInfoCache->clearUserInfo();
  140. }
  141. return $result;
  142. });
  143. }
  144. /**
  145. * 删除数据
  146. * @param $ids
  147. */
  148. public function destroy($ids)
  149. {
  150. if (is_array($ids)) {
  151. if (count($ids) > 1) {
  152. throw new ApiException('禁止批量删除操作');
  153. }
  154. $ids = $ids[0];
  155. }
  156. if ($ids == 1) {
  157. throw new ApiException('超级管理员禁止删除');
  158. }
  159. $query = $this->model->where('id', $ids);
  160. $query->auth([
  161. 'id' => $this->adminInfo['id'],
  162. 'dept' => $this->adminInfo['deptList']
  163. ]);
  164. $user = $query->findOrEmpty();
  165. if ($user->isEmpty()) {
  166. throw new ApiException('没有权限操作该数据');
  167. }
  168. $userInfoCache = new UserInfoCache($ids);
  169. $userInfoCache->clearUserInfo();
  170. parent::destroy($ids);
  171. }
  172. /**
  173. * 用户登录
  174. * @param string $username
  175. * @param string $password
  176. * @param string $type
  177. * @return array
  178. */
  179. public function login(string $username, string $password, string $type): array
  180. {
  181. $adminInfo = $this->model->where('username', $username)->findOrEmpty();
  182. $status = 1;
  183. $message = '登录成功';
  184. if ($adminInfo->isEmpty()) {
  185. $message = '账号或密码错误,请重新输入!';
  186. throw new ApiException($message);
  187. }
  188. if ($adminInfo->status === 2) {
  189. $status = 0;
  190. $message = '您已被禁止登录!';
  191. }
  192. if (!password_verify($password, $adminInfo->password)) {
  193. $status = 0;
  194. $message = '账号或密码错误,请重新输入!';
  195. }
  196. if ($status === 0) {
  197. // 登录事件
  198. Event::emit('user.login', compact('username','status','message'));
  199. throw new ApiException($message);
  200. }
  201. $adminInfo->login_time = date('Y-m-d H:i:s');
  202. $adminInfo->login_ip = request()->getRealIp();
  203. $adminInfo->save();
  204. $token = JwtToken::generateToken([
  205. 'id' => $adminInfo->id,
  206. 'username' => $adminInfo->username,
  207. 'type' => $type
  208. ]);
  209. // 登录事件
  210. $admin_id = $adminInfo->id;
  211. Event::emit('user.login', compact('username','status','message','admin_id'));
  212. return $token;
  213. }
  214. /**
  215. * 密码修改
  216. * @param $adminId
  217. * @param $oldPassword
  218. * @param $newPassword
  219. * @return bool
  220. */
  221. public function modifyPassword($adminId, $oldPassword, $newPassword): bool
  222. {
  223. $model = $this->model->findOrEmpty($adminId);
  224. if (password_verify($oldPassword, $model->password)) {
  225. $model->password = password_hash($newPassword, PASSWORD_DEFAULT);
  226. return $model->save();
  227. } else {
  228. throw new ApiException('原密码错误');
  229. }
  230. }
  231. /**
  232. * 修改数据
  233. */
  234. public function authEdit($id, $data)
  235. {
  236. if ($this->adminInfo['id'] > 1) {
  237. // 判断用户是否可以操作
  238. $query = SystemUser::where('id', $id);
  239. $query->auth([
  240. 'id' => $this->adminInfo['id'],
  241. 'dept' => $this->adminInfo['deptList']
  242. ]);
  243. $user = $query->findOrEmpty();
  244. if ($user->isEmpty()) {
  245. throw new ApiException('没有权限操作该数据');
  246. }
  247. }
  248. parent::edit($id, $data);
  249. }
  250. }