Commit 59779442 by sxy

feat: 根据功能生成路由文件

parent 5c47b24d
......@@ -10,43 +10,43 @@ class AppCtl extends CtlBase {
constructor() {
super("common", CtlBase.getServiceName(AppCtl));
this.userCtl = system.getObject("service.auth.userSve");
this.comS=system.getObject("service.common.companySve");
this.comS = system.getObject("service.common.companySve");
}
async findAndCountAll(pobj, qobj, req) {
let comtemp=await this.comS.findById(pobj.company_id)
pobj.myappstrs=comtemp.appids
let rtn=await super.findAndCountAll(pobj,qobj,req)
let comtemp = await this.comS.findById(pobj.company_id)
pobj.myappstrs = comtemp.appids
let rtn = await super.findAndCountAll(pobj, qobj, req)
return rtn
}
async findAllApps(p, q, req) {
var rtns = await this.service.findAllApps(p.userid);
return system.getResult(rtns);
}
async getApp(p,q,req) {
let app= await this.cacheManager["AppCache"].cache(p.appkey, null);
return system.getResult({funcJson:JSON.parse(app.functionJSON)});
async getApp(p, q, req) {
let app = await this.cacheManager["AppCache"].cache(p.appkey, null);
return system.getResult({ funcJson: JSON.parse(app.functionJSON) });
}
async buildFrontRouter(p,q,req){
let appkey=p.appkeyForRoute
let app= await this.cacheManager["AppCache"].cache(appkey, null);
let funcJSONOBJ= JSON.parse(app.functionJSON)
let rtn=await this.service.buildFrontRouter(funcJSONOBJ)
async buildFrontRouter(p, q, req) {
let appkey = p.appkeyForRoute
let app = await this.cacheManager["AppCache"].cache(appkey, null);
let funcJSONOBJ = JSON.parse(app.functionJSON)
let rtn = await this.service.buildFrontRouter(funcJSONOBJ, app.id)
return system.getResult(rtn)
}
async getFuncs(p,q,req){
let appkey=p.appkey
let app= await this.cacheManager["AppCache"].cache(appkey, null);
return system.getResult({funcJson:JSON.parse(app.functionJSON)})
//return system.getResult({funcJson:[]})
async getFuncs(p, q, req) {
let appkey = p.appkey
let app = await this.cacheManager["AppCache"].cache(appkey, null);
return system.getResult({ funcJson: JSON.parse(app.functionJSON) })
//return system.getResult({funcJson:[]})
}
async saveFuncTree(p,q,req){
let rtn=await this.service.saveFuncTree(p)
return system.getResult(rtn)
async saveFuncTree(p, q, req) {
let rtn = await this.service.saveFuncTree(p)
return system.getResult(rtn)
}
async create(pobj, queryobj, req) {
pobj.creator_id = pobj.userid;//设置创建者
......
const system=require("../system");
const settings=require("../../config/settings.js");
const reclient=system.getObject("util.redisClient");
const system = require("../system");
const settings = require("../../config/settings.js");
const reclient = system.getObject("util.redisClient");
const md5 = require("MD5");
//获取平台配置兑换率
//初次登录的赠送数量
//创建一笔交易
//同时增加账户数量,增加系统平台账户
var dbf=system.getObject("db.common.connection");
var db=dbf.getCon();
db.sync({force:true}).then(async ()=>{
const apps=await system.getObject("service.common.appSve");
const usS=await system.getObject("service.auth.userSve");
let appnew=await apps.create( {
"name":"center-app",
"domainName":"t9.com",
"backend":"192.168.4.1",
"isSystem":true
var dbf = system.getObject("db.common.connection");
var db = dbf.getCon();
db.sync({ force: true }).then(async () => {
const apps = await system.getObject("service.common.appSve");
const usS = await system.getObject("service.auth.userSve");
let appnew = await apps.create({
"name": "center-app",
"domainName": "t9.com",
"backend": "192.168.1.148",
"isSystem": true,
"title": "center-app"
});
let Role=db.models["role"];
await Role.create({code:"ta",name:"租户",isSystem:true,app_id:appnew.id,company_id:settings.pmcompanyid})
await Role.create({code:"pr",name:"个人",isSystem:true,app_id:appnew.id,company_id:settings.pmcompanyid})
let usuper=await usS.pmregister({userName:"sm",password:"951753",isSuper:true,isAdmin:true,isSystem:true,isEnabled:true,nickName:"superman",app_id:appnew.id,company_id:settings.id})
appnew.creator_id=usuper.user.id
let Role = db.models["role"];
await Role.create({ code: "ta", name: "租户", isSystem: true, app_id: appnew.id, company_id: settings.pmcompanyid })
await Role.create({ code: "pr", name: "个人", isSystem: true, app_id: appnew.id, company_id: settings.pmcompanyid })
let usuper = await usS.pmregister({ userName: "sm", password: "951753", isSuper: true, isAdmin: true, isSystem: true, isEnabled: true, nickName: "superman", app_id: appnew.id, company_id: settings.id })
appnew.creator_id = usuper.user.id
await appnew.save()
//创建role
......
......@@ -2,69 +2,54 @@ const system = require("../../../system");
const ServiceBase = require("../../sve.base");
const settings = require("../../../../config/settings");
const appconfig = system.getSysConfig();
const fs=require("fs")
const fs = require("fs")
class AppService extends ServiceBase {
constructor() {
super("common", ServiceBase.getDaoName(AppService));
this.userS = system.getObject("service.auth.userSve");
this.routeDao = system.getObject("db.common.routeDao");
this.ossC=system.getObject("util.ossClient");
this.ossC = system.getObject("util.ossClient");
}
async getApp(p) {
let app = this.cacheManager["AppCache"].cache(p.appkey, null);
return app;
}
//[{code:root,title:"",children:[]}]
//code->name,title,children
// {
// path: '/',
// name: '_home',
// component: 'replace yours',
// meta: {
// hideInMenu: true,
// title: '快捷面板',
// notCache: false,
// icon: 'replace yours'
// },
// children: []
// }
async makeRoute(cds,parent,results){
cds.forEach(item=>{
let tmpResult={}
tmpResult.name=item.code
tmpResult.path=item.code
tmpResult.component="replace yours"
tmpResult.meta={
hideInMenu:false,
title:item.title,
notCache:false,
icon:'replace yours'
// 组装 前端 路由
async makeRoute(cds, parent) {
let results = [];
for (let item of cds) {
let tmpResult = {};
tmpResult.name = item.code;
tmpResult.path = item.code;
tmpResult.component = "replace yours"
tmpResult.meta = {
hideInMenu: false,
title: item.title,
notCache: false,
icon: 'replace yours'
}
results.push(tmpResult)
if(item.children.length>0){
if(!tmpResult["children"]){
tmpResult["children"]=[]
}
this.makeRoute(item.children,item,results)
}else{
if (item.children && item.children.length > 0) {
tmpResult.children = [];
tmpResult.children = await this.makeRoute(item.children, item);
}
})
results.push(tmpResult);
}
return results
}
async buildFrontRouter(funcJSON,appid){
async buildFrontRouter(funcJSON, appid) {
var self = this;
return this.db.transaction(async function (t) {
let results=[]
await makeRoute(funcJSON.children,null,results)
let keyfile=self.getUUID()+".json"
let dirfile="/tmp/"+keyfile
fs.writeFileSync(dirfile)
let result =await this.ossC.upfile(key,dirfile);
let upurl=result.url
let upobj={id:appid,docUrl:upurl}
let results = await self.makeRoute(funcJSON[0].children, null);
let keyfile = self.getUUID() + ".json"
let dirfile = "/tmp/" + keyfile
fs.writeFileSync(dirfile, JSON.stringify(results));
let result = await self.ossC.upfile(keyfile, dirfile);
let upurl = result.url
let upobj = { id: appid, docUrl: upurl }
fs.unlinkSync(dirfile)
await self.db.models.app.update(upobj,{where:{id:appid},transaction:t})
return {uprul:upurl}
await self.db.models.app.update(upobj, { where: { id: appid }, transaction: t })
return { uprul: upurl }
})
}
async findAllApps(uid) {
......@@ -113,17 +98,17 @@ class AppService extends ServiceBase {
return this.db.transaction(async function (t) {
var app = await self.dao.create(pobj, t);
//创建后台应用服务
let urltmp="http://" + app.backend
if(app.backport){
urltmp+=":"+app.backport
let urltmp = "http://" + app.backend
if (app.backport) {
urltmp += ":" + app.backport
}
let svobj = await self.cjsonregister(AppService.newServiceUrl(), { name: app.name, url: urltmp})
let svobj = await self.cjsonregister(AppService.newServiceUrl(), { name: app.name, url: urltmp })
//添加路由
let ps = ["/web/auth/userCtl/pmgetUserByCode","/web/auth/userCtl/pmlogin", "/web/auth/userCtl/pmregister", "/web/auth/userCtl/pmSendVCode", "/web/auth/userCtl/pmloginByVCode"]
let ps = ["/web/auth/userCtl/pmgetUserByCode", "/web/auth/userCtl/pmlogin", "/web/auth/userCtl/pmregister", "/web/auth/userCtl/pmSendVCode", "/web/auth/userCtl/pmloginByVCode"]
let routeobj = await self.cjsonregister(AppService.newRouteUrl(app.name),
{ name: app.name, paths: ps, hosts: [app.domainName], strip_path: false })
let ps2 = ["/api", "/web"]
let routeapi = await self.cjsonregister(AppService.newRouteUrl(app.name), { name: app.name + "_api", hosts: [app.domainName], paths: ps2,strip_path: false })
let routeapi = await self.cjsonregister(AppService.newRouteUrl(app.name), { name: app.name + "_api", hosts: [app.domainName], paths: ps2, strip_path: false })
let r1 = await self.routeDao.create({ name: app.name, center_id: routeobj.id, app_id: app.id, shosts: app.domainName, spaths: ps.join(",") }, t);
let r2 = await self.routeDao.create({ name: app.name + "_api", center_id: routeapi.id, app_id: app.id, shosts: app.domainName, spaths: ps2.join(",") }, t);
//给api路由启动插件
......@@ -146,31 +131,31 @@ class AppService extends ServiceBase {
async translateInitSels(funcobjs) {
funcobjs.forEach((item) => {
console.log(item.title)
if (item.children && item.children.length>0) {
if (item.children && item.children.length > 0) {
this.translateInitSels(item.children)
} else {
if(item.auths && item.auths.length>0){
item.sels=[]
}
if (item.auths && item.auths.length > 0) {
item.sels = []
}
}
})
}
async saveFuncTree(pobj){
async saveFuncTree(pobj) {
var self = this;
return this.db.transaction(async function (t) {
//如果存在functionJSON,那么就需要转换,构建编码路径
if (pobj.funcJson) {
// let funcobjs = JSON.parse(pobj.functionJSON)
await self.translateInitSels(pobj.funcJson)
pobj.functionJSON= JSON.stringify(pobj.funcJson)
pobj.functionJSON = JSON.stringify(pobj.funcJson)
}
let appcache=await self.cacheManager["AppCache"].cache(pobj.appkey);
let upobj={id:appcache.id,functionJSON:pobj.functionJSON}
let appcache = await self.cacheManager["AppCache"].cache(pobj.appkey);
let upobj = { id: appcache.id, functionJSON: pobj.functionJSON }
await self.dao.update(upobj, t)
//令缓存失效
await self.cacheManager["AppCache"].invalidate(pobj.appkey);
let appcache2=await self.dao.model.findById(appcache.id,{transaction:t});
return {funcJson:JSON.parse(appcache2.functionJSON)}
let appcache2 = await self.dao.model.findById(appcache.id, { transaction: t });
return { funcJson: JSON.parse(appcache2.functionJSON) }
})
}
//删除应用
......@@ -181,7 +166,7 @@ class AppService extends ServiceBase {
if (pobj.functionJSON != "") {
let funcobjs = JSON.parse(pobj.functionJSON)
//await self.translateWithBizCode(funcobjs,null)
pobj.functionJSON= JSON.stringify(funcobjs)
pobj.functionJSON = JSON.stringify(funcobjs)
}
await self.dao.update(pobj, t)
let upobj = await self.dao.findById(pobj.id)
......
var co = require('co');
var OSS = require('ali-oss');
class OSSClient{
constructor(){
this.client=new OSS({
class OSSClient {
constructor() {
this.client = new OSS({
endpoint: 'https://oss-cn-beijing.aliyuncs.com',
accessKeyId: 'LTAIyAUK8AD04P5S',
accessKeySecret: 'DHmRtFlw2Zr3KaRwUFeiu7FWATnmla'
accessKeyId: 'LTAI4GC5tSKvqsH2hMqj6pvd',
accessKeySecret: '3KV9nIwW8qkTGlrPmAe3HnR3fzM6r5'
});
this.client.useBucket('gsb-zc');
}
async downfile(key){
var me=this;
var result=await co(function* () {
var result = yield me.client.get(key, '/tmp/'+key);
async downfile(key) {
var me = this;
var result = await co(function* () {
var result = yield me.client.get(key, '/tmp/' + key);
return result;
});
return result;
}
async upfile(key,filepath){
var me=this;
var result=await co(function* () {
async upfile(key, filepath) {
var me = this;
var result = await co(function* () {
var result = yield me.client.put(key, filepath);
return result;
})
return result;
}
async putBuffer (key,buf) {
async putBuffer(key, buf) {
try {
var result = await this.client.put(key, buf);
console.log(result);
......@@ -36,7 +36,7 @@ class OSSClient{
}
}
}
module.exports=OSSClient;
module.exports = OSSClient;
// var oss=new OSSClient();
// var key="netsharp_QSzjD4HdKdTmRR6b5486pEA3AbsW8Pr8.jpg"
// oss.upfile(key,"/usr/devws/OMC/igirl-api/r3.jpg").then(function(result){
......
......@@ -4,6 +4,7 @@
"description": "h5framework",
"main": "main.js",
"scripts": {
"dev": "nodemon main.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "jy",
......@@ -31,6 +32,7 @@
"exif-js": "^2.3.0",
"express": "^4.16.2",
"express-session": "^1.15.6",
"glob": "^7.1.6",
"gm": "^1.23.1",
"jsonwebtoken": "^8.5.1",
"log4js": "^2.10.0",
......
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