diff --git a/app/Constants/ErrorCode.php b/app/Constants/ErrorCode.php index 8d21eeb..cd35bb3 100644 --- a/app/Constants/ErrorCode.php +++ b/app/Constants/ErrorCode.php @@ -85,6 +85,11 @@ enum ErrorCode: int implements ErrorCodeInterface */ case CONTENT_CANNOT_SAVE_CAUSED_BY_SHARE = 1201; + /** + * @Message("原神信息不存在") + */ + case YS_USER_NOT_EXIST = 1300; + public function getMessage(array $translate = null): string { $arguments = []; diff --git a/app/Constants/GachaType.php b/app/Constants/GachaType.php new file mode 100644 index 0000000..dab46b6 --- /dev/null +++ b/app/Constants/GachaType.php @@ -0,0 +1,45 @@ +build()->getSecret(); return di()->get(Encrypter::class)->decrypt($this->content, $secret); } + + public function isYuanShen(): bool + { + return $this->type === ContentType::YUAN_SHEN; + } } diff --git a/app/Model/YsGachaLog.php b/app/Model/YsGachaLog.php index 07b0dff..791c568 100644 --- a/app/Model/YsGachaLog.php +++ b/app/Model/YsGachaLog.php @@ -27,6 +27,8 @@ */ class YsGachaLog extends Model { + public bool $incrementing = false; + /** * The table associated with the model. */ diff --git a/app/Service/ContentService.php b/app/Service/ContentService.php index 67c1f16..b7bed0a 100644 --- a/app/Service/ContentService.php +++ b/app/Service/ContentService.php @@ -127,6 +127,10 @@ public function save( throw $throwable; } + if ($user) { + di()->get(YsGachaLogService::class)->load($user->id); + } + return true; } } diff --git a/app/Service/Dao/YsUserDao.php b/app/Service/Dao/YsUserDao.php index 61c944a..4f0a15a 100644 --- a/app/Service/Dao/YsUserDao.php +++ b/app/Service/Dao/YsUserDao.php @@ -12,11 +12,23 @@ namespace App\Service\Dao; +use App\Constants\ErrorCode; +use App\Exception\BusinessException; use App\Model\YsUser; use Han\Utils\Service; class YsUserDao extends Service { + public function first(int $id, bool $throw = false): ?YsUser + { + $model = YsUser::findFromCache($id); + if (! $model && $throw) { + throw new BusinessException(ErrorCode::YS_USER_NOT_EXIST); + } + + return $model; + } + public function firstByContentId(int $contentId): ?YsUser { return YsUser::query()->where('content_id', $contentId)->first(); diff --git a/app/Service/YsGachaLogService.php b/app/Service/YsGachaLogService.php new file mode 100644 index 0000000..4b646e8 --- /dev/null +++ b/app/Service/YsGachaLogService.php @@ -0,0 +1,68 @@ +get(Redis::class)->set($key, '1', ['EX' => 3600, 'NX'])) { + try { + $user = di()->get(YsUserDao::class)->first($userId); + if (! $user) { + return; + } + + foreach (GachaType::enums() as $type) { + $id = 0; + while (true) { + $result = di()->get(Mihoyo::class)->getGachaLog($user->auth_key, $type, $id); + if (empty($result['list'])) { + break; + } + foreach ($result['list'] as $re) { + $id = $re['id']; + + $model = new YsGachaLog(); + $model->id = $re['id']; + $model->uid = $re['uid']; + $model->gacha_type = $re['gacha_type']; + $model->time = $re['time']; + $model->name = $re['name']; + $model->item_type = $re['item_type']; + $model->rank_type = $re['rank_type']; + try { + $model->save(); + } catch (Throwable) { + break 2; + } + } + } + } + } finally { + di()->get(Redis::class)->del($key); + } + } + } +}