Commit 899828bd authored by 何娜's avatar 何娜

taxH5 修改获取城市配置接口+通知优化

parent 36e0828b
Pipeline #6989 passed with stage
in 2 seconds
......@@ -3,6 +3,13 @@
const Controller = require('egg').Controller;
class ScriptController extends Controller {
constructor(ctx) {
super(ctx);
this.cityListRule = {
appKey: 'string',
};
}
async fetchScripts() {
const { ctx, service } = this;
const { appKey } = ctx.query;
......@@ -71,6 +78,36 @@ class ScriptController extends Controller {
const data = await service.scripts.fetchHubSeripts(hubId);
ctx.success(data);
}
async partnerScripts() {
const { ctx, service } = this;
try {
ctx.validate(this.cityListRule, ctx.query);
const { appKey } = ctx.query;
let limitScriptsList = false;
const ret = await service.partner.fetchInfo(appKey);
if (ret) {
limitScriptsList = ret.itExcludeScripts;
} else {
ctx.error({ message: '无效的appKey' });
return;
}
let data = await service.scripts.fetchScripts();
if (limitScriptsList) {
data = data.filter(item => {
if (item.state === 'offline' || item.state === 'online') {
return !(limitScriptsList.includes(item.id));
}
});
}
ctx.body = {
code: 0,
data,
};
} catch (err) {
ctx.error(err);
}
}
}
module.exports = ScriptController;
......@@ -32,7 +32,7 @@ module.exports = {
this.body = { code: -1, msg: '请求参数有误' };
return;
}
if (!/token/.test(err.message)) {
if (!/token|appKey/.test(err.message)) {
if (/[a-zA-Z]+/.test(err.message)) {
this.body = { code: err.code || -1, msg: '系统错误, 请稍后再试' };
return;
......
......@@ -36,7 +36,8 @@ module.exports = app => {
itRouter.post('/getToken', controller.token.partnerToken); // 合作方获取token
itRouter.post('/getorderSn', controller.order.partnerOrder); // 合作方获取orderSn
itRouter.post('/getCityConfig', controller.task.cityConfigs); // 获取城市配置
itRouter.post('/getCityConfig', controller.task.cityConfigs); // 获取城市配置 老接口备用
itRouter.get('/getCityList', controller.script.partnerScripts);//合作方获取城市配置
itRouter.post('/getData', controller.order.partnerData); // 合作方拉数据
itRouter.get('/orderData/:appKey/:orderId', controller.order.orderShow);//获取展示页面数据
......
......@@ -3,157 +3,159 @@
const Service = require('egg').Service;
class PartnerService extends Service {
constructor(ctx) {
super(ctx);
const { config: { partnerAPI } } = this;
this.partnerAPI = partnerAPI;
}
constructor(ctx) {
super(ctx);
const {config: {partnerAPI}} = this;
this.partnerAPI = partnerAPI;
}
async fetchTheme(appKey) {
const { ctx, partnerAPI: { fetchTheme, host, redisThemePrefix } } = this;
if (appKey) {
const data = await this.app.redis.get(redisThemePrefix + appKey);
if (data) {
try {
return JSON.parse(data);
} catch (err) {
ctx.logger.error('【Scripts】fetchParams', redisThemePrefix + appKey, 'result:', data, err);
await this.app.redis.del(redisThemePrefix + appKey);
async fetchTheme(appKey) {
const {ctx, partnerAPI: {fetchTheme, host, redisThemePrefix}} = this;
if (appKey) {
const data = await this.app.redis.get(redisThemePrefix + appKey);
if (data) {
try {
return JSON.parse(data);
} catch (err) {
ctx.logger.error('【Scripts】fetchParams', redisThemePrefix + appKey, 'result:', data, err);
await this.app.redis.del(redisThemePrefix + appKey);
}
}
}
}
}
const ret = await ctx.curl(`${host + fetchTheme}`, {
charset: 'utf-8',
timeout: ['30s', '30s'],
dataType: 'json',
method: 'GET',
data: {
appKey,
subject: 'it'
}
});
ctx.logger.info(`【Partner】 fetchTheme ${host + fetchTheme}/${appKey}`, ret.data);
if (ret.status === 200 && ret.data && ret.data.length > 0 && ret.data[0].appKey === appKey) {
await this.app.redis.set(redisThemePrefix + appKey, JSON.stringify(ret.data[0]), 'EX', 300);
const ret = await ctx.curl(`${host + fetchTheme}`, {
charset: 'utf-8',
timeout: ['30s', '30s'],
dataType: 'json',
method: 'GET',
data: {
appKey,
subject: 'it'
}
});
ctx.logger.info(`【Partner】 fetchTheme ${host + fetchTheme}/${appKey}`, ret.data);
if (ret.status === 200 && ret.data && ret.data.length > 0 && ret.data[0].appKey === appKey) {
await this.app.redis.set(redisThemePrefix + appKey, JSON.stringify(ret.data[0]), 'EX', 300);
}
return ret.data[0];
}
return ret.data[0];
}
async fetchInfo(appKey){
const { ctx, partnerAPI: { fetchInfo, host, redisInfoPrefix } } = this;
if (appKey) {
const data = await this.app.redis.get(redisInfoPrefix + appKey);
ctx.logger.info('【Scripts】fetchInfo redis data', redisInfoPrefix + appKey, 'result:', data);
if (data) {
try {
return JSON.parse(data);
} catch (err) {
ctx.logger.info('【Scripts】fetchInfo redis err', redisInfoPrefix + appKey, 'result:', data, err);
await this.app.redis.del(redisInfoPrefix + appKey);
async fetchInfo(appKey) {
const {ctx, partnerAPI: {fetchInfo, host, redisInfoPrefix}} = this;
if (appKey) {
const data = await this.app.redis.get(redisInfoPrefix + appKey);
ctx.logger.info('【Scripts】fetchInfo redis data', redisInfoPrefix + appKey, 'result:', data);
if (data) {
try {
return JSON.parse(data);
} catch (err) {
ctx.logger.info('【Scripts】fetchInfo redis err', redisInfoPrefix + appKey, 'result:', data, err);
await this.app.redis.del(redisInfoPrefix + appKey);
}
}
}
}
}
const ret = await ctx.curl(`${host + fetchInfo}`, {
charset: 'utf-8',
timeout: ['30s', '30s'],
dataType: 'json',
method: 'GET',
data: {
appKey
}
});
ctx.logger.info(`【Partner】 fetchTheme ${host + fetchInfo}?appKey=${appKey}`, ret.data);
if (ret.status === 200 && ret.data && ret.data.length > 0 && ret.data[0].appKey === appKey) {
await this.app.redis.set(redisInfoPrefix + appKey, JSON.stringify(ret.data[0]), 'EX', 300);
const ret = await ctx.curl(`${host + fetchInfo}`, {
charset: 'utf-8',
timeout: ['30s', '30s'],
dataType: 'json',
method: 'GET',
data: {
appKey
}
});
ctx.logger.info(`【Partner】 fetchTheme ${host + fetchInfo}?appKey=${appKey}`, ret.data);
if (ret.status === 200 && ret.data && ret.data.length > 0 && ret.data[0].appKey === appKey) {
await this.app.redis.set(redisInfoPrefix + appKey, JSON.stringify(ret.data[0]), 'EX', 300);
}
return ret.data[0];
}
return ret.data[0];
}
async fetchScripts(appKey) {
const { ctx, partnerAPI: { fetchScripts, host, redisScriptsPrefix } } = this;
async fetchScripts(appKey) {
const {ctx, partnerAPI: {fetchScripts, host, redisScriptsPrefix}} = this;
if (appKey) {
const data = await this.app.redis.get(redisScriptsPrefix + appKey);
if (data) {
try {
return JSON.parse(data);
} catch (err) {
ctx.logger.error('【Partner】 fetchScripts', redisScriptsPrefix + appKey, 'result:', JSON.stringify(data), err);
await this.app.redis.del(redisScriptsPrefix + appKey);
if (appKey) {
const data = await this.app.redis.get(redisScriptsPrefix + appKey);
if (data) {
try {
return JSON.parse(data);
} catch (err) {
ctx.logger.error('【Partner】 fetchScripts', redisScriptsPrefix + appKey, 'result:', JSON.stringify(data), err);
await this.app.redis.del(redisScriptsPrefix + appKey);
}
}
}
}
}
const url = `${host + fetchScripts}/${appKey}`;
const ret = await ctx.curl(url, {
charset: 'utf-8',
timeout: ['30s', '30s'],
dataType: 'json',
method: 'GET',
});
ctx.logger.info(`【Partner】 fetchTheme ${url}`, JSON.stringify(ret.data));
if (ret.status === 200 && ret.data && ret.data.id === appKey) {
await this.app.redis.set(redisScriptsPrefix + appKey, JSON.stringify(ret.data), 'EX', 300);
const url = `${host + fetchScripts}/${appKey}`;
const ret = await ctx.curl(url, {
charset: 'utf-8',
timeout: ['30s', '30s'],
dataType: 'json',
method: 'GET',
});
ctx.logger.info(`【Partner】 fetchTheme ${url}`, JSON.stringify(ret.data));
if (ret.status === 200 && ret.data && ret.data.id === appKey) {
await this.app.redis.set(redisScriptsPrefix + appKey, JSON.stringify(ret.data), 'EX', 300);
}
return ret.data;
}
return ret.data;
}
async fetchAgreements(id) {
const { ctx, partnerAPI: { fetchAgreements, host, redisAgreementsPrefix } } = this;
const data = await this.app.redis.get(redisAgreementsPrefix + id);
if (data) {
try {
return JSON.parse(data);
} catch (err) {
ctx.logger.error('【Partner】 fetchAgreements', redisAgreementsPrefix + id, 'result:', JSON.stringify(data), err);
await this.app.redis.del(redisAgreementsPrefix + id);
}
}
const url = `${host + fetchAgreements}/${id}`;
const ret = await ctx.curl(url, {
charset: 'utf-8',
timeout: ['30s', '30s'],
dataType: 'json',
method: 'GET',
});
ctx.logger.info(`【Partner】 fetchAgreements ${url}`, JSON.stringify(ret.data));
if (ret.status === 200 && ret.data && ret.data.id === id) {
await this.app.redis.set(redisScriptsPrefix + appKey, JSON.stringify(ret.data), 300);
async fetchAgreements(id) {
const {ctx, partnerAPI: {fetchAgreements, host, redisAgreementsPrefix}} = this;
const data = await this.app.redis.get(redisAgreementsPrefix + id);
if (data) {
try {
return JSON.parse(data);
} catch (err) {
ctx.logger.error('【Partner】 fetchAgreements', redisAgreementsPrefix + id, 'result:', JSON.stringify(data), err);
await this.app.redis.del(redisAgreementsPrefix + id);
}
}
const url = `${host + fetchAgreements}/${id}`;
const ret = await ctx.curl(url, {
charset: 'utf-8',
timeout: ['30s', '30s'],
dataType: 'json',
method: 'GET',
});
ctx.logger.info(`【Partner】 fetchAgreements ${url}`, JSON.stringify(ret.data));
if (ret.status === 200 && ret.data && ret.data.id === id) {
await this.app.redis.set(redisScriptsPrefix + appKey, JSON.stringify(ret.data), 300);
}
return ret.data;
}
return ret.data;
}
async notice(order) {
const { ctx } = this;
ctx.logger.info(`【Partner】 notice into`);
const { orderId, notifyUrl, userId } = order;
if (notifyUrl) {
try{
const ret = await ctx.curl(notifyUrl, {
charset: 'utf-8',
timeout: ['30s', '30s'],
contentType: 'json',
method: 'POST',
data: {
orderId,
userId,
status: 'success'
},
async notice(order) {
const {ctx} = this;
const {orderId, notifyUrl, userId} = order;
const dbRes = await service.order.getOneByOrderId({ orderId, notice: 1 });
if (!dbRes && notifyUrl) {
try {
const ret = await ctx.curl(notifyUrl, {
charset: 'utf-8',
timeout: ['30s', '30s'],
contentType: 'json',
method: 'POST',
data: {
orderSn: orderId,
userId: userId || '',
status: 'success'
},
}
);
ctx.logger.info(`【Partner】 notice ${notifyUrl}`, 'orderId:', orderId, 'result:', ret.status, JSON.stringify(ret.data));
if (ret.status == 200) {
await order.update({notice: 1});
} else {
await order.update({notice: ret.status});
}
);
ctx.logger.info(`【Partner】 notice ${notifyUrl}`, 'orderId:', orderId, 'result:', ret.status, JSON.stringify(ret.data));
if (ret.status == 200) {
await order.update({ notice: 1 });
}else{
await order.update({ notice: ret.status });
} catch (e) {
ctx.logger.info(`【Partner】 notice err ${e}`)
} finally {
return
}
}catch (e) {
ctx.logger.info(`【Partner】 notice err ${e}`)
}finally {
return
}else{
ctx.logger.info('【Partner】 notice no send', 'orderId:', orderId);
}
}
}
}
module.exports = PartnerService;
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