From ba215923eccc49d0ea343c9caba4d315e9564948 Mon Sep 17 00:00:00 2001 From: zhl Date: Wed, 22 Dec 2021 11:23:27 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0jump=E6=B8=B8=E6=88=8F?= =?UTF-8?q?=E8=B5=84=E6=96=99=E7=BC=93=E5=AD=98=E7=9A=84=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/gameproxy.controller.ts | 65 +++++++++++++++++++++++++ src/models/GameCache.ts | 45 +++++++++++++++++ src/models/GamePriceCache.ts | 37 ++++++++++++++ src/services/jump.svr.ts | 13 +++++ 4 files changed, 160 insertions(+) create mode 100644 src/controllers/gameproxy.controller.ts create mode 100644 src/models/GameCache.ts create mode 100644 src/models/GamePriceCache.ts create mode 100644 src/services/jump.svr.ts diff --git a/src/controllers/gameproxy.controller.ts b/src/controllers/gameproxy.controller.ts new file mode 100644 index 0000000..4483244 --- /dev/null +++ b/src/controllers/gameproxy.controller.ts @@ -0,0 +1,65 @@ +import BaseController from '../common/base.controller' +import { role, router } from '../decorators/router' +import { GameCache } from '../models/GameCache' +import { getJumpGameInfo, getJumpGamePrice } from '../services/jump.svr' +import { GamePriceCache } from '../models/GamePriceCache' + +class GameProxyController extends BaseController { + /** + * 获取游戏详情 + */ + @role('anon') + @router('get /api/svr/getgameinfoext') + async gameInfo(req: any) { + let { accountId, oldGameId } = req.params + oldGameId += '' + let game = await GameCache.insertOrUpdate({ oldGameId }, {}) + if (game.inited) { + try { + let res = await getJumpGameInfo(oldGameId) + if (res.success) { + const data = res.data + game.jumpGame = data.jumpGame + game.dlcList = data.dlcList + game.jumpGameExt = data.jumpGameExt + game.otherPlatVersion = data.otherPlatVersion + game.inited = false + await game.save() + } + } catch (err) { + console.log(err) + } + } + return game.toJson() + } + + /** + * 获取游戏详情 + */ + @role('anon') + @router('get /api/svr/getgameprice') + async gamePrice(req: any) { + let { accountId, oldGameId, nums } = req.params + nums = nums | 0 || 0 + oldGameId += '' + let game = await GamePriceCache.insertOrUpdate({ oldGameId }, {}) + if (game.inited) { + try { + let res = await getJumpGamePrice(oldGameId) + if (res.success) { + const data = res.data + game.countries = data.countries + game.prices = data.prices + await game.save() + } + } catch (err) { + console.log(err) + } + } + let result = game.toJson() + if (nums > 0 && result.prices.length > nums) { + result.prices.length = nums + } + return result + } +} diff --git a/src/models/GameCache.ts b/src/models/GameCache.ts new file mode 100644 index 0000000..3a2740f --- /dev/null +++ b/src/models/GameCache.ts @@ -0,0 +1,45 @@ +import { BaseModule } from './Base' +import { dbconn } from '../decorators/dbconn' +import { getModelForClass, modelOptions, prop, Severity, mongoose, index } from '@typegoose/typegoose' + +const jsonExcludeKeys = ['updatedAt', '__v', '_id', 'createdAt', 'isBot', 'type', 'inited'] + +@dbconn() +@index({ oldGameId: 1 }, { unique: true }) +@modelOptions({ + schemaOptions: { collection: 'jump_game_cache', timestamps: true }, + options: { allowMixed: Severity.ALLOW }, +}) +class GameCacheClass extends BaseModule { + @prop() + public oldGameId: string + + @prop({ type: mongoose.Schema.Types.Mixed }) + public jumpGame: any + + @prop({ type: mongoose.Schema.Types.Mixed }) + public dlcList: any + + @prop({ type: mongoose.Schema.Types.Mixed }) + public jumpGameExt: any + + @prop({ type: mongoose.Schema.Types.Mixed }) + public otherPlatVersion: any + @prop({ default: true }) + public inited: boolean + + public toJson(): any { + let result: any = {} + // @ts-ignore + for (let key in this._doc) { + if (jsonExcludeKeys.indexOf(key) == -1) { + result[key] = this[key] + } + } + return result + } +} + +export const GameCache = getModelForClass(GameCacheClass, { + existingConnection: GameCacheClass.db, +}) diff --git a/src/models/GamePriceCache.ts b/src/models/GamePriceCache.ts new file mode 100644 index 0000000..ec740b7 --- /dev/null +++ b/src/models/GamePriceCache.ts @@ -0,0 +1,37 @@ +import { BaseModule } from './Base' +import { dbconn } from '../decorators/dbconn' +import { getModelForClass, modelOptions, prop, Severity, mongoose, index } from '@typegoose/typegoose' + +const jsonExcludeKeys = ['updatedAt', '__v', '_id', 'createdAt', 'isBot', 'type', 'inited'] + +@dbconn() +@index({ oldGameId: 1 }, { unique: true }) +@modelOptions({ + schemaOptions: { collection: 'jump_game_price_cache', timestamps: true }, + options: { allowMixed: Severity.ALLOW }, +}) +class GamePriceCacheClass extends BaseModule { + @prop() + public oldGameId: string + @prop({ type: mongoose.Schema.Types.Mixed }) + countries: any + @prop({ type: mongoose.Schema.Types.Mixed }) + public prices: any + @prop({ default: true }) + public inited: boolean + + public toJson(): any { + let result: any = {} + // @ts-ignore + for (let key in this._doc) { + if (jsonExcludeKeys.indexOf(key) == -1) { + result[key] = this[key] + } + } + return result + } +} + +export const GamePriceCache = getModelForClass(GamePriceCacheClass, { + existingConnection: GamePriceCacheClass.db, +}) diff --git a/src/services/jump.svr.ts b/src/services/jump.svr.ts new file mode 100644 index 0000000..4c0c1b8 --- /dev/null +++ b/src/services/jump.svr.ts @@ -0,0 +1,13 @@ +import axios from 'axios' + +export async function getJumpGameInfo(gameId: string) { + const link = `https://switch.jumpvg.com/jump/game/detail?clickFrom=-1&id=${gameId}&path=find_discount_gamedetail_switch&platform=1&version=3` + const { data } = await axios.get(link) + return data +} + +export async function getJumpGamePrice(gameId: string) { + const link = `https://switch.jumpvg.com/jump/price/getAllPriceByGame?id=${gameId}&platform=1` + const { data } = await axios.get(link) + return data +}