简化数据返回方法

This commit is contained in:
zhl 2021-01-07 14:19:53 +08:00
parent 8f9c5e3f64
commit a4d4dce8ee
4 changed files with 34 additions and 13 deletions

View File

@ -97,7 +97,7 @@
id: '', // 卡组id id: '', // 卡组id
heroid: '', // 英雄id heroid: '', // 英雄id
selected: true, // 当前选择的卡组 selected: true, // 当前选择的卡组
iddefault: false, // 是否是默认卡组, 默认卡组不能编辑 isdefault: false, // 是否是默认卡组, 默认卡组不能编辑
cards: [{ cards: [{
cardid: '', // 卡牌id cardid: '', // 卡牌id
owned: true, // 是否已拥有 owned: true, // 是否已拥有

View File

@ -47,9 +47,9 @@ export class ApiServer {
logger.info('find api router', config.method || 'all', config.path, controller.name); logger.info('find api router', config.method || 'all', config.path, controller.name);
// @ts-ignore // @ts-ignore
self.server[config.method || 'all'](config.path, { 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; request.roles = config.roles;
await this.apiAuth(request, reply, done); await this.apiAuth(request, reply);
} }
}, controller); }, controller);
}) })
@ -100,8 +100,19 @@ export class ApiServer {
reply.send({errcode: statusCode, errmsg: error ? error.message : 'unknown error'}) reply.send({errcode: statusCode, errmsg: error ? error.message : 'unknown error'})
}) })
} }
/**
* ,
* {
* errcode: 0,
* errmsg?: '',
* data: any
* }
* @private
*/
private setFormatSend() { private setFormatSend() {
this.server.addHook('preSerialization', async (request: FastifyRequest, reply: FastifyReply, payload) => { this.server.addHook('preSerialization', async (request: FastifyRequest, reply: FastifyReply, payload) => {
reply.header('', '')
// @ts-ignore // @ts-ignore
if (!payload.errcode) { if (!payload.errcode) {
payload = { payload = {

View File

@ -8,11 +8,11 @@ export default class AccountController extends BaseController {
@router('post /api/:accountid/uinfo') @router('post /api/:accountid/uinfo')
async info(req: any, res: any) { async info(req: any, res: any) {
let {accountid} = req.params; let {accountid} = req.params;
if (true) { // if (true) {
throw new ZError(101, 'no acc'); // throw new ZError(101, 'no acc');
} // }
let account = (await Account.findOrCreate({accountid})).doc; let account = (await Account.findOrCreate({_id: accountid})).doc;
return account.toJson(); return account.toJson();
} }
} }

View File

@ -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'; import {dbconn} from '../decorators/dbconn';
// @ts-ignore // @ts-ignore
import findOrCreate from 'mongoose-findorcreate'; import findOrCreate from 'mongoose-findorcreate';
import {Base, FindOrCreate, TimeStamps} from "@typegoose/typegoose/lib/defaultClasses"; import {Base, FindOrCreate, TimeStamps} from "@typegoose/typegoose/lib/defaultClasses";
interface AccountClass extends Base, TimeStamps {} interface AccountClass extends Base<string>, TimeStamps {}
@dbconn() @dbconn()
@index({ accountid: 1}, { unique: true }) // @index({ _id: 1}, { unique: true })
@plugin(findOrCreate) @plugin(findOrCreate)
@modelOptions({schemaOptions: {collection: "account", timestamps: true}}) @modelOptions({schemaOptions: {collection: "account", timestamps: true}})
class AccountClass extends FindOrCreate{ class AccountClass extends FindOrCreate{
@prop() @prop()
public accountid: string; public _id: string;
@prop() @prop()
public city?: string; public city?: string;
@prop({default: false}) @prop({default: false})
@ -24,9 +31,12 @@ class AccountClass extends FindOrCreate{
@prop() @prop()
public lastLogin?: Date; public lastLogin?: Date;
@prop({type: mongoose.Schema.Types.Mixed})
public moreinfo: any;
public toJson() { public toJson() {
return { return {
accountid: this.accountid, accountid: this._id,
} }
} }
} }