Commit 875f76a2 by 蒋勇

d

parent fcdd7d84
......@@ -4,12 +4,17 @@ var settings = require("../../../../config/settings");
class ChannelAPI extends APIBase {
constructor() {
super();
this.channelS=system.getObject("service.common.channelSve")
}
async getChannels(hostname){
let cacheManager = system.getObject("db.common.cacheManager");
let channels=await cacheManager["ChannelCache"].cache(hostname)
return channels;
}
async channelHandle(channelobj,path,data){
let rtn=await this.channelS.channelHandle(channelobj,path,data)
return rtn;
}
classDesc() {
return {
groupName: "auth",
......
......@@ -6,7 +6,6 @@ const CtlBase = require("../../ctl.base");
class ChannelCtl extends CtlBase {
constructor() {
super("common", CtlBase.getServiceName(ChannelCtl));
this.appS=system.getObject("service.common.appSve")
}
}
module.exports = ChannelCtl;
......@@ -17,7 +17,9 @@ class ChannelCache extends CacheBase {
}
async buildCacheVal(cachekey, inputkey, val, ex, ...items) {
const configValue = await this.channelDao.model.findOne(
{where: { routehost: inputkey}});
{where: { routehost: inputkey},
include:[{model:this.db.models.pathtomethod,as:"pts"}]
});
if (configValue) {
return JSON.stringify(configValue);
}
......
......@@ -65,7 +65,7 @@ class DbFactory{
//渠道和渠道路径方法映射
this.db.models.pathtomethod.belongsTo(this.db.models.channel,{constraints: false,});
this.db.models.channel.hasMany(this.db.models.pathtomethod,as:"pts",{constraints: false,});
this.db.models.channel.hasMany(this.db.models.pathtomethod,{as:"pts",constraints: false,});
}
//async getCon(){,用于使用替换table模型内字段数据使用
getCon(){
......
......@@ -5,6 +5,25 @@ const appconfig = system.getSysConfig();
class ChannelService extends ServiceBase {
constructor() {
super("common", ServiceBase.getDaoName(ChannelService));
this.handlers={}
}
async channelHandle(channelobj,path,datajson){
let p=channelobj.pts.filter(item=>{
return item.path==path
})
if(p.length==0){
throw Error("请配置渠道流量的路径方法映射")
}
let fileName=channelobj.code
let method=p[0].method
if(!this.handlers[fileName]) {
this.handlers[fileName]=require("./channelhandlers/"+fileName+".js")
}
if( !this.handlers[fileName][method]){
throw Error(`请在${fileName}文件中定义渠道流量的处理方法${method}`)
}
let rtn=await this.handlers[fileName][method](datajson)
return rtn;
}
async create(p,q,req){
let self =this
......
class TxHandler {
constructor() {
}
/**
* 需要调用生成者接口,把数据丢入队列
* 组装生产者需要的数据结构
* @param {*} datajson
*/
async addChance(datajson){
console.log("put in queue",datajson)
return {}
}
}
module.exports = new TxHandler();
\ No newline at end of file
var url = require("url");
var System = require("../../base/system");
const chnelapi=System.getObject("api.common.channels")
module.exports = function (app) {
app.all("*",async function(req, res,next){
if(!channels){
next()
}else{
res.end(JSON.stringify({status:0,msg:"ok"}));
try{
let channel=await chnelapi.getChannels(req.headers["x-forwarded-host"])
if(!channel){
next()
}else{
let rtn=await chnelapi.channelHandle(channel,req.path,req.body)
if(rtn!=null){
res.end(JSON.stringify({status:0,msg:"ok"}));
}else{
res.end(JSON.stringify({status:-1,msg:"fail"}));
}
}
}catch(e){
console.log(e)
res.end(JSON.stringify({status:-1,msg:"fail"}));
}
})
app.get('/api/:gname/:qname/:method', function (req, res) {
......
......@@ -37,6 +37,7 @@
"jsonwebtoken": "^8.5.1",
"log4js": "^2.10.0",
"method-override": "^2.3.10",
"moment": "^2.26.0",
"morgan": "^1.9.0",
"multer": "^1.3.0",
"mysql2": "^1.5.3",
......@@ -45,6 +46,7 @@
"pdfcrowd": "^4.2.0",
"pinyin": "^2.8.3",
"qr-image": "^3.2.0",
"request": "^2.88.2",
"sequelize": "^4.37.8",
"sequelize-cli": "^4.1.1",
"serve-favicon": "^2.4.5",
......
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