error(__('User center already closed')); } } /** * 个人中心 */ public function index() { $apptype = $this->request->param('apptype'); $platform = $this->request->param('platform'); $logincode = $this->request->param('logincode'); $info = $this->auth->getUserInfo(); $info['order'] = [ 'created' => Order::where('user_id', $this->auth->id)->where('orderstate', 0)->where('paystate', 0)->count(), 'paid' => Order::where('user_id', $this->auth->id)->where('orderstate', 0)->where('paystate', 1)->where('shippingstate', 0)->count(), 'evaluate' => Order::where('user_id', $this->auth->id)->where('orderstate', 0)->where('paystate', 1)->where('shippingstate', 2)->count() ]; $info['avatar'] = cdnurl($info['avatar'], true); $info['gender_text'] = UserEnum::getGenderText($this->auth->getUser()->gender ?? 0); $info['age'] = $this->auth->getUser()->age ?? 0; $signin = get_addon_info('signin'); $info['is_install_signin'] = ($signin && $signin['state']); $firstlogin = $this->auth->jointime === $this->auth->logintime; //判断是否显示昵称更新提示 $profilePrompt = false; $config = get_addon_config('shop'); if ($config['porfilePrompt'] === 'firstlogin') { $profilePrompt = $this->auth->jointime === $this->auth->logintime; } elseif ($config['porfilePrompt'] === 'everylogin') { $profilePrompt = true; } elseif ($config['porfilePrompt'] === 'disabled') { $profilePrompt = false; } $showProfilePrompt = false; if ($profilePrompt) { $showProfilePrompt = !$info['nickname'] || stripos($info['nickname'], '微信用户') !== false || preg_match("/^\d{3}\*{4}\d{4}$/", $info['nickname']); } $openid = ''; //如果有传登录code,则获取openid if ($logincode) { $json = (new \addons\shop\library\Wechat\Service())->getWechatSession($logincode); $openid = $json['openid'] ?? ''; } $data['openid'] = $openid; $this->success('', [ 'userInfo' => $info, 'openid' => $openid, 'showProfilePrompt' => $showProfilePrompt ]); } /** * 个人资料 */ public function profile() { $user = $this->auth->getUser(); $params = $this->request->param(); // 字段不传就报错 所以默认给值 $username = $params['username'] ?? ''; $avatar = $params['avatar'] ?? ''; $nickname = $params['nickname'] ?? ''; $bio = $params['bio'] ?? ''; $age = $params['age'] ?? ''; $gender = $params['gender'] ?? ''; // 验证器 // 替换有域名的头像 $avatar = str_replace(cdnurl('', true), '', $avatar); $params['avatar'] = $avatar; $validate = new \app\api\validate\User(); if (!$validate->check($params, [], 'profile')) { $this->error($validate->getError()); } // username 不传,则不修改 if ($username) { $user->username = $username; $exists = \app\common\model\User::where('username', $username)->where('id', '<>', $this->auth->id)->find(); if ($exists) { $this->error(__('Username already exists')); } } $user->bio = $bio; $user->nickname = $nickname; $user->username = $username; $user->avatar = $avatar; $user->age = $age; $user->gender = $gender; $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 getSigned() { $url = $this->request->param('url', '', 'trim'); $js_sdk = new \addons\shop\library\Jssdk(); $data = $js_sdk->getSignedPackage($url); $this->success('', $data); } }