Commit 2b0242df by 蒋勇

d

parent 36da0228
......@@ -11,12 +11,12 @@ class APIBase{
console.log(req.headers)
let custtags=req.headers["x-consumetag"].split("|");
req.xctx={
appkey:req.headers["x-appkey"],
appkey:req.headers["x-app-key"],
companyid:custtags[0].split("_")[1],
password:custtags[1].split("_")[1],
username:req.headers["x-consumer-username"],
credid:req.headers["x-credential-identifier"],
companykey:req.headers["x-company-key"],
companykey:req.headers["x-company-key"],//这个头没有必要,因为来访companyid有值了
}
var rtn = await this[methodname](pobj, query, req);
return rtn;
......
......@@ -26,18 +26,41 @@ class CtlBase {
}
async setContextParams(pobj, qobj, req) {
let custtags = req.headers["x-consumetag"]?req.headers["x-consumetag"].split("|"):null;
//当自由用户注册时,需要根据前端传来的companykey,查询出公司,给companyid赋值
req.xctx = {
appkey: req.headers["x-app-key"],
appkey: req.headers["x-app-key"],//用于系统管理区分应用,比如角色
companyid: custtags?custtags[0].split("_")[1]:null,
password: custtags?custtags[1].split("_")[1]:null,
username: req.headers["x-consumer-username"],
credid: req.headers["x-credential-identifier"],
companykey:req.headers["x-company-key"],
companykey:req.headers["x-company-key"],//专用于自由用户注册,自由用户用于一定属于某个存在的公司
}
if(!req.xctx.appkey){
return [-200,"请求头缺少应用x-app-key"]
}else{
let app=await this.cacheManager["AppCache"].cache(req.xctx.appkey);
req.xctx.appid=app.id;
pobj.app_id=app.id;//传递参数对象里注入app_id
}
//平台注册时,companyid,companykey都为空
//自由注册时,companykey不能为空
// if(!req.xctx.companyid && !req.xctx.companykey){
// return [-200,"请求头缺少应用x-app-key"]
// }
if(!req.xctx.companyid && creq.xctx.companykey){
let comptmp=await this.cacheManager["CompanyCache"].cache(req.xctx.companykey);
req.xctx.companyid=comptmp.id;
}
if(req.xctx.companyid){//在请求传递数据对象注入公司id
pobj.company_id=req.xctx.companyid;
}
}
async doexec(methodname, pobj, query, req) {
try {
await this.setContextParams(pobj, query, req);
let xarg=await this.setContextParams(pobj, query, req);
if(xarg[0]<0){
return system.getResultFail(...xarg);
}
//从请求头里面取appkey_consumename
// var consumeName=req.headers[""]
// var appkey=
......
......@@ -29,9 +29,10 @@ class UserCtl extends CtlBase {
if (!pobj.userName || !pobj.password) {
return system.getResult(null, "请检查用户名和密码是否存在")
}
p.companykey = req.xctx.companykey;
if (!p.companykey) {
return system.getResult(null, "自有用户创建需要提供公司KEY");
//p.company_id = req.xctx.companyid;//控制基类里已经添加
if (!p.company_id) {
return system.getResultFail(-201, "自有用户创建需要提供公司KEY");
}
let rtn = await this.service.pmregisterByFreeUser(p, q);
return rtn;
......@@ -47,7 +48,6 @@ class UserCtl extends CtlBase {
if (!pobj.userName || !pobj.password) {
return system.getResult(null, "请检查用户名和密码是否存在")
}
p.company_id = req.xctx.companyid;
let rtn = await this.service.registerByTantent(p, q);
return rtn;
}
......
const CacheBase = require("../cache.base");
const system = require("../../system");
const settings = require("../../../config/settings");
//缓存首次登录的赠送的宝币数量
class AppCache extends CacheBase {
constructor() {
class AppCache extends CacheBase{
constructor(){
super();
this.prefix="g_centerappkey:";
this.appDao=system.getObject("db.common.appDao");
}
// isdebug() {
// return settings.env == "dev";
// }
desc() {
return "应用服务实体缓存";
}
prefix() {
return "g_vcode_"
}
async buildCacheVal(cachekey, inputkey, val, ex, ...items) {
//inputkey采用appkey_mobile的形式
var mobile = inputkey;
var tmplCode = val;
var signName = items ? items[0] : "";
var vcode = await this.smsUtil.getUidStr(6, 10);
if (!tmplCode && !signName) {
this.smsUtil.sendMsg(mobile, vcode);
} //tmplCode为发送短信编码,需在阿里开通,signName为短信头描述信息,二者没有传递则用默认的发送验证码
else {
this.smsUtil.aliSendMsg(mobile, tmplCode, signName, JSON.stringify({ code: vcode }));
}
return JSON.stringify({ vcode: vcode });
}
isdebug(){
return settings.env=="dev";
}
desc(){
return "缓存本地应用对象";
}
prefix(){
return "g_applocal_"
}
async buildCacheVal(cachekey,inputkey, val, ex, ...items) {
const configValue=await this.appDao.findOne({where:{appkey:inputkey}});
if (configValue) {
return JSON.stringify(configValue);
}
return null;
}
}
module.exports = VCodeCache;
module.exports=AppCache;
\ No newline at end of file
const CacheBase = require("../cache.base");
const system = require("../../system");
const settings = require("../../../config/settings");
class CompanyCache extends CacheBase{
constructor(){
super();
this.prefix="g_centercompanykey:";
this.companyDao=system.getObject("db.common.companyDao");
}
isdebug(){
return settings.env=="dev";
}
desc(){
return "缓存统一公司对象";
}
prefix(){
return "gc_companylocal_"
}
async buildCacheVal(cachekey,inputkey, val, ex, ...items) {
const configValue=await this.companyDao.findOne({where:{companykey:inputkey}});
if (configValue) {
return JSON.stringify(configValue);
}
return null;
}
}
module.exports=CompanyCache;
\ No newline at end of file
......@@ -48,19 +48,13 @@ class UserService extends ServiceBase {
if (rolecodes[0] == settings.pmroleid["ta"]) {//如果注册时,角色是租户,那么需要创建默认公司
let cmp = await self.companyDao.create({ name: p.userName + "的公司", companykey: cmpkey }, t);
p.company_id = cmp.id;
} else {
if(!p.company_id){
cmpkey = p.companykey;//否则是租户协助创建个人访客,采用租户获得的公司key
//按照公司key,获取公司 to do cache
let compfind = await self.companyDao.model.findOne({ where: { companykey: cmpkey }, transaction: t })
p.company_id = compfind.id;
}
}
}
//p.company_id----已经在控制器基类中设置
let u = await self.dao.create(p, t)
//设置默认角色,租户
//设置默认普通角色,由于有了租户概念,所以注册时,需要知道当前租户和应用的id 才可以设置默认角色 todo
var roles = await self.roleDao.model.findAll({ where: { id: { [self.db.Op.in]: rolecodes } } }, transaction: t });
var roles = await self.roleDao.model.findAll({ where: { id: { [self.db.Op.in]: rolecodes } } ,app_id:p.app_id,company_id:p.company_id}, transaction: t });
if(roles && roles.length>0){
await u.setRoles(roles, { transaction: t });
}
......@@ -165,7 +159,7 @@ class UserService extends ServiceBase {
return rtn;
} else {
//先按照用户名查续身份信息,获取key,secret,
let u = await this.pmregister({ userName: mobile, nickName: mobile,rolecodes:p.rolecodes,companykey:p.companykey});
let u = await this.pmregister({ userName: mobile, nickName: mobile,rolecodes:p.rolecodes,company_id:p.company_id,app_id:p.app_id});
let token = await this.cmakejwt(u.jwtkey, u.jwtsecret, null);
rtn.token = token;
rtn.user = u;
......
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