明树Git Lab

Commit ead0be59 authored by zfp1's avatar zfp1

update

parent 3c3f5ea8
...@@ -29,7 +29,16 @@ async function login(req, res, next) { ...@@ -29,7 +29,16 @@ async function login(req, res, next) {
req.body.password = encArr[1]; req.body.password = encArr[1];
} }
const { mobile, password } = req.body; const { mobile, password } = req.body;
const user = await DB.User.findOne({ where: { mobile, del: 0, enable: 0 }, raw: true }); let user = await DB.User.findOne({
where: { mobile, del: 0, enable: 0 },
include: {
model: DB.Role,
as: 'roles',
where: { del: 0 },
attributes: {exclude: ['system_user_role']}
}
});
user = user.toJSON();
if (!user) { if (!user) {
return res.sendError(errorMessage.loginError); return res.sendError(errorMessage.loginError);
} }
...@@ -85,7 +94,17 @@ async function getUserInfo(req, res, next) { ...@@ -85,7 +94,17 @@ async function getUserInfo(req, res, next) {
if (mobile) { if (mobile) {
query.mobile = mobile; query.mobile = mobile;
} }
const ret = await DB.User.findOne({ where: query, raw: true }); const ret = await DB.User.findOne({
where: query,
// raw: true,
attributes: { exclude: ['password', 'salt'] },
include: {
model: DB.Role,
as: 'roles',
where: { del: 0 },
// attributes: ['id']
}
});
return res.sendData(ret); return res.sendData(ret);
} catch (error) { } catch (error) {
next(error); next(error);
...@@ -123,7 +142,7 @@ async function listUser(req, res, next) { ...@@ -123,7 +142,7 @@ async function listUser(req, res, next) {
} }
search.limit = limit; search.limit = limit;
search.offset = offset; search.offset = offset;
search.attributes = req.body.attributes || {exclude: ['password', 'salt']}; search.attributes = req.body.attributes || { exclude: ['password', 'salt'] };
let ret = await DB.User.findAndCountAll(search); let ret = await DB.User.findAndCountAll(search);
return res.sendData(ret); return res.sendData(ret);
} catch (error) { } catch (error) {
...@@ -144,7 +163,7 @@ async function deleteUser(req, res, next) { ...@@ -144,7 +163,7 @@ async function deleteUser(req, res, next) {
if (_.isEmpty(search)) { if (_.isEmpty(search)) {
return res.sendError(errorMessage.resourceNotFound); return res.sendError(errorMessage.resourceNotFound);
} }
const ret = await DB.User.update({ del: 1 }, {where: search} );//删除标记 const ret = await DB.User.update({ del: 1 }, { where: search });//删除标记
return res.sendData(ret); return res.sendData(ret);
} catch (error) { } catch (error) {
next(error); next(error);
...@@ -154,31 +173,31 @@ async function deleteUser(req, res, next) { ...@@ -154,31 +173,31 @@ async function deleteUser(req, res, next) {
async function setUserRole(req, res, next) { async function setUserRole(req, res, next) {
try { try {
let {userId, roleIds} = req.body; let { userId, roleIds } = req.body;
//1. 先查被设置用户 有什么角色 //1. 先查被设置用户 有什么角色
const roles = await DB.UserRole.findAll({where: {user_id: req.body.userId}, raw: true, attributes: ['role_id','user_id']}); const roles = await DB.UserRole.findAll({ where: { user_id: req.body.userId }, raw: true, attributes: ['role_id', 'user_id'] });
//需新增得 //需新增得
let dbRoleIds = [], needAddRoleIds = [], needDelRoleIds = []; let dbRoleIds = [], needAddRoleIds = [], needDelRoleIds = [];
for (let index = 0; index < roles.length; index++) { for (let index = 0; index < roles.length; index++) {
const element = roles[index]; const element = roles[index];
dbRoleIds.push(element.role_id); dbRoleIds.push(element.role_id);
if(!roleIds.includes(element.role_id)) { if (!roleIds.includes(element.role_id)) {
// 1. roleIds里面没有 但是关系表里有的 需要删除 // 1. roleIds里面没有 但是关系表里有的 需要删除
needDelRoleIds.push(element.role_id); needDelRoleIds.push(element.role_id);
} }
} }
for (let index = 0; index < roleIds.length; index++) { for (let index = 0; index < roleIds.length; index++) {
const element = roleIds[index]; const element = roleIds[index];
if(!dbRoleIds.includes(element)) { if (!dbRoleIds.includes(element)) {
needAddRoleIds.push(element); needAddRoleIds.push(element);
} }
} }
if(needAddRoleIds.length) { if (needAddRoleIds.length) {
let objs = needAddRoleIds.map(o => {return {user_id: userId, role_id: o}}); let objs = needAddRoleIds.map(o => { return { user_id: userId, role_id: o } });
await DB.UserRole.bulkCreate(objs); await DB.UserRole.bulkCreate(objs);
} }
if(needDelRoleIds.length) { if (needDelRoleIds.length) {
await DB.UserRole.destroy({where: {user_id: userId, role_id: {[Op.in]: needDelRoleIds}}}); await DB.UserRole.destroy({ where: { user_id: userId, role_id: { [Op.in]: needDelRoleIds } } });
} }
return res.sendData(); return res.sendData();
} catch (error) { } catch (error) {
......
...@@ -30,13 +30,15 @@ RequestLog.belongsTo(User, { foreignKey: 'user_id' }); ...@@ -30,13 +30,15 @@ RequestLog.belongsTo(User, { foreignKey: 'user_id' });
User.belongsToMany(Role, { User.belongsToMany(Role, {
through: 'system_user_role', // 中间表名 through: 'system_user_role', // 中间表名
foreignKey: 'user_id', // 用户ID外键 foreignKey: 'user_id', // 用户ID外键
otherKey: 'role_id' // 角色ID外键 otherKey: 'role_id', // 角色ID外键
as: 'roles',
}); });
Role.belongsToMany(User, { Role.belongsToMany(User, {
through: 'system_user_role', through: 'system_user_role',
foreignKey: 'role_id', foreignKey: 'role_id',
otherKey: 'user_id' otherKey: 'user_id',
as: 'users'
}); });
Role.belongsToMany(Menu, { Role.belongsToMany(Menu, {
......
...@@ -46,7 +46,7 @@ const User = sequelize.define('User', { ...@@ -46,7 +46,7 @@ const User = sequelize.define('User', {
User.sync({ User.sync({
// force: false, // force: false,
// force: true ,//会删除已存在表并重新创建 // force: true ,//会删除已存在表并重新创建
alter: true // alter: true
}) })
.then(() => { .then(() => {
console.log('User 表同步成功'); console.log('User 表同步成功');
......
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