find($this->userinfo['id']); return $this->success('ok', $user); } //编辑用户 public function edit(GroupUserModel $userModel) { $data = $this->request->post(); try { validate(UserValidate::class)->scene('edit')->check($data); $user = $userModel->find($this->userinfo['id']); if (!$user) { return $this->error('用户不存在'); } $user->save($data); return $this->success('ok'); } catch (ValidateException $e) { return $this->error($e->getError()); } } //修改密码 public function password(GroupUserModel $userModel) { $data = $this->request->post(); try { validate(UserValidate::class)->scene('password')->check($data); $user = $userModel->find($this->userinfo['id']); if (!$user) { return $this->error('用户不存在'); } if ($user->password != md5(md5($data['oldpassword'] . $user->salt))) { return $this->error('旧密码错误'); } $user->password = md5(md5($data['password'] . $user->salt)); $user->save(); return $this->success('ok'); } catch (ValidateException $e) { return $this->error($e->getError()); } } public function login(ApiAuthGroupService $authService, GroupUserModel $userModel) { $data = $this->request->post(); try { validate(UserValidate::class)->scene('login')->check($data); $terminal = $this->request->post('terminal/d', 0); $user = $authService->login($data['username'], $data['password'], $terminal); } catch (ValidateException $e) { return $this->error($e->getError()); } catch (\Exception $e) { return $this->error($e->getMessage()); } return $this->success('ok', $user); } //退出登录 public function logout() { GroupMysqlAdapter::logout($this->userinfo['id']); return $this->success('ok'); } //获取全部人员 public function getAllUser(GroupUserModel $userModel) { $list = $userModel->where('status', 'normal')->field('id,nickname,avatar')->select(); return $this->success('ok', $list); } //组员列表 public function group_list(GroupUserModel $userModel) { $list = $userModel->where('status', 'normal')->where('pid', $this->userinfo['id'])->select(); return $this->success('ok', $list); } //添加组员 public function add_group_user(GroupUserModel $groupUserModel,UserModel $userModel) { $file = request()->file('avatar'); $param = $this->request->post(); try { validate(UserValidate::class)->scene('add_group_user')->check($param); $todayTime = strtotime('today'); //判断手机号和身份证号是否注册今天是否添加到该团队 $where_item_id_card = []; $where_item_id_card[]=['pid', '=',$this->userinfo['id']]; $where_item_id_card[]=['id_card', '=',$param['id_card']]; $where_item_id_card[]=['createtime', '>=',$todayTime]; $group_user_data = $groupUserModel->where($where_item_id_card)->find(); if(!empty($group_user_data)) return $this->error('该身份证号已添加',$param['id_card']); $where_item_mobile = []; $where_item_mobile[]=['pid', '=',$this->userinfo['id']]; $where_item_mobile[]=['mobile', '=',$param['mobile']]; $where_item_mobile[]=['createtime', '>=',$todayTime]; $group_user_data = $groupUserModel->where($where_item_mobile)->find(); if(!empty($group_user_data)) return $this->error('该手机号已添加'); $user = $userModel->find($this->userinfo['id']); $pid=$user['id']; $time=time(); $add_data=[ 'pid'=>$pid, 'nickname'=>$param['nickname'], 'id_card'=>$param['id_card'], 'avatar'=>'', 'mobile'=>$param['mobile'], 'sex'=>$param['sex'], 'role'=>'1', 'status'=>'normal', 'joinip'=>request()->ip(), 'jointime'=>$time ]; if (!empty($file)) { // 文件夹 $folder = strtotime('today'); $fileName = time() . '_' . mt_rand(1000, 9999) . '.png'; $avatar = $file->move('uploads/avatar/' . $folder, $fileName); //检查目标文件是否存在 if (!empty($avatar)) { //有图片上传,进行添加用户 $add_data['avatar']=$avatar; $res=$groupUserModel->save($add_data); if($res) return $this->success('添加成功',$res); } } } catch (ValidateException $e) { return $this->error($e->getError()); } return $this->error('添加失败'); } //修改组员 public function edit_group_user(GroupUserModel $groupUserModel,UserModel $userModel) { $file = request()->file('avatar'); $param = $this->request->post(); try { validate(UserValidate::class)->scene('edit_group_user')->check($param); $edit_data=$groupUserModel->find($param['id']); $todayTime = strtotime('today'); //判断手机号和身份证号是否注册今天是否添加到该团队 $where_item_id_card = []; $where_item_id_card[]=['pid', '=',$this->userinfo['id']]; $where_item_id_card[]=['id_card', '=',$param['id_card']]; $where_item_id_card[]=['id_card', '<>',$edit_data['id_card']]; $where_item_id_card[]=['createtime', '>=',$todayTime]; $group_user_data = $groupUserModel->where($where_item_id_card)->find(); if(!empty($group_user_data)) return $this->error('该身份证号已添加',$param['id_card']); $where_item_mobile = []; $where_item_mobile[]=['pid', '=',$this->userinfo['id']]; $where_item_mobile[]=['mobile', '=',$param['mobile']]; $where_item_mobile[]=['mobile', '<>',$edit_data['mobile']]; $where_item_mobile[]=['createtime', '>=',$todayTime]; $group_user_data = $groupUserModel->where($where_item_mobile)->find(); if(!empty($group_user_data)) return $this->error('该手机号已添加'); $user = $userModel->find($this->userinfo['id']); $pid=$user['id']; $time=time(); $add_data=[ 'pid'=>$pid, 'nickname'=>$param['nickname'], 'id_card'=>$param['id_card'], 'mobile'=>$param['mobile'], 'sex'=>$param['sex'], 'role'=>'1', 'status'=>'normal', 'joinip'=>request()->ip(), 'updatetime'=>$time ]; if (!empty($file)) { // 文件夹 $folder = strtotime('today'); $fileName = time() . '_' . mt_rand(1000, 9999) . '.png'; $avatar = $file->move('uploads/avatar/' . $folder, $fileName); //检查目标文件是否存在 if (!empty($avatar)) { if(!empty($edit_data['avatar'])){ $avatar_file=root_path() . 'public' . DS . $edit_data['avatar']; if (file_exists($avatar_file)) { unlink($avatar_file); } } //有图片上传,进行添加用户 $add_data['avatar']=$avatar; $res=$edit_data->save($add_data); if($res) return $this->success('修改成功',$res); } }else{ $res=$edit_data->save($add_data); if($res) return $this->success('修改成功',$res); } } catch (ValidateException $e) { return $this->error($e->getError()); } return $this->error('修改失败'); } }