Commit a03ada50 by 王昆

gsb

parent b1611386
......@@ -60,11 +60,8 @@ class ActionAPI extends APIBase {
break;
// 组织机构
case "addOrg":
opResult = await this.orgSve.apiAddOrg(action_body);
break;
case "updOrg":
opResult = await this.orgSve.apiUpdOrg(action_body);
case "saveOrg":
opResult = await this.orgSve.saveOrg(action_body);
break;
case "delOrg":
opResult = await this.orgSve.apiDelOrg(action_body);
......@@ -123,24 +120,8 @@ class ActionAPI extends APIBase {
opResult = await this.roleSve.setAuth(action_body);
break;
// 用户
case "addUser":
if (action_body.uctype === 1) {
action_body.org = await this.orgSve.findById(Number(action_body.org_id));
if (!action_body.org) {
return system.getResult(null, `组织机构不存在`);
}
}
opResult = await this.userSve.add(action_body);
break;
case "updUser":
if (action_body.uctype === 1) {
action_body.org = await this.orgSve.findById(Number(action_body.org_id));
if (!action_body.org) {
return system.getResult(null, `组织机构不存在`);
}
}
opResult = await this.userSve.upd(action_body);
case "saveUser":
opResult = await this.userSve.saveUser(action_body);
break;
case "userInfo":
opResult = await this.userSve.info(action_body);
......
const system = require("../../../system");
const Dao = require("../../dao.base");
class OrgDao extends Dao {
constructor() {
super(Dao.getModelName(OrgDao));
......@@ -7,14 +8,14 @@ class OrgDao extends Dao {
/**
* 条件查询
* @param {} params
* @param {} params
*/
async findUserByOrgId(params){
async findUserByOrgId(params) {
let sql = [];
sql.push(`SELECT c.ucid,c.ucname,c.uctype,c.saas_id,c.isEnabled,c.created_at,c.updated_at,c.deleted_at
FROM uc_org a INNER JOIN uc_user_org b ON a.id = b.org_id INNER JOIN uc_user c ON b.user_id = c.id
WHERE 1=1 and ( c.deleted_at is null or c.deleted_at > NOW() ) `);
if(params.id){
if (params.id) {
sql.push("AND a.id = :id");
}
return await this.customQuery(sql.join(" "), params);
......@@ -28,12 +29,36 @@ class OrgDao extends Dao {
sql.push("FROM uc_org");
sql.push("WHERE deleted_at IS NULL");
var params = {};
if(saas_id) {
if (saas_id) {
sql.push("AND saas_id = :saas_id");
params.saas_id = saas_id;
}
return this.customQuery(sql.join(" "), params);
}
async mapByIds(ids) {
let result = {};
if (!ids || ids.length == 0) {
return result;
}
let sql = [];
sql.push("SELECT");
sql.push("*");
sql.push("FROM uc_org");
sql.push("WHERE id IN (:ids)");
let list = await this.customQuery(sql.join(" "), {
ids: ids
});
if (!list || list.length == 0) {
return result;
}
for (let item of list) {
result[item.id] = item;
}
return result;
}
}
module.exports = OrgDao;
\ No newline at end of file
......@@ -188,7 +188,9 @@ class UserDao extends Dao {
if(params.uctypeId) {
sql.push("AND t1.uctypeId = :uctypeId");
}
if (params.org_id) {
sql.push("AND t1.org_id = :org_id");
}
sql.push("GROUP BY t1.id");
var list = await this.customQuery(sql.join(" "), params);
if (!list || list.length == 0) {
......@@ -197,5 +199,17 @@ class UserDao extends Dao {
return list;
}
async updateOrg(org_id, orgpath) {
let sql = "UPDATE uc_user SET orgpath = :orgpath WHERE org_id = :org_id";
await this.customUpdate(sql, {org_id: org_id, orgpath: orgpath});
}
async updateOrgMain(org_id, orgpath, ids) {
let sql1 = "UPDATE `uc_user` SET orgpath = CONCAT(:orgpath, id, '/') WHERE org_id = :org_id";
await this.customUpdate(sql1, {org_id: org_id, orgpath: orgpath});
let sql2 = "UPDATE `uc_user` SET orgpath = :orgpath WHERE id IN (:ids)";
await this.customUpdate(sql2, {orgpath: orgpath, ids: ids});
}
}
module.exports = UserDao;
\ No newline at end of file
......@@ -56,5 +56,27 @@ class UserroleDao extends Dao {
}
return result;
}
async mapByUserIds2(userIds) {
let result = {};
let sql = [];
sql.push("SELECT");
sql.push("t2.id, t1.user_id, t2.`code`, t2.`name`");
sql.push("FROM uc_user_role t1")
sql.push("INNER JOIN uc_role t2 ON t1.`role_id` = t2.`id`");
sql.push("WHERE t1.`user_id` IN (:userIds)");
let list = await this.customQuery(sql.join(" "), {userIds: userIds});
if (!list || list.length == 0) {
return {};
}
for (let item of list) {
let roles = result[item.user_id] || [];
roles.push(item);
result[item.user_id] = roles;
}
return result;
}
}
module.exports = UserroleDao;
\ No newline at end of file
......@@ -7,6 +7,7 @@ module.exports = (db, DataTypes) => {
path: DataTypes.STRING,
pid: DataTypes.INTEGER,
saas_id: DataTypes.INTEGER,
main_ids: DataTypes.STRING,
is_leaf: {
type: DataTypes.BOOLEAN,
defaultValue: true
......
......@@ -5,6 +5,8 @@ module.exports = (db, DataTypes) => {
return db.define("user", {
ucid: DataTypes.STRING,
ucname: DataTypes.STRING,
mobile: DataTypes.STRING,
realName: DataTypes.STRING,
password: DataTypes.STRING,
uctype: DataTypes.INTEGER,
uctypeId: DataTypes.STRING,
......
......@@ -4,6 +4,8 @@ const settings = require("../../../../config/settings")
class OrgService extends ServiceBase {
constructor() {
super("org", ServiceBase.getDaoName(OrgService));
this.userDao = system.getObject("db.user.userDao");
}
/**
* 添加
......@@ -106,39 +108,58 @@ class OrgService extends ServiceBase {
* 添加
* @param {T} params
*/
async addOrg(params) {
async saveOrg(params) {
try {
var pid = Number(params.pid || 0);
let id = this.trim(params.id);
params.orgname = this.trim(params.orgname);
params.saas_id = Number(params.saas_id || 0);
params.main_ids = this.trim(params.main_ids);
let mainIds = [];
for (let mid of params.main_ids.split(",")) {
if (mid) {
mainIds.push(mid);
}
}
if (!params.saas_id) {
return system.getResult(-1, `saas_id不存在`)
return system.getResult(-1, `saas_id不存在`);
}
if (!params.orgname) {
return system.getResult(-1, `参数错误 组织名称不能为空`)
}
let _orgByName = await this.findOne({
let exists = await this.findOne({
orgname: params.orgname,
saas_id: params.saas_id
});
if (_orgByName) {
return system.getResult(-1, `参数错误 组织名称已经存在`);
if (id) {
if (exists && id != exists.id) {
return system.getResult(-1, `参数错误 ${params.orgname} 组织名称已经存在`);
}
} else {
if (exists) {
return system.getResult(-1, `参数错误 ${params.orgname} 组织名称已经存在`);
}
}
let mainUser;
let path = "";
if (pid === 0) {
// 验证是否存在其他权限
var exist = await this.findCount({
where: {
saas_id: saas_id
if(!id) {
// 验证是否存在其他权限
var exist = await this.findCount({
where: {
saas_id: saas_id
}
});
if (exist) {
return system.getResult(null, "菜单根目录已经存在");
}
});
if (exist) {
return system.getResult(null, "菜单根目录已经存在");
}
path = `/${params.orgname}`;
} else {
let _org = await this.findOne({
......@@ -150,9 +171,20 @@ class OrgService extends ServiceBase {
path = `${_org.path}/${params.orgname}`;
}
params.path = path;
let res = await this.dao.create(params);
return system.getResult(res);
let org;
if(id) {
await this.dao.update(params);
org = params;
} else {
org = await this.dao.create(params);
}
await this.userDao.updateOrg(org.id, path + "/");
if (mainIds && mainIds.length > 0) {
await this.userDao.updateOrgMain(org.id, path + "/", mainIds);
}
return system.getResultSuccess();
} catch (error) {
console.log(error);
return system.getResult(-1, `系统错误 错误信息 ${error}`);
}
}
......
......@@ -7,11 +7,9 @@ class UserService extends ServiceBase {
super("user", ServiceBase.getDaoName(UserService));
this.userinfoDao = system.getObject("db.user.userinfoDao");
this.userroleDao = system.getObject("db.user.userroleDao");
this.authSve = system.getObject("service.auth.authSve");
this.roleDao = system.getObject("db.role.roleDao");
this.orgDao = system.getObject("db.org.orgDao");
}
......@@ -119,6 +117,90 @@ class UserService extends ServiceBase {
}
}
async saveUser(obj) {
try {
var id = obj.id;
var roleIds = obj.roles || [];
var org_id = this.trim(obj.org_id);
let roles = [];
for (let roleId of roleIds) {
roles.push({role_id: roleId});
}
if (!obj.saas_id) {
return system.getResult(null, "saas_id不存在");
}
if (id) {
let u = await this.findById(id);
if (u) {
user.id = u.id;
}
}
let user = {};
let exist = await this.findOne({
ucname: obj.ucname
});
if (user.id) {
if (exist && exist.id != user.id) {
return system.getResult(null, `用户名【${ucname}】已存在`);
}
} else {
if (exist) {
return system.getResult(null, `用户名【${ucname}】已存在`);
}
user.password = await this.getEncryptStr(obj.password);
}
let org = await this.orgDao.findById(org_id);
if (!org) {
return system.getResult(null, `组织机构不存在`);
}
user.ucid = this.trim(obj.ucid);
user.ucname = this.trim(obj.ucname);
user.uctype = 1;
user.uctypeId = "";
user.mobile = this.trim(obj.mobile);
user.realName = this.trim(obj.realName);
user.org_id = org_id;
var self = this;
user = await self.db.transaction(async function (t) {
if (user.id) {
await self.dao.update(user, t);
} else {
// 创建商户
user = await self.dao.create(user, t);
}
await self.userroleDao.delByUserId(user.id, t);
if (roles && roles.length > 0) {
for (var r of roles) {
r.user_id = user.id;
}
await self.userroleDao.bulkCreate(roles, t);
}
user.orgpath = org.path + "/" + user.id + "/";
await self.dao.update({
id: user.id,
orgpath: user.orgpath
}, t);
return user;
});
return system.getResultSuccess(user);
} catch (error) {
console.log(error);
if (error.name == 'SequelizeValidationError') {
return system.getResult(-1, `用户名重复`);
}
return system.getResult(-1, `系统错误 错误信息${error}`);
}
}
async add(obj) {
try {
var roles = obj.roles || [];
......@@ -192,7 +274,7 @@ class UserService extends ServiceBase {
return system.getResultSuccess(user);
} catch (error) {
return system.getResult(-1,`系统错误 错误信息${error}`);
return system.getResult(-1, `系统错误 错误信息${error}`);
}
}
......@@ -206,7 +288,7 @@ class UserService extends ServiceBase {
var mobile = this.trim(obj.mobile);
var realName = this.trim(obj.realName);
var isMain = obj.isMain || 0;
var user = {
id: id,
uctype: uctype,
......@@ -215,23 +297,23 @@ class UserService extends ServiceBase {
orgpath: "",
isMain: obj.isMain || 0,
}
if (Number(this.trim(user.uctype)) === 1) {
user.orgpath = (isMain ? org.path : org.path + "/" + id) + "/";
}
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) {
......@@ -243,7 +325,7 @@ class UserService extends ServiceBase {
});
return system.getResultSuccess(user);
} catch (error) {
return system.getResult(-1,`系统错误 错误信息 ${error}`);
return system.getResult(-1, `系统错误 错误信息 ${error}`);
}
}
......@@ -297,7 +379,8 @@ class UserService extends ServiceBase {
for (let item of result.rows) {
this.handleDate(item, ["created_at"], null, -8);
}
await this.setRoleIds(result.rows);
await this.setRoles(result.rows);
await this.setOrg(result.rows);
}
return system.getResultSuccess(result);
}
......@@ -329,12 +412,11 @@ class UserService extends ServiceBase {
async mapByIds(params) {
let rs = await this.dao.findMapByIds(params.ids);
return system.getResultSuccess(rs);
}
async findUsers(params) {
if(params.roleCodes && params.roleCodes.length > 0) {
if (params.roleCodes && params.roleCodes.length > 0) {
var roleIds = await this.roleDao.findIdsByCode(params.roleCodes, params.saas_id);
if (!roleIds) {
return [];
......@@ -345,7 +427,25 @@ class UserService extends ServiceBase {
return system.getResultSuccess(rs);
}
async setRoleIds(rows) {
async setOrg(rows) {
if (!rows || rows.length == 0) {
return;
}
let orgIds = [];
for (let row of rows) {
orgIds.push(row.org_id);
}
let map = await this.orgDao.mapByIds(orgIds);
for (let row of rows) {
let org = map[row.org_id] || {};
row.orgname = org.orgname || "";
row.org = org;
}
}
async setRoles(rows) {
if (!rows || rows.length == 0) {
return;
}
......@@ -355,11 +455,21 @@ class UserService extends ServiceBase {
userIds.push(row.id);
}
let roleIdMap = await this.userroleDao.mapByUserIds(userIds);
let rolesMap = await this.userroleDao.mapByUserIds2(userIds);
for (let row of rows) {
row.roleIds = roleIdMap[row.id] || [];
let roleIds = [];
let roleNames = [];
let roles = rolesMap[row.id] || [];
for (let r of roles) {
roleIds.push(r.id);
roleNames.push(r.name);
}
row.roleIds = roleIds.join(",");
row.roleNames = roleNames.join(",");
row.roles = roles;
}
}
}
module.exports = UserService;
\ 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