简化数据返回方法

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

View File

@ -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 = {

View File

@ -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();
}
}

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';
// @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<string>, 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,
}
}
}