From bfa35c5308e6805250265b3c387e44466919bf25 Mon Sep 17 00:00:00 2001 From: ctexthuang Date: Mon, 31 Mar 2025 15:45:04 +0800 Subject: [PATCH] fix : user site --- app/Controller/Api/SystemController.php | 8 ++ app/Controller/Api/UserController.php | 11 +- app/Service/Api/System/AliStsService.php | 114 ++++++++++++++++++ app/Service/Api/User/UpdateProfileService.php | 66 ++++++++++ 4 files changed, 198 insertions(+), 1 deletion(-) create mode 100644 app/Service/Api/System/AliStsService.php create mode 100644 app/Service/Api/User/UpdateProfileService.php diff --git a/app/Controller/Api/SystemController.php b/app/Controller/Api/SystemController.php index 6612d3e..d125aac 100644 --- a/app/Controller/Api/SystemController.php +++ b/app/Controller/Api/SystemController.php @@ -7,6 +7,7 @@ namespace App\Controller\Api; use App\Controller\AbstractController; use App\Middleware\Api\JwtAuthMiddleware; use App\Service\Api\IndexService; +use App\Service\Api\System\AliStsService; use App\Service\Api\System\CityListService; use App\Service\Api\System\MiniWxConfigService; use App\Service\Api\System\SiteListService; @@ -56,4 +57,11 @@ class SystemController extends AbstractController { return (new IndexService)->handle(); } + + #[RequestMapping(path: "sts/accredit", methods: "GET")] + #[Middleware(JwtAuthMiddleware::class)] + public function aliSts() + { + return (new AliStsService)->handle(); + } } diff --git a/app/Controller/Api/UserController.php b/app/Controller/Api/UserController.php index 91bfa41..9daae7c 100644 --- a/app/Controller/Api/UserController.php +++ b/app/Controller/Api/UserController.php @@ -13,7 +13,9 @@ use App\Service\Api\User\InviteListService; use App\Service\Api\User\MyPageService; use App\Service\Api\User\SiteService; use App\Service\Api\User\UnBindPhoneService; +use App\Service\Api\User\UpdateProfileService; use Hyperf\HttpServer\Annotation\Controller; +use Hyperf\HttpServer\Annotation\Middleware; use Hyperf\HttpServer\Annotation\Middlewares; use Hyperf\HttpServer\Annotation\RequestMapping; use Hyperf\Validation\Annotation\Scene; @@ -137,8 +139,15 @@ class UserController extends AbstractController return (new MyPageService)->handle(); } + /** + * @return array + */ + #[RequestMapping(path: 'update_profile',methods: 'POST')] + #[Scene(scene: 'update_profile')] public function updateProfile() { - + return (new UpdateProfileService)->handle(); } + + } diff --git a/app/Service/Api/System/AliStsService.php b/app/Service/Api/System/AliStsService.php new file mode 100644 index 0000000..3136a6d --- /dev/null +++ b/app/Service/Api/System/AliStsService.php @@ -0,0 +1,114 @@ +bucket = config('ali.bucket'); + $this->roleArn = config('ali.role_arn'); //acs:ram::1987853712163999:role/video-access + } + + /** + * @return array + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ + public function handle(): array + { + $payload = [ + 'durationSeconds' => $this->seconds, + 'roleArn' => $this->roleArn, + 'roleSessionName' => 'adminUpload', + 'policy' => [ + 'Version' => '1', + 'Statement' => [ + [ + 'Effect' => 'Allow', + 'Action' => [ + 'oss:*' + ], + 'Resource' => [ + sprintf('acs:oss:*:*:%s', $this->bucket), + sprintf('acs:oss:*:*:%s/*', $this->bucket), + ] + ], + [ + 'Effect' => 'Deny', + 'Action' => [ + 'oss:DeleteBucket' + ], + 'Resource' => [ + sprintf('acs:oss:*:*:%s', $this->bucket), + ] + ], + [ + 'Effect' => 'Allow', + 'Action' => [ + 'oss:DeleteObject' + ], + 'Resource' => [ + sprintf('acs:oss:*:*:%s/*', $this->bucket), + ] + ], + + ], + ] + ]; + + $res = $this->getAliStsControls($payload); + $this->log->info(__CLASS__.':'.__FUNCTION__.':授权oss信息:'.json_encode($res)); + + $aliResponse = $res->body->credentials; + if (empty($aliResponse)) throw new ErrException('授权失败'); + + return $this->return->success('success',[ + 'access_key_id' => $aliResponse->accessKeyId, + 'access_key_secret' => $aliResponse->accessKeySecret, + 'expiration' => date('Y-m-d H:i:s',strtotime($aliResponse->expiration)),//UTC时间转为北京时间 + 'security_token' => $aliResponse->securityToken, + 'callback_url' => config('ali.callback_url'), + 'bucket' => $this->bucket, + 'region' => config('ali.region'), + ]); + } +} \ No newline at end of file diff --git a/app/Service/Api/User/UpdateProfileService.php b/app/Service/Api/User/UpdateProfileService.php new file mode 100644 index 0000000..4fdfd02 --- /dev/null +++ b/app/Service/Api/User/UpdateProfileService.php @@ -0,0 +1,66 @@ +userInfo = $this->getUserInfo($this->userId); + if (empty($this->userInfo)) throw new ErrException('用户异常'); + + if (!empty($this->request->input('avatar_id'))) $this->updateAvatar(); + + if (!empty($this->request->input('nickname'))) $this->updateNickname(); + + return $this->return->success(); + } + + /** + * @return void + */ + private function updateNickname(): void + { + $this->userInfo->nickname = $this->request->input('nickname'); + + if (!$this->userInfo->save()) throw new ErrException('更新昵称失败'); + } + + /** + * @return void + */ + private function updateAvatar(): void + { + $avatar_id = $this->request->input('avatar_id'); + + $this->updateOssObjects([$avatar_id]); + if ($this->userInfo->avatar_id > 0) $this->updateOssObjectsDisable([$this->userInfo->avatar_id]); + $this->userInfo->avatar_id = $this->request->input('avatar_id'); + + if (!$this->userInfo->save()) throw new ErrException('更新头像失败'); + } +} \ No newline at end of file