明树Git Lab

Commit 50497a8a authored by zfp1's avatar zfp1

update

parent d277e9b0
Pipeline #108298 passed with stage
in 3 seconds
...@@ -3,7 +3,7 @@ const _ = require('lodash'); ...@@ -3,7 +3,7 @@ const _ = require('lodash');
const CryptoJS = require('crypto-js'); const CryptoJS = require('crypto-js');
const { Op } = require('sequelize'); const { Op } = require('sequelize');
const userModule = require('../module/userModule'); const userModule = require('../module/userModule');
const errorMessage = require('../utils/errorMessage'); const errorMessage = require('../utils/errorMessage');
async function regist(req, res, next) { async function regist(req, res, next) {
...@@ -45,7 +45,7 @@ async function login(req, res, next) { ...@@ -45,7 +45,7 @@ async function login(req, res, next) {
// attributes: ['id', 'name', 'parentId'], // attributes: ['id', 'name', 'parentId'],
through: { attributes: [] }, through: { attributes: [] },
}] }]
}, },
{ {
model: DB.Depart, model: DB.Depart,
as: 'departs', as: 'departs',
...@@ -85,7 +85,7 @@ async function login(req, res, next) { ...@@ -85,7 +85,7 @@ async function login(req, res, next) {
delete element.menus; delete element.menus;
newRoles.push(element); newRoles.push(element);
} }
menus = _.filter(_.uniqBy(menus, 'id'), (o) => {return o.del == 0}); menus = _.filter(_.uniqBy(menus, 'id'), (o) => { return o.del == 0 });
user.roles = newRoles || []; user.roles = newRoles || [];
// let allMenus = await DB.Menu.findAll({where: {del: 0}, raw: true}); // let allMenus = await DB.Menu.findAll({where: {del: 0}, raw: true});
user.menus = utils.buildTree(menus); user.menus = utils.buildTree(menus);
...@@ -112,17 +112,17 @@ async function getUserRoleMenuList(req, res, next) { ...@@ -112,17 +112,17 @@ async function getUserRoleMenuList(req, res, next) {
// attributes: ['id', 'name', 'parentId'], // attributes: ['id', 'name', 'parentId'],
through: { attributes: [] }, through: { attributes: [] },
}] }]
}, },
// { // {
// model: DB.Depart, // model: DB.Depart,
// as: 'departs', // as: 'departs',
// // where: { del: 0 }, 当刚注册的用户 没有分配角色、部门时加上条件会查不到 // // where: { del: 0 }, 当刚注册的用户 没有分配角色、部门时加上条件会查不到
// attributes: ["id", "name"], // attributes: ["id", "name"],
// through: { attributes: [] }, // through: { attributes: [] },
// } // }
] ]
}); });
if (!user) { if (!user) {
return res.sendError(errorMessage.resourceNotFound); return res.sendError(errorMessage.resourceNotFound);
} }
...@@ -137,7 +137,7 @@ async function getUserRoleMenuList(req, res, next) { ...@@ -137,7 +137,7 @@ async function getUserRoleMenuList(req, res, next) {
delete element.menus; delete element.menus;
newRoles.push(element); newRoles.push(element);
} }
let allMenus = await DB.Menu.findAll({where: {del: 0}, raw: true}); let allMenus = await DB.Menu.findAll({ where: { del: 0 }, raw: true });
let newMenus = utils.buildTree(allMenus); let newMenus = utils.buildTree(allMenus);
return res.sendData({ return res.sendData({
menus: newMenus, menus: newMenus,
...@@ -160,13 +160,13 @@ async function createUser(req, res, next) { ...@@ -160,13 +160,13 @@ async function createUser(req, res, next) {
body.salt = salt; body.salt = salt;
body.password = passwordHash; body.password = passwordHash;
const checkMobile = await DB.User.findOne({ const checkMobile = await DB.User.findOne({
where: { where: {
mobile: body.mobile, mobile: body.mobile,
del: 0 del: 0
} }
}); });
if (checkMobile) { if (checkMobile) {
return res.sendError(errorMessage.mobileDuplicate) return res.sendError(errorMessage.mobileDuplicate)
} }
const ret = await DB.User.create(body); const ret = await DB.User.create(body);
if (!(ret && ret.id)) { if (!(ret && ret.id)) {
...@@ -204,22 +204,22 @@ async function getUserInfo(req, res, next) { ...@@ -204,22 +204,22 @@ async function getUserInfo(req, res, next) {
// raw: true, // raw: true,
attributes: { exclude: ['password', 'salt'] }, attributes: { exclude: ['password', 'salt'] },
include: [ include: [
{ {
model: DB.Role, model: DB.Role,
as: 'roles', as: 'roles',
// where: { del: 0 }, // where: { del: 0 },
// attributes: ['id'] // attributes: ['id']
},{ }, {
model: DB.Depart, model: DB.Depart,
as: 'departs', as: 'departs',
// attributes: ["id"], // attributes: ["id"],
// through: { attributes: [] }, // through: { attributes: [] },
}, { }, {
model: DB.Position, model: DB.Position,
as: 'positions', as: 'positions',
// attributes: ["id"], // attributes: ["id"],
// through: { attributes: [] }, // through: { attributes: [] },
} }
] ]
}); });
return res.sendData(ret); return res.sendData(ret);
...@@ -265,7 +265,7 @@ async function updateUser(req, res, next) { ...@@ -265,7 +265,7 @@ async function updateUser(req, res, next) {
const departIds = body.departs.map(o => { return o && o.id || o }); const departIds = body.departs.map(o => { return o && o.id || o });
console.log(departIds, "==================") console.log(departIds, "==================")
await userModule.setUserDepart(user.id, departIds, user.departs || []); await userModule.setUserDepart(user.id, departIds, user.departs || []);
console.log('user.departs',user.departs); console.log('user.departs', user.departs);
delete body.departs; delete body.departs;
} }
if (body.roles && body.roles.length) { if (body.roles && body.roles.length) {
...@@ -277,14 +277,16 @@ async function updateUser(req, res, next) { ...@@ -277,14 +277,16 @@ async function updateUser(req, res, next) {
if (body.positions && body.positions.length) { if (body.positions && body.positions.length) {
//跟现有情况对比 确定增删关系 //跟现有情况对比 确定增删关系
const positionIds = body.positions.map(o => { return o && o.id || o }); const positionIds = body.positions.map(o => { return o && o.id || o });
await userModule.setUserPosition(user.id, positionIds, user.positions || []); await userModule.setUserPosition(user.id, positionIds, user.positions || []);
delete body.roles; delete body.roles;
} }
// //
if(user.mobile != req.body.mobile) { if (req.body.mobile) {
let mt = await DB.User.findOne({where: {mobile: req.body.mobile, del: 0}}); if (user.mobile != req.body.mobile) {
if(mt) { let mt = await DB.User.findOne({ where: { mobile: req.body.mobile, del: 0 } });
return res.sendError(errorMessage.mobileDuplicate) if (mt) {
return res.sendError(errorMessage.mobileDuplicate)
}
} }
} }
const ret = await DB.User.update(body, { where: { id: body.id } }); const ret = await DB.User.update(body, { where: { id: body.id } });
...@@ -305,8 +307,8 @@ async function listUser(req, res, next) { ...@@ -305,8 +307,8 @@ async function listUser(req, res, next) {
if (req.body.name) { if (req.body.name) {
search.where = { search.where = {
[Op.or]: [ [Op.or]: [
{name: { [Op.like]: `%${req.body.name}%` }}, { name: { [Op.like]: `%${req.body.name}%` } },
{mobile: { [Op.like]: `%${req.body.mobile}%` }} { mobile: { [Op.like]: `%${req.body.mobile}%` } }
], ],
del: 0 del: 0
} }
...@@ -314,22 +316,22 @@ async function listUser(req, res, next) { ...@@ -314,22 +316,22 @@ async function listUser(req, res, next) {
search.limit = limit; search.limit = limit;
search.offset = offset; search.offset = offset;
search.include = [ search.include = [
{ {
model: DB.Role, model: DB.Role,
as: 'roles', as: 'roles',
through: { attributes: [] }, through: { attributes: [] },
}, { }, {
model: DB.Depart, model: DB.Depart,
as: 'departs', as: 'departs',
through: { attributes: [] }, through: { attributes: [] },
}, { }, {
model: DB.Position, model: DB.Position,
as: 'positions', as: 'positions',
through: { attributes: [] }, through: { attributes: [] },
} }
]; ];
search.distinct = true, // ✅ 关键:去重 search.distinct = true, // ✅ 关键:去重
search.col = 'id' search.col = 'id'
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);
...@@ -352,11 +354,11 @@ async function deleteUser(req, res, next) { ...@@ -352,11 +354,11 @@ async function deleteUser(req, res, next) {
} }
const ret = await DB.User.update({ del: 1 }, { where: search });//删除标记 const ret = await DB.User.update({ del: 1 }, { where: search });//删除标记
//删除用户与角色绑定 //删除用户与角色绑定
await DB.UserRole.destroy({where: {userId: req.body.id}}); await DB.UserRole.destroy({ where: { userId: req.body.id } });
//删除用户与部门绑定 //删除用户与部门绑定
await DB.UserDepart.destroy({where: {userId: req.body.id}}); await DB.UserDepart.destroy({ where: { userId: req.body.id } });
//删除用户与岗位绑定 //删除用户与岗位绑定
await DB.UserPosition.destroy({where: {userId: req.body.id}}); await DB.UserPosition.destroy({ where: { userId: req.body.id } });
return res.sendData(ret); return res.sendData(ret);
} catch (error) { } catch (error) {
next(error); next(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