添加获取用户信息的接口

This commit is contained in:
zhl 2021-01-07 17:51:09 +08:00
parent a4d4dce8ee
commit 9e4719eadb
8 changed files with 283 additions and 22 deletions

View File

@ -6,7 +6,7 @@
通用返回JSON结构, 接口Response的数据结构说明只包含data部分
``` JSON
{
"errcode": 0, //0:成功
"errcode": 0, //0:成功 4: 帐号被封, 100: 所有未定义的错误
"errmsg": "", //错误描述
"data": {}, // 数据
}
@ -26,7 +26,31 @@
3. Response: JSON
```js
{
accountid: '',
cards: [number],
heros: [{
heroid: number,
owned: true, // 是否已拥有
usetype: 1, // 赛季专属, 0: 通用, 1: 赛季排位专用, 2: 匹配专用
level: number, // 等级
exp: number, // 当前的经验值
free: false, // 是否免费
free_expire: 1609919293, // 免费到期时间, 0: 说明是永久免费
}],
moneys: {
'coin': 0, // 金币
'diamond': 0, // 钻石
'hero_shard': 0, // 通用英雄碎片
'hero_exp': 0, // 通用英雄经验
'hero_shard_heroid': 0, // 英雄专用碎片
'hero_exp_heroid': 0, // 英雄专用经验
'card_scroll': 0, //抽卡卷轴
},
normal_stat: [0, 0, 0, 0] //匹配: 胜利场数, 失败场数, 平局场数, 掉线场数
season_stat: [0, 0, 0, 0] // 当前赛季状态
season_rank: 1 // 当前赛季排名
}
```
### 2. 可用卡牌信息
@ -55,7 +79,7 @@
### 3. 可用英雄列表
1. Method: POST
2. URI: /api/:accountid
2. URI: /api/:accountid/heros
| 字段 | 说明 |
| -------- | -------------------------------------- |
@ -70,12 +94,12 @@
```js
[{
heroid: '', // 英雄id
heroid: 1022, // 英雄id
owned: true, // 是否已拥有
ban: false, // 是否被禁用
usetype: 1, // 赛季专属, 0: 通用, 1: 赛季排位专用, 2: 匹配专用
free: false, // 是否免费
free_expire: 1609919293 // 免费到期时间
free_expire: 1609919293 // 免费到期时间, 0: 说明是永久免费
level: 1, // 等级
exp: 0, // 当前的经验值
}]
@ -94,7 +118,7 @@
```js
[{
id: '', // 卡组id
gid: '', // 卡组id
heroid: '', // 英雄id
selected: true, // 当前选择的卡组
isdefault: false, // 是否是默认卡组, 默认卡组不能编辑

View File

@ -42,9 +42,11 @@ export class ApiServer {
logger.log('register api routers');
let self = this;
for (let [controller, config] of RouterMap.decoratedRouters) {
let controllers = Array.isArray(controller) ? controller : [controller]
let controllers =
Array.isArray(controller) ? controller : [controller]
controllers.forEach((controller) => {
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
self.server[config.method || 'all'](config.path, {
preValidation: async function (request: FastifyRequest, reply: FastifyReply) {
@ -54,7 +56,6 @@ export class ApiServer {
}, controller);
})
}
// this.server.register(new AccountRouter().applyAll, {prefix: 'user', logLevel: 'debug'});
}
/**
* controller

View File

@ -2,17 +2,46 @@ import BaseController from "../common/base.controller";
import {role, router} from "../decorators/router";
import {Account} from "../models/Account";
import {ZError} from "../common/ZError";
import {Hero} from "../models/subdoc/Hero";
export default class AccountController extends BaseController {
@role('anon')
@router('post /api/:accountid/uinfo')
async info(req: any, res: any) {
async info(req: any) {
let {accountid} = req.params;
// if (true) {
// throw new ZError(101, 'no acc');
// }
let account = (await Account.findOrCreate({_id: accountid})).doc;
return account.toJson();
let result: any = {accountid: account.id};
if (account.locked) {
throw new ZError(4, 'account locked');
}
result.cards = account.cards;
let heros: any[] = [];
for(let [key, hero] of account.heros) {
heros.push({
heroid: hero.heroid,
owned: true,
usetype: 0,
level: hero.level,
exp: hero.exp,
free: hero.free,
free_expire: 0
});
}
//TODO:: 添加限免英雄和免费英雄
result.heros = heros;
// let hero = new Hero();
// hero.free = true;
// hero.level = 1;
// hero.exp = 0;
// hero.heroid = 30012;
// account.heros.set(30012+'', hero)
await account.save();
result.moneys = account.moneys;
result.normal_stat= account.normal_stat;
result.season_stat = account.season_stat;
result.extinfo = account.extinfo;
result.rank = 0;
return result;
}
}

View File

@ -9,30 +9,74 @@ import {
import {dbconn} from '../decorators/dbconn';
// @ts-ignore
import findOrCreate from 'mongoose-findorcreate';
import {Base, FindOrCreate, TimeStamps} from "@typegoose/typegoose/lib/defaultClasses";
import {
Base,
FindOrCreate,
TimeStamps
} from "@typegoose/typegoose/lib/defaultClasses";
import {Card} from "./subdoc/Card";
import {Hero} from "./subdoc/Hero";
interface AccountClass extends Base<string>, TimeStamps {}
@dbconn()
// @index({ _id: 1}, { unique: true })
@plugin(findOrCreate)
@modelOptions({schemaOptions: {collection: "account", timestamps: true}})
@modelOptions({schemaOptions:
{collection: "account", timestamps: true}
})
class AccountClass extends FindOrCreate{
@prop()
public _id: string;
@prop()
public city?: string;
@prop({default: false})
public locked: boolean;
@prop()
public lockedTime?: Date;
public lockedtime?: number;
@prop()
public comment?: string;
@prop()
public lastLogin?: Date;
/**
*
*/
@prop({ type: () => [Number] })
public cards: number[];
/**
*
*/
@prop({ type: Hero, default: new Map() })
public heros: Map<string, Hero>;
/**
*
* coin: 金币
* diamond: 钻石
* hero_shard: 通用英雄碎片
* hero_exp: 通用英雄经验
* hero_shard_heroid: 英雄专用碎片
* hero_exp_heroid: 英雄专用经验
* card_scroll: 抽卡卷轴
*/
@prop({ type: Number, default: new Map() })
public moneys: Map<string, number>;
/**
* 匹配: 胜利场数, , , 线
*/
@prop({type: Number, default: [0,0,0,0]})
public normal_stat: number[];
/**
* 当前赛季: 胜利场数, , , 线
*/
@prop({type: Number, default: [0,0,0,0]})
public season_stat: number[];
/**
* ,
*/
@prop({type: mongoose.Schema.Types.Mixed})
public moreinfo: any;
public extinfo: any;
public toJson() {
return {
@ -41,5 +85,7 @@ class AccountClass extends FindOrCreate{
}
}
export const Account = getModelForClass(AccountClass,
// @ts-ignore
export const Account = getModelForClass(AccountClass, {existingConnection: AccountClass['db']});
{existingConnection: AccountClass['db']});

64
src/models/CardGroup.ts Normal file
View File

@ -0,0 +1,64 @@
import {
getModelForClass,
index,
modelOptions,
prop
} from "@typegoose/typegoose";
import {Base, TimeStamps} from "@typegoose/typegoose/lib/defaultClasses";
import {dbconn} from "../decorators/dbconn";
import {Card} from "./subdoc/Card";
interface CardGroupClass extends Base<string>, TimeStamps {
}
/**
*
*/
@dbconn()
@index({accountid: 1, heroid: 1, deleted: 1}, {unique: false})
@modelOptions({
schemaOptions:
{collection: "card_group", timestamps: true}
})
class CardGroupClass {
@prop({required: true})
public accountid!: string;
@prop({required: true})
public heroid!: number;
/**
*
*/
@prop({default: false})
public selected: boolean;
/**
*
*/
@prop({default: false})
public isdefault: boolean;
@prop({_id: false, type: () => [Card]})
public cards: Card[];
/**
*
*/
@prop({default: false})
public deleted: boolean;
/**
*
*/
@prop()
public deletetime: Date;
public toJson() {
return {
heroid: this.heroid,
selected: this.selected,
isdefault: this.isdefault,
cards: this.cards.map(o => o.toJson())
}
}
}
export const CardGroup = getModelForClass(CardGroupClass,
// @ts-ignore
{existingConnection: CardGroupClass['db']})

36
src/models/GameRecord.ts Normal file
View File

@ -0,0 +1,36 @@
import {dbconn} from "../decorators/dbconn";
import {
getModelForClass,
index,
modelOptions,
prop
} from "@typegoose/typegoose";
import {Base, TimeStamps} from "@typegoose/typegoose/lib/defaultClasses";
interface GameRecordClass extends Base<string>, TimeStamps {
}
/**
*
*/
@dbconn()
@index({accountid: 1}, {unique: false})
@modelOptions({
schemaOptions:
{collection: "game_record", timestamps: true}
})
class GameRecordClass {
@prop()
public accountid: string;
toJson() {
return {
accountid: this.accountid,
}
}
}
export const GameRecord = getModelForClass(GameRecordClass,
// @ts-ignore
{existingConnection: GameRecordClass['db']})

46
src/models/subdoc/Card.ts Normal file
View File

@ -0,0 +1,46 @@
import {prop} from "@typegoose/typegoose";
/**
*
*/
export class Card {
@prop()
public cardid: number;
/**
*
*/
@prop()
public owned: boolean;
/**
*
*/
@prop({default: false})
public ban: boolean;
/**
*
* 0: 通用, 1: 赛季排位专用, 2: 匹配专用
*/
@prop({default: 0})
public usetype: number;
/**
*
*/
@prop({default: false})
public free: boolean;
/**
*
*/
@prop()
public free_expire: number;
public toJson() {
return {
cardid: this.cardid,
owned: this.owned,
ban: this.ban,
usetype: this.usetype,
free: this.free,
free_expire: this.free_expire
}
}
}

15
src/models/subdoc/Hero.ts Normal file
View File

@ -0,0 +1,15 @@
import {prop} from "@typegoose/typegoose";
export class Hero {
@prop()
public heroid: number;
/**
*
*/
@prop({default: false})
public free: boolean;
@prop({default: 1})
public level: number;
@prop({default: 0})
public exp: number;
}