diff --git a/app/Controller/UserController.php b/app/Controller/UserController.php index 4bc4479..4ffaa82 100644 --- a/app/Controller/UserController.php +++ b/app/Controller/UserController.php @@ -21,4 +21,35 @@ use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface; class UserController extends AbstractController { + /** + * 修改用户头像 + * @return ResponseInterface + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ + public function putUserAvatar(): ResponseInterface + { + $request = $this->container->get(UserRequest::class); + $request->scene('putUserAvatar')->validateResolved(); + + $UserService = new UserService(); + $data = $UserService->putUserAvatar(); + return $this->response->json($data); + } + + /** + * 修改用户名 + * @return ResponseInterface + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ + public function putUserName(): ResponseInterface + { + $request = $this->container->get(UserRequest::class); + $request->scene('putUserName')->validateResolved(); + + $UserService = new UserService(); + $data = $UserService->putUserName(); + return $this->response->json($data); + } } \ No newline at end of file diff --git a/app/Request/UserRequest.php b/app/Request/UserRequest.php index 1108467..bed6664 100644 --- a/app/Request/UserRequest.php +++ b/app/Request/UserRequest.php @@ -10,7 +10,12 @@ use Hyperf\Validation\Request\FormRequest; class UserRequest extends FormRequest { protected array $scenes = [ - + 'putUserAvatar' => [ // 修改用户头像 + 'avatar', + ], + 'putUserName' => [ // 修改用户名 + 'user_name', + ], ]; /** @@ -27,6 +32,8 @@ class UserRequest extends FormRequest public function rules(): array { return [ + 'avatar' => 'required|url', + 'user_name' => 'required', ]; } @@ -36,6 +43,9 @@ class UserRequest extends FormRequest public function messages(): array { return [ + 'avatar.required' => HttpEnumCode::getMessage(HttpEnumCode::CLIENT_HTTP_ERROR), + 'avatar.url' => HttpEnumCode::getMessage(HttpEnumCode::CLIENT_HTTP_ERROR), + 'user_name.required' => HttpEnumCode::getMessage(HttpEnumCode::CLIENT_HTTP_ERROR), ]; } } diff --git a/app/Services/UserService.php b/app/Services/UserService.php index 5fed989..ec28174 100644 --- a/app/Services/UserService.php +++ b/app/Services/UserService.php @@ -4,11 +4,12 @@ namespace App\Services; use App\Constants\HttpEnumCode; use App\Model\SubTemplate; -use App\Model\SubUser; +use App\Model\User; use App\Model\User as UserModel; use App\Model\UserDoctor; use App\Model\UserDoctorInfo; use App\Model\UserPatient; +use App\Utils\PcreMatch; use Extend\Wechat\Wechat; use Hyperf\Amqp\Result; use Hyperf\DbConnection\Db; @@ -68,6 +69,140 @@ class UserService extends BaseService return success(); } + /** + * 修改用户头像 + * @return array + */ + public function putUserAvatar(): array + { + $user_info = $this->request->getAttribute("userInfo") ?? []; + + $avatar = $this->request->input('avatar'); + + // 获取用户信息 + $params = array(); + $params['user_id'] = $user_info['user_id']; + $user = User::getOne($params); + if (empty($user)){ + return fail(); + } + + // 匹配去除oss网址 + $avatar = PcreMatch::pregRemoveOssWebsite($avatar); + + // 获取对应用户信息 + if ($user['user_type'] == 1){ + // 患者 + $params = array(); + $params['user_id'] = $user_info['user_id']; + $user_patient = UserPatient::getOne($params); + if (empty($user_patient)){ + return fail(); + } + + if ($user_patient['avatar'] != $avatar) { + $data = array(); + $data['avatar'] = $avatar; + + $params = array(); + $params['patient_id'] = $user_patient['patient_id']; + UserPatient::editUserPatient($params,$data); + } + }elseif ($user['user_type'] == 2){ + // 医生 + $params = array(); + $params['user_id'] = $user_info['user_id']; + $user_doctor = UserDoctor::getOne($params); + if (empty($user_doctor)){ + return fail(); + } + + if ($user_doctor['avatar'] != $avatar) { + $data = array(); + $data['avatar'] = $avatar; + + $params = array(); + $params['doctor_id'] = $user_doctor['doctor_id']; + UserDoctor::editUserDoctor($params,$data); + } + }elseif ($user['user_type'] == 3){ + // 药师 + $params = array(); + $params['user_id'] = $user_info['user_id']; + $user_pharmacist = UserPatient::getOne($params); + if (empty($user_pharmacist)){ + return fail(); + } + + if ($user_pharmacist['avatar'] != $avatar) { + $data = array(); + $data['avatar'] = $avatar; + + $params = array(); + $params['pharmacist_id'] = $user_pharmacist['pharmacist_id']; + UserPatient::editUserPatient($params,$data); + } + } + + return success(); + } + + /** + * 修改用户名 + * @return array + */ + public function putUserName(): array + { + $user_info = $this->request->getAttribute("userInfo") ?? []; + + $user_name = $this->request->input('user_name'); + + // 获取用户信息 + $params = array(); + $params['user_id'] = $user_info['user_id']; + $user = User::getOne($params); + if (empty($user)){ + return fail(); + } + + if ($user['user_type'] != 1){ + return fail(HttpEnumCode::HTTP_ERROR,"禁止修改"); + } + + if ($user['user_name'] == $user_name){ + return success(); + } + + Db::beginTransaction(); + try { + // 修改用户表 + $data = array(); + $data['user_name'] = $user_name; + + $params = array(); + $params['user_id'] = $user['user_id']; + User::editUser($params,$data); + + // 修改对应用户表 + if ($user['user_type'] == 1){ + // 患者 + $data = array(); + $data['user_name'] = $user_name; + + $params = array(); + $params['user_id'] = $user['user_id']; + UserPatient::editUserPatient($params,$data); + } + + Db::commit(); + } catch (\Exception $e) { + Db::rollBack(); + return fail(HttpEnumCode::SERVER_ERROR, $e->getMessage()); + } + + return success(); + } + /** * 通过user_id获取用户openid * @param string|int $user_id diff --git a/config/routes.php b/config/routes.php index bebbeb8..9192d42 100644 --- a/config/routes.php +++ b/config/routes.php @@ -184,6 +184,7 @@ Router::addGroup('/patient', function () { }); + // 个人中心-我的医生 Router::addGroup('/my_doctor', function () { // 删除我的医生 @@ -340,5 +341,11 @@ Router::addGroup('/pay', function () { }); }); -// 添加订阅消息 -Router::post('/sub', [UserController::class, 'addSubMessage']); \ No newline at end of file +// 用户 +Router::addGroup('/user', function () { + // 修改用户头像 + Router::put('/avatar', [UserController::class, 'putUserAvatar']); + + // 修改用户名 + Router::put('/name', [UserController::class, 'putUserName']); +}); \ No newline at end of file