添加后台连接用的后端
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
import { Service } from 'egg';
|
||||
const csprng = require('csprng');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
var gamedata = {};
|
||||
|
||||
function initData () {
|
||||
fs.readdirSync(__dirname + '/../resource')
|
||||
.filter(function(file) {
|
||||
return (file.indexOf(".") !== 0) && (file !== "index.js");
|
||||
})
|
||||
//筛选有文件名且不是index进行遍历
|
||||
.forEach(function(file) {
|
||||
var name = file.split('.')[0];
|
||||
try {
|
||||
gamedata[name] = JSON.parse(
|
||||
fs.readFileSync(path.resolve(__dirname, "../resource/" + file))
|
||||
);
|
||||
} catch(e) {
|
||||
console.error('【文件缺少】:' + file);
|
||||
gamedata[name] = [];
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
initData();
|
||||
|
||||
/**
|
||||
* Utils Service
|
||||
*/
|
||||
export default class Utils extends Service {
|
||||
/**
|
||||
* 生成 len 长度的随机字符串
|
||||
* @param len 长度
|
||||
* @param radix 基数
|
||||
*/
|
||||
public generateStr(len: number, radix = 36) {
|
||||
return `${csprng(len, radix)}`;
|
||||
}
|
||||
|
||||
public genCode(len) {
|
||||
const chars = '123456789ABCDEFGHJKLMNPQRSTWXYZabcdefghijklmnopqrstuvwxyz';
|
||||
const charArr = chars.split('');
|
||||
let code = '';
|
||||
for (let i = 0; i < len; i++) {
|
||||
code += charArr[Math.floor(Math.random() * charArr.length)];
|
||||
}
|
||||
return code;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成指定长度的随机数
|
||||
* @param len 随机数长度
|
||||
*/
|
||||
public generateNum(len: number) {
|
||||
let code = '';
|
||||
for (let i = 0; i < len; i++) {
|
||||
code += parseInt(`${Math.random() * 10}`);
|
||||
}
|
||||
return code;
|
||||
}
|
||||
|
||||
public exceptionResult(status) {
|
||||
const { code, simStr } = status;
|
||||
return { status: code, data: simStr };
|
||||
}
|
||||
|
||||
public getGamedata(key) {
|
||||
return gamedata[key];
|
||||
}
|
||||
|
||||
public getHeroById(hid) {
|
||||
let heros = gamedata['dic_zyz_hero']||[];
|
||||
return heros.find(cur => {
|
||||
return cur.heroId == hid;
|
||||
});
|
||||
}
|
||||
public getWarById(warid) {
|
||||
let warInfo = gamedata['dic_zyz_gk']||[];
|
||||
return warInfo.find(cur => {
|
||||
return cur.war_id == warid
|
||||
});
|
||||
}
|
||||
|
||||
public getGoodById(gid) {
|
||||
console.log(gid)
|
||||
let goodsInfo = gamedata['goods']||[];
|
||||
return goodsInfo.find(cur => {
|
||||
return cur.good_id == gid
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user