userId = empty($this->userId) ? $this->userInfo->id : $this->userId; $this->userInfo = $this->getUserInfo($this->userId); if (empty($this->userId)) { throw new ErrException('登录失败'); } //todo 判断注销 判断封号 //todo 更新登录时间 } /** * @var AdminUser */ #[Inject] protected AdminUser $adminUserModel; /** * 返回值 * @return array * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface */ protected function getReturn():array { $adminUserInfo = $this->adminUserModel->getAdminInfoByBindUserId($this->userId); $loginReturn = [ 'id' => $this->userId, 'is_bind_mobile' => $this->userInfo->mobile ? UserCode::IS_BIND_PHONE : UserCode::IS_NOT_BIND_PHONE, 'nickName' => $this->userInfo->nickName, 'is_default_avatar' => $this->userInfo->avatar_id == 0 ? UserCode::IS_DEFAULT_AVATAR : UserCode::IS_NOT_DEFAULT_AVATAR, 'role' => $adminUserInfo->role_id ?? 0, // 'is_newcomer_coupon_pop_up' => $this->isNewcomerCouponPopUp, ]; $loginReturn['token'] = $this->cryptoFactory->cryptoClass('jwt', json_encode($loginReturn))->encrypt(); $this->userCache->setUserToken($this->userId, $loginReturn['token']); return $loginReturn; } /** * 判断是不是没有加锁 * @param $type * @return void * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface * @throws \RedisException */ protected function checkLock($type): void { $this->lockKey = match ($type){ 1 => ApiRedisKey::LoginAndRegisterByMobileLock($this->mobile), 2 => ApiRedisKey::LoginAndRegisterByCodeLock($this->jsCode) }; if (0 == ($this->redis->addLock($this->lockKey))){ throw new ErrException('请勿重复点击'); } } /** * 添加用户 * @return void */ protected function addUser(): void { $model = new User(); //默认头像和默认名称 $model->nickname = '用户'.StringUtil::randStr(6); $model->avatar_id = 0; $model->reg_ip = SystemUtil::getClientIp(); $model->mobile = $this->mobile; if (!$model->save()) throw new ErrException('数据保存失败-注册失败'); $this->userId = $model->id; $this->userInfo = $model; } /** * 添加账户 * @return void */ protected function addAccount(): void { $model = new UserAccount(); $model->user_id = $this->userId; $model->balance = 0; $model->integral = 0; if (!$model->save()) throw new ErrException('注册失败-00002'); } // /** // * @var CouponTemplate $couponTemplateModel // */ // #[Inject] // protected CouponTemplate $couponTemplateModel; // /** // * @return void // * @throws ContainerExceptionInterface // * @throws NotFoundExceptionInterface // */ // protected function addCoupon(): void // { // $couponTemplateId = $this->configCache->getConfigValueByKey(ConfigCode::COUPONS_FOR_NEWCOMERS); // $couponValidity = $this->configCache->getConfigValueByKey(ConfigCode::NEWBIE_COUPON_VALIDITY); // // // 随便一个为0代表不赠送 // if ($couponValidity == 0 || $couponTemplateId == 0) return; // // $couponTemplateId = explode(',', $couponTemplateId); // $couponTemplateList = $this->couponTemplateModel->getDataByIds($couponTemplateId); // // $insertCoupon = []; // foreach ($couponTemplateId as $one){ // if ($couponTemplateList[$one]['status'] == CouponCode::COUPON_TEMPLATE_STATUS_ENABLE) continue; // // $insertCoupon[] = [ // 'coupon_template_id' => $one, // 'coupon_dispense_id' => CouponCode::SYSTEMIC_DISTRIBUTION, // 'user_id' => $this->userId, // 'status' => CouponCode::COUPON_STATUS_UNUSED, // 'coupon_name' => $couponTemplateList[$one]['name'], // 'validity_start_time' => date('Y-m-d H:i:s'), // 'validity_end_time' => date('Y-m-d', strtotime('+'.$couponValidity.' days')), // ]; // } // // if (empty($insertCoupon)) return; // // if (!(new UserCoupon)->insert($insertCoupon)) throw new ErrException('注册失败-00003'); // // $this->isNewcomerCouponPopUp = 1; // } /** * @return void * @throws RandomException */ protected function addInviteCode(): void { $inviteCode = $this->generateStableCode($this->userId); if ($this->userModel->checkInviteCodeIsUse($inviteCode) == 1) return; if (!$this->userModel->where('id', $this->userId)->update(['invite_code' => $inviteCode])) throw new ErrException('注册失败-00004'); } /** * @return void */ protected function checkInvite(): void { if (!$this->request->input('invite_code')) return; $userId = $this->userModel->getUserIdByInviteCode($this->request->input('invite_code')); if (empty($userId)) return; if ($this->userInviteModel->checkIsInvite($this->userId) == 1) return; $userInviteModel = new UserInvite(); $userInviteModel->user_id = $this->userId; $userInviteModel->invitee_user_id = $userId; $userInviteModel->channel = UserCode::INVITE_CHANNEL_USER; //todo 预先普通用户渠道 $userInviteModel->status = UserCode::INVITE_STATUS_REGISTER; if (!$userInviteModel->save()) throw new ErrException('注册失败-00005'); } abstract protected function register(); }