修改router标签, 支持配置多个路由, 修改服务端调用的方法, 将路径改为svr
This commit is contained in:
parent
a9445be97b
commit
9df9d0c216
@ -44,19 +44,17 @@ export class ApiServer {
|
|||||||
logger.log('register api routers');
|
logger.log('register api routers');
|
||||||
let self = this;
|
let self = this;
|
||||||
for (let [controller, config] of RouterMap.decoratedRouters) {
|
for (let [controller, config] of RouterMap.decoratedRouters) {
|
||||||
let controllers =
|
for(let data of config.data) {
|
||||||
Array.isArray(controller) ? controller : [controller]
|
logger.info('find api router', data.method || 'all',
|
||||||
controllers.forEach((controller) => {
|
data.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[data.method || 'all'](data.path, {
|
||||||
preValidation: async function (request: FastifyRequest, reply: FastifyReply) {
|
preValidation: async function (request: FastifyRequest, reply: FastifyReply) {
|
||||||
request.roles = config.roles;
|
request.roles = config.roles;
|
||||||
await this.apiAuth(request, reply);
|
await this.apiAuth(request, reply);
|
||||||
}
|
}
|
||||||
}, controller);
|
}, controller);
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
|
@ -24,8 +24,7 @@ export default class CardController extends BaseController {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@role('svr')
|
@router('get /svr/:accountid/group_info/:heroid/:gid')
|
||||||
@router('get /api/:accountid/group_info/:heroid/:gid')
|
|
||||||
async cardGroupInfo(req: any) {
|
async cardGroupInfo(req: any) {
|
||||||
let {accountid, heroid, gid} = req.params;
|
let {accountid, heroid, gid} = req.params;
|
||||||
let record;
|
let record;
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
import BaseController from "../common/base.controller";
|
import BaseController from "../common/base.controller";
|
||||||
import {router} from "../decorators/router";
|
import {router} from "../decorators/router";
|
||||||
import {ZError} from "../common/ZError";
|
import {ZError} from "../common/ZError";
|
||||||
|
import {BaseConst} from "../constants/BaseConst";
|
||||||
|
import {CardGroup} from "../models/CardGroup";
|
||||||
|
import {Card} from "../models/subdoc/Card";
|
||||||
|
|
||||||
export default class HeroController extends BaseController {
|
export default class HeroController extends BaseController {
|
||||||
@router('post /api/:accountid/heros')
|
@router('post /api/:accountid/heros')
|
||||||
@ -15,6 +18,7 @@ export default class HeroController extends BaseController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@router('post /api/:accountid/hero/unlock/:heroid')
|
@router('post /api/:accountid/hero/unlock/:heroid')
|
||||||
|
@router('post /svr/:accountid/hero/unlock/:heroid')
|
||||||
async unlockHero(req: any) {
|
async unlockHero(req: any) {
|
||||||
let account = req.user;
|
let account = req.user;
|
||||||
let {heroid, type} = req.params;
|
let {heroid, type} = req.params;
|
||||||
@ -23,7 +27,35 @@ export default class HeroController extends BaseController {
|
|||||||
}
|
}
|
||||||
let hero;
|
let hero;
|
||||||
if (!type) {
|
if (!type) {
|
||||||
hero = await account.unlockHero(heroid);
|
let userMoney = !req.url.startsWith('/svr');
|
||||||
|
hero = await account.unlockHero(heroid, userMoney);
|
||||||
|
// 将该英雄的默认卡组添加到玩家的卡组中,
|
||||||
|
// 将默认卡组里的卡添加到玩家可用卡牌中
|
||||||
|
let cardSet = new Set(account.cards);
|
||||||
|
let cfg = global.$cfg.get(BaseConst.HERO).get(parseInt(hero.heroid));
|
||||||
|
let cardgroup = new CardGroup({});
|
||||||
|
let cards: Card[] = [];
|
||||||
|
for (let i = 1; i < 10; i++) {
|
||||||
|
if (!cfg[`follower${i}id`]) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
let card = new Card();
|
||||||
|
card.cardid = cfg[`follower${i}id`];
|
||||||
|
card.owned = false;
|
||||||
|
card.ban = false;
|
||||||
|
card.usetype = 0;
|
||||||
|
card.free = true;
|
||||||
|
card.free_expire = 0;
|
||||||
|
cards.push(card);
|
||||||
|
cardSet.add(card.cardid);
|
||||||
|
}
|
||||||
|
cardgroup.accountid = account.id;
|
||||||
|
cardgroup.heroid = hero.heroid;
|
||||||
|
cardgroup.selected = false;
|
||||||
|
cardgroup.isdefault = true;
|
||||||
|
cardgroup.cards = cards;
|
||||||
|
await cardgroup.save();
|
||||||
|
account.cards = [...cardSet];
|
||||||
await account.save();
|
await account.save();
|
||||||
} else {
|
} else {
|
||||||
hero = await account.tryHero(heroid);
|
hero = await account.tryHero(heroid);
|
||||||
|
@ -17,7 +17,7 @@ export default class RecordController extends BaseController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@role('anon')
|
@role('anon')
|
||||||
@router('post /api/record/save')
|
@router('post /svr/record/save')
|
||||||
async upload(req: any) {
|
async upload(req: any) {
|
||||||
let record = new GameRecord(req.params);
|
let record = new GameRecord(req.params);
|
||||||
await record.save();
|
await record.save();
|
||||||
|
@ -1,12 +1,16 @@
|
|||||||
import BaseController from '../common/base.controller';
|
import BaseController from '../common/base.controller';
|
||||||
|
|
||||||
|
export class RouterData {
|
||||||
|
target?: any
|
||||||
|
method?: string
|
||||||
|
path?: string
|
||||||
|
fun?: Function
|
||||||
|
}
|
||||||
export class RouterMap {
|
export class RouterMap {
|
||||||
static decoratedRouters: Map<Function | Function[], {
|
static decoratedRouters: Map<Function, {
|
||||||
target?: any,
|
|
||||||
method?: string,
|
|
||||||
path?: string,
|
|
||||||
roles?: string[],
|
roles?: string[],
|
||||||
permissions?: string[]
|
permissions?: string[],
|
||||||
|
data?: RouterData[]
|
||||||
}> = new Map()
|
}> = new Map()
|
||||||
}
|
}
|
||||||
export function router(route?: string) {
|
export function router(route?: string) {
|
||||||
@ -23,17 +27,26 @@ export function router(route?: string) {
|
|||||||
const [method, path] = split
|
const [method, path] = split
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
const key = target[name];
|
const key = target[name];
|
||||||
let routerObj = {
|
let routerData = new RouterData();
|
||||||
target: target,
|
routerData.target = target;
|
||||||
path: path,
|
routerData.method = method;
|
||||||
method: method
|
routerData.path = path;
|
||||||
};
|
// @ts-ignore
|
||||||
|
routerData.fun = target[name];
|
||||||
|
|
||||||
if (RouterMap.decoratedRouters.has(key)) {
|
if (RouterMap.decoratedRouters.has(key)) {
|
||||||
let objCurrent = RouterMap.decoratedRouters.get(key);
|
let objCurrent = RouterMap.decoratedRouters.get(key);
|
||||||
Object.assign(objCurrent, routerObj);
|
if (!objCurrent.data) {
|
||||||
|
objCurrent.data = [routerData];
|
||||||
|
} else {
|
||||||
|
objCurrent.data.push(routerData);
|
||||||
|
}
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
RouterMap.decoratedRouters.set(target[name], objCurrent);
|
RouterMap.decoratedRouters.set(target[name], objCurrent);
|
||||||
} else {
|
} else {
|
||||||
|
let routerObj = {
|
||||||
|
data: [routerData]
|
||||||
|
};
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
RouterMap.decoratedRouters.set(target[name], routerObj);
|
RouterMap.decoratedRouters.set(target[name], routerObj);
|
||||||
}
|
}
|
||||||
|
@ -74,6 +74,11 @@ class UserClass extends FindOrCreate{
|
|||||||
@prop({type: Number, default: [0,0,0,0]})
|
@prop({type: Number, default: [0,0,0,0]})
|
||||||
public season_stat: number[];
|
public season_stat: number[];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 当前赛季排位分
|
||||||
|
*/
|
||||||
|
@prop({type: Number, default: 1000})
|
||||||
|
public season_score: number;
|
||||||
/**
|
/**
|
||||||
* 所有未定义的信息, 供扩展
|
* 所有未定义的信息, 供扩展
|
||||||
*/
|
*/
|
||||||
@ -86,12 +91,13 @@ class UserClass extends FindOrCreate{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async unlockHero(heroid: number) {
|
public async unlockHero(heroid: number, useMoney: boolean) {
|
||||||
if (this.heros.has(heroid + '')) {
|
if (this.heros.has(heroid + '')) {
|
||||||
if (!this.heros.get(heroid + '').trial) {
|
if (!this.heros.get(heroid + '').trial) {
|
||||||
throw new ZError(102, '你已经解锁了该英雄');
|
throw new ZError(102, '你已经解锁了该英雄');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (useMoney) {
|
||||||
let money0 = MoneyTypeConst.getHeroShard(heroid);
|
let money0 = MoneyTypeConst.getHeroShard(heroid);
|
||||||
let money1 = MoneyTypeConst.HERO_SHARD;
|
let money1 = MoneyTypeConst.HERO_SHARD;
|
||||||
let count0 = this.moneys.has(money0) ? this.moneys.get(money0) : 0;
|
let count0 = this.moneys.has(money0) ? this.moneys.get(money0) : 0;
|
||||||
@ -116,6 +122,7 @@ class UserClass extends FindOrCreate{
|
|||||||
count1 = count1 - needCount;
|
count1 = count1 - needCount;
|
||||||
this.moneys.set(money1, count1);
|
this.moneys.set(money1, count1);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
let hero = new Hero();
|
let hero = new Hero();
|
||||||
hero.heroid = heroid;
|
hero.heroid = heroid;
|
||||||
hero.free = false;
|
hero.free = false;
|
||||||
|
@ -26,11 +26,15 @@ const apiAuthPlugin: FastifyPluginAsync = async function(
|
|||||||
|
|
||||||
// 只有路由配置的role为anon才不需要过滤
|
// 只有路由配置的role为anon才不需要过滤
|
||||||
fastify.decorate("apiAuth", async function(request: FastifyRequest, reply: FastifyReply) {
|
fastify.decorate("apiAuth", async function(request: FastifyRequest, reply: FastifyReply) {
|
||||||
|
if (request.url.startsWith('/svr')) {
|
||||||
|
// @ts-ignore
|
||||||
|
let { accountid } = request.params;
|
||||||
|
if (accountid) {
|
||||||
|
request.user = await User.findById(accountid);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
if (!request.roles || request.roles.indexOf('anon') == -1) {
|
if (!request.roles || request.roles.indexOf('anon') == -1) {
|
||||||
try {
|
try {
|
||||||
if (request.roles && request.roles.indexOf('svr') >= 0) {
|
|
||||||
// TODO: check svr
|
|
||||||
} else {
|
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
let { accountid, sessionid } = request.params;
|
let { accountid, sessionid } = request.params;
|
||||||
//TODO: 增加sessionid的校验
|
//TODO: 增加sessionid的校验
|
||||||
@ -52,12 +56,12 @@ const apiAuthPlugin: FastifyPluginAsync = async function(
|
|||||||
return reply.send({code: 4, msg: 'account locked'});
|
return reply.send({code: 4, msg: 'account locked'});
|
||||||
}
|
}
|
||||||
request.user = account;
|
request.user = account;
|
||||||
}
|
|
||||||
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return reply.send({code: 401, msg: 'need auth'})
|
return reply.send({code: 401, msg: 'need auth'})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
})
|
})
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user