fix : user site

This commit is contained in:
2025-03-31 15:45:04 +08:00
parent d84ffd4c9e
commit bfa35c5308
4 changed files with 198 additions and 1 deletions

View File

@@ -0,0 +1,66 @@
<?php
/**
* This service file is part of item.
*
* @author ctexthuang
* @contact ctexthuang@qq.com
*/
declare(strict_types=1);
namespace App\Service\Api\User;
use App\Exception\ErrException;
use App\Service\Api\BaseService;
use App\Service\ServiceTrait\Api\GetUserInfoTrait;
use App\Service\ServiceTrait\Common\OssTrait;
class UpdateProfileService extends BaseService
{
use GetUserInfoTrait;
use OssTrait;
/**
* @var mixed
*/
protected mixed $userInfo;
/**
* @return array
*/
public function handle(): array
{
$this->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('更新头像失败');
}
}