UserService.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. $user['manage'] = [];
  90. if(!empty($member->member_json)){
  91. $memberJson = json_decode($member->member_json,true);
  92. if(isset($memberJson['user']) && isset($memberJson['user']['permission'])){
  93. $user['permission'] = $memberJson['user']['permission'];
  94. }
  95. if(isset($memberJson['manage'])){
  96. $user['manage'] = $memberJson['manage'];
  97. }
  98. }
  99. return json_success('', $user);
  100. }
  101. /**
  102. * @Desc 添加用户
  103. * @Author Gorden
  104. * @Date 2024/2/21 13:50
  105. *
  106. * @param $params
  107. * @return \support\Response
  108. */
  109. public static function insertUser($params)
  110. {
  111. try {
  112. $data = [
  113. 'user_id' => self::generateUserId(),
  114. 'join_user_role_id' => $params['join_user_role_id'] ?? 0,
  115. 'join_user_dept_id' => $params['join_user_dept_id'] ?? 0,
  116. 'user_status' => $params['user_status'] ?? '',
  117. 'user_category' => $params['user_category'] ?? '',
  118. 'user_name' => $params['user_name'] ?? '',
  119. 'user_login_name' => $params['user_login_name'] ?? '',
  120. 'user_login_pass' => md5(sha1($params['user_login_pass'])),
  121. 'user_mobile' => $params['user_mobile'] ?? '',
  122. 'user_remark' => $params['user_remark'] ?? '',
  123. // 'user_extend_json' => !empty($params['user_extend_json']) ? $params['user_extend_json'] : '{}',
  124. 'user_addtimes' => time()
  125. ];
  126. $extendJson['avatar'] = str_replace(getenv('STORAGE_DOMAIN'),'',$params['avatar']);
  127. $extendJson['info'] = $params['info'] ?? '[]';
  128. $data['user_extend_json'] = json_encode($extendJson);
  129. if (!SysUser::insert($data)) {
  130. throw new \Exception('添加用户失败');
  131. }
  132. } catch (\Exception $e) {
  133. dump($e->getMessage());
  134. return json_fail('添加用户失败');
  135. }
  136. return json_success('添加用户成功');
  137. }
  138. /**
  139. * @Desc 修改用户
  140. * @Author Gorden
  141. * @Date 2024/2/21 14:06
  142. *
  143. * @param $id
  144. * @param $params
  145. * @return \support\Response
  146. */
  147. public static function updateUser($id, $params)
  148. {
  149. try {
  150. $data = [
  151. 'join_user_role_id' => $params['join_user_role_id'],
  152. 'join_user_dept_id' => $params['join_user_dept_id'],
  153. 'user_login_name' => $params['user_login_name'],
  154. 'user_status' => $params['user_status'],
  155. 'user_category' => $params['user_category'],
  156. 'user_name' => $params['user_name'],
  157. 'user_remark' => $params['user_remark'],
  158. // 'user_extend_json' => !empty($params['user_extend_json']) ? $params['user_extend_json'] : '{}'
  159. ];
  160. $user = SysUser::where('user_id', $id)->first();
  161. if(!empty($user->user_extend_json)){
  162. $extendJson = json_decode($user->user_extend_json,true);
  163. }else{
  164. $extendJson = [];
  165. }
  166. $extendJson['avatar'] = str_replace(getenv('STORAGE_DOMAIN'),'',$params['avatar']);
  167. $extendJson['info'] = $params['info'];
  168. $data['user_extend_json'] = json_encode($extendJson);
  169. // 密码变动
  170. if (!empty($params['user_login_pass'])) {
  171. $data['user_login_pass'] = md5(sha1($params['user_login_pass']));
  172. }
  173. SysUser::where('user_id', $id)->update($data);
  174. } catch (\Exception $e) {
  175. dump($e->getMessage());
  176. return json_fail('修改用户失败');
  177. }
  178. return json_success('修改用户成功');
  179. }
  180. /**
  181. * @Desc 修改用户状态
  182. * @Author Gorden
  183. * @Date 2024/2/21 15:03
  184. *
  185. * @param $id
  186. * @param $params
  187. * @return \support\Response
  188. */
  189. public static function updateStatus($id, $params)
  190. {
  191. try {
  192. if (!SysUser::where('user_id', $id)->update(['user_status' => $params['user_status']])) {
  193. throw new \Exception('用户状态修改失败');
  194. }
  195. } catch (\Exception $e) {
  196. return json_fail('用户状态修改失败');
  197. }
  198. return json_success('用户状态修改成功');
  199. }
  200. /**
  201. * @Desc 修改密码
  202. * @Author Gorden
  203. * @Date 2024/4/19 10:01
  204. *
  205. * @param $id
  206. * @param $params
  207. * @return \support\Response
  208. */
  209. public static function updatePassword($id, $params)
  210. {
  211. if ($params['user_login_pass'] != $params['confirmNewPassword']) {
  212. return json_fail('两次密码不一致');
  213. }
  214. $user = SysUser::where('user_id', $id)->first();
  215. if (md5(sha1($params['userPassword'])) != $user->user_login_pass) {
  216. return json_fail('原密码错误');
  217. }
  218. try {
  219. SysUser::where('user_id', $id)->update(['user_login_pass' => md5(sha1($params['user_login_pass']))]);
  220. return json_success('密码修改成功');
  221. } catch (\Exception $e) {
  222. dump($e->getMessage());
  223. return json_fail('密码修改失败');
  224. }
  225. }
  226. /**
  227. * @Desc 删除用户
  228. * @Author Gorden
  229. * @Date 2024/2/21 15:11
  230. *
  231. * @param $id
  232. * @return \support\Response
  233. */
  234. public static function delUser(Request $request)
  235. {
  236. $ids = $request->post('user_id');
  237. if (!$ids) {
  238. return json_fail("数据错误");
  239. }
  240. try {
  241. if (is_array($ids)) {
  242. SysUser::whereIn('user_id', $ids)->delete();
  243. } else {
  244. SysUser::where('user_id', $ids)->delete();
  245. }
  246. } catch (\Exception $e) {
  247. return json_fail('删除用户失败');
  248. }
  249. return json_success('删除用户成功');
  250. }
  251. public static function checkUserExist($account)
  252. {
  253. return SysUser::where('user_login_name', $account)->exists();
  254. }
  255. public static function generateUserId()
  256. {
  257. $random = random_string(6,'up');
  258. return 'UR' . date('ymdH') . $random;
  259. }
  260. }