UserController.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. $manage = $request->post('manage',[]);
  134. if (!$memberId || !$userId) {
  135. return json_fail('参数异常');
  136. }
  137. try {
  138. $member = Member::where('member_id', $memberId)->first();
  139. $memberJson = [];
  140. if (!empty($member->member_json)) {
  141. $memberJson = json_decode($member->member_json, true);
  142. // if(isset($memberJson['user'])){
  143. // return json_fail('您选择的会员已被绑定,请选择其他会员');
  144. // }
  145. }
  146. if(!isset($manage['AppAgent']['url'])){
  147. unset($manage['AppAgent']);
  148. }
  149. if(!isset($manage['dayTotalManage']['url'])){
  150. unset($manage['dayTotalManage']);
  151. }
  152. if(!isset($manage['dayTotalPremises']['url'])){
  153. unset($manage['dayTotalPremises']);
  154. }
  155. $memberJson['user'] = [
  156. 'user_id' => $userId,
  157. 'permission' => $permission
  158. ];
  159. if(!empty($manage)){
  160. $memberJson['manage'] = $manage;
  161. }
  162. $member->member_json = json_encode($memberJson);
  163. $member->save();
  164. return json_success('success');
  165. } catch (\Exception $e) {
  166. return json_fail('关联会员失败');
  167. }
  168. }
  169. /**
  170. * @return \support\Response
  171. */
  172. public function selectDoctorList()
  173. {
  174. $roleId = SysRole::where('role_name', '医生')->value('role_id');
  175. $user = SysUser::where('join_user_role_id', $roleId)
  176. ->select('user_id','user_name')
  177. ->get();
  178. return json_success('', $user);
  179. }
  180. public function selectSalesmanList(Request $request)
  181. {
  182. $keywords = $request->get('keywords','');
  183. if(!$keywords){
  184. return json_fail('参数异常');
  185. }
  186. $users = SysUser::leftJoin('sys_dept','sys_dept.dept_id','=','sys_user.join_user_dept_id')
  187. ->where(function($query) use ($keywords){
  188. $query->where('sys_user.user_name','like','%'.$keywords.'%')
  189. ->orWhere('sys_user.user_mobile','like','%'.$keywords.'%')
  190. ->orWhere('sys_dept.dept_name','like','%'.$keywords.'%');
  191. })->where('sys_dept.dept_name','<>','')
  192. ->where('sys_dept.dept_category','营业场所')
  193. ->select('sys_user.user_id','sys_user.user_name','sys_user.user_mobile','sys_dept.dept_name')
  194. ->get()
  195. ->toArray();
  196. return json_success('',$users);
  197. }
  198. }