SystemUserLogic.php 9.1 KB

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