UserController.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. namespace app\admin\controller\sys_manage;
  3. use app\admin\service\sys_manage\DeptService;
  4. use app\admin\service\sys_manage\RoleService;
  5. use app\admin\service\sys_manage\UserService;
  6. use app\admin\validate\sys_manage\UserValidate;
  7. use app\model\Member;
  8. use app\model\SysCategory;
  9. use app\model\SysRole;
  10. use app\model\SysUser;
  11. use support\Request;
  12. class UserController
  13. {
  14. /**
  15. * @Desc 用户列表
  16. * @Author Gorden
  17. * @Date 2024/2/21 14:46
  18. *
  19. * @param Request $request
  20. * @return \support\Response
  21. */
  22. public function userList(Request $request)
  23. {
  24. return UserService::userList($request);
  25. }
  26. public function selectTeacherList(Request $request)
  27. {
  28. return UserService::selectTeacherList($request);
  29. }
  30. /**
  31. * @Desc 用户详情
  32. * @Author Gorden
  33. * @Date 2024/2/21 14:53
  34. *
  35. * @param $id
  36. * @return \support\Response
  37. */
  38. public function userInfo($id)
  39. {
  40. return UserService::userInfo($id);
  41. }
  42. /**
  43. * @Desc 添加用户
  44. * @Author Gorden
  45. * @Date 2024/2/21 14:53
  46. *
  47. * @param Request $request
  48. * @return \support\Response
  49. */
  50. public function addUser(Request $request)
  51. {
  52. $validate = new UserValidate();
  53. if (!$validate->scene('add')->check($request->post())) {
  54. return json_fail($validate->getError());
  55. }
  56. // 账号是否已存在
  57. if (UserService::checkUserExist($request->post('user_login_name'))) {
  58. return json_fail('账号已存在');
  59. }
  60. // 部门是否存在
  61. if (!empty($request->post('join_user_dept_id')) && !DeptService::checkDeptExist($request->post('join_user_dept_id'))) {
  62. return json_fail('部门不存在');
  63. }
  64. // 角色是否存在
  65. if (!empty($request->post('join_user_role_id')) && !RoleService::checkRoleExist($request->post('join_user_role_id'))) {
  66. return json_fail('角色不存在');
  67. }
  68. return UserService::insertUser($request->post());
  69. }
  70. /**
  71. * @Desc 修改用户
  72. * @Author Gorden
  73. * @Date 2024/2/21 14:07
  74. *
  75. * @param $id
  76. * @param Request $request
  77. * @return \support\Response
  78. */
  79. public function updateUser($id, Request $request)
  80. {
  81. $validate = new UserValidate();
  82. if (!$validate->scene('update')->check($request->post())) {
  83. return json_fail($validate->getError());
  84. }
  85. // 部门是否存在
  86. if (!empty($request->post('join_user_dept_id')) && !DeptService::checkDeptExist($request->post('join_user_dept_id'))) {
  87. return json_fail('部门不存在');
  88. }
  89. // 角色是否存在
  90. if (!empty($request->post('join_user_role_id')) && !RoleService::checkRoleExist($request->post('join_user_role_id'))) {
  91. return json_fail('角色不存在');
  92. }
  93. return UserService::updateUser($id, $request->post());
  94. }
  95. /**
  96. * @Desc 修改用户状态
  97. * @Author Gorden
  98. * @Date 2024/2/21 15:03
  99. *
  100. * @param $id
  101. * @param Request $request
  102. * @return \support\Response
  103. */
  104. public function updateStatus($id, Request $request)
  105. {
  106. $validate = new UserValidate();
  107. if (!$validate->scene('update_status')->check($request->post())) {
  108. return json_fail($validate->getError());
  109. }
  110. return UserService::updateStatus($id, $request->post());
  111. }
  112. public function updatePassword($id, Request $request)
  113. {
  114. return UserService::updatePassword($id, $request->post());
  115. }
  116. /**
  117. * @Desc 删除用户
  118. * @Author Gorden
  119. * @Date 2024/2/21 15:11
  120. *
  121. * @param $id
  122. * @return \support\Response
  123. */
  124. public function delUser(Request $request)
  125. {
  126. return UserService::delUser($request);
  127. }
  128. public function correlationMember(Request $request)
  129. {
  130. $memberId = $request->post('member_id', '');
  131. $userId = $request->post('user_id', '');
  132. $permission = $request->post('permission', []);
  133. if (!$memberId || !$userId) {
  134. return json_fail('参数异常');
  135. }
  136. try {
  137. $member = Member::where('member_id', $memberId)->first();
  138. $memberJson = [];
  139. if (!empty($member->member_json)) {
  140. $memberJson = json_decode($member->member_json, true);
  141. // if(isset($memberJson['user'])){
  142. // return json_fail('您选择的会员已被绑定,请选择其他会员');
  143. // }
  144. }
  145. $memberJson['user'] = [
  146. 'user_id' => $userId,
  147. 'permission' => $permission
  148. ];
  149. $member->member_json = json_encode($memberJson);
  150. $member->save();
  151. return json_success('success');
  152. } catch (\Exception $e) {
  153. return json_fail('关联会员失败');
  154. }
  155. }
  156. /**
  157. * @return \support\Response
  158. */
  159. public function selectDoctorList()
  160. {
  161. $roleId = SysRole::where('role_name', '医生')->value('role_id');
  162. $user = SysUser::where('join_user_role_id', $roleId)
  163. ->select('user_id','user_name')
  164. ->get();
  165. return json_success('', $user);
  166. }
  167. public function selectSalesmanList(Request $request)
  168. {
  169. $keywords = $request->get('keywords','');
  170. if(!$keywords){
  171. return json_fail('参数异常');
  172. }
  173. $users = SysUser::leftJoin('sys_dept','sys_dept.dept_id','=','sys_user.join_user_dept_id')
  174. ->where(function($query) use ($keywords){
  175. $query->where('sys_user.user_name','like','%'.$keywords.'%')
  176. ->orWhere('sys_user.user_mobile','like','%'.$keywords.'%')
  177. ->orWhere('sys_dept.dept_name','like','%'.$keywords.'%');
  178. })->where('sys_dept.dept_name','<>','')
  179. ->where('sys_dept.dept_category','营业场所')
  180. ->select('sys_user.user_id','sys_user.user_name','sys_user.user_mobile','sys_dept.dept_name')
  181. ->get()
  182. ->toArray();
  183. return json_success('',$users);
  184. }
  185. }