diff --git a/src/controllers/gameext.controller.ts b/src/controllers/gameext.controller.ts new file mode 100644 index 0000000..080bb87 --- /dev/null +++ b/src/controllers/gameext.controller.ts @@ -0,0 +1,112 @@ +import BaseController from '../common/base.controller' +import { role, router } from '../decorators/router' +import { ZanInfo } from '../models/ZanInfo' +import { ZError } from '../common/ZError' +import { AccountCollect } from '../models/AccountCollect' + +class GameExtController extends BaseController { + /** + * 获取游戏点赞信息 + */ + @role('anon') + @router('post /api/svr/zan/info') + async reqZanInfosList(req: any) { + let { accountId, games, type } = req.params + type = type || 'game' + let records = await ZanInfo.find({ recordId: { $in: games }, type }) + let results: any = {} + for (let record of records) { + let data = record.toJson() + let did = 0 + if (accountId) { + did = data.accounts.findIndex(o => o === accountId) > -1 ? 1 : 0 + } + results[record.recordId] = { count: record.count, did } + } + return results + } + + /** + * 为一个游戏点赞或取消点赞 + */ + @role('anon') + @router('post /api/svr/zan/update') + async updateGameZan(req: any) { + let { accountId, game, type, act } = req.params + type = type || 'game' + if (!accountId) { + throw new ZError(11, 'accountId needed') + } + if (!game) { + throw new ZError(12, 'gameId needed') + } + let record = await ZanInfo.insertOrUpdate({ recordId: game, type }, {}) + const index = record.accounts.indexOf(accountId) + if (act) { + if (index > -1) { + throw new ZError(13, 'already had') + } + record.count += 1 + record.accounts.push(accountId) + await record.save() + } else { + if (index === -1) { + throw new ZError(13, 'not had') + } + record.count -= 1 + record.accounts.splice(index, 1) + await record.save() + } + return { count: record.count } + } + + /** + * 获取某玩家收藏信息 + */ + @role('anon') + @router('post /api/svr/collect/list') + async collectList(req: any) { + let { accountId, type } = req.params + type = type || 'game' + let record = await AccountCollect.insertOrUpdate({ accountId }, {}) + let results = [] + for (let _d of record.games) { + if (_d.type === type) { + results.push(_d.recordId) + } + } + return results + } + + /** + * 收藏或取消收藏 + */ + @role('anon') + @router('post /api/svr/collect/update') + async updateCollect(req: any) { + let { accountId, game, type, act } = req.params + type = type || 'game' + if (!accountId) { + throw new ZError(11, 'accountId needed') + } + if (!game) { + throw new ZError(12, 'gameId needed') + } + let record = await AccountCollect.insertOrUpdate({ accountId }, {}) + let index = record.games.findIndex(o => o.recordId === game && o.type === type) + let exists = index > -1 + if (act) { + if (exists) { + throw new ZError(13, 'already add') + } + record.games.push({ recordId: game, type }) + } else { + if (!exists) { + throw new ZError(13, 'not add') + } + record.games.splice(index, 1) + } + await record.save() + return {} + } +} diff --git a/src/models/AccountCollect.ts b/src/models/AccountCollect.ts new file mode 100644 index 0000000..f869d99 --- /dev/null +++ b/src/models/AccountCollect.ts @@ -0,0 +1,41 @@ +import { dbconn } from '../decorators/dbconn' +import { getModelForClass, index, modelOptions, prop, ReturnModelType } from '@typegoose/typegoose' +import { BaseModule } from './Base' +import { customAlphabet } from 'nanoid' +const nanoid = customAlphabet('2345678abcdefghjkmnpqrstwxy', 32) + +const jsonExcludeKeys = ['updatedAt', '__v', '_id', 'createdAt', 'isBot', 'type'] + +class RecordInfo { + @prop() + public recordId: string + @prop() + public type: string +} +/** + * 存储用户收藏信息 + */ +@dbconn() +@index({ accountId: 1 }, { unique: true }) +@modelOptions({ + schemaOptions: { collection: 'account_collect', timestamps: true }, +}) +class AccountCollectClass extends BaseModule { + @prop() + accountId: string + @prop({ type: () => [RecordInfo], default: [] }) + games: RecordInfo[] + + 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 AccountCollect = getModelForClass(AccountCollectClass, { existingConnection: AccountCollectClass.db }) diff --git a/src/models/ZanInfo.ts b/src/models/ZanInfo.ts new file mode 100644 index 0000000..41533b4 --- /dev/null +++ b/src/models/ZanInfo.ts @@ -0,0 +1,40 @@ +import { dbconn } from '../decorators/dbconn' +import { getModelForClass, index, modelOptions, prop } from '@typegoose/typegoose' +import { BaseModule } from './Base' + +const jsonExcludeKeys = ['updatedAt', '__v', '_id', 'createdAt', 'isBot', 'type'] + +/** + * 存储游戏, 文章等的点赞信息 + */ +@dbconn() +@index({ recordId: 1, type: 1 }, { unique: true }) +@modelOptions({ + schemaOptions: { collection: 'zan_info', timestamps: true }, +}) +class ZanInfoClass extends BaseModule { + @prop() + recordId: string + + @prop() + type: string + + @prop({ default: 0 }) + count: number + + @prop({ type: () => [String], default: [] }) + accounts: string[] + + 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 ZanInfo = getModelForClass(ZanInfoClass, { existingConnection: ZanInfoClass.db })