Commit 24e3e9b8 by sxy

feat: 渠道

parent 497a5fa6
...@@ -8,8 +8,16 @@ class ChannelCtl extends CtlBase { ...@@ -8,8 +8,16 @@ class ChannelCtl extends CtlBase {
super("common", CtlBase.getServiceName(ChannelCtl)); super("common", CtlBase.getServiceName(ChannelCtl));
} }
async refQuery(pobj, qobj, req) { async refQuery(pobj, qobj, req) {
let rtn=await this.service.refQuery(pobj); let rtn = await this.service.refQuery(pobj);
return rtn return rtn
} }
async authorizeUser(pobj, qobj, req) {
if (!pobj.channelId) {
return system.getResult(null, "channelId can not be empty,100290");
}
await this.service.authorizeUser(pobj);
return system.getResult("SUCCESS");
}
} }
module.exports = ChannelCtl; module.exports = ChannelCtl;
...@@ -5,6 +5,7 @@ class UserCache extends CacheBase { ...@@ -5,6 +5,7 @@ class UserCache extends CacheBase {
constructor() { constructor() {
super(); super();
this.userDao = system.getObject("db.auth.userDao"); this.userDao = system.getObject("db.auth.userDao");
this.channelDao = system.getObject("db.common.channelDao");
} }
isdebug() { isdebug() {
return settings.env == "dev"; return settings.env == "dev";
...@@ -26,7 +27,20 @@ class UserCache extends CacheBase { ...@@ -26,7 +27,20 @@ class UserCache extends CacheBase {
}); });
if (configValue && configValue[0]) { if (configValue && configValue[0]) {
return JSON.stringify(configValue[0]); let data = JSON.parse(JSON.stringify(configValue[0]));
let channelDatas = await this.channelDao.findAll({});
channelDatas = JSON.parse(JSON.stringify(channelDatas));
channelDatas.forEach(item => {
if (item.userids && item.userids.split(",").includes(data.id.toString())) {
data.channel = {
id: item.id,
code: item.code,
name: item.name
}
}
});
// 获取渠道信息
return JSON.stringify(data);
} }
return null; return null;
} }
......
const system = require("../../../system"); const system = require("../../../system");
const settings = require("../../../../config/settings"); const settings = require("../../../../config/settings");
const appconfig=system.getSysConfig(); const appconfig = system.getSysConfig();
module.exports = (db, DataTypes) => { module.exports = (db, DataTypes) => {
return db.define("channel", { return db.define("channel", {
code:{ code: {
type: DataTypes.STRING, type: DataTypes.STRING,
allowNull: false, allowNull: false,
}, },
...@@ -14,7 +14,11 @@ module.exports = (db, DataTypes) => { ...@@ -14,7 +14,11 @@ module.exports = (db, DataTypes) => {
routehost: { routehost: {
type: DataTypes.STRING, type: DataTypes.STRING,
allowNull: false, allowNull: false,
}//和user的from相同,在注册user时,去创建 },//和user的from相同,在注册user时,去创建
userids: {
type: DataTypes.STRING,
allowNull: true,
},
}, { }, {
paranoid: true,//假的删除 paranoid: true,//假的删除
underscored: true, underscored: true,
......
...@@ -5,71 +5,79 @@ const appconfig = system.getSysConfig(); ...@@ -5,71 +5,79 @@ const appconfig = system.getSysConfig();
class ChannelService extends ServiceBase { class ChannelService extends ServiceBase {
constructor() { constructor() {
super("common", ServiceBase.getDaoName(ChannelService)); super("common", ServiceBase.getDaoName(ChannelService));
this.handlers={} this.handlers = {}
} }
async channelHandle(channelobj,path,datajson){ async channelHandle(channelobj, path, datajson) {
let p=channelobj.pts.filter(item=>{ let p = channelobj.pts.filter(item => {
return item.path==path return item.path == path
}) })
if(p.length==0){ if (p.length == 0) {
throw Error("请配置渠道流量的路径方法映射") throw Error("请配置渠道流量的路径方法映射")
} }
let fileName=channelobj.code let fileName = channelobj.code
let method=p[0].method let method = p[0].method
if(!this.handlers[fileName]) { if (!this.handlers[fileName]) {
this.handlers[fileName]=require("./channelhandlers/"+fileName+".js") this.handlers[fileName] = require("./channelhandlers/" + fileName + ".js")
} }
if( !this.handlers[fileName][method]){ if (!this.handlers[fileName][method]) {
throw Error(`请在${fileName}文件中定义渠道流量的处理方法${method}`) throw Error(`请在${fileName}文件中定义渠道流量的处理方法${method}`)
} }
let rtn=await this.handlers[fileName][method](datajson); let rtn = await this.handlers[fileName][method](datajson);
return rtn; return rtn;
} }
async create(p,q,req){ async create(p, q, req) {
let self =this let self = this
return this.db.transaction(async function (t) { return this.db.transaction(async function (t) {
let u = await self.dao.create(p,t) let u = await self.dao.create(p, t)
//创建路由--针对于特定来源渠道域名的路由,给中台服务添加路由 //创建路由--针对于特定来源渠道域名的路由,给中台服务添加路由
try{ try {
let ps = ["/"] let ps = ["/"]
let routeobj = await self.cjsonregister(ChannelService.newRouteUrl(settings.pmappname), let routeobj = await self.cjsonregister(ChannelService.newRouteUrl(settings.pmappname),
{ name: p.code, paths: ps, hosts: [p.routehost], strip_path: false }) { name: p.code, paths: ps, hosts: [p.routehost], strip_path: false })
}catch(e){ } catch (e) {
await self.cdel(ChannelService.routeUrl(p.code)) await self.cdel(ChannelService.routeUrl(p.code))
throw new Error("创建渠道报错") throw new Error("创建渠道报错")
} }
return u return u
}) })
} }
async update(p,q,req){ async update(p, q, req) {
let self =this let self = this
return this.db.transaction(async function (t) { return this.db.transaction(async function (t) {
let old=await self.dao.findOne({id:p.id}) let old = await self.dao.findOne({ id: p.id })
let u = await self.dao.update(p,t) let u = await self.dao.update(p, t)
//创建路由--针对于特定来源渠道域名的路由,给中台服务添加路由 //创建路由--针对于特定来源渠道域名的路由,给中台服务添加路由
try{ try {
await self.cdel(ChannelService.routeUrl(old.code)) await self.cdel(ChannelService.routeUrl(old.code))
let ps = ["/"] let ps = ["/"]
let routeobj = await self.cjsonregister(ChannelService.newRouteUrl(settings.pmappname), let routeobj = await self.cjsonregister(ChannelService.newRouteUrl(settings.pmappname),
{ name: p.code, paths: ps, hosts: [p.routehost], strip_path: false }) { name: p.code, paths: ps, hosts: [p.routehost], strip_path: false })
}catch(e){ } catch (e) {
throw new Error("创建渠道报错") throw new Error("创建渠道报错")
} }
return u return u
}) })
} }
async delete(p,q,req){ async delete(p, q, req) {
let self =this let self = this
return this.db.transaction(async function (t) { return this.db.transaction(async function (t) {
let u = await self.dao.delete(p,t) let u = await self.dao.delete(p, t)
//创建路由--针对于特定来源渠道域名的路由,给中台服务添加路由 //创建路由--针对于特定来源渠道域名的路由,给中台服务添加路由
try{ try {
await self.cdel(ChannelService.routeUrl(u.code)) await self.cdel(ChannelService.routeUrl(u.code))
}catch(e){ } catch (e) {
throw new Error("创建渠道报错") throw new Error("创建渠道报错")
} }
return u return u
}) })
} }
async authorizeUser(p, q, req) {
await this.dao.updateByWhere({
userids: p.userids
}, {
id: p.channelId
})
}
} }
module.exports = ChannelService; module.exports = ChannelService;
\ No newline at end of file
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