Commit d07e239d authored by 何娜's avatar 何娜

taxapi 限制appKey查询次数

parent ea5f3f25
Pipeline #7259 passed with stage
in 9 seconds
......@@ -3,153 +3,154 @@
const Controller = require('egg').Controller;
class OrderController extends Controller {
constructor(ctx) {
super(ctx);
constructor(ctx) {
super(ctx);
this.fetchOrderIdRule = {
appKey: 'string',
cityId: 'string'
};
this.fetchOrderIdRule = {
appKey: 'string',
cityId: 'string'
};
this.showRule = {
sign: 'string',
params: {
type: 'object',
rule: {
token: 'string',
orderSn: {
type: 'string',
format: /^(\w{8})-(\w{4})-(\w{4})-(\w{4})-(\w{12})$/,
message: '订单号格式错误',
},
appKey: 'string',
timestamp: 'string',
},
},
};
this.showRule = {
sign: 'string',
params: {
type: 'object',
rule: {
token: 'string',
orderSn: {
type: 'string',
format: /^(\w{8})-(\w{4})-(\w{4})-(\w{4})-(\w{12})$/,
message: '订单号格式错误',
},
appKey: 'string',
timestamp: 'string',
},
},
};
this.fetchRule = {
signParams: {
type: 'object',
rule: {
sign: 'string',
params: {
type: 'object',
rule: {
appKey: 'string',
timestamp: 'string',
token: 'string',
this.fetchRule = {
signParams: {
type: 'object',
rule: {
sign: 'string',
params: {
type: 'object',
rule: {
appKey: 'string',
timestamp: 'string',
token: 'string',
},
},
}
},
},
}
},
cityId: 'string'
};
}
cityId: 'string'
};
}
//拉取数据接口
async partnerShow() {
const { ctx, service } = this;
try {
ctx.validate(this.showRule);
const { appKey, orderSn } = ctx.request.body.params;
await service.signature.signatureCheck(ctx.request.body);
let data = await service.cache.get({ key: orderSn });
let result = '任务还在执行或未输入';
if (data) {
if (data.status === 'success') {
const result = await service.storage.read(orderSn, appKey);
ctx.body = {
code: 0,
data: result
};
return;
} else if (data.note) {
result = data.note.message;
//拉取数据接口
async partnerShow() {
const {ctx, service} = this;
try {
ctx.validate(this.showRule);
const {appKey, orderSn} = ctx.request.body.params;
await service.signature.signatureCheck(ctx.request.body);
let data = await service.cache.get({key: orderSn});
let result = '任务还在执行或未输入';
if (data) {
if (data.status === 'success') {
await service.partner.appKeyLimit(appKey);
const result = await service.storage.read(orderSn, appKey);
ctx.body = {
code: 0,
data: result
};
return;
} else if (data.note) {
result = data.note.message;
}
ctx.body = {code: -1, msg: result};
return;
}
} catch (err) {
ctx.logger.error('partnerShow', err);
ctx.status = 200;
if (err.code == 'invalid_param') {
return ctx.body = {code: -1, msg: err.message || ''};
}
return ctx.body = {code: err.code || -1, msg: err.message || ''};
}
ctx.body = {code: -1, msg: result};
return;
}
} catch (err) {
ctx.logger.error('partnerShow', err);
ctx.status = 200;
if (err.code == 'invalid_param') {
return ctx.body = { code: -1, msg: err.message || '' };
}
return ctx.body = { code: err.code || -1, msg: err.message || '' };
}
}
//创建订单号接口
async fetchOrderId() {
const { ctx, service } = this;
try {
ctx.validate(this.fetchOrderIdRule, ctx.query);
const { appKey, cityId } = ctx.query;
// const appKeyInfo = await service.partner.fetchInfo(appKey);
// ctx.logger.info(appKey, ',appKeyInfo,', appKeyInfo);
// if (!(appKeyInfo && appKeyInfo.valid === true && appKeyInfo.enableIt === true)) {
// return ctx.fail('未开通此服务');
// }
//创建订单号接口
async fetchOrderId() {
const {ctx, service} = this;
try {
ctx.validate(this.fetchOrderIdRule, ctx.query);
const {appKey, cityId} = ctx.query;
// const appKeyInfo = await service.partner.fetchInfo(appKey);
// ctx.logger.info(appKey, ',appKeyInfo,', appKeyInfo);
// if (!(appKeyInfo && appKeyInfo.valid === true && appKeyInfo.enableIt === true)) {
// return ctx.fail('未开通此服务');
// }
const orderId = ctx.helper.getUuid();
const taskId = await service.task.create({cityId});
await service.order.create({
orderId,
taskId,
cityId,
notifyUrl: ctx.app.notifyMap.get(appKey) && ctx.app.notifyMap.get(appKey).notifyUrl || '',
appkey: appKey,
status: 'init',
});
ctx.body = {
code: 0,
data: {
orderSn: orderId
const orderId = ctx.helper.getUuid();
const taskId = await service.task.create({cityId});
await service.order.create({
orderId,
taskId,
cityId,
notifyUrl: ctx.app.notifyMap.get(appKey) && ctx.app.notifyMap.get(appKey).notifyUrl || '',
appkey: appKey,
status: 'init',
});
ctx.body = {
code: 0,
data: {
orderSn: orderId
}
}
} catch (err) {
ctx.logger.error('fetchOrderId', err);
ctx.status = 200;
if (err.code == 'invalid_param') {
return ctx.body = {code: -1, msg: err.message || ''};
}
return ctx.body = {code: err.code || -1, msg: err.message || ''};
}
}
} catch (err) {
ctx.logger.error('fetchOrderId', err);
ctx.status = 200;
if (err.code == 'invalid_param') {
return ctx.body = { code: -1, msg: err.message || '' };
}
return ctx.body = { code: err.code || -1, msg: err.message || '' };
}
}
//获取帮助配置接口
async help(){
const { ctx, service } = this;
const { cityId } = ctx.query;
const cityName = await await service.scripts.fetchScriptName(cityId);
const helpData = await service.scripts.fetchHelp(cityId);
if(helpData && helpData.length > 0){
let helpInfo = [];
for(let item of helpData[0].help_list){
helpInfo.push({
desc: item.content,
link: {
label: item.button_name,
url: item.button_url,
},
})
}
ctx.success({
code: "0",
data: {
cityId,
city: cityName,
help: helpInfo
//获取帮助配置接口
async help() {
const {ctx, service} = this;
const {cityId} = ctx.query;
const cityName = await await service.scripts.fetchScriptName(cityId);
const helpData = await service.scripts.fetchHelp(cityId);
if (helpData && helpData.length > 0) {
let helpInfo = [];
for (let item of helpData[0].help_list) {
helpInfo.push({
desc: item.content,
link: {
label: item.button_name,
url: item.button_url,
},
})
}
});
}else{
return ctx.body = {
code: "-1",
msg: "未查询到此地区帮助信息"
}
ctx.success({
code: "0",
data: {
cityId,
city: cityName,
help: helpInfo
}
});
} else {
return ctx.body = {
code: "-1",
msg: "未查询到此地区帮助信息"
}
}
}
}
}
module.exports = OrderController;
'use strict';
module.exports = app => {
const { DataTypes } = app.Sequelize;
const nodeLimit = app.model.define('nodeLimit', {
id: {
type: DataTypes.INTEGER,
allowNull: false,
autoIncrement: true,
primaryKey: true,
field: 'id',
},
companyName: {
type: DataTypes.STRING(255),
allowNull: true,
field: 'companyName',
},
appKey: {
type: DataTypes.STRING(255),
allowNull: false,
field: 'appKey',
},
type: {
type: DataTypes.STRING(255),
allowNull: true,
field: 'type',
},
currentCount: {
type: DataTypes.INTEGER(20),
allowNull: true,
defaultValue: 0,
field: 'currentCount',
},
limitCount: {
type: DataTypes.INTEGER(20),
allowNull: true,
defaultValue: 0,
field: 'limitCount',
},
createTime: {
type: DataTypes.DATE,
allowNull: true,
defaultValue: app.Sequelize.literal('CURRENT_TIMESTAMP'),
field: 'createTime',
},
updateTime: {
type: DataTypes.DATE,
allowNull: true,
defaultValue: app.Sequelize.literal('CURRENT_TIMESTAMP'),
field: 'updateTime',
},
text: {
type: DataTypes.STRING(255),
allowNull: true,
field: 'text1',
},
text2: {
type: DataTypes.STRING(255),
allowNull: true,
field: 'text2',
},
},
{
tableName: 'node_limit',
timestamps: false,
});
return nodeLimit;
};
......@@ -25,8 +25,8 @@ module.exports = {
notifyMap.set(customer.appKey, { notifyUrl: customer.callBackUrl });
}
});
ctx.app.notifyMap = notifyMap;
}
ctx.app.notifyMap = notifyMap;
} catch (e) {
ctx.logger.error('【schedule/notifyUrlTask】catch error:', JSON.stringify(e));
}
......
......@@ -61,6 +61,50 @@ class PartnerService extends Service {
}
return;
}
async appKeyLimit(appKey) {
const { ctx, partnerAPI: { redisappKeyLimitPrefix } } = this;
let data = await this.app.redis.get(redisappKeyLimitPrefix);
if (data) {
data = JSON.parse(data);
if (data.includes(appKey)) {
let limit = await this.app.redis.get(redisappKeyLimitPrefix + appKey);
if (limit) {
limit = JSON.parse(limit);
if (limit.currentCount >= limit.limitCount) {
ctx.throw(422, { message: 'appKey查询数量超过限制,请联系技术人员' });
} else {
// limit.currentCount++;
// await this.app.redis.set(redisappKeyLimitPrefix + appKey, JSON.stringify({ currentCount: limit.currentCount, limitCount: limit.limitCount }), 'EX', 300);
await ctx.model.query('UPDATE node_limit set currentCount = currentCount + 1 where appKey = ?', {
type: 'UPDATE',
replacements: [appKey],
});
}
}
}
} else {
const dbRes = await ctx.model.NodeLimit.findAll({
attributes: ['appKey', 'currentCount', 'limitCount'],
where: { type: 'tax' },
});
if (dbRes) {
const appKeyArray = [];
for (const item of dbRes) {
const { appKey, currentCount, limitCount } = item;
appKeyArray.push(appKey);
await this.app.redis.set(redisappKeyLimitPrefix + appKey, JSON.stringify({ currentCount, limitCount }), 'EX', 300);
}
await this.app.redis.set(redisappKeyLimitPrefix, JSON.stringify(appKeyArray), 'EX', 300);
if (appKeyArray.includes(appKey)) {
await ctx.model.query('UPDATE node_limit set currentCount = currentCount + 1 where appKey = ?', {
type: 'UPDATE',
replacements: [appKey],
});
}
}
}
}
}
module.exports = PartnerService;
......@@ -34,6 +34,7 @@ module.exports = () => {
redisScriptsPrefix: 'URANUS.IT.PARNTERS.SCRIPTS',
redisAgreementsPrefix: 'URANUS.IT.PARNTERS.Agreements',
redisInfoPrefix: 'URANUS.IT.PARNTERS.Info',
redisappKeyLimitPrefix: 'URANUS.IT.PARNTERS.Limit',
};
config.scriptsAPI = {
......
......@@ -37,6 +37,7 @@ module.exports = () => {
redisScriptsPrefix: 'URANUS.IT.PARNTERS.SCRIPTS',
redisAgreementsPrefix: 'URANUS.IT.PARNTERS.Agreements',
redisInfoPrefix: 'URANUS.IT.PARNTERS.Info',
redisappKeyLimitPrefix: 'URANUS.IT.PARNTERS.Limit',
};
config.scriptsAPI = {
......
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