UserService.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <?php
  2. namespace app\admin\service\sys_manage;
  3. use app\model\Member;
  4. use app\model\SysUser;
  5. use support\Db;
  6. use support\Request;
  7. class UserService
  8. {
  9. /**
  10. * @Desc 用户列表
  11. * @Author Gorden
  12. * @Date 2024/2/21 14:46
  13. *
  14. * @param $page
  15. * @param $limit
  16. * @param $keywords
  17. * @return \support\Response
  18. */
  19. public static function userList(Request $request)
  20. {
  21. $page = $request->get('page', 1);
  22. $pageSize = $request->get('pageSize', 10);
  23. $username = $request->get('username', '');
  24. $loginName = $request->get('login_name', '');
  25. $rows = SysUser::with(['role','dept'])
  26. ->select('join_user_role_id', 'join_user_dept_id', 'user_id', 'user_status', 'user_category', 'user_name', 'user_login_name', 'user_mobile', 'user_remark', 'user_extend_json', 'user_addtimes')
  27. ->when($username != '', function ($query) use ($username) {
  28. $query->where('user_name', 'like', '%' . $username . '%');
  29. })->when($loginName != '', function ($query) use ($loginName) {
  30. $query->where('user_login_name', 'like', '%' . $loginName . '%');
  31. })
  32. ->orderBy('user_addtimes', 'DESC')
  33. ->forPage($page, $pageSize)
  34. ->get()
  35. ->toArray();
  36. foreach($rows as &$item){
  37. if(!empty($item['user_extend_json'])){
  38. $extendJson = json_decode($item['user_extend_json'],true);
  39. $item['avatar'] = !empty($extendJson['avatar']) ? getenv('STORAGE_DOMAIN').$extendJson['avatar'] : '';
  40. $item['info'] = $extendJson['info'] ?? '';
  41. }
  42. }
  43. $total = SysUser::when($username != '', function ($query) use ($username) {
  44. $query->where('user_name', 'like', '%' . $username . '%');
  45. })->count();
  46. return json_success('', compact('rows', 'page', 'pageSize', 'total'));
  47. }
  48. public static function selectTeacherList(Request $request)
  49. {
  50. $roleId = "28";
  51. $user = SysUser::where('join_user_role_id',$roleId)
  52. ->select('user_id','user_name')
  53. ->get()
  54. ->toArray();
  55. return json_success('',$user);
  56. }
  57. /**
  58. * @Desc 用户详情
  59. * @Author Gorden
  60. * @Date 2024/2/21 14:52
  61. *
  62. * @param $id
  63. * @return \support\Response
  64. */
  65. public static function userInfo($id)
  66. {
  67. $user = SysUser::select('join_user_role_id', 'join_user_dept_id', 'user_status', 'user_category', 'user_name', 'user_login_name', 'user_mobile', 'user_remark', 'user_extend_json', 'user_addtimes')
  68. ->where('user_id', $id)
  69. ->first();
  70. if (!$user) {
  71. return json_fail('用户不存在');
  72. }
  73. $user = $user->toArray();
  74. $member = Member::with([
  75. 'cert'=>function($query){
  76. $query->select('join_cert_member_id','member_cert_name');
  77. }
  78. ])->whereJsonContains('member_json->user->user_id',$id)
  79. ->first();
  80. $memberTitle = $member->cert ? $member->cert->member_cert_name.'-'.$member->member_mobile : $member->member_mobile;
  81. $user['member_id'] = $member['member_id'];
  82. $user['memberList'] = [
  83. [
  84. 'key'=>$member->member_id,
  85. 'value'=>$memberTitle
  86. ]
  87. ];
  88. $user['permission'] = [];
  89. if(!empty($member->member_json)){
  90. $memberJson = json_decode($member->member_json,true);
  91. if(isset($memberJson['user']) && isset($memberJson['user']['permission'])){
  92. $user['permission'] = $memberJson['user']['permission'];
  93. }
  94. }
  95. return json_success('', $user);
  96. }
  97. /**
  98. * @Desc 添加用户
  99. * @Author Gorden
  100. * @Date 2024/2/21 13:50
  101. *
  102. * @param $params
  103. * @return \support\Response
  104. */
  105. public static function insertUser($params)
  106. {
  107. try {
  108. $data = [
  109. 'user_id' => self::generateUserId(),
  110. 'join_user_role_id' => $params['join_user_role_id'] ?? 0,
  111. 'join_user_dept_id' => $params['join_user_dept_id'] ?? 0,
  112. 'user_status' => $params['user_status'] ?? '',
  113. 'user_category' => $params['user_category'] ?? '',
  114. 'user_name' => $params['user_name'] ?? '',
  115. 'user_login_name' => $params['user_login_name'] ?? '',
  116. 'user_login_pass' => md5(sha1($params['user_login_pass'])),
  117. 'user_mobile' => $params['user_mobile'] ?? '',
  118. 'user_remark' => $params['user_remark'] ?? '',
  119. // 'user_extend_json' => !empty($params['user_extend_json']) ? $params['user_extend_json'] : '{}',
  120. 'user_addtimes' => time()
  121. ];
  122. $extendJson['avatar'] = str_replace(getenv('STORAGE_DOMAIN'),'',$params['avatar']);
  123. $extendJson['info'] = $params['info'] ?? '[]';
  124. $data['user_extend_json'] = json_encode($extendJson);
  125. if (!SysUser::insert($data)) {
  126. throw new \Exception('添加用户失败');
  127. }
  128. } catch (\Exception $e) {
  129. dump($e->getMessage());
  130. return json_fail('添加用户失败');
  131. }
  132. return json_success('添加用户成功');
  133. }
  134. /**
  135. * @Desc 修改用户
  136. * @Author Gorden
  137. * @Date 2024/2/21 14:06
  138. *
  139. * @param $id
  140. * @param $params
  141. * @return \support\Response
  142. */
  143. public static function updateUser($id, $params)
  144. {
  145. try {
  146. $data = [
  147. 'join_user_role_id' => $params['join_user_role_id'],
  148. 'join_user_dept_id' => $params['join_user_dept_id'],
  149. 'user_login_name' => $params['user_login_name'],
  150. 'user_status' => $params['user_status'],
  151. 'user_category' => $params['user_category'],
  152. 'user_name' => $params['user_name'],
  153. 'user_remark' => $params['user_remark'],
  154. // 'user_extend_json' => !empty($params['user_extend_json']) ? $params['user_extend_json'] : '{}'
  155. ];
  156. $user = SysUser::where('user_id', $id)->first();
  157. if(!empty($user->user_extend_json)){
  158. $extendJson = json_decode($user->user_extend_json,true);
  159. }else{
  160. $extendJson = [];
  161. }
  162. $extendJson['avatar'] = str_replace(getenv('STORAGE_DOMAIN'),'',$params['avatar']);
  163. $extendJson['info'] = $params['info'];
  164. $data['user_extend_json'] = json_encode($extendJson);
  165. // 密码变动
  166. if (!empty($params['user_login_pass'])) {
  167. $data['user_login_pass'] = md5(sha1($params['user_login_pass']));
  168. }
  169. SysUser::where('user_id', $id)->update($data);
  170. } catch (\Exception $e) {
  171. dump($e->getMessage());
  172. return json_fail('修改用户失败');
  173. }
  174. return json_success('修改用户成功');
  175. }
  176. /**
  177. * @Desc 修改用户状态
  178. * @Author Gorden
  179. * @Date 2024/2/21 15:03
  180. *
  181. * @param $id
  182. * @param $params
  183. * @return \support\Response
  184. */
  185. public static function updateStatus($id, $params)
  186. {
  187. try {
  188. if (!SysUser::where('user_id', $id)->update(['user_status' => $params['user_status']])) {
  189. throw new \Exception('用户状态修改失败');
  190. }
  191. } catch (\Exception $e) {
  192. return json_fail('用户状态修改失败');
  193. }
  194. return json_success('用户状态修改成功');
  195. }
  196. /**
  197. * @Desc 修改密码
  198. * @Author Gorden
  199. * @Date 2024/4/19 10:01
  200. *
  201. * @param $id
  202. * @param $params
  203. * @return \support\Response
  204. */
  205. public static function updatePassword($id, $params)
  206. {
  207. if ($params['user_login_pass'] != $params['confirmNewPassword']) {
  208. return json_fail('两次密码不一致');
  209. }
  210. $user = SysUser::where('user_id', $id)->first();
  211. if (md5(sha1($params['userPassword'])) != $user->user_login_pass) {
  212. return json_fail('原密码错误');
  213. }
  214. try {
  215. SysUser::where('user_id', $id)->update(['user_login_pass' => md5(sha1($params['user_login_pass']))]);
  216. return json_success('密码修改成功');
  217. } catch (\Exception $e) {
  218. dump($e->getMessage());
  219. return json_fail('密码修改失败');
  220. }
  221. }
  222. /**
  223. * @Desc 删除用户
  224. * @Author Gorden
  225. * @Date 2024/2/21 15:11
  226. *
  227. * @param $id
  228. * @return \support\Response
  229. */
  230. public static function delUser(Request $request)
  231. {
  232. $ids = $request->post('user_id');
  233. if (!$ids) {
  234. return json_fail("数据错误");
  235. }
  236. try {
  237. if (is_array($ids)) {
  238. SysUser::whereIn('user_id', $ids)->delete();
  239. } else {
  240. SysUser::where('user_id', $ids)->delete();
  241. }
  242. } catch (\Exception $e) {
  243. return json_fail('删除用户失败');
  244. }
  245. return json_success('删除用户成功');
  246. }
  247. public static function checkUserExist($account)
  248. {
  249. return SysUser::where('user_login_name', $account)->exists();
  250. }
  251. public static function generateUserId()
  252. {
  253. $random = random_string(8,'up');
  254. return 'UR' . date('YmdHis') . $random;
  255. }
  256. }