error(__('User center already closed')); } } /** * 个人中心 */ public function index() { $logincode = $this->request->param('logincode'); $info = $this->auth->getUserInfo(); $info['avatar'] = !empty($info['avatar']) ? cdnurl($info['avatar'], true) :''; $info['gender_text'] = UserEnum::getGenderText($this->auth->getUser()->gender ?? 0); $info['age'] = $this->auth->getUser()->age ?? 0; // 判断是否需要编辑个人信息 个人信息是否 都填了 昵称、性别、年龄 $isNeedEditProfile = 0; if (!$info['nickname'] || !$info['gender'] || !$info['age']) { $isNeedEditProfile = 1; } $info['is_need_editprofile'] = $isNeedEditProfile; $openid = ''; //如果有传登录code,则获取openid if ($logincode) { $json = (new WechatService(Wechat::miniProgram()))->getWechatSession($logincode); $openid = $json['openid'] ?? ''; } $this->success('', [ 'userInfo' => $info, 'openid' => $openid, ]); } /** * 个人资料 */ public function profile() { $user = $this->auth->getUser(); $params = $this->request->param(); // 定义允许修改的字段 $allowedFields = ['username', 'avatar', 'nickname', 'bio', 'age', 'gender']; $updateData = []; // 只处理前端实际传递的参数 foreach ($allowedFields as $field) { if (isset($params[$field])) { $updateData[$field] = $params[$field]; } } // 如果没有任何要更新的字段 if (empty($updateData)) { $this->error('请至少提供一个要修改的字段'); } // 处理头像URL(如果传递了avatar字段) if (isset($updateData['avatar'])) { $updateData['avatar'] = str_replace(cdnurl('', true), '', $updateData['avatar']); $params['avatar'] = $updateData['avatar']; } // 验证器 $validate = new \app\api\validate\User(); if (!$validate->check($params, [], 'profile')) { $this->error($validate->getError()); } // 检查用户名是否重复(如果传递了username字段) if (isset($updateData['username']) && !empty($updateData['username'])) { $exists = \app\common\model\User::where('username', $updateData['username']) ->where('id', '<>', $this->auth->id) ->find(); if ($exists) { $this->error(__('Username already exists')); } } // 只更新传递的字段 foreach ($updateData as $field => $value) { $user->$field = $value; } $user->save(); $this->success('修改成功!'); } /** * 保存头像 */ public function avatar() { $user = $this->auth->getUser(); $avatar = $this->request->post('avatar'); if (!$avatar) { $this->error("头像不能为空"); } $avatar = str_replace(cdnurl('', true), '', $avatar); $user->avatar = $avatar; $user->save(); $this->success('修改成功!'); } /** * 注销登录 */ public function logout() { $this->auth->logout(); $this->success(__('Logout successful'), ['__token__' => $this->request->token()]); } /** * * 注销账号 * @param string $mobile 手机号 */ public function cancelaccount() { $params = $this->request->param(); $type = $params['type'] ?? ''; $mobile = $params['mobile'] ?? ''; $email = $params['email'] ?? ''; $captcha = $params['captcha'] ?? ''; // 使用验证器进行数据验证 $validate = new \app\api\validate\UserCancel(); if (!$validate->check($params, [], 'cancel')) { $this->error($validate->getError()); } if ($type == 'mobile') { $user = \app\common\model\User::getByMobile($mobile); if (!$user) { $this->error(__('User not found')); } if (!Env::get('app.app_debug') && $captcha != Env::get('app.DEFAULT_SMSCODE')) { $ret = Sms::check($mobile, $captcha, 'resetpwd'); if (!$ret) { $this->error(__('Captcha is incorrect')); } } Sms::flush($mobile, 'resetpwd'); } else { $user = \app\common\model\User::getByEmail($email); if (!$user) { $this->error(__('User not found')); } $ret = Ems::check($email, $captcha, 'resetpwd'); if (!$ret) { $this->error(__('Captcha is incorrect')); } Ems::flush($email, 'resetpwd'); } // 删除用户 $ret = $this->auth->delete($user->id); if ($ret) { $this->success(__('Cancel account successful')); } else { $this->error($this->auth->getError()); } } /** * 换绑手机号 */ public function changeMobile() { $params = $this->request->param(); $mobile = $params['mobile'] ?? ''; $captcha = $params['captcha'] ?? ''; // 验证器 $validate = new \app\api\validate\User(); if (!$validate->check($params, [], 'changeMobile')) { $this->error($validate->getError()); } $user = $this->auth->getUser(); if ($user->mobile == $mobile) { $this->error(__('手机号不能与当前手机号相同')); } // 换绑手机号 $user = \app\common\model\User::getByMobile($mobile); if ($user) { $this->error(__('手机号已存在')); } if (!Env::get('app.app_debug') && $captcha != Env::get('app.DEFAULT_SMSCODE')) { $ret = Sms::check($mobile, $captcha, 'resetpwd'); if (!$ret) { $this->error(__('Captcha is incorrect')); } } Sms::flush($mobile, 'resetpwd'); $this->auth->getUser()->save(['mobile' => $mobile]); $this->success(__('换绑手机号成功')); } /** * 第三方授权信息 */ public function thirdOauth() { $userId = $this->auth->id; $provider = $this->request->param('provider', ''); $platform = $this->request->param('platform', ''); if (!in_array($platform, ['miniProgram', 'officialAccount', 'openPlatform'])) { $this->error(__('Invalid parameters')); } $where = [ 'platform' => $platform, 'user_id' => $userId ]; if ($provider !== '') { $where['provider'] = $provider; } $oauth = Third::where($where)->field('nickname, avatar, platform, provider')->find(); $this->success('', $oauth); } }