明树Git Lab
Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
J
jt_backend
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
zengfanpei
jt_backend
Commits
384ce04a
Commit
384ce04a
authored
Dec 01, 2025
by
zhangqi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
增加查询条件
parent
dc52e81e
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
67 additions
and
5 deletions
+67
-5
userController.js
controller/userController.js
+30
-4
userModule.js
module/userModule.js
+37
-1
No files found.
controller/userController.js
View file @
384ce04a
...
...
@@ -108,12 +108,13 @@ async function createUser(req, res, next) {
if
(
!
(
ret
&&
ret
.
id
))
{
return
res
.
sendError
(
errorMessage
.
databaseQueryError
);
}
//处理部门 角色
//处理部门 角色
岗位
body
.
departs
=
body
.
departs
||
[];
body
.
roles
=
body
.
roles
||
[];
body
.
positions
=
body
.
positions
||
[];
let
userDeparts
=
body
.
departs
.
map
(
o
=>
{
return
{
userId
:
ret
.
id
,
departId
:
o
&&
o
.
id
||
o
}
});
let
userRoles
=
body
.
roles
.
map
(
o
=>
{
return
{
userId
:
ret
.
id
,
roleId
:
o
&&
o
.
id
||
o
}
});
let
userPositions
=
body
.
positions
s
.
map
(
o
=>
{
return
{
userId
:
ret
.
id
,
roleId
:
o
&&
o
.
id
||
o
}
});
let
userPositions
=
body
.
positions
.
map
(
o
=>
{
return
{
userId
:
ret
.
id
,
roleId
:
o
&&
o
.
id
||
o
}
});
await
DB
.
UserDepart
.
bulkCreate
(
userDeparts
);
await
DB
.
UserRole
.
bulkCreate
(
userRoles
);
await
DB
.
UserPosition
.
bulkCreate
(
userPositions
);
...
...
@@ -162,13 +163,16 @@ async function updateUser(req, res, next) {
{
model
:
DB
.
Role
,
as
:
'roles'
,
where
:
{
del
:
0
},
attributes
:
[
"id"
],
through
:
{
attributes
:
[]
},
},
{
model
:
DB
.
Depart
,
as
:
'departs'
,
where
:
{
del
:
0
},
attributes
:
[
"id"
],
through
:
{
attributes
:
[]
},
},
{
model
:
DB
.
Position
,
as
:
'positions'
,
attributes
:
[
"id"
],
through
:
{
attributes
:
[]
},
}
...
...
@@ -182,6 +186,7 @@ async function updateUser(req, res, next) {
//跟现有情况对比 确定增删关系
const
departIds
=
body
.
departs
.
map
(
o
=>
{
return
o
&&
o
.
id
||
o
});
await
userModule
.
setUserDepart
(
user
.
id
,
departIds
,
user
.
departs
);
console
.
log
(
'user.departs'
,
user
.
departs
);
delete
body
.
departs
;
}
if
(
body
.
roles
&&
body
.
roles
.
length
)
{
...
...
@@ -190,6 +195,12 @@ async function updateUser(req, res, next) {
await
userModule
.
setUserRole
(
user
.
id
,
roleIds
,
user
.
roles
);
delete
body
.
roles
;
}
if
(
body
.
positions
&&
body
.
positions
.
length
)
{
//跟现有情况对比 确定增删关系
const
positionIds
=
body
.
positions
.
map
(
o
=>
{
return
o
&&
o
.
id
||
o
});
await
userModule
.
setUserPosition
(
user
.
id
,
positionIds
,
user
.
positions
);
delete
body
.
roles
;
}
const
ret
=
await
DB
.
User
.
update
(
body
,
{
where
:
{
id
:
body
.
id
}
});
return
res
.
sendData
(
ret
);
}
catch
(
error
)
{
...
...
@@ -215,6 +226,21 @@ async function listUser(req, res, next) {
}
search
.
limit
=
limit
;
search
.
offset
=
offset
;
search
.
include
=
[
{
model
:
DB
.
Role
,
as
:
'roles'
,
through
:
{
attributes
:
[]
},
},
{
model
:
DB
.
Depart
,
as
:
'departs'
,
through
:
{
attributes
:
[]
},
},
{
model
:
DB
.
Position
,
as
:
'positions'
,
through
:
{
attributes
:
[]
},
}
];
search
.
attributes
=
req
.
body
.
attributes
||
{
exclude
:
[
'password'
,
'salt'
]
};
let
ret
=
await
DB
.
User
.
findAndCountAll
(
search
);
return
res
.
sendData
(
ret
);
...
...
module/userModule.js
View file @
384ce04a
...
...
@@ -46,7 +46,7 @@ async function setUserDepart(userId, departIds, departs) {
dbIds
.
push
(
element
.
roleId
);
if
(
!
departIds
.
includes
(
element
.
roleId
))
{
// 1. departIds里面没有 但是关系表里有的 需要删除
needDelIds
.
push
(
element
.
roleId
);
needDelIds
.
push
(
element
.
roleId
);
}
}
for
(
let
index
=
0
;
index
<
departIds
.
length
;
index
++
)
{
...
...
@@ -64,6 +64,40 @@ async function setUserDepart(userId, departIds, departs) {
}
}
/**
*
* @param {*} userId 被设置用户
* @param {*} positionIds 被设置岗位
* @param {*} positions 当前已拥有岗位
*/
async
function
setUserPosition
(
userId
,
positionIds
,
positions
)
{
//需新增得
let
dbIds
=
[],
needAddIds
=
[],
needDelIds
=
[];
for
(
let
index
=
0
;
index
<
positions
.
length
;
index
++
)
{
const
element
=
positions
[
index
];
dbIds
.
push
(
element
.
roleId
);
if
(
!
positionIds
.
includes
(
element
.
roleId
))
{
// 1. departIds里面没有 但是关系表里有的 需要删除
needDelIds
.
push
(
element
.
roleId
);
}
}
for
(
let
index
=
0
;
index
<
positionIds
.
length
;
index
++
)
{
const
element
=
positions
[
index
];
if
(
!
dbIds
.
includes
(
element
))
{
needAddIds
.
push
(
element
);
}
}
if
(
needAddIds
.
length
)
{
let
objs
=
needAddIds
.
map
(
o
=>
{
return
{
userId
:
userId
,
roleId
:
o
}
});
await
DB
.
UserRole
.
bulkCreate
(
objs
);
}
if
(
needDelIds
.
length
)
{
await
DB
.
UserRole
.
destroy
({
where
:
{
userId
:
userId
,
roleId
:
{
[
Op
.
in
]:
needDelIds
}
}
});
}
}
/**
*
* @param {*} roleId 被设置角色
...
...
@@ -95,6 +129,7 @@ async function setRoleMenu(roleId, menuIds, rolemenus) {
}
}
async
function
getProjectApprover
(
userId
,
roleCode
)
{
// 获取当前用户公司的 具体角色 的用户列表
let
user
=
await
DB
.
User
.
findOne
({
where
:
{
id
:
userId
}
});
...
...
@@ -105,6 +140,7 @@ async function getProjectApprover(userId, roleCode) {
module
.
exports
=
{
setUserRole
,
setUserDepart
,
setUserPosition
,
setRoleMenu
,
getProjectApprover
,
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment