diff --git a/src/api/controllers/game_user.controller.ts b/src/api/controllers/game_user.controller.ts index 9911c0a..e15eb5a 100644 --- a/src/api/controllers/game_user.controller.ts +++ b/src/api/controllers/game_user.controller.ts @@ -25,6 +25,14 @@ class GameUserController extends BaseController { } let user = (await GameUser.findOrCreate({ accountId })).doc user.updateFromReq(req.params) + let sid = req.params.shop + if (sid) { + const shop = await Shop.fetchByID(sid) + if (shop) { + user.shop = shop.id + user.shops.pushOnce(shop.id) + } + } await user.save() const token = await res.jwtSign({ id: user.id, accountId }) return { token } diff --git a/src/api/controllers/shop.controller.ts b/src/api/controllers/shop.controller.ts index d578ca2..6e2a6bb 100644 --- a/src/api/controllers/shop.controller.ts +++ b/src/api/controllers/shop.controller.ts @@ -5,6 +5,7 @@ import { ZError } from '../../common/ZError' import { ShopExam } from '../../models/shop/ShopExam' import { ShopActivity } from '../../models/shop/ShopActivity' import { ShopGameExt } from '../../models/shop/ShopGameExt' +import { GameUser } from '../../models/user/GameUser' class ShopController extends BaseController { /** @@ -79,21 +80,26 @@ class ShopController extends BaseController { } @role('anon') - @router('post /api/:accountid/shop') + @router('post /api/:accountId/shop') async shopInfo(req) { - let { sid } = req.params + let { sid, accountId } = req.params if (!sid) { throw new ZError(10, `shopInfo 没有店铺id: ${sid}`) } - sid = sid.split('|')[0] - if (!validShopId(sid)) { - throw new ZError(10, `shopInfo 没有店铺id或者店铺id格式不正确: ${sid}`) + const shopId = sid.split('|')[0] + if (!validShopId(shopId)) { + throw new ZError(10, `shopInfo 没有店铺id或者店铺id格式不正确: ${shopId}`) } let rspData: any = {} - let shop = await Shop.fetchByID(sid) + let shop = await Shop.fetchByID(shopId) if (!shop) { throw new ZError(11, '未找到对应的店铺') } + let user = (await GameUser.findOrCreate({ accountId })).doc + user.shop = shop.id + user.shops.pushOnce(shop.id) + user.loginParams = sid.split('|').slice(1) + await user.save() rspData.gameCfg = {} if (!!shop.gameInfo) { let gameInfo = shop.gameInfo diff --git a/src/models/user/GameUser.ts b/src/models/user/GameUser.ts index 0ede4fd..60296e4 100644 --- a/src/models/user/GameUser.ts +++ b/src/models/user/GameUser.ts @@ -1,6 +1,7 @@ import { getModelForClass, index, modelOptions, prop, ReturnModelType } from '@typegoose/typegoose' import { BaseModule } from '../Base' import { dbconn } from '../../decorators/dbconn' +import { Shop } from '../shop/Shop' @dbconn() @index({ accountId: 1 }, { unique: true }) @@ -45,6 +46,9 @@ class GameUserClass extends BaseModule { @prop() public shop: string + @prop({ type: () => [String] }) + public loginParams: string[] + public static async getByAccountID(this: ReturnModelType, accountId: string) { let records = await this.find({ accountId }).limit(1) return records.length > 0 ? records[0] : null @@ -58,13 +62,6 @@ class GameUserClass extends BaseModule { } return map } - - public updateFromReq(data: any) { - super.updateFromReq(data) - if (data.shop) { - this.shops.pushOnce(data.shop) - } - } } export const GameUser = getModelForClass(GameUserClass, { existingConnection: GameUserClass.db })