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('order_status',OrderEnum::STATUS_CREATE)->count(), 'paid' => Order::where('user_id', $this->auth->id)->where('order_status',OrderEnum::STATUS_PAY)->count(), //待收货 'shipped' => Order::where('user_id', $this->auth->id)->where('order_status',OrderEnum::STATUS_SHIP)->count(), // 已完成 'completed' => Order::where('user_id', $this->auth->id)->where('order_status',OrderEnum::STATUS_CONFIRM)->count(), ]; $info['avatar'] = $info['avatar'] ? cdnurl($info['avatar'], true) : cdnurl(Config::get('shop.user_default_avatar') , true); $info['gender_text'] = UserEnum::getGenderText($this->auth->getUser()->gender ?? 0); $info['age'] = $this->auth->getUser()->age ?? 0; $openid = ''; $this->success('', [ 'userInfo' => $info, 'openid' => $openid, ]); } /** * 个人资料 */ public function profile() { $user = $this->auth->getUser(); $params = $this->request->param(); // 验证器 - 只验证传递的字段 $validate = new \app\api\validate\User(); if (!$validate->check($params, [], 'profile')) { $this->error($validate->getError()); } // 只有字段有值时才修改 if (isset($params['username']) && !empty($params['username'])) { $username = $params['username']; $exists = \app\common\model\User::where('username', $username)->where('id', '<>', $this->auth->id)->find(); if ($exists) { $this->error(__('Username already exists')); } $user->username = $username; } if (isset($params['avatar']) && !empty($params['avatar'])) { // 替换有域名的头像 $avatar = str_replace(cdnurl('', true), '', $params['avatar']); $user->avatar = $avatar; } if (isset($params['nickname']) && !empty($params['nickname'])) { $user->nickname = $params['nickname']; } if (isset($params['bio']) && !empty($params['bio'])) { $user->bio = $params['bio']; } if (isset($params['age']) && !empty($params['age'])) { $user->age = $params['age']; } if (isset($params['gender']) && !empty($params['gender'])) { $user->gender = $params['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(__('换绑手机号成功')); } }