Commit ef9e7c04 by 王昆

gsb

parent 79c97af3
......@@ -6,9 +6,10 @@ const md5 = require("MD5");
class APIBase extends DocBase {
constructor() {
super();
this.cacheManager = system.getObject("db.common.cacheManager");
this.logCtl = system.getObject("web.common.oplogCtl");
this.merchantSve = system.getObject("service.merchant.merchantSve");
this.oplogSve = system.getObject("service.common.oplogSve");
this.SIGN_GNAME = ['sign'];
this.EXCEPT_KEYS = ['sign', 'requestid'];
}
getUUID() {
var uuid = uuidv4();
......@@ -21,96 +22,79 @@ class APIBase extends DocBase {
}
return o.toString().trim();
}
/**
* 验证签名
* @param {*} params 要验证的参数
* @param {*} app_key 应用的校验key
*/
async verifySign(params, app_key) {
if (!params) {
return system.getResult(null, "请求参数为空");
async validSign(params) {
let appId = this.trim(params.appId);
let timestamp = Number(params.timestamp);
let nonceStr = Number(params.nonceStr);
let now = new Date().getTime();
if (now - timestamp > 60 * 60 * 60 * 1000) {
return system.getResultFail(1000000, "请求超时");
}
if (!appId) {
return system.getResultFail(1000000, "请填写appId");
}
if (!params.sign) {
return system.getResult(null, "请求参数sign为空");
if (!nonceStr) {
return system.getResultFail(1000000, "随机码为空");
}
// TODO redis通过sign幂等验证
// 幂等验证代码xxxx
let app = await this.merchantSve.apiWidthCache({id: appId})
if (!app || !app.id) {
return system.getResultFail(1000000, "appId不存在");
}
if (!params.times_tamp) {
return system.getResult(null, "请求参数times_tamp为空");
if (!app.is_enabled) {
return system.getResultFail(1000000, "该应用已失效");
}
var signArr = [];
var keys = Object.keys(params).sort();
if (keys.length == 0) {
return system.getResult(null, "请求参数信息为空");
if (!app.merchant_id) {
return system.getResultFail(1000000, "该应用未绑定商户");
}
let keys = Object.keys(params).sort();
let signArr = [];
for (let k = 0; k < keys.length; k++) {
const tKey = keys[k];
if (tKey != "sign" && params[tKey]) {
let tKey = keys[k];
if (this.EXCEPT_KEYS.indexOf(tKey) == -1 && params[tKey]) {
signArr.push(tKey + "=" + params[tKey]);
}
}
if (signArr.length == 0) {
return system.getResult(null, "请求参数组装签名参数信息为空");
}
var resultSignStr = signArr.join("&") + "&key=" + app_key;
var resultTmpSign = md5(resultSignStr).toUpperCase();
if (params.sign != resultTmpSign) {
return system.getResult(null, "签名验证失败");
let signStr = signArr.join("&") + "&key=" + app.secret;
let sign = md5(signStr).toUpperCase();
console.log(params.sign, signStr, sign);
if (params.sign != sign) {
return system.getResultFail(1001001, "签名验证失败");
}
params.app = app;
return system.getResultSuccess();
}
/**
* 白名单验证
* @param {*} gname 组名
* @param {*} methodname 方法名
*/
async isCheckWhiteList(gname, methodname) {
var fullname = gname + "." + methodname;
var lst = [
"test.testApi"
];
var x = lst.indexOf(fullname);
return x >= 0;
}
async checkAcck(gname, methodname, pobj, query, req) {
var appInfo = null;
var result = system.getResultSuccess();
var ispass = await this.isCheckWhiteList(gname, methodname);
var appkey = req.headers["accesskey"];
var app_id = req.headers["app_id"];
if (ispass) {
return result;
}//在白名单里面
if (app_id) {
// var signResult = await this.verifySign(pobj.action_body, appInfo.appSecret);
// if (signResult.status != 0) {
// result.status = system.signFail;
// result.msg = signResult.msg;
// }
}//验签
else if (appkey) {
appInfo = await this.cacheManager["ApiAccessKeyCheckCache"].cache(appkey, { status: true }, 3000);
if (!appInfo || !appInfo.app) {
result.status = system.tokenFail;
result.msg = "请求头accesskey失效,请重新获取";
}
}//验证accesskey
else {
result.status = -1;
result.msg = "请求头没有相关访问参数,请验证后在进行请求";
async checkSign(gname, params) {
if (this.SIGN_GNAME.indexOf(gname) != -1) {
return await this.validSign(params);
}
return result;
return system.getResultSuccess();
}
async doexec(gname, methodname, pobj, query, req) {
var requestid = this.getUUID();
pobj.requestid = requestid;
let rtn;
try {
let signRes = await this.checkSign(gname, pobj);
if (signRes.status !== 0) {
return signRes;
}
rtn = await this[methodname](pobj, query, req) || {};
rtn.requestid = requestid;
return rtn;
} catch (e) {
console.log(e.stack, "api调用出现异常,请联系管理员..........")
rtn = system.getResultFail(-200, "出现异常,请联系管理员");
rtn.requestid = requestid;
}
try {
return rtn;
} finally {
this.oplogSve.createDb({
appid: "",
appkey: "",
......@@ -122,10 +106,7 @@ class APIBase extends DocBase {
agent: req.uagent,
opTitle: "api服务提供方appKey:" + settings.appKey,
});
} catch (e) {
console.log(new Date(), requestid, e.stack);
}
return rtn;
}
}
module.exports = APIBase;
......
......@@ -8,7 +8,7 @@ class ActionAPI extends APIBase {
this.authSve = system.getObject("service.sign.authSve");
this.merchantSve = system.getObject("service.merchant.merchantSve");
// this.userSve = system.getObject("service.user.userSve");
this.enginsignSve = system.getObject("service.engine.enginesignSve");
this.enginsignSve = system.getObject("service.engine.enginesignSve");
}
/**
* 接口跳转
......@@ -25,11 +25,6 @@ class ActionAPI extends APIBase {
return system.getResult(null, "action_type参数不能为空");
}
try {
// 验证签名
let signRes = await this.validSign(pobj.action_body);
if (signRes.status !== 0) {
return signRes;
}
result = await this.handleRequest(pobj.action_process, pobj.action_type, pobj.action_body);
} catch (error) {
console.log(error);
......@@ -79,46 +74,7 @@ class ActionAPI extends APIBase {
return opResult;
}
async validSign(params) {
// 1000000 报文参数问题 1001001 签名错误
let appId = this.trim(params.appId);
let timestamp = Number(params.timestamp);
let nonceStr = Number(params.nonceStr);
let now = new Date().getTime();
if (now - timestamp > 60 * 60 * 60 * 1000) {
return system.getResultFail(1000000, "请求超时");
}
if (!appId) {
return system.getResultFail(1000000, "请填写appId");
}
if (!nonceStr) {
return system.getResultFail(1000000, "随机码为空");
}
// TODO redis通过sign幂等验证
// 幂等验证代码xxxx
let app = await this.merchantSve.apiInfo({id: appId})
if (!app.data || !app.data.id) {
return system.getResultFail(1000000, "appId不存在");
}
app = app.data;
let keys = Object.keys(params).sort();
let signArr = [];
for (let k = 0; k < keys.length; k++) {
let tKey = keys[k];
if (tKey != "sign" && params[tKey]) {
signArr.push(tKey + "=" + params[tKey]);
}
}
let sign = md5(signArr.join("&") + "&key=" + app.secret).toUpperCase();
console.log(params.sign, sign);
if (params.sign != sign) {
return system.getResultFail(1001001, "签名验证失败");
}
return system.getResultSuccess();
}
exam() {
return `<pre><pre/>`;
......
var APIBase = require("../../api.base");
var system = require("../../../system");
class TestAPI extends APIBase {
constructor() {
super();
this.authSve = system.getObject("service.sign.authSve");
}
async nameTwo(pobj, query, req) {
return await this.authSve.nameTwo(pobj);
}
exam() {
return "";
}
classDesc() {
return {
groupName: "",
groupDesc: "",
name: "",
desc: "",
exam: "",
};
}
methodDescs() {
return [
{
methodDesc: "",
methodName: "",
paramdescs: [
{
paramDesc: "",
paramName: "",
paramType: "",
defaultValue: "",
}
],
rtnTypeDesc: "",
rtnType: ""
}
];
}
}
module.exports = TestAPI;
const system = require("../../../system");
const Dao = require("../../dao.base");
class TradeLogDao extends Dao {
constructor() {
super(Dao.getModelName(TradeLogDao));
}
}
module.exports = TradeLogDao;
\ No newline at end of file
const system = require("../../../system");
const settings = require("../../../../config/settings");
const uiconfig = system.getUiConfig2(settings.appKey);
module.exports = (db, DataTypes) => {
return db.define("tradelog", {
requestid: DataTypes.STRING,
merchant_id: DataTypes.STRING,
product_id: DataTypes.STRING,
product_specifications: DataTypes.BIGINT,
result: DataTypes.INTEGER,
}, {
paranoid: false,//假的删除
underscored: true,
version: true,
freezeTableName: true,
timestamps: true,
updatedAt: false,
//freezeTableName: true,
// define the table's name
tableName: 'api_trade_log',
validate: {
},
indexes: [
]
});
}
......@@ -7,59 +7,14 @@ class FeeService extends ServiceBase {
// 引擎不可以引用任何
}
/**
* 账户查询
* @param params
* account_id: 账户id
* @returns
*/
async account(params) {
try {
return await this.callms("engine_fee", "accountInfo", params);
} catch (error) {
return system.getResult(null, `系统错误 错误信息 ${error}`);
}
}
/**
* @param params
* {
* "currentPage": 1,
* "pageSize": 10,
* "account_id": "1",
* "trade_type": "1",
* "trade_no": "123313",
* "tradeTimeBegin": "2020-06-26 04:21:31",
* "tradeTimeEnd": "2020-06-26 05:11:31"
* }
* @returns
*/
async accountTradePage(params) {
// 交易计费
async trade(params) {
try {
return await this.callms("engine_fee", "accountTrade", params);
} catch (error) {
return system.getResult(null, `系统错误 错误信息 ${error}`);
}
}
/**
* account_id
* @param params
* {
* "account_id": "1",
* "trade_type": "1",
* "trade_nos": ['1','2'],
* }
* @returns {Promise<{msg: string, data: (*|null), bizmsg: string, status: number}|{msg: string, data, bizmsg: *|string, status: number}|any|undefined>}
*/
async tradeMapByIds(params) {
try {
return await this.callms("engine_fee", "tradeMapByIds", params);
} catch (error) {
return system.getResult(null, `系统错误 错误信息 ${error}`);
}
}
}
module.exports = FeeService;
\ No newline at end of file
......@@ -4,11 +4,32 @@ const ServiceBase = require("../../svems.base")
class MerchantService extends ServiceBase {
constructor() {
super();
this.redisClient = system.getObject("util.redisClient");
this.APP_CACHE_KEY = "APP_CACHE_KEY_";
}
async apiInfo(params) {
async apiInfoById(params) {
let apires = await this.callms("sve_merchant", "apiInfoById", params) || {};
return apires.data;
}
async apiWidthCache(params) {
try {
return await this.callms("sve_merchant", "apiInfoById", params);
let key = this.APP_CACHE_KEY + params.app_id;
let app = await this.redisClient.get(key);
if (app) {
app = JSON.parse(app);
}
if(!app || !app.id || params.forceUpdate) {
app = await this.apiInfoById(params);
if (!app) {
// TODO 缓存穿透,暂时不做,没时间了
return null;
}
await this.redisClient.setWithEx(key, JSON.stringify(app), 60 * 5);
return app;
}
return app;
} catch (error) {
return system.getResult(null, `系统错误 错误信息 ${error}`);
}
......
const system = require("../../../system");
const ServiceBase = require("../../svems.base")
class OrderService extends ServiceBase {
constructor() {
super();
this.orderproductSve = system.getObject("service.order.orderproductSve");
this.productSve = system.getObject("service.product.productSve");
}
async getMerchantOrderProduct(merchantId, apiPrev) {
let orderProductList = await this.orderproductSve.getMerchantLiveProduct({merchant_id: merchantId});
if (!orderProductList || orderProductList.length == 0) {
return system.getResult(1002001, "暂无可用订单,请先下单");
}
let productIds = [];
for (let op of orderProductList) {
productIds.push(Number(op.product_id || 0));
}
let productMap = await this.productSve.getMapByIds({ids: productIds});
productMap = productMap.data;
let orderProduct;
for (let op of orderProductList) {
let product = productMap[Number(op.product_id || 0)];
if (product.api && product.api.startsWith(apiPrev)) {
orderProduct = op;
orderProduct.channel = Number(product.api.split("_")[1]);
break;
}
}
if (!orderProduct) {
return system.getResult(1002002, "订单未设置该产品");
}
return system.getResultSuccess(orderProduct);
}
}
module.exports = OrderService;
\ No newline at end of file
const system = require("../../../system");
const ServiceBase = require("../../svems.base")
class OrderProductService extends ServiceBase {
constructor() {
super();
}
// 查商户下订单可用产品
async getMerchantLiveProduct(params) {
let res = await this.callms("sve_order", "getMerchantLiveProduct", params);
if (res.status == 0) {
return res.data;
}
return [];
}
}
module.exports = OrderProductService;
\ No newline at end of file
const system = require("../../../system");
const ServiceBase = require("../../svems.base")
class ProductService extends ServiceBase {
constructor() {
super();
}
async getByIds (params) {
try {
return await this.callms("engine_product", "getByIds", params)
} catch (error) {
throw error
}
}
async getMapByIds (params) {
try {
return await this.callms("engine_product", "getMapByIds", params)
} catch (error) {
throw error
}
}
async apiMap (params) {
try {
return await this.callms("engine_product", "apiMap", params)
} catch (error) {
throw error
}
}
}
module.exports = ProductService;
\ No newline at end of file
......@@ -3,13 +3,28 @@ const ServiceBase = require("../../svems.base")
class AuthService extends ServiceBase {
constructor() {
// 1000000 报文参数问题 1001001 签名错误 1002001费用不足 1002002 订单未设置该产品
super();
this.feeSve = system.getObject("service.fee.feeSve");
this.orderSve = system.getObject("service.order.orderSve");
this.tradelogDao = system.getObject("db.common.tradelogDao");
}
async nameTwo(params) {
try {
// 通过应用id查询商户订单信息,确定产品认证接口
let app = params.app;
let merchantId = app.merchant_id;
// 获取订单产品
let orderProduct = await this.orderSve.getMerchantOrderProduct(merchantId, "nameTwo");
if (orderProduct.status !== 0) {
return orderProduct;
}
params.orderProduct = orderProduct.data;
return this.doAuth(params);
} catch (error) {
console.log(error);
return system.getResult(null, `系统错误 错误信息 ${error}`);
}
}
......@@ -29,12 +44,55 @@ class AuthService extends ServiceBase {
}
async doAuth(params) {
// 1. 扣费
let orderProduct = params.orderProduct;
// 扣费
let tres = await this.trade(orderProduct);
if (tres.status !== 0) {
return tres;
}
let tradeLog = tres.data.tradeLog;
let trade = tres.data.trade;
// 2. 调用认证引擎
// 3. 异步调用订单消费逻辑
// 4. 返回认证结果
}
// 交易
async trade(orderProduct) {
let tradeLog = await this.tradelogDao.create({
requestid: params.requestid,
merchant_id: orderProduct.merchant_id,
product_id: orderProduct.product_id,
product_specifications: orderProduct.product_specifications,
result: 0,
});
let tradeRes = await this.feeSve.trade({
account_id: orderProduct.engine_account_id,
trade_amt: orderProduct.product_specifications,
trade_no: tradeLog.id,
trade_desc: "二要素验证",
});
if (tradeRes.status !== 0) {
tradeLog.result = 2;
tradeLog.fee = 0;
tradeLog.fee_remark = "扣费失败," + tradeRes.msg;
await tradeLog.save();
return system.getResult(1002001, "扣费失败," + tradeRes.msg);
} else {
tradeLog.result = 1;
tradeLog.fee = 1
tradeLog.fee_remark = "扣费成功";
await tradeLog.save();
}
return system.getResultSuccess({
tradeLog: tradeLog,
trade: tradeRes.data
})
}
}
module.exports = AuthService;
\ No newline at end of file
......@@ -172,18 +172,16 @@ class System {
let dev = "http://39.107.234.14";
return {
// 产品引擎
engine_product: local + ":3571" + path,
engine_product: dev + ":3571" + path,
// 计费引擎
engine_fee: local + ":3572" + path,
engine_fee: dev + ":3572" + path,
// 认证引擎
engine_auth: local + ":3573" + path,
// 签约引擎
engine_sign: dev + ":3574" + path,
engine_auth: dev + ":3573" + path,
// 用户服务
sve_uc: local + ":3651" + path,
sve_uc: dev + ":3651" + path,
// 商户服务
sve_merchant: local + ":3652" + path,
sve_merchant: dev + ":3652" + path,
// 订单服务
sve_order: dev + ":3653" + path,
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment