diff --git a/docs/api.md b/docs/api.md index e23bdf0..976fb3f 100644 --- a/docs/api.md +++ b/docs/api.md @@ -97,7 +97,7 @@ id: '', // 卡组id heroid: '', // 英雄id selected: true, // 当前选择的卡组 - iddefault: false, // 是否是默认卡组, 默认卡组不能编辑 + isdefault: false, // 是否是默认卡组, 默认卡组不能编辑 cards: [{ cardid: '', // 卡牌id owned: true, // 是否已拥有 diff --git a/src/api.server.ts b/src/api.server.ts index 7f8253c..94d715f 100644 --- a/src/api.server.ts +++ b/src/api.server.ts @@ -47,9 +47,9 @@ export class ApiServer { logger.info('find api router', config.method || 'all', config.path, controller.name); // @ts-ignore self.server[config.method || 'all'](config.path, { - preValidation: async function (request: { roles: string[]; }, reply: any, done: any) { + preValidation: async function (request: FastifyRequest, reply: FastifyReply) { request.roles = config.roles; - await this.apiAuth(request, reply, done); + await this.apiAuth(request, reply); } }, controller); }) @@ -100,8 +100,19 @@ export class ApiServer { reply.send({errcode: statusCode, errmsg: error ? error.message : 'unknown error'}) }) } + + /** + * 格式化接口返回数据, 统一封装成如下格式 + * { + * errcode: 0, + * errmsg?: '', + * data: any + * } + * @private + */ private setFormatSend() { this.server.addHook('preSerialization', async (request: FastifyRequest, reply: FastifyReply, payload) => { + reply.header('', '') // @ts-ignore if (!payload.errcode) { payload = { diff --git a/src/controllers/AccountController.ts b/src/controllers/AccountController.ts index e64785b..da776b6 100644 --- a/src/controllers/AccountController.ts +++ b/src/controllers/AccountController.ts @@ -8,11 +8,11 @@ export default class AccountController extends BaseController { @router('post /api/:accountid/uinfo') async info(req: any, res: any) { let {accountid} = req.params; - if (true) { - throw new ZError(101, 'no acc'); - } - let account = (await Account.findOrCreate({accountid})).doc; - + // if (true) { + // throw new ZError(101, 'no acc'); + // } + let account = (await Account.findOrCreate({_id: accountid})).doc; return account.toJson(); } + } diff --git a/src/models/Account.ts b/src/models/Account.ts index 8a67796..005ada0 100644 --- a/src/models/Account.ts +++ b/src/models/Account.ts @@ -1,18 +1,25 @@ -import {prop, getModelForClass, modelOptions, index, plugin} from '@typegoose/typegoose'; +import { + prop, + getModelForClass, + modelOptions, + index, + plugin, + mongoose +} from '@typegoose/typegoose'; import {dbconn} from '../decorators/dbconn'; // @ts-ignore import findOrCreate from 'mongoose-findorcreate'; import {Base, FindOrCreate, TimeStamps} from "@typegoose/typegoose/lib/defaultClasses"; -interface AccountClass extends Base, TimeStamps {} +interface AccountClass extends Base, TimeStamps {} @dbconn() -@index({ accountid: 1}, { unique: true }) +// @index({ _id: 1}, { unique: true }) @plugin(findOrCreate) @modelOptions({schemaOptions: {collection: "account", timestamps: true}}) class AccountClass extends FindOrCreate{ @prop() - public accountid: string; + public _id: string; @prop() public city?: string; @prop({default: false}) @@ -24,9 +31,12 @@ class AccountClass extends FindOrCreate{ @prop() public lastLogin?: Date; + @prop({type: mongoose.Schema.Types.Mixed}) + public moreinfo: any; + public toJson() { return { - accountid: this.accountid, + accountid: this._id, } } }