Commit 24e3e9b8 by sxy

feat: 渠道

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