User.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. /**
  3. * Created by PhpStorm
  4. * User raingad@foxmail.com
  5. * Date 2022/12/14 17:24
  6. */
  7. namespace app\manage\controller;
  8. use app\BaseController;
  9. use app\enterprise\model\{User as UserModel,GroupUser,Friend};
  10. use app\manage\model\Config;
  11. use think\facade\Db;
  12. class User extends BaseController
  13. {
  14. // 获取用户列表
  15. public function index()
  16. {
  17. $map = [];
  18. $model=new UserModel();
  19. $param = $this->request->param();
  20. //搜索关键词
  21. if ($keyword = $this->request->param('keywords')) {
  22. $model = $model->whereLike('realname|account|name_py|email', '%' . $keyword . '%');
  23. }
  24. // 排序
  25. $order='user_id DESC';
  26. if ($param['order_field'] ?? '') {
  27. $order = orderBy($param['order_field'],$param['order_type'] ?? 1);
  28. }
  29. $list = $this->paginate($model->where($map)->order($order));
  30. if ($list) {
  31. $data = $list->toArray()['data'];
  32. foreach($data as $k=>$v){
  33. $data[$k]['avatar']=avatarUrl($v['avatar'],$v['realname'],$v['user_id'],120);
  34. $data[$k]['location']=$v['last_login_ip'] ? implode(" ", \Ip::find($v['last_login_ip'])) : '--';
  35. $data[$k]['reg_location']=$v['register_ip'] ? implode(" ", \Ip::find($v['register_ip'])) : '--';
  36. $data[$k]['last_login_time']=$v['last_login_time'] ? date('Y-m-d H:i:s',$v['last_login_time']) : '--';
  37. unset($data[$k]['password']);
  38. }
  39. }
  40. return success('', $data, $list->total(), $list->currentPage());
  41. }
  42. // 添加用户
  43. public function add()
  44. {
  45. try{
  46. $data = $this->request->param();
  47. $user=new UserModel();
  48. $verify=$user->checkAccount($data);
  49. if(!$verify){
  50. return warning($user->getError());
  51. }
  52. $salt=\utils\Str::random(4);
  53. $data['password'] = password_hash_tp($data['password'],$salt);
  54. $data['salt'] =$salt;
  55. $data['register_ip'] =$this->request->ip();
  56. $data['name_py'] = pinyin_sentence($data['realname']);
  57. $user->save($data);
  58. $data['user_id']=$user->user_id;
  59. return success('添加成功', $data);
  60. }catch (\Exception $e){
  61. return error('添加失败');
  62. }
  63. }
  64. // 修改用户
  65. public function edit()
  66. {
  67. try{
  68. $data = $this->request->param();
  69. $user=new UserModel();
  70. $verify=$user->checkAccount($data);
  71. if(!$verify){
  72. return warning($user->getError());
  73. }
  74. $user=UserModel::find($data['user_id']);
  75. $user->account =$data['account'];
  76. $user->realname =$data['realname'];
  77. $user->email =$data['email'];
  78. $user->remark=$data['remark'];
  79. $user->sex =$data['sex'];
  80. // 只有超管才能设置管理员
  81. if($this->userInfo['user_id']==1){
  82. $user->role =$data['role'];
  83. }
  84. $user->status =$data['status'];
  85. $user->name_py= pinyin_sentence($data['realname']);
  86. $user->save();
  87. return success('修改成功', $data);
  88. }catch (\Exception $e){
  89. return error('修改失败');
  90. }
  91. }
  92. // 删除用户
  93. public function del()
  94. {
  95. $user_id = $this->request->param('user_id');
  96. $user=UserModel::find($user_id);
  97. if(!$user || $user->user_id==1){
  98. return warning('用户不存在');
  99. }
  100. Db::startTrans();
  101. try{
  102. // 删除其好友关系
  103. Friend::where('create_user', $user_id)->whereOr(['friend_user_id'=>$user_id])->delete();
  104. // 删除其群组关系
  105. GroupUser::where('user_id', $user_id)->delete();
  106. UserModel::destroy($user_id);
  107. Db::commit();
  108. return success('删除成功');
  109. }catch (\Exception $e){
  110. Db::rollback();
  111. return error($e->getMessage());
  112. }
  113. }
  114. // 修改用户状态
  115. public function setStatus()
  116. {
  117. $user_id = $this->request->param('user_id');
  118. $user=UserModel::find($user_id);
  119. if(!$user){
  120. return warning('用户不存在');
  121. }
  122. try{
  123. $status = $this->request->param('status',0);
  124. UserModel::where('user_id', $user_id)->update(['status'=>$status]);
  125. return success('修改成功');
  126. }catch (\Exception $e){
  127. return error('修改失败');
  128. }
  129. }
  130. // 获取用户信息
  131. public function detail()
  132. {
  133. $user_id = $this->request->param('user_id');
  134. $user=UserModel::find($user_id);
  135. if(!$user){
  136. return error('用户不存在');
  137. }
  138. $user->avatar=avatarUrl($user->avatar,$user->realname,$user->user_id,120);
  139. $location='';
  140. if($user->last_login_ip){
  141. $location=implode(" ", \Ip::find($user->last_login_ip));
  142. }
  143. $user->location=$location;
  144. $user->password='';
  145. return success('', $user);
  146. }
  147. // 设置用户角色
  148. public function setRole()
  149. {
  150. $user_id = $this->request->param('user_id');
  151. $user=UserModel::find($user_id);
  152. if(!$user){
  153. return warning('用户不存在');
  154. }
  155. try{
  156. $role = $this->request->param('role');
  157. UserModel::where('user_id', $user_id)->update(['role'=>$role]);
  158. return success('修改成功');
  159. }catch (\Exception $e){
  160. return error('修改失败');
  161. }
  162. }
  163. // 修改密码
  164. public function editPassword()
  165. {
  166. $user_id = $this->request->param('user_id');
  167. $user=UserModel::find($user_id);
  168. if(!$user){
  169. return warning('用户不存在');
  170. }
  171. try{
  172. $password = $this->request->param('password','');
  173. if($password){
  174. $salt=$user->salt;
  175. $user->password= password_hash_tp($password,$salt);
  176. }
  177. $user->save();
  178. return success('修改成功');
  179. }catch (\Exception $e){
  180. return error('修改失败');
  181. }
  182. }
  183. }