Commit 6dd0480e by 王昆

gsb

parent 78a765c4
...@@ -47,17 +47,17 @@ class ActionAPI extends APIBase { ...@@ -47,17 +47,17 @@ class ActionAPI extends APIBase {
break; break;
case "delSaas": case "delSaas":
opResult = await this.userSve.apiFindUserBySaasId(action_body); opResult = await this.userSve.apiFindUserBySaasId(action_body);
if(opResult.status==0 && opResult.data.length==0){ if (opResult.status == 0 && opResult.data.length == 0) {
opResult = await this.saasSve.apiDelSaas(action_body); opResult = await this.saasSve.apiDelSaas(action_body);
}else{ } else {
opResult = system.getResult(-1,`当前SAAS不能删除`); opResult = system.getResult(-1, `当前SAAS不能删除`);
} }
break; break;
case "listSaas": case "listSaas":
opResult = await this.saasSve.apiListSaas(action_body); opResult = await this.saasSve.apiListSaas(action_body);
break; break;
// 组织机构 // 组织机构
case "addOrg": case "addOrg":
opResult = await this.orgSve.apiAddOrg(action_body); opResult = await this.orgSve.apiAddOrg(action_body);
break; break;
...@@ -65,61 +65,76 @@ class ActionAPI extends APIBase { ...@@ -65,61 +65,76 @@ class ActionAPI extends APIBase {
opResult = await this.orgSve.apiUpdOrg(action_body); opResult = await this.orgSve.apiUpdOrg(action_body);
break; break;
case "delOrg": case "delOrg":
opResult = await this.orgSve.apiDelOrg(action_body); opResult = await this.orgSve.apiDelOrg(action_body);
break; break;
case "listOrg": case "listOrg":
opResult = await this.orgSve.apiListOrg(action_body); opResult = await this.orgSve.apiListOrg(action_body);
break; break;
case "findById": case "findById":
opResult = await this.orgSve.apiFindById(action_body); opResult = await this.orgSve.apiFindById(action_body);
break; break;
case "byPid": case "byPid":
opResult = await this.orgSve.apiByPid(action_body); opResult = await this.orgSve.apiByPid(action_body);
break; break;
case "tree": case "tree":
opResult = await this.orgSve.apiTree(); opResult = await this.orgSve.apiTree();
break; break;
// 菜单权限 // 菜单权限
case "addAuth": case "addAuth":
return this.authSve.add(action_body); opResult = this.authSve.add(action_body);
break; break;
case "updAuth": case "updAuth":
return this.authSve.upd(action_body); opResult = this.authSve.upd(action_body);
break; break;
case "tree": case "tree":
return this.authSve.tree(action_body); opResult = this.authSve.tree(action_body);
break; break;
case "byPid": case "byPid":
return this.authSve.byPid(action_body); opResult = this.authSve.byPid(action_body);
break; break;
case "delAuth": case "delAuth":
// TODO 验证是否有角色关联 // TODO 验证是否有角色关联
return this.authSve.del(action_body); opResult = this.authSve.del(action_body);
break; break;
// 角色 // 角色
case "addRole": case "addRole":
opResult = await this.roleSve.apiAddRole(action_body); opResult = await this.roleSve.apiAddRole(action_body);
break; break;
case "updRole": case "updRole":
opResult = await this.roleSve.apiUpdRole(action_body); opResult = await this.roleSve.apiUpdRole(action_body);
break; break;
case "delRole": case "delRole":
opResult = await this.roleSve.apiDelRole(action_body); opResult = await this.roleSve.apiDelRole(action_body);
break; break;
case "listRole": case "listRole":
opResult = await this.roleSve.apiListRole(action_body); opResult = await this.roleSve.apiListRole(action_body);
break; break;
// 用户 // 用户
case "addUser": case "addUser":
action_body.org = await this.orgSve.findById(Number(action_body.org_id));
if (!action_body.org) {
opResult = system.getResult(null, `组织机构不存在`);
} else {
opResult = this.userSve.add(action_body);
}
break; break;
case "updUser": case "updUser":
action_body.org = await this.orgSve.findById(Number(action_body.org_id));
if (!action_body.org) {
opResult = system.getResult(null, `当前SAAS不能删除`);
} else {
opResult = this.userSve.upd(action_body);
}
break;
case "userInfo":
opResult = this.userSve.info(action_body);
break; break;
case "delUser": case "delUser":
break; break;
case "listUser": case "listUser":
opResult = this.userSve.pageByCondition(action_body);
break; break;
} }
return opResult; return opResult;
......
...@@ -32,14 +32,14 @@ class Dao { ...@@ -32,14 +32,14 @@ class Dao {
} }
//批量插入 //批量插入
async bulkCreate(objs) { async bulkCreate(objs, t) {
if (!objs || objs.length == 0) { if (!objs || objs.length == 0) {
return; return;
} }
for (var obj of objs) { if (t) {
if (!obj.id) { return await this.model.bulkCreate(objs, {
obj.id = await this.redisClient.genrateId(this.modelName); transaction: t
} });
} }
return await this.model.bulkCreate(objs); return await this.model.bulkCreate(objs);
} }
...@@ -156,8 +156,10 @@ class Dao { ...@@ -156,8 +156,10 @@ class Dao {
console.log(qc); console.log(qc);
return qc; return qc;
} }
async findAll(obj){ async findAll(obj) {
return await this.model.findAll({where: obj || {}}); return await this.model.findAll({
where: obj || {}
});
} }
async findAndCountAll(qobj, t) { async findAndCountAll(qobj, t) {
......
...@@ -5,32 +5,99 @@ class UserDao extends Dao { ...@@ -5,32 +5,99 @@ class UserDao extends Dao {
super(Dao.getModelName(UserDao)); super(Dao.getModelName(UserDao));
} }
/** /**
* 条件查询 * 条件查询
* @param {} params * @param {} params
*/ */
async findUserBySaasId(params){ async findUserBySaasId(params) {
let sql = []; let sql = [];
sql.push('select ucid,ucname,uctype,saas_id,isEnabled from uc_user where 1 = 1'); sql.push('select ucid,ucname,uctype,saas_id,isEnabled from uc_user where 1 = 1');
if(params.ucid){ if (params.ucid) {
sql.push("AND ucid = :ucid"); sql.push("AND ucid = :ucid");
} }
if(params.ucname){ if (params.ucname) {
sql.push("AND ucname = :ucname"); sql.push("AND ucname = :ucname");
} }
if(params.uctype){ if (params.uctype) {
sql.push("AND uctype = :uctype"); sql.push("AND uctype = :uctype");
} }
if(params.saas_id){ if (params.saas_id) {
sql.push("AND saas_id = :saas_id"); sql.push("AND saas_id = :saas_id");
} }
if(params.isEnabled){ if (params.isEnabled) {
sql.push("AND saas_id = :isEnabled"); sql.push("AND saas_id = :isEnabled");
} }
sql.push(`AND (deleted_at > NOW() or deleted_at is null)`); sql.push(`AND (deleted_at > NOW() or deleted_at is null)`);
return await this.customQuery(sql.join(" "), params); return await this.customQuery(sql.join(" "), params);
} }
async countByCondition(params) {
var sql = [];
sql.push("SELECT");
sql.push("count(1) as num");
sql.push("FROM uc_user t1");
sql.push("INNER JOIN uc_user_info t2 ON t1.id = t2.id");
sql.push("WHERE 1 = 1");
var list = await this.customQuery(sql.join(" "), params);
if (!list || list.length == 0) {
return 0;
}
return list[0].num;
}
async listByCondition(params) {
params.startRow = Number(params.startRow || 0);
params.pageSize = Number(params.pageSize || 10);
var sql = [];
sql.push("SELECT");
sql.push("t1.id, t1.ucid, t1.ucname, t1.uctype, t1.org_id, t1.orgpath,");
sql.push("t1.isMain, t1.saas_id, t1.isEnabled, t1.created_at,");
sql.push("t2.mobile, t2.realName");
sql.push("FROM uc_user t1");
sql.push("INNER JOIN uc_user_info t2 ON t1.id = t2.id");
sql.push("WHERE 1 = 1");
this.setCondition(sql, params);
sql.push("ORDER BY t1.id DESC");
sql.push("LIMIT :startRow, :pageSize");
return await this.customQuery(sql.join(" "), params);
}
setCondition(params, sql) {
if (!params || !sql) {
return;
}
if(params.saas_id) {
sql.push("AND t1.saas_id = :saas_id");
}
if(params.ucnameLike) {
sql.push("AND t1.ucname LIKE :ucnameLike");
}
if(params.mobileLike) {
sql.push("AND t2.mobile LIKE :mobileLike");
}
if(params.realNameLike) {
sql.push("AND t2.realName LIKE :realNameLike");
}
if(params.uctype) {
sql.push("AND t1.uctype LIKE :uctype");
}
if(params.createBegin) {
sql.push("AND t1.created_at >= :createBegin");
}
if(params.createEnd) {
sql.push("AND t1.created_at <= :createEnd");
}
}
} }
module.exports = UserDao; module.exports = UserDao;
\ No newline at end of file
...@@ -4,5 +4,19 @@ class UserroleDao extends Dao { ...@@ -4,5 +4,19 @@ class UserroleDao extends Dao {
constructor() { constructor() {
super(Dao.getModelName(UserroleDao)); super(Dao.getModelName(UserroleDao));
} }
async delByUserId(user_id, t) {
var sql = "DELETE FROM uc_user_role WHERE user_id = :user_id";
await this.customUpdate(sql, {
user_id: user_id
}, t) || [];
}
async listByUserId(user_id, t) {
var sql = "SELECT * FROM uc_user_role WHERE user_id = :user_id";
return await this.customQuery(sql, {
user_id: user_id
}, t) || [];
}
} }
module.exports = UserroleDao; module.exports = UserroleDao;
\ No newline at end of file
...@@ -4,7 +4,8 @@ const settings = require("../../../../config/settings") ...@@ -4,7 +4,8 @@ const settings = require("../../../../config/settings")
class UserService extends ServiceBase { class UserService extends ServiceBase {
constructor() { constructor() {
super("user", ServiceBase.getDaoName(UserService)); super("user", ServiceBase.getDaoName(UserService));
this.user this.userinfoDao = system.getObject("db.user.userinfoDao");
this.userroleDao = system.getObject("db.user.userroleDao");
} }
...@@ -12,29 +13,27 @@ class UserService extends ServiceBase { ...@@ -12,29 +13,27 @@ class UserService extends ServiceBase {
* 条件查询 * 条件查询
* @param {*} params * @param {*} params
*/ */
async apiFindUserBySaasId(params){ async apiFindUserBySaasId(params) {
try { try {
return await this.findUserBySaasId(params); return await this.findUserBySaasId(params);
} catch (error) { } catch (error) {
return system.getResult(-1,`系统错误 错误信息 ${error}`); return system.getResult(-1, `系统错误 错误信息 ${error}`);
} }
} }
/**************************************************************** */
/**************************************************************** */
/** /**
* 条件查询 * 条件查询
* @param {*} params * @param {*} params
*/ */
async findUserBySaasId(params){ async findUserBySaasId(params) {
try { try {
let res = await this.dao.findUserBySaasId(params); let res = await this.dao.findUserBySaasId(params);
return system.getResult(res); return system.getResult(res);
} catch (error) { } catch (error) {
return system.getResult(-1,`系统错误 错误信息 ${error}`); return system.getResult(-1, `系统错误 错误信息 ${error}`);
} }
} }
...@@ -42,42 +41,156 @@ class UserService extends ServiceBase { ...@@ -42,42 +41,156 @@ class UserService extends ServiceBase {
var roles = obj.roles || []; var roles = obj.roles || [];
var org = obj.org; var org = obj.org;
var saas_id = Number(obj.saas_id || 0); var saas_id = Number(obj.saas_id || 0);
var ucid = this.trim(obj.ucid);
var ucname = this.trim(obj.ucname); var ucname = this.trim(obj.ucname);
var password = this.trim(obj.password); var password = this.trim(obj.password);
var uctype = this.trim(obj.uctype); var uctype = this.trim(obj.uctype);
var mobile = this.trim(obj.mobile); var mobile = this.trim(obj.mobile);
var realName = this.trim(obj.realName); var realName = this.trim(obj.realName);
var isMain = obj.isMain || 0;
if(!saas_id) { if (!saas_id) {
// TODO 写到这 return system.getResult(null, "请指定saas_id");
}
var exist = await this.findOne({
ucname: ucname
});
if (exist) {
return system.getResult(null, `用户名【${ucname}】已存在`);
} }
var user = { var user = {
saas_id: saas_id,
ucid: ucid, ucid: ucid,
ucname: ucname, ucname: ucname,
password: password, password: await this.getEncryptStr(password),
uctype: uctype, uctype: uctype,
org_id: org.id, org_id: org.id,
isMain: obj.isMain || 0, isMain: isMain,
orgpath: "", orgpath: "",
password: "",
isEnabled: 1, isEnabled: 1,
} }
var orgpath = org.path;
var info = { var info = {
mobile: mobile, mobile: mobile,
realName: realName, realName: realName,
} }
var self = this; var self = this;
await self.db.transaction(async function (t) { user = await self.db.transaction(async function (t) {
// 创建商户 // 创建商户
user = await self.dao.create(user, t); user = await self.dao.create(user, t);
info.id = user.id;
info = await self.userinfoDao.create(info, t);
if (roles && roles.length > 0) {
for (var r of roles) {
r.user_id = user.id;
}
await self.userroleDao.bulkCreate(roles, t);
}
if (!isMain) {
orgpath = orgpath + "/" + user.id;
}
user.orgpath = orgpath;
await self.dao.update({
id: user.id,
orgpath: orgpath
}, t);
return user; return user;
}); });
return system.getResultSuccess(user);
} }
async upd(obj) {
var id = obj.id;
var roles = obj.roles || [];
var org = obj.org;
var uctype = this.trim(obj.uctype);
var mobile = this.trim(obj.mobile);
var realName = this.trim(obj.realName);
var isMain = obj.isMain || 0;
var user = {
id: id,
org_id: org.id,
orgpath: isMain ? org.path : org.path + "/" + id,
isMain: obj.isMain || 0,
}
var info = {
id: id,
mobile: mobile,
realName: realName,
}
var self = this;
user = await self.db.transaction(async function (t) {
// 创建商户
await self.dao.update(user, t);
await self.userinfoDao.update(info, t);
await self.userroleDao.delByUserId(id, t);
if (roles && roles.length > 0) {
for (var r of roles) {
r.user_id = id;
}
await self.userroleDao.bulkCreate(roles, t);
}
return user;
});
return system.getResultSuccess(user);
}
async info(params) {
var id = Number(params.id || 0);
var user = await this.dao.getById(id);
if (!user) {
return system.getResult(null, "用户不存在");
}
var info = await this.userinfoDao.getById(id);
user.mobile = info.mobile;
user.realName = info.realName;
user.roles = await this.userroleDao.listByUserId(id);
this.handleDate(user, ["created_at"], null, -8);
return system.getResultSuccess(user);
}
async pageByCondition(params) {
var result = {
count: 0,
rows: []
};
var currentPage = Number(params.currentPage || 1);
var pageSize = Number(params.pageSize || 10);
var total = await this.dao.countByCondition(params);
if (total == 0) {
return result;
}
result.count = total;
params.startRow = (currentPage - 1) * pageSize;
result.rows = await this.dao.listByCondition(params) || [];
return system.getResultSuccess(result);
}
async delUser(params) {
await this.delete({
id: params.id
});
return system.getResultSuccess();
}
} }
module.exports = UserService; module.exports = UserService;
\ No newline at end of file
...@@ -277,8 +277,8 @@ class ServiceBase { ...@@ -277,8 +277,8 @@ class ServiceBase {
if (!str) { if (!str) {
throw new Error("字符串不能为空"); throw new Error("字符串不能为空");
} }
var md5 = md5(str + "_" + settings.salt); var pwd = md5(str + "_" + settings.salt);
return md5.toString().toLowerCase(); return pwd.toString().toLowerCase();
} }
} }
module.exports = ServiceBase; module.exports = ServiceBase;
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