Commit ea8dfdd3 by 孙亚楠

修改user查询接口

parents 2514986a 784c8438
...@@ -9,7 +9,6 @@ class ActionAPI extends APIBase { ...@@ -9,7 +9,6 @@ class ActionAPI extends APIBase {
this.userSve = system.getObject("service.user.userSve"); this.userSve = system.getObject("service.user.userSve");
this.roleSve = system.getObject("service.role.roleSve"); this.roleSve = system.getObject("service.role.roleSve");
this.authSve = system.getObject("service.auth.authSve"); this.authSve = system.getObject("service.auth.authSve");
} }
/** /**
...@@ -76,11 +75,11 @@ class ActionAPI extends APIBase { ...@@ -76,11 +75,11 @@ class ActionAPI extends APIBase {
case "findById": case "findById":
opResult = await this.orgSve.apiFindById(action_body); opResult = await this.orgSve.apiFindById(action_body);
break; break;
case "byPid": case "orgByPid":
opResult = await this.orgSve.apiByPid(action_body); opResult = await this.orgSve.apiByPid(action_body);
break; break;
case "orgTree": case "orgTree":
opResult = await this.orgSve.apiTree(); opResult = await this.orgSve.apiTree(action_body);
break; break;
// 菜单权限 // 菜单权限
...@@ -96,7 +95,7 @@ class ActionAPI extends APIBase { ...@@ -96,7 +95,7 @@ class ActionAPI extends APIBase {
case "authInfo": case "authInfo":
opResult = this.authSve.info(action_body); opResult = this.authSve.info(action_body);
break; break;
case "byPid": case "authByPid":
opResult = this.authSve.byPid(action_body); opResult = this.authSve.byPid(action_body);
break; break;
case "delAuth": case "delAuth":
...@@ -120,31 +119,50 @@ class ActionAPI extends APIBase { ...@@ -120,31 +119,50 @@ class ActionAPI extends APIBase {
case "roleQueryById": case "roleQueryById":
opResult = await this.roleSve.apiQueryById(action_body); opResult = await this.roleSve.apiQueryById(action_body);
break; break;
case "setAuth":
opResult = await this.roleSve.setAuth(action_body);
break;
// 用户 // 用户
case "addUser": case "addUser":
if (action_body.uctype === 1) {
action_body.org = await this.orgSve.findById(Number(action_body.org_id)); action_body.org = await this.orgSve.findById(Number(action_body.org_id));
if (!action_body.org) { if (!action_body.org) {
opResult = system.getResult(null, `组织机构不存在`); return system.getResult(null, `组织机构不存在`);
} else { }
opResult = this.userSve.add(action_body);
} }
opResult = this.userSve.add(action_body);
break; break;
case "updUser": case "updUser":
if (action_body.uctype === 1) {
action_body.org = await this.orgSve.findById(Number(action_body.org_id)); action_body.org = await this.orgSve.findById(Number(action_body.org_id));
if (!action_body.org) { if (!action_body.org) {
opResult = system.getResult(null, `当前SAAS不能删除`); return system.getResult(null, `组织机构不存在`);
} else { }
opResult = this.userSve.upd(action_body);
} }
opResult = this.userSve.upd(action_body);
break; break;
case "userInfo": case "userInfo":
opResult = this.userSve.info(action_body); opResult = this.userSve.info(action_body);
break; break;
case "enabled":
opResult = this.userSve.enabled(action_body);
break;
case "delUser": case "delUser":
opResult = this.userSve.delUser(action_body);
break; break;
case "listUser": case "userPage":
opResult = this.userSve.pageByCondition(action_body); opResult = this.userSve.pageByCondition(action_body);
break; break;
case "updPassword":
opResult = this.userSve.updPassword(action_body);
break;
case "login":
opResult = this.userSve.login(action_body);
break;
case "loginByUcid":
opResult = this.userSve.loginByUcid(action_body);
break;
} }
return opResult; return opResult;
} }
......
...@@ -5,8 +5,38 @@ class AuthDao extends Dao { ...@@ -5,8 +5,38 @@ class AuthDao extends Dao {
super(Dao.getModelName(AuthDao)); super(Dao.getModelName(AuthDao));
} }
async all() { async all(saas_id, attrs) {
return this.customQuery("SELECT * FROM uc_auth WHERE deleted_at IS NULL"); attrs = attrs || "*";
var sql = [];
sql.push("SELECT");
sql.push(attrs);
sql.push("FROM uc_auth");
sql.push("WHERE deleted_at IS NULL");
var params = {};
if(saas_id) {
sql.push("AND saas_id = :saas_id");
params.saas_id = saas_id;
}
return this.customQuery(sql.join(" "), params);
}
async byRoleIds(params) {
if(!params.roleIds || params.roleIds.length == 0) {
return [];
}
var sql = [];
sql.push("SELECT");
sql.push("t1.id, t1.`pid`, t1.`name`, t1.`icon`, t1.`path`");
sql.push("FROM uc_auth t1");
sql.push("INNER JOIN `uc_role_auth` t2 ON t1.`id` = t2.`auth_id`");
sql.push("WHERE t2.`role_id` IN (:roleIds)");
if(params.menuType) {
sql.push("AND t1.menuType = :menuType");
}
if(params.saas_id) {
sql.push("AND t1.saas_id = :saas_id");
}
return this.customQuery(sql.join(" "), params);
} }
} }
module.exports = AuthDao; module.exports = AuthDao;
\ No newline at end of file
...@@ -20,10 +20,20 @@ class OrgDao extends Dao { ...@@ -20,10 +20,20 @@ class OrgDao extends Dao {
return await this.customQuery(sql.join(" "), params); return await this.customQuery(sql.join(" "), params);
} }
async all() { async all(saas_id, attrs) {
return this.customQuery("SELECT * FROM uc_org WHERE deleted_at IS NULL"); attrs = attrs || "*";
var sql = [];
sql.push("SELECT");
sql.push(attrs);
sql.push("FROM uc_org");
sql.push("WHERE deleted_at IS NULL");
var params = {};
if(saas_id) {
sql.push("AND saas_id = :saas_id");
params.saas_id = saas_id;
}
return this.customQuery(sql.join(" "), params);
} }
} }
module.exports = OrgDao; module.exports = OrgDao;
\ No newline at end of file
...@@ -4,5 +4,12 @@ class RoleauthDao extends Dao { ...@@ -4,5 +4,12 @@ class RoleauthDao extends Dao {
constructor() { constructor() {
super(Dao.getModelName(RoleauthDao)); super(Dao.getModelName(RoleauthDao));
} }
async delByRoleId(role_id, t) {
var sql = "DELETE FROM uc_role_auth WHERE role_id = :role_id";
await this.customUpdate(sql, {
role_id: role_id
}, t) || [];
}
} }
module.exports = RoleauthDao; module.exports = RoleauthDao;
\ No newline at end of file
...@@ -32,13 +32,37 @@ class UserDao extends Dao { ...@@ -32,13 +32,37 @@ class UserDao extends Dao {
return await this.customQuery(sql.join(" "), params); return await this.customQuery(sql.join(" "), params);
} }
async getByUcname(ucname) {
var sql = "SELECT * FROM uc_user WHERE ucname = :ucname AND deleted_at IS NULL";
var list = await this.customQuery(sql, {
ucname: ucname,
});
if (!list || list.length == 0) {
return null;
}
return list[0];
}
async getByUcid(ucid) {
var sql = "SELECT * FROM uc_user WHERE ucid = :ucid AND deleted_at IS NULL";
var list = await this.customQuery(sql, {
ucid: ucid
});
if (!list || list.length == 0) {
return null;
}
return list[0];
}
async countByCondition(params) { async countByCondition(params) {
var sql = []; var sql = [];
sql.push("SELECT"); sql.push("SELECT");
sql.push("count(1) as num"); sql.push("count(1) as num");
sql.push("FROM uc_user t1"); sql.push("FROM uc_user t1");
sql.push("INNER JOIN uc_user_info t2 ON t1.id = t2.id"); sql.push("INNER JOIN uc_user_info t2 ON t1.id = t2.id");
sql.push("WHERE 1 = 1"); sql.push("WHERE t1.deleted_at IS NULL");
this.setCondition(sql, params);
var list = await this.customQuery(sql.join(" "), params); var list = await this.customQuery(sql.join(" "), params);
if (!list || list.length == 0) { if (!list || list.length == 0) {
...@@ -58,7 +82,7 @@ class UserDao extends Dao { ...@@ -58,7 +82,7 @@ class UserDao extends Dao {
sql.push("t2.mobile, t2.realName"); sql.push("t2.mobile, t2.realName");
sql.push("FROM uc_user t1"); sql.push("FROM uc_user t1");
sql.push("INNER JOIN uc_user_info t2 ON t1.id = t2.id"); sql.push("INNER JOIN uc_user_info t2 ON t1.id = t2.id");
sql.push("WHERE 1 = 1"); sql.push("WHERE t1.deleted_at IS NULL");
this.setCondition(sql, params); this.setCondition(sql, params);
...@@ -67,37 +91,45 @@ class UserDao extends Dao { ...@@ -67,37 +91,45 @@ class UserDao extends Dao {
return await this.customQuery(sql.join(" "), params); return await this.customQuery(sql.join(" "), params);
} }
setCondition(params, sql) { setCondition(sql, params) {
if (!params || !sql) { if (!params || !sql) {
return; return;
} }
if(params.saas_id) { if (params.saas_id) {
sql.push("AND t1.saas_id = :saas_id"); sql.push("AND t1.saas_id = :saas_id");
} }
if(params.ucnameLike) { if (params.ucname) {
sql.push("AND t1.ucname LIKE :ucnameLike"); sql.push("AND t1.ucname LIKE :ucname");
} }
if(params.mobileLike) { if (params.mobile) {
sql.push("AND t2.mobile LIKE :mobileLike"); sql.push("AND t2.mobile LIKE :mobile");
} }
if(params.realNameLike) { if (params.realName) {
sql.push("AND t2.realName LIKE :realNameLike"); sql.push("AND t2.realName LIKE :realName");
} }
if(params.uctype) { if (params.uctype) {
sql.push("AND t1.uctype LIKE :uctype"); sql.push("AND t1.uctype LIKE :uctype");
} }
if(params.createBegin) { if (params.createBegin) {
sql.push("AND t1.created_at >= :createBegin"); sql.push("AND t1.created_at >= :createBegin");
} }
if(params.createEnd) { if (params.createEnd) {
sql.push("AND t1.created_at <= :createEnd"); sql.push("AND t1.created_at <= :createEnd");
} }
if (params.isEnabled === 0 || params.isEnabled === 1) {
sql.push("AND t1.isEnabled = :isEnabled");
}
if(params.orgpath) {
sql.push("AND t1.orgpath LIKE :orgpath");
}
} }
} }
module.exports = UserDao; module.exports = UserDao;
\ No newline at end of file
...@@ -4,5 +4,7 @@ class UserinfoDao extends Dao { ...@@ -4,5 +4,7 @@ class UserinfoDao extends Dao {
constructor() { constructor() {
super(Dao.getModelName(UserinfoDao)); super(Dao.getModelName(UserinfoDao));
} }
} }
module.exports = UserinfoDao; module.exports = UserinfoDao;
\ No newline at end of file
...@@ -12,11 +12,24 @@ class UserroleDao extends Dao { ...@@ -12,11 +12,24 @@ class UserroleDao extends Dao {
}, t) || []; }, t) || [];
} }
async listByUserId(user_id, t) { async listByUserId(user_id, attrs, t) {
var sql = "SELECT * FROM uc_user_role WHERE user_id = :user_id"; attrs = attrs || "*";
var sql = "SELECT " + attrs + " FROM uc_user_role WHERE user_id = :user_id";
return await this.customQuery(sql, { return await this.customQuery(sql, {
user_id: user_id user_id: user_id
}, t) || []; }, t) || [];
} }
async findUserRoles(user_id) {
var sql = [];
sql.push("SELECT");
sql.push("t2.`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` = :user_id");
return await this.customQuery(sql.join(" "), {
user_id: user_id
}) || [];
}
} }
module.exports = UserroleDao; module.exports = UserroleDao;
\ No newline at end of file
...@@ -3,7 +3,7 @@ const settings = require("../../../../config/settings"); ...@@ -3,7 +3,7 @@ const settings = require("../../../../config/settings");
const uiconfig = system.getUiConfig2(settings.appKey); const uiconfig = system.getUiConfig2(settings.appKey);
module.exports = (db, DataTypes) => { module.exports = (db, DataTypes) => {
return db.define("roleauth", { return db.define("roleauth", {
user_id: DataTypes.INTEGER, role_id: DataTypes.INTEGER,
auth_id: DataTypes.INTEGER, auth_id: DataTypes.INTEGER,
}, { }, {
paranoid: true, //假的删除 paranoid: true, //假的删除
......
...@@ -4,7 +4,7 @@ const uiconfig = system.getUiConfig2(settings.appKey); ...@@ -4,7 +4,7 @@ const uiconfig = system.getUiConfig2(settings.appKey);
module.exports = (db, DataTypes) => { module.exports = (db, DataTypes) => {
return db.define("userrole", { return db.define("userrole", {
user_id: DataTypes.INTEGER, user_id: DataTypes.INTEGER,
org_id: DataTypes.INTEGER, role_id: DataTypes.INTEGER,
}, { }, {
paranoid: true, //假的删除 paranoid: true, //假的删除
underscored: true, underscored: true,
......
...@@ -12,7 +12,7 @@ class AuthService extends ServiceBase { ...@@ -12,7 +12,7 @@ class AuthService extends ServiceBase {
var pid = Number(obj.pid || 0); var pid = Number(obj.pid || 0);
if (!saas_id) { if (!saas_id) {
return system.getResult(null, "请指定saas_id"); return system.getResult(null, "saas_id不存在");
} }
if (pid === 0) { if (pid === 0) {
...@@ -47,7 +47,7 @@ class AuthService extends ServiceBase { ...@@ -47,7 +47,7 @@ class AuthService extends ServiceBase {
} }
var auth = await this.findById(id); var auth = await this.findById(id);
if (!saas_id) { if (!saas_id) {
return system.getResult(null, "请指定saas_id"); return system.getResult(null, "saas_id不存在");
} }
if (saas_id != auth.saas_id) { if (saas_id != auth.saas_id) {
...@@ -68,11 +68,11 @@ class AuthService extends ServiceBase { ...@@ -68,11 +68,11 @@ class AuthService extends ServiceBase {
var list = await this.dao.findAll({ var list = await this.dao.findAll({
pid: obj.pid || 0, pid: obj.pid || 0,
}); });
return list; return system.getResultSuccess(list);
} }
async tree() { async tree(params) {
var all = await this.dao.all(); var all = await this.dao.all(params.saas_id, "id, orgname, path, pid");
var pmap = {}; var pmap = {};
for (var item of all) { for (var item of all) {
...@@ -88,8 +88,35 @@ class AuthService extends ServiceBase { ...@@ -88,8 +88,35 @@ class AuthService extends ServiceBase {
for(var item of all) { for(var item of all) {
item.children = pmap[item.id] || []; item.children = pmap[item.id] || [];
} }
return system.getResultSuccess(pmap[0][0]);
}
async byRoleIds(params) {
return await this.dao.byRoleIds(params);
}
async menuByRoleIds(params) {
params.menuType = 1;
var all = await this.byRoleIds(params);
var pmap = {};
for (var item of all) {
var list = pmap[item.pid];
if (!list) {
list = [];
}
list.push(item);
pmap[item.pid] = list;
}
for(var item of all) {
item.childs = pmap[item.id] || [];
}
return system.getResultSuccess(pmap[0][0]);
}
return system.getResultSuccess(pmap[0]); async authByRoleIds(params) {
params.menuType = 2;
return await this.byRoleIds(params);
} }
async info(obj) { async info(obj) {
......
...@@ -55,7 +55,7 @@ class OrgService extends ServiceBase { ...@@ -55,7 +55,7 @@ class OrgService extends ServiceBase {
/** /**
* 根据ID查询该组织机构下的所有用户 * 根据ID查询该组织机构下的所有用户
*/ */
async apiFindUserByOrgId(params){ async apiFindUserByOrgId(params) {
try { try {
return this.findUserByOrgId(params.id); return this.findUserByOrgId(params.id);
} catch (error) { } catch (error) {
...@@ -67,11 +67,11 @@ class OrgService extends ServiceBase { ...@@ -67,11 +67,11 @@ class OrgService extends ServiceBase {
* 根据ID查明细 * 根据ID查明细
* @param {*} params * @param {*} params
*/ */
async apiQueryById(params){ async apiQueryById(params) {
try { try {
return await this.queryById(params) return await this.queryById(params)
} catch (error) { } catch (error) {
return system.getResult(-1,`系统错误 错误信息 ${error}`); return system.getResult(-1, `系统错误 错误信息 ${error}`);
} }
} }
...@@ -79,11 +79,11 @@ class OrgService extends ServiceBase { ...@@ -79,11 +79,11 @@ class OrgService extends ServiceBase {
* 根据Ip查明细 * 根据Ip查明细
* @param {*} params * @param {*} params
*/ */
async apiByPid(params){ async apiByPid(params) {
try { try {
return await this.byPid(params) return await this.byPid(params)
} catch (error) { } catch (error) {
return system.getResult(-1,`系统错误 错误信息 ${error}`); return system.getResult(-1, `系统错误 错误信息 ${error}`);
} }
} }
...@@ -91,11 +91,11 @@ class OrgService extends ServiceBase { ...@@ -91,11 +91,11 @@ class OrgService extends ServiceBase {
* 根据ID查明细 * 根据ID查明细
* @param {*} params * @param {*} params
*/ */
async apiTree(params){ async apiTree(params) {
try { try {
return await this.tree(params) return await this.tree(params)
} catch (error) { } catch (error) {
return system.getResult(-1,`系统错误 错误信息 ${error}`); return system.getResult(-1, `系统错误 错误信息 ${error}`);
} }
} }
...@@ -110,26 +110,50 @@ class OrgService extends ServiceBase { ...@@ -110,26 +110,50 @@ class OrgService extends ServiceBase {
try { try {
var pid = Number(params.pid || 0); var pid = Number(params.pid || 0);
params.orgname = this.trim(params.orgname); params.orgname = this.trim(params.orgname);
if(!params.orgname){return system.getResult(-1,`参数错误 组织名称不能为空`)} params.saas_id = Number(params.saas_id || 0);
let _orgByName = await this.findOne({orgname:params.orgname});
if(_orgByName){ if (!params.saas_id) {
return system.getResult(-1,`参数错误 组织名称已经存在`); return system.getResult(-1, `saas_id不存在`)
}
if (!params.orgname) {
return system.getResult(-1, `参数错误 组织名称不能为空`)
}
let _orgByName = await this.findOne({
orgname: params.orgname,
saas_id: params.saas_id
});
if (_orgByName) {
return system.getResult(-1, `参数错误 组织名称已经存在`);
} }
let path = ""; let path = "";
if(pid===0){ if (pid === 0) {
// 验证是否存在其他权限
var exist = await this.findCount({
where: {
saas_id: saas_id
}
});
if (exist) {
return system.getResult(null, "菜单根目录已经存在");
}
path = `/${params.orgname}`; path = `/${params.orgname}`;
}else{ } else {
let _org = await this.findOne({id:pid}); let _org = await this.findOne({
if(!_org){ id: pid
return system.getResult(-1,`参数错误 父节点不存在`); });
if (!_org) {
return system.getResult(-1, `参数错误 父节点不存在`);
} }
path =`${_org.path}/${params.orgname}`; path = `${_org.path}/${params.orgname}`;
} }
params.path=path; params.path = path;
let res = await this.dao.create(params); let res = await this.dao.create(params);
return system.getResult(res); return system.getResult(res);
} catch (error) { } catch (error) {
return system.getResult(-1,`系统错误 错误信息 ${error}`); return system.getResult(-1, `系统错误 错误信息 ${error}`);
} }
} }
...@@ -167,20 +191,20 @@ class OrgService extends ServiceBase { ...@@ -167,20 +191,20 @@ class OrgService extends ServiceBase {
} }
//检查组织机构下是否有用户 //检查组织机构下是否有用户
let _usersRes = await this.findUserByOrgId(params.id); let _usersRes = await this.findUserByOrgId(params.id);
if(_usersRes.status===-1){ if (_usersRes.status === -1) {
return _usersRes; return _usersRes;
} }
if(_usersRes.data.length!=0){ if (_usersRes.data.length != 0) {
return system.getResult(-1,`该组织机构不能删除`); return system.getResult(-1, `该组织机构不能删除`);
} }
//检查父节点 下是否有子节点 //检查父节点 下是否有子节点
let _orgChildren = await this.dao.model.findAll({ let _orgChildren = await this.dao.model.findAll({
where:{ where: {
pid:this.trim(params.id) pid: this.trim(params.id)
} }
}); });
if(_orgChildren.length!=0){ if (_orgChildren.length != 0) {
return system.getResult(-1,`该组织机构不能删除`); return system.getResult(-1, `该组织机构不能删除`);
} }
//删除 //删除
let res = await _org.destroy(); let res = await _org.destroy();
...@@ -225,8 +249,8 @@ class OrgService extends ServiceBase { ...@@ -225,8 +249,8 @@ class OrgService extends ServiceBase {
} }
} }
async queryById(params){ async queryById(params) {
if(!params.id){ if (!params.id) {
return system.getResult(-1, `参数错误 ID不能为空`); return system.getResult(-1, `参数错误 ID不能为空`);
} }
try { try {
...@@ -235,22 +259,22 @@ class OrgService extends ServiceBase { ...@@ -235,22 +259,22 @@ class OrgService extends ServiceBase {
this.handleDate(_org,['updated_at'],null,-8); this.handleDate(_org,['updated_at'],null,-8);
return system.getResult(_org); return system.getResult(_org);
} catch (error) { } catch (error) {
return system.getResult(-1,`系统错误 错误信息 ${error}`); return system.getResult(-1, `系统错误 错误信息 ${error}`);
} }
} }
/** /**
* 根据ID查询该组织机构下的所有用户 * 根据ID查询该组织机构下的所有用户
*/ */
async findUserByOrgId(id){ async findUserByOrgId(id) {
if(!Number(id)){ if (!Number(id)) {
return system.getResult(-1,`参数错误 组织机构不能为空`); return system.getResult(-1, `参数错误 组织机构不能为空`);
} }
try { try {
let res = await this.dao.findUserByOrgId(id); let res = await this.dao.findUserByOrgId(id);
return system.getResult(res); return system.getResult(res);
} catch (error) { } catch (error) {
return system.getResult(-1,`系统错误 错误信息 ${error}`); return system.getResult(-1, `系统错误 错误信息 ${error}`);
} }
} }
...@@ -261,16 +285,16 @@ class OrgService extends ServiceBase { ...@@ -261,16 +285,16 @@ class OrgService extends ServiceBase {
}); });
return system.getResult(list); return system.getResult(list);
} catch (error) { } catch (error) {
return system.getResult(-1,`系统错误 错误信息 ${error}`); return system.getResult(-1, `系统错误 错误信息 ${error}`);
} }
} }
/** /**
* 结构树 * 结构树
*/ */
async tree() { async tree(params) {
try { try {
var all = await this.dao.all(); var all = await this.dao.all(params.saas_id);
var pmap = {}; var pmap = {};
for (var item of all) { for (var item of all) {
item.label = item.orgname; item.label = item.orgname;
...@@ -286,7 +310,7 @@ class OrgService extends ServiceBase { ...@@ -286,7 +310,7 @@ class OrgService extends ServiceBase {
} }
return system.getResult(pmap[0]); return system.getResult(pmap[0]);
} catch (error) { } catch (error) {
return system.getResult(-1,`系统错误 错误信息 ${error}`); return system.getResult(-1, `系统错误 错误信息 ${error}`);
} }
} }
......
...@@ -3,6 +3,8 @@ const ServiceBase = require("../../sve.base") ...@@ -3,6 +3,8 @@ const ServiceBase = require("../../sve.base")
class RoleService extends ServiceBase { class RoleService extends ServiceBase {
constructor() { constructor() {
super("role", ServiceBase.getDaoName(RoleService)); super("role", ServiceBase.getDaoName(RoleService));
this.roleauthDao = system.getObject("db.role.roleauthDao");
} }
/** /**
...@@ -49,7 +51,6 @@ class RoleService extends ServiceBase { ...@@ -49,7 +51,6 @@ class RoleService extends ServiceBase {
} }
} }
/** /**
* role 查询列表 * role 查询列表
* @param {*} params * @param {*} params
...@@ -62,6 +63,32 @@ class RoleService extends ServiceBase { ...@@ -62,6 +63,32 @@ class RoleService extends ServiceBase {
} }
} }
async setAuth(params) {
var saas_id = Number(params.saas_id || 0);
var role = await this.findById(params.id);
var authIds = params.authIds;
if(!role) {
return system.getResult(null, "角色不存在");
}
if(role.saas_id !== saas_id) {
return system.getResult(null, "权限不足");
}
var self = this;
// 先删
await this.roleauthDao.delByRoleId(role.id);
var list = [];
for(var auth_id of authIds) {
list.push({auth_id: auth_id, role_id: role.id});
}
// 后存
if(list.length > 0) {
await this.roleauthDao.bulkCreate(list)
}
return system.getResultSuccess();
}
......
...@@ -6,6 +6,9 @@ class UserService extends ServiceBase { ...@@ -6,6 +6,9 @@ class UserService extends ServiceBase {
super("user", ServiceBase.getDaoName(UserService)); super("user", ServiceBase.getDaoName(UserService));
this.userinfoDao = system.getObject("db.user.userinfoDao"); this.userinfoDao = system.getObject("db.user.userinfoDao");
this.userroleDao = system.getObject("db.user.userroleDao"); this.userroleDao = system.getObject("db.user.userroleDao");
this.authSve = system.getObject("service.auth.authSve");
} }
...@@ -21,6 +24,18 @@ class UserService extends ServiceBase { ...@@ -21,6 +24,18 @@ class UserService extends ServiceBase {
} }
} }
/**
* 根据path查询所有的用户
* @param {*} params
*/
async apiQueryUserByPath(params){
try {
return await this.queryUserByPath(params);
} catch (error) {
return system.getResult(-1, `系统错误 错误信息 ${error}`);
}
}
/**************************************************************** */ /**************************************************************** */
...@@ -37,9 +52,62 @@ class UserService extends ServiceBase { ...@@ -37,9 +52,62 @@ class UserService extends ServiceBase {
} }
} }
async login(obj) {
var user = await this.dao.getByUcname(obj.ucname);
// 验证登录合法
if (!user) {
return system.getResult(null, "用户名或密码错误");
}
if (!user.isEnabled) {
return system.getResult(null, "用户已禁用");
}
var loginPwd = await this.getEncryptStr(obj.password);
if (loginPwd != user.password) {
return system.getResult(null, "用户名或密码错误");
}
await this.setLoginUser(user);
return system.getResultSuccess(user);
}
async loginByUcid(obj) {
var user = await this.dao.getByUcid(obj.ucid);
// 验证登录合法
if (!user) {
return system.getResult(null, "用户名或密码错误");
}
await this.setLoginUser(user);
return system.getResultSuccess(user);
}
async setLoginUser(user) {
// 登录成功,补充登录所需内容
// 详情
user.info = await this.userinfoDao.findById(user.id);
// 角色
user.roles = await this.userroleDao.findUserRoles(user.id);
// 构建请求权限接口参数
var roleIds = [];
for (var role of user.roles) {
roleIds.push(role.id);
}
var authParams = {
roleIds: roleIds,
saas_id: user.saas_id,
}
// 菜单权限
user.menus = await this.authSve.menuByRoleIds(authParams);
// 接口权限
user.auths = await this.authSve.authByRoleIds(authParams);
}
async add(obj) { async add(obj) {
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 ucid = this.trim(obj.ucid);
var ucname = this.trim(obj.ucname); var ucname = this.trim(obj.ucname);
...@@ -50,7 +118,7 @@ class UserService extends ServiceBase { ...@@ -50,7 +118,7 @@ class UserService extends ServiceBase {
var isMain = obj.isMain || 0; var isMain = obj.isMain || 0;
if (!saas_id) { if (!saas_id) {
return system.getResult(null, "请指定saas_id"); return system.getResult(null, "saas_id不存在");
} }
var exist = await this.findOne({ var exist = await this.findOne({
...@@ -66,13 +134,13 @@ class UserService extends ServiceBase { ...@@ -66,13 +134,13 @@ class UserService extends ServiceBase {
ucname: ucname, ucname: ucname,
password: await this.getEncryptStr(password), password: await this.getEncryptStr(password),
uctype: uctype, uctype: uctype,
org_id: org.id, org_id: org.id || 0,
isMain: isMain, isMain: isMain,
orgpath: "", orgpath: "",
isEnabled: 1, isEnabled: 1,
} }
var orgpath = org.path; var orgpath = org.path || "";
var info = { var info = {
mobile: mobile, mobile: mobile,
...@@ -94,14 +162,14 @@ class UserService extends ServiceBase { ...@@ -94,14 +162,14 @@ class UserService extends ServiceBase {
await self.userroleDao.bulkCreate(roles, t); await self.userroleDao.bulkCreate(roles, t);
} }
if (!isMain) { if (user.uctype === 1) {
orgpath = orgpath + "/" + user.id; orgpath = isMain ? orgpath : orgpath + "/" + user.id;
}
user.orgpath = orgpath;
await self.dao.update({ await self.dao.update({
id: user.id, id: user.id,
orgpath: orgpath orgpath: orgpath
}, t); }, t);
}
user.orgpath = orgpath;
return user; return user;
}); });
...@@ -111,7 +179,7 @@ class UserService extends ServiceBase { ...@@ -111,7 +179,7 @@ class UserService extends ServiceBase {
async upd(obj) { async upd(obj) {
var id = obj.id; var id = obj.id;
var roles = obj.roles || []; var roles = obj.roles || [];
var org = obj.org; var org = obj.org || {};
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);
...@@ -119,11 +187,13 @@ class UserService extends ServiceBase { ...@@ -119,11 +187,13 @@ class UserService extends ServiceBase {
var user = { var user = {
id: id, id: id,
org_id: org.id, org_id: org.id || "",
orgpath: isMain ? org.path : org.path + "/" + id, orgpath: "",
isMain: obj.isMain || 0, isMain: obj.isMain || 0,
} }
if (user.uctype === 1) {
user.orgpath = isMain ? org.path : org.path + "/" + id;
}
var info = { var info = {
id: id, id: id,
mobile: mobile, mobile: mobile,
...@@ -159,11 +229,21 @@ class UserService extends ServiceBase { ...@@ -159,11 +229,21 @@ class UserService extends ServiceBase {
user.mobile = info.mobile; user.mobile = info.mobile;
user.realName = info.realName; user.realName = info.realName;
user.roles = await this.userroleDao.listByUserId(id); user.roles = await this.userroleDao.listByUserId(id, "user_id, role_id");
this.handleDate(user, ["created_at"], null, -8); this.handleDate(user, ["created_at"], null, -8);
return system.getResultSuccess(user); return system.getResultSuccess(user);
} }
async enabled(params) {
var user = await this.dao.findById(params.id);
if (!user) {
return system.getResult(null, "用户不存在");
}
user.isEnabled = Number(params.enabled || 0) == 0 ? false : true;
await user.save();
return system.getResultSuccess();
}
async pageByCondition(params) { async pageByCondition(params) {
var result = { var result = {
count: 0, count: 0,
...@@ -171,6 +251,9 @@ class UserService extends ServiceBase { ...@@ -171,6 +251,9 @@ class UserService extends ServiceBase {
}; };
var currentPage = Number(params.currentPage || 1); var currentPage = Number(params.currentPage || 1);
var pageSize = Number(params.pageSize || 10); var pageSize = Number(params.pageSize || 10);
if(params.orgpath) {
params.orgpath = params.orgpath + "%";
}
var total = await this.dao.countByCondition(params); var total = await this.dao.countByCondition(params);
if (total == 0) { if (total == 0) {
...@@ -180,17 +263,37 @@ class UserService extends ServiceBase { ...@@ -180,17 +263,37 @@ class UserService extends ServiceBase {
result.count = total; result.count = total;
params.startRow = (currentPage - 1) * pageSize; params.startRow = (currentPage - 1) * pageSize;
result.rows = await this.dao.listByCondition(params) || []; result.rows = await this.dao.listByCondition(params) || [];
if (result.rows) {
for (var item of result.rows) {
this.handleDate(item, ["created_at"], null, -8);
}
}
return system.getResultSuccess(result); return system.getResultSuccess(result);
} }
async delUser(params) { async delUser(params) {
var user = await this.findById(params.id);
if (!user) {
return system.getResultSuccess();
}
if (user.saas_id != params.saas_id) {
return system.getResult(null, "权限不足");
}
await this.delete({ await this.delete({
id: params.id id: params.id
}); });
return system.getResultSuccess(); return system.getResultSuccess();
} }
async updPassword(params) {
var user = await this.findById(params.id);
if (!user) {
return system.getResult(null, "用户不存在");
}
user.password = await this.getEncryptStr(params.password);
await user.save();
return system.getResultSuccess();
}
} }
module.exports = UserService; module.exports = UserService;
\ No newline at end of file
...@@ -274,6 +274,7 @@ class ServiceBase { ...@@ -274,6 +274,7 @@ class ServiceBase {
} }
async getEncryptStr(str) { async getEncryptStr(str) {
str = this.trim(str);
if (!str) { if (!str) {
throw new Error("字符串不能为空"); throw new Error("字符串不能为空");
} }
......
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