129 lines
3.2 KiB
JavaScript
129 lines
3.2 KiB
JavaScript
const app = require('j7/app');
|
|
const utils = require('j7/utils');
|
|
const log = require('j7/log');
|
|
const metawrap = require('./metawrap');
|
|
|
|
const metaClasses = [];
|
|
|
|
const MT_CONFIG = 0;
|
|
const MT_WEB3 = 1;
|
|
const MT_CONTRACT = 2;
|
|
|
|
function registerMetaClass(fileName, idx, primKey, wrapClass) {
|
|
metaClasses.push({
|
|
'fileName' : fileName,
|
|
'idx' : idx,
|
|
'primKey' : primKey,
|
|
'wrapClass': wrapClass,
|
|
'rawList' : [],
|
|
'rawHash' : {},
|
|
'wrapList' : [],
|
|
'wrapHash' : {},
|
|
});
|
|
}
|
|
|
|
function load() {
|
|
metaClasses.forEach(function (metaClass) {
|
|
const json = utils.readJsonFromFile(metaClass['fileName']);
|
|
if (!json) {
|
|
throw new Error('读取配置' + metaClass['fileName'] + '失败');
|
|
}
|
|
if (Array.isArray(json)) {
|
|
metaClass['rawList'] = json;
|
|
|
|
} else {
|
|
metaClass['rawList'].push(json);
|
|
}
|
|
let idx = 0;
|
|
metaClass['rawList'].forEach(function (item) {
|
|
const wrapOjb = new metawrap[metaClass['wrapClass']](item);
|
|
const wrapProxy = new Proxy(wrapOjb, wrapOjb._getHandler());
|
|
metaClass['wrapList'] = wrapProxy;
|
|
if (metaClass['primKey'] == '') {
|
|
metaClass['rawHash'][idx] = item;
|
|
metaClass['wrapHash'][idx] = wrapProxy;
|
|
} else {
|
|
metaClass['rawHash'][item[metaClass['primKey']]] = item;
|
|
metaClass['wrapHash'][item[metaClass['primKey']]] = wrapProxy;
|
|
}
|
|
++idx;
|
|
});
|
|
//log.debug(utils.jsonEncode(metaClass));
|
|
});
|
|
}
|
|
|
|
function init() {
|
|
let configDir = './config/';
|
|
if (utils.isOnlineEnv()) {
|
|
configDir = '../config/';
|
|
}
|
|
registerMetaClass(configDir + 'config.json',
|
|
MT_CONFIG,
|
|
'',
|
|
'Config'
|
|
);
|
|
registerMetaClass(configDir + 'web3.json',
|
|
MT_CONFIG,
|
|
'',
|
|
'Web3'
|
|
);
|
|
registerMetaClass(configDir + 'contract.json',
|
|
MT_CONTRACT,
|
|
'name',
|
|
'Contract'
|
|
);
|
|
load();
|
|
}
|
|
|
|
function getMetaClass(metaIdx) {
|
|
return metaIdx >= 0 && metaIdx < metaClasses.length ? metaClasses[metaIdx] : null;
|
|
}
|
|
|
|
function getMetaByKey(metaIdx, key) {
|
|
const metaClass = getMetaClass(metaIdx);
|
|
return metaClass && key in metaClass['wrapHash'] ? metaClass['wrapHash'][key] : null;
|
|
}
|
|
|
|
function getMetaList(metaIdx) {
|
|
const metaClass = getMetaClass(metaIdx);
|
|
return metaClass ? metaClass['rawList'] : null;
|
|
}
|
|
|
|
function getWeb3Conf() {
|
|
return getMetaByKey(MT_WEB3, '0');
|
|
}
|
|
|
|
function getContracts() {
|
|
return getMetaList(MT_CONTRACT);
|
|
}
|
|
|
|
function getContractByName(name) {
|
|
return getMetaByKey(MT_CONTRACT, name);
|
|
}
|
|
|
|
function getContractByAddress(address) {
|
|
let contract = null;
|
|
getContracts().forEach((item) => {
|
|
if (item['address'] == address) {
|
|
contract = item;
|
|
}
|
|
});
|
|
return contract;
|
|
}
|
|
|
|
function getUserAddress() {
|
|
return getWeb3Conf()['user_address'];
|
|
}
|
|
|
|
function getPrivateKey() {
|
|
return getWeb3Conf()['private_key'];
|
|
}
|
|
|
|
exports.init = init;
|
|
exports.getWeb3Conf = getWeb3Conf;
|
|
exports.getContracts = getContracts;
|
|
exports.getContractByName = getContractByName;
|
|
exports.getContractByAddress = getContractByAddress;
|
|
exports.getUserAddress = getUserAddress;
|
|
exports.getPrivateKey = getPrivateKey;
|