// +---------------------------------------------------------------------- declare (strict_types = 1); namespace app\member\controller; use think\Request; use ywxapp\service\FileStorageService; use ywxapp\model\MemberProfile; use ywxapp\model\Medal; use app\member\validate\Profile as ProfileValidate; use ywxapp\controller\MemberBase; /** * Profile 类 * * @author ywxapp */ class Profile extends MemberBase { /** * 不需要登录验证的方法 * @var array */ protected $noNeedLogin = []; /** * 不需要权限验证的方法 * @var array */ protected $noNeedVerify = ['*']; /** * 控制器初始化 initialize * @return void */ protected function initialize() {} /** * 个人资料页 */ public function index() { $user = $this->auth->model; $this->view->assign('info', $user); $this->view->assign('profile', $user->profile ?? new MemberProfile()); $this->view->assign('medals', Medal::getUserMedals((int) $user->uid)); return $this->view->fetch(); } /** * 保存个人资料 */ public function update() { if (! $this->request->isPost()) { $this->result->error('请求方式错误'); } $data = $this->request->param(); $validate = new ProfileValidate(); if (! $validate->check($data)) { $this->result->error($validate->getError()); } $user = $this->auth->model; // 基础资料(用户表) $user->nickname = $data['nickname'] ?? $user->nickname; $user->avatar = $data['avatar'] ?? $user->avatar; $user->email = $data['email'] ?? $user->email; $user->mobile = $data['mobile'] ?? $user->mobile; $user->save(); // 扩展资料(user_profile 表) $profile = $user->profile; if (! $profile) { $profile = new MemberProfile(); $profile->uid = $user->uid; } if (isset($data['gender'])) { $profile->gender = (int) $data['gender']; } if (isset($data['bio'])) { $profile->bio = $data['bio']; } $profile->save(); $this->result->success('保存成功'); } /** * 修改密码页 */ public function password() { return $this->view->fetch('profile/password'); } /** * 保存新密码 */ public function passwordSave() { if (! $this->request->isPost()) { $this->result->error('请求方式错误'); } $data = $this->request->param(); $validate = new ProfileValidate(); if (! $validate->scene('password')->check($data)) { $this->result->error($validate->getError()); } $user = $this->auth->model; $old = (string) $this->request->param('old_password', ''); $new = (string) $this->request->param('password', ''); $new2 = (string) $this->request->param('password_confirm', ''); if ($new === '' || $new !== $new2) { $this->result->error('两次新密码不一致或为空'); } if (! $user->checkPassword($old)) { $this->result->error('原密码错误'); } $user->resetPassword($new); $this->result->success('密码修改成功'); } /** * 头像上传(裁剪后的图片) */ public function avatar() { if ($this->request->isPost()) { $file = $this->request->file('file'); if (! $file || ! $file->isValid()) { $this->result->error('请选择上传文件', 400); } $type = $this->request->param('type', 'avatar'); $storage = new FileStorageService(); $result = $storage->upload($file, $type . '/' . date('Ymd')); $info = $this->auth->model; if ($info) { $info->avatar = $result['storage'] == 'local' ? '/storage/' . $result['path'] : $result['url']; $info->save(); $this->result->success(['src' => $info->avatar], '头像上传成功'); } else { $this->result->error($file->getError(), 500); } } return $this->view->fetch(); } }