修改状态码机制,初步改造web-server中接口返回
This commit is contained in:
@@ -1,11 +1,18 @@
|
||||
export const STATUS_SUC = {code: 0, simStr: '成功'};
|
||||
export const STATUS_WRONG_PARMS = {code: 1, simStr: '参数错误'};
|
||||
export const STATUS_TOKEN_ERR = {code: 2, simStr: 'token失效'};
|
||||
export const STATUS_INTERNAL_ERR = {code: 3, simStr: '内部错误'};
|
||||
|
||||
export const SMS_IN_60S = {code: 100001, simStr: '60秒内只能发送一次'};
|
||||
export const SMS_CNT_LIMIT = {code: 100002, simStr: '今日短信条数已达上限'};
|
||||
export const SMS_INVALID = {code: 100003, simStr: '验证码无效'};
|
||||
|
||||
export const SERVER_NOT_FOUND = {code: 200001, simStr: '未找到服务器列表'};
|
||||
export const ROLE_NOT_FOUND = {code: 200002, simStr: '未找到角色'};
|
||||
export const STATUS = {
|
||||
// 接口或系统状态 0 - 9999
|
||||
SUCCESS: { code: 0, simStr: '成功' },
|
||||
WRONG_PARMS: { code: 1, simStr: '参数错误' },
|
||||
TOKEN_ERR: { code: 2, simStr: 'token失效' },
|
||||
INTERNAL_ERR: { code: 3, simStr: '内部错误' },
|
||||
// 账号相关状态 10001 - 19999
|
||||
SMS_IN_60S: { code: 10001, simStr: '60秒内只能发送一次' },
|
||||
SMS_CNT_LIMIT: { code: 10002, simStr: '今日短信条数已达上限' },
|
||||
SMS_INVALID: { code: 10003, simStr: '验证码无效' },
|
||||
SERVER_NOT_FOUND: { code: 10004, simStr: '未找到服务器列表' },
|
||||
ROLE_NOT_FOUND: { code: 10005, simStr: '未找到角色' },
|
||||
// 战斗相关状态 20001 - 29999
|
||||
TOWER_RESET_ERR: { code: 20001, simStr: '只能重置当前层' }
|
||||
// 养成相关状态 30001 - 39999
|
||||
// 社交相关状态 40001 - 49999
|
||||
// 运营模块相关状态 50001 - 59999
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { STATUS } from './../consts/statusCode';
|
||||
|
||||
import { getGamedata } from './gamedata';
|
||||
import { HeroModel } from '../db/Hero';
|
||||
@@ -179,4 +180,9 @@ export function getRandEelm(source: Array<any> = [], cnt = 1): Array<any> {
|
||||
}
|
||||
}
|
||||
return source.filter((_item, idx) => idxs.has(idx));
|
||||
}
|
||||
|
||||
export function resResult(status: {code: number, simStr: string}, data = null) {
|
||||
const { code, simStr } = status;
|
||||
return {code, msg: simStr, data};
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
import { SERVER_NOT_FOUND } from '@consts/statusCode';
|
||||
import { STATUS_SUC } from '@consts/statusCode';
|
||||
import { STATUS } from '@consts/statusCode';
|
||||
import { GameModel } from '@db/Game';
|
||||
import { Controller } from 'egg';
|
||||
|
||||
@@ -9,9 +8,9 @@ export default class GameController extends Controller {
|
||||
const { serverType } = ctx.request.body;
|
||||
const serverList: Array<any> = await GameModel.getServerListByType(serverType);
|
||||
if (serverList && serverList.length > 0) {
|
||||
ctx.body = { status: STATUS_SUC.code, data: { serverList } };
|
||||
ctx.body = ctx.service.utils.resResult(STATUS.SUCCESS, { serverList });
|
||||
} else {
|
||||
ctx.body = ctx.service.utils.exceptionResult(SERVER_NOT_FOUND);
|
||||
ctx.body = ctx.service.utils.resResult(STATUS.SERVER_NOT_FOUND);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { ENCRYPT_KEY, ENCRYPT_IV } from './../../../shared/consts/consts';
|
||||
import { ENCRYPT_KEY, ENCRYPT_IV } from '@consts/consts';
|
||||
import { Context } from 'egg';
|
||||
const crypto = require('crypto');
|
||||
const isJSON = require('koa-is-json');
|
||||
|
||||
@@ -1,17 +1,17 @@
|
||||
import { STATUS_TOKEN_ERR, STATUS_WRONG_PARMS } from '@consts/statusCode';
|
||||
import { UserModel } from './../db/User';
|
||||
import { STATUS } from '@consts/statusCode';
|
||||
import { UserModel } from '@db/User';
|
||||
|
||||
module.exports = () => {
|
||||
return async function tokenParser(ctx, next) {
|
||||
if (!ctx.request.body || !ctx.request.body.token) {
|
||||
console.error('token not found');
|
||||
ctx.body = ctx.service.utils.exceptionResult(STATUS_WRONG_PARMS);
|
||||
ctx.body = ctx.service.utils.resResult(STATUS.WRONG_PARMS);
|
||||
return;
|
||||
}
|
||||
const user = await UserModel.findUserByToken(ctx.request.body.token);
|
||||
if (!user) {
|
||||
console.error('token invalid');
|
||||
ctx.body = ctx.service.utils.exceptionResult(STATUS_TOKEN_ERR);
|
||||
ctx.body = ctx.service.utils.resResult(STATUS.TOKEN_ERR);
|
||||
return;
|
||||
}
|
||||
ctx.uid = user.uid;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { RoleModel } from '@db/Role';
|
||||
import { UserModel } from '@db/User';
|
||||
import { SMS_IN_60S, SMS_CNT_LIMIT, STATUS_SUC, STATUS_WRONG_PARMS, SMS_INVALID, ROLE_NOT_FOUND } from '@consts/statusCode';
|
||||
import { STATUS } from '@consts/statusCode';
|
||||
import { smsModel } from '@db/Sms';
|
||||
import { Service } from 'egg';
|
||||
import Counter from '@db/Counter';
|
||||
@@ -51,10 +51,10 @@ export default class Auth extends Service {
|
||||
const sms = await smsModel.findByTel(tel, false);
|
||||
if (sms) {
|
||||
if (await sms.timeLimit(10000)) {
|
||||
return this.ctx.service.utils.exceptionResult(SMS_IN_60S);
|
||||
return this.ctx.service.utils.resResult(STATUS.SMS_IN_60S);
|
||||
}
|
||||
if (await sms.cntLimit(8)) {
|
||||
return this.ctx.service.utils.exceptionResult(SMS_CNT_LIMIT);
|
||||
return this.ctx.service.utils.resResult(STATUS.SMS_CNT_LIMIT);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -68,7 +68,7 @@ export default class Auth extends Service {
|
||||
const smsResult = await this.sendSmsCodeByGuodu(tel, code);
|
||||
console.log(smsResult);
|
||||
await smsModel.updateByTel(tel, code, false, new Date(), sms?.hasSendToday() ? sms.countToday + 1 : 1);
|
||||
return this.ctx.service.utils.exceptionResult(STATUS_SUC);
|
||||
return this.ctx.service.utils.resResult(STATUS.SUCCESS);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -87,19 +87,19 @@ export default class Auth extends Service {
|
||||
return telVerify;
|
||||
}
|
||||
if (!_.isString(code) || code.length !== 6) {
|
||||
return ctx.service.utils.exceptionResult(STATUS_WRONG_PARMS);
|
||||
return ctx.service.utils.resResult(STATUS.WRONG_PARMS);
|
||||
}
|
||||
|
||||
// 手机验证码核验
|
||||
const smsValid: boolean = await smsModel.validateSms(tel, code);
|
||||
if (!smsValid) {
|
||||
return ctx.service.utils.exceptionResult(SMS_INVALID);
|
||||
return ctx.service.utils.resResult(STATUS.SMS_INVALID);
|
||||
}
|
||||
|
||||
// 用户注册登录
|
||||
const token = ctx.service.utils.generateStr(256);
|
||||
const user = await UserModel.updateToken(tel, token, platform, pkgName, serverType);
|
||||
return { status: STATUS_SUC.code, data: { token, uid: user?.uid } };
|
||||
return ctx.service.utils.resResult(STATUS.SUCCESS, { token, uid: user?.uid });
|
||||
}
|
||||
|
||||
public async checkRole(serverId: number) {
|
||||
@@ -107,9 +107,9 @@ export default class Auth extends Service {
|
||||
const { uid } = ctx;
|
||||
const role = await RoleModel.findByUid(uid, serverId);
|
||||
if (role) {
|
||||
return { status: STATUS_SUC.code, data: { roleId: role.roleId } };
|
||||
return ctx.service.utils.resResult(STATUS.SUCCESS, { roleId: role.roleId });
|
||||
}
|
||||
return ctx.service.utils.exceptionResult(ROLE_NOT_FOUND);
|
||||
return ctx.service.utils.resResult(STATUS.ROLE_NOT_FOUND);
|
||||
}
|
||||
|
||||
public async createRole(serverId: number, roleName: string) {
|
||||
@@ -121,8 +121,8 @@ export default class Auth extends Service {
|
||||
const seqId = await Counter.getNewCounter('role') || -1;
|
||||
const role = await RoleModel.createRole(uid, serverId, { roleId, code, roleName, seqId });
|
||||
if (role) {
|
||||
return { status: STATUS_SUC.code, data: { roleId: role.roleId } };
|
||||
return ctx.service.utils.resResult(STATUS.SUCCESS, { roleId: role.roleId });
|
||||
}
|
||||
return ctx.service.utils.exceptionResult(ROLE_NOT_FOUND);
|
||||
return ctx.service.utils.resResult(STATUS.ROLE_NOT_FOUND);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { Service } from 'egg';
|
||||
import { resResult as pubResult } from '../pubUtils/util';
|
||||
const csprng = require('csprng');
|
||||
/**
|
||||
* Utils Service
|
||||
@@ -35,8 +36,7 @@ export default class Utils extends Service {
|
||||
return code;
|
||||
}
|
||||
|
||||
public exceptionResult(status) {
|
||||
const { code, simStr } = status;
|
||||
return { status: code, data: simStr };
|
||||
public resResult(status: {code: number, simStr: string}, data?) {
|
||||
return pubResult(status, data);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user