明树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
8d249dc2
Commit
8d249dc2
authored
Nov 29, 2025
by
zfp1
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update
parent
813abdf1
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
190 additions
and
246 deletions
+190
-246
resourceController.js
controller/resourceController.js
+190
-246
No files found.
controller/resourceController.js
View file @
8d249dc2
...
...
@@ -7,302 +7,246 @@ const { Op } = require('sequelize');
* 资源库controller
*/
async
function
createResource
(
req
,
res
,
next
)
{
try
{
const
body
=
req
.
body
;
// await DB.Resources.bulkCreate(
// [
// { "key": "k1a92f", "name": "公交车" },
// { "key": "u3b81x", "name": "地铁" },
// { "key": "p9c44m", "name": "高铁" },
// { "key": "n7d22t", "name": "出租车" },
// { "key": "q4e93z", "name": "轻轨" },
// { "key": "t6f81k", "name": "磁悬浮列车" },
// { "key": "r8g55w", "name": "轮渡" },
// { "key": "s2h19p", "name": "共享单车" },
// { "key": "w9j73v", "name": "电动车" },
// { "key": "a5k33q", "name": "自驾车" },
// { "key": "y1l87u", "name": "长途客车" },
// { "key": "d3m41b", "name": "飞机" },
// { "key": "f8n22h", "name": "直升机" },
// { "key": "c4p66r", "name": "城际铁路" },
// { "key": "m2q99s", "name": "轨道交通" },
// { "key": "j7r55d", "name": "大巴车" },
// { "key": "z3s13e", "name": "顺风车" },
// { "key": "e9t80y", "name": "三轮车" },
// { "key": "h5u28i", "name": "地面有轨电车" },
// { "key": "b6v71o", "name": "高速公路客运" }
// ]
// )
const
ret
=
await
DB
.
Resources
.
create
(
body
);
return
res
.
sendData
(
ret
);
}
catch
(
error
)
{
next
(
error
);
}
try
{
const
body
=
req
.
body
;
// await DB.Resources.bulkCreate(
// [
// { "key": "k1a92f", "name": "公交车" },
// { "key": "u3b81x", "name": "地铁" },
// { "key": "p9c44m", "name": "高铁" },
// { "key": "n7d22t", "name": "出租车" },
// { "key": "q4e93z", "name": "轻轨" },
// { "key": "t6f81k", "name": "磁悬浮列车" },
// { "key": "r8g55w", "name": "轮渡" },
// { "key": "s2h19p", "name": "共享单车" },
// { "key": "w9j73v", "name": "电动车" },
// { "key": "a5k33q", "name": "自驾车" },
// { "key": "y1l87u", "name": "长途客车" },
// { "key": "d3m41b", "name": "飞机" },
// { "key": "f8n22h", "name": "直升机" },
// { "key": "c4p66r", "name": "城际铁路" },
// { "key": "m2q99s", "name": "轨道交通" },
// { "key": "j7r55d", "name": "大巴车" },
// { "key": "z3s13e", "name": "顺风车" },
// { "key": "e9t80y", "name": "三轮车" },
// { "key": "h5u28i", "name": "地面有轨电车" },
// { "key": "b6v71o", "name": "高速公路客运" }
// ]
// )
const
ret
=
await
DB
.
Resources
.
create
(
body
);
return
res
.
sendData
(
ret
);
}
catch
(
error
)
{
next
(
error
);
}
};
async
function
updateResource
(
req
,
res
,
next
)
{
try
{
const
body
=
req
.
body
;
if
(
!
body
.
id
)
{
return
res
.
sendError
(
errorMessage
.
resourceNotFound
)
try
{
const
body
=
req
.
body
;
if
(
!
body
.
id
)
{
return
res
.
sendError
(
errorMessage
.
resourceNotFound
)
}
const
ret
=
await
DB
.
Resources
.
update
(
body
,
{
where
:
{
id
:
body
.
id
}
});
return
res
.
sendData
(
ret
);
}
catch
(
error
)
{
next
(
error
);
}
const
ret
=
await
DB
.
Resources
.
update
(
body
,
{
where
:
{
id
:
body
.
id
}
});
return
res
.
sendData
(
ret
);
}
catch
(
error
)
{
next
(
error
);
}
}
async
function
deleteResource
(
req
,
res
,
next
)
{
try
{
if
(
!
req
.
body
.
id
)
{
return
res
.
sendError
(
errorMessage
.
resourceNotFound
);
try
{
if
(
!
req
.
body
.
id
)
{
return
res
.
sendError
(
errorMessage
.
resourceNotFound
);
}
const
ret
=
await
DB
.
Resources
.
update
({
del
:
1
},
{
where
:
{
id
:
req
.
body
.
id
}
});
return
res
.
sendData
(
ret
);
}
catch
(
error
)
{
next
(
error
);
}
const
ret
=
await
DB
.
Resources
.
update
({
del
:
1
},
{
where
:
{
id
:
req
.
body
.
id
}
});
return
res
.
sendData
(
ret
);
}
catch
(
error
)
{
next
(
error
);
}
}
async
function
listResource
(
req
,
res
,
next
)
{
try
{
let
search
=
{
where
:
{
del
:
0
}
};
let
page
=
req
.
body
.
page
||
1
;
let
limit
=
req
.
body
.
pageSize
||
10
;
//翻页
let
offset
=
(
page
-
1
)
*
limit
;
if
(
req
.
body
.
name
)
{
search
.
where
=
{
[
Op
.
or
]:
[
//根据名字模糊搜索
{
name
:
{
[
Op
.
like
]:
`%
${
req
.
body
.
name
}
%`
}
}
],
del
:
0
,
}
try
{
let
search
=
{
where
:
{
del
:
0
}
};
let
page
=
req
.
body
.
page
||
1
;
let
limit
=
req
.
body
.
pageSize
||
10
;
//翻页
let
offset
=
(
page
-
1
)
*
limit
;
if
(
req
.
body
.
name
)
{
search
.
where
=
{
[
Op
.
or
]:
[
//根据名字模糊搜索
{
name
:
{
[
Op
.
like
]:
`%
${
req
.
body
.
name
}
%`
}
}
],
del
:
0
,
}
}
search
.
limit
=
limit
;
search
.
offset
=
offset
;
//指定显示字段,空数组会报错
search
.
attributes
=
req
.
body
.
attributes
||
null
;
let
ret
=
await
DB
.
Resources
.
findAndCountAll
(
search
);
return
res
.
sendData
(
ret
);
}
catch
(
error
)
{
next
(
error
);
}
search
.
limit
=
limit
;
search
.
offset
=
offset
;
//指定显示字段,空数组会报错
search
.
attributes
=
req
.
body
.
attributes
||
null
;
let
ret
=
await
DB
.
Resources
.
findAndCountAll
(
search
);
return
res
.
sendData
(
ret
);
}
catch
(
error
)
{
next
(
error
);
}
};
/**
* 资源库字段controller
*/
async
function
createResourceInfo
(
req
,
res
,
next
)
{
try
{
const
body
=
req
.
body
;
if
(
!
body
.
resourceId
)
{
return
res
.
sendError
(
errorMessage
.
resourceNotFound
)
}
const
resource
=
await
DB
.
Resources
.
findOne
({
where
:
{
id
:
body
.
resourceId
}
});
if
(
!
resource
)
{
return
res
.
sendError
(
errorMessage
.
resourceNotFound
)
try
{
const
body
=
req
.
body
;
if
(
!
body
.
resourceId
)
{
return
res
.
sendError
(
errorMessage
.
resourceNotFound
)
}
const
resource
=
await
DB
.
Resources
.
findOne
({
where
:
{
id
:
body
.
resourceId
}
});
if
(
!
resource
)
{
return
res
.
sendError
(
errorMessage
.
resourceNotFound
)
}
const
ret
=
await
DB
.
ResourcesInfo
.
create
(
body
);
return
res
.
sendData
(
ret
);
}
catch
(
error
)
{
next
(
error
);
}
const
ret
=
await
DB
.
ResourcesInfo
.
create
(
body
);
return
res
.
sendData
(
ret
);
}
catch
(
error
)
{
next
(
error
);
}
};
async
function
updateResourceInfo
(
req
,
res
,
next
)
{
try
{
const
body
=
req
.
body
;
if
(
!
body
.
id
)
{
return
res
.
sendError
(
errorMessage
.
resourceNotFound
)
try
{
const
body
=
req
.
body
;
if
(
!
body
.
id
)
{
return
res
.
sendError
(
errorMessage
.
resourceNotFound
)
}
const
ret
=
await
DB
.
ResourcesInfo
.
update
(
body
,
{
where
:
{
id
:
body
.
id
}
});
return
res
.
sendData
(
ret
);
}
catch
(
error
)
{
next
(
error
);
}
const
ret
=
await
DB
.
ResourcesInfo
.
update
(
body
,
{
where
:
{
id
:
body
.
id
}
});
return
res
.
sendData
(
ret
);
}
catch
(
error
)
{
next
(
error
);
}
}
async
function
deleteResourceInfo
(
req
,
res
,
next
)
{
try
{
if
(
!
req
.
body
.
id
)
{
return
res
.
sendError
(
errorMessage
.
resourceNotFound
);
try
{
if
(
!
req
.
body
.
id
)
{
return
res
.
sendError
(
errorMessage
.
resourceNotFound
);
}
const
ret
=
await
DB
.
ResourcesInfo
.
update
({
del
:
1
},
{
where
:
{
id
:
req
.
body
.
id
}
});
return
res
.
sendData
(
ret
);
}
catch
(
error
)
{
next
(
error
);
}
const
ret
=
await
DB
.
ResourcesInfo
.
update
({
del
:
1
},
{
where
:
{
id
:
req
.
body
.
id
}
});
return
res
.
sendData
(
ret
);
}
catch
(
error
)
{
next
(
error
);
}
}
async
function
listResourceInfo
(
req
,
res
,
next
)
{
try
{
const
body
=
req
.
body
;
if
(
!
body
.
resourceId
)
{
return
res
.
sendError
(
errorMessage
.
resourceNotFound
)
}
const
resource
=
await
DB
.
Resources
.
findOne
({
where
:
{
id
:
body
.
resourceId
},
raw
:
true
});
let
search
=
{
where
:
{
[
Op
.
and
]:
[
{
del
:
0
},
{
resourceId
:
resource
.
id
},
(
req
.
body
.
name
?
[{
value
:
{
[
Op
.
like
]:
`%
${
req
.
body
.
name
}
%`
}
}]
:
[])
]
},
raw
:
true
}
//指定显示字段,空数组会报错
// search.attributes = req.body.attributes || null;
let
ret
=
await
DB
.
ResourcesInfo
.
findAll
(
search
);
console
.
log
(
"ret"
,
ret
)
if
(
resource
.
type
===
1
)
{
return
res
.
sendData
(
ret
);
}
else
if
(
resource
.
type
===
2
)
{
//ret为json数据数组
const
treeData
=
buildTree
(
ret
);
return
res
.
sendData
(
treeData
);
try
{
const
body
=
req
.
body
;
if
(
!
body
.
resourceId
)
{
return
res
.
sendError
(
errorMessage
.
resourceNotFound
)
}
const
resource
=
await
DB
.
Resources
.
findOne
({
where
:
{
id
:
body
.
resourceId
},
raw
:
true
});
let
search
=
{
where
:
{
[
Op
.
and
]:
[
{
del
:
0
},
{
resourceId
:
resource
.
id
},
(
req
.
body
.
name
?
[{
value
:
{
[
Op
.
like
]:
`%
${
req
.
body
.
name
}
%`
}
}]
:
[])
]
},
raw
:
true
}
//指定显示字段,空数组会报错
// search.attributes = req.body.attributes || null;
let
ret
=
await
DB
.
ResourcesInfo
.
findAll
(
search
);
console
.
log
(
"ret"
,
ret
)
if
(
resource
.
type
===
1
)
{
return
res
.
sendData
(
ret
);
}
else
if
(
resource
.
type
===
2
)
{
//ret为json数据数组
const
treeData
=
buildTree
(
ret
);
return
res
.
sendData
(
treeData
);
}
}
catch
(
error
)
{
next
(
error
);
}
}
catch
(
error
)
{
next
(
error
);
}
};
async
function
saveAllResourceInfo
(
req
,
res
,
next
)
{
try
{
const
{
resourceId
,
data
=
[]
}
=
req
.
body
;
if
(
!
resourceId
||
!
Array
.
isArray
(
data
))
{
return
res
.
sendError
(
errorMessage
.
resourceNotFound
);
}
const
resource
=
await
DB
.
Resources
.
findOne
({
where
:
{
id
:
resourceId
},
raw
:
true
});
try
{
const
{
resourceId
,
data
=
[]
}
=
req
.
body
;
if
(
!
resource
)
{
return
res
.
sendError
(
errorMessage
.
resourceNotFound
);
}
if
(
resource
.
type
===
1
)
{
const
existingRecords
=
await
DB
.
ResourcesInfo
.
findAll
({
where
:
{
del
:
0
,
resourceId
:
resourceId
},
raw
:
true
});
console
.
log
(
"existingRecords"
,
existingRecords
);
const
existMap
=
new
Map
();
existingRecords
.
forEach
(
r
=>
existMap
.
set
(
r
.
id
,
r
));
const
toCreate
=
[];
const
toUpdate
=
[];
const
incomingIds
=
new
Set
();
for
(
const
item
of
data
)
{
if
(
item
.
resourceId
!=
resourceId
)
{
console
.
log
(
item
.
value
,
"不属于当前资源,跳过"
);
continue
;
if
(
!
resourceId
||
!
Array
.
isArray
(
data
))
{
return
res
.
sendError
(
errorMessage
.
resourceNotFound
);
}
console
.
log
(
"每条aaa记录"
,
item
);
const
resource
=
await
DB
.
Resources
.
findOne
({
where
:
{
id
:
resourceId
},
raw
:
true
});
const
record
=
{
...
item
,
//绑定ID
resourceId
:
Number
(
resourceId
),
del
:
0
};
//传进来有id,数据库能查到的为更新
if
(
item
.
id
!=
null
&&
existMap
.
has
(
item
.
id
))
{
toUpdate
.
push
(
record
);
incomingIds
.
add
(
item
.
id
);
}
else
{
//没id的统一为新增
toCreate
.
push
(
record
);
if
(
!
resource
)
{
return
res
.
sendError
(
errorMessage
.
resourceNotFound
);
}
}
// 数据库中的id,但是没传进来的自动为删除
const
toDeleteIds
=
existingRecords
.
filter
(
r
=>
!
incomingIds
.
has
(
r
.
id
))
.
map
(
r
=>
r
.
id
);
console
.
log
(
'本次新增:'
,
toCreate
.
length
);
console
.
log
(
'本次更新:'
,
toUpdate
.
length
);
console
.
log
(
'本次软删:'
,
toDeleteIds
);
if
(
toCreate
.
length
>
0
)
{
await
DB
.
ResourcesInfo
.
bulkCreate
(
toCreate
);
}
if
(
toUpdate
.
length
>
0
)
{
for
(
const
item
of
toUpdate
)
{
await
DB
.
ResourcesInfo
.
update
(
item
,
{
where
:
{
id
:
item
.
id
}
});
let
toCreate
=
[],
toKeep
=
[];
if
(
data
.
length
)
{
for
(
let
index
=
0
;
index
<
data
.
length
;
index
++
)
{
const
element
=
data
[
index
];
if
(
element
.
id
)
{
toKeep
.
push
(
element
.
id
);
}
else
{
element
.
resourceId
=
resource
.
id
;
toCreate
.
push
(
element
);
}
}
}
}
if
(
toDeleteIds
.
length
>
0
)
{
await
DB
.
ResourcesInfo
.
destroy
(
{
del
:
1
},
{
where
:
{
id
:
toDeleteIds
}
}
);
}
const
ret
=
await
DB
.
ResourcesInfo
.
findAll
({
where
:
{
del
:
0
,
resourceId
:
resourceId
},
raw
:
true
});
return
res
.
sendData
(
ret
);
if
(
toKeep
.
length
)
{
await
DB
.
ResourcesInfo
.
destory
({
where
:
{[
Op
.
nin
]:
toKeep
}
})
}
if
(
toCreate
.
length
)
{
await
DB
.
ResourcesInfo
.
bulkCreate
(
toCreate
);
}
return
res
.
sendData
();
}
catch
(
error
)
{
console
.
error
(
'saveAllResourceInfo error:'
,
error
);
next
(
error
);
}
return
res
.
sendData
([]);
}
catch
(
error
)
{
console
.
error
(
'saveAllResourceInfo error:'
,
error
);
next
(
error
);
}
}
async
function
listResourceAll
(
req
,
res
,
next
)
{
try
{
let
datas
=
await
DB
.
Resources
.
findAll
(
{
where
:
{
del
:
0
},
include
:
[
{
model
:
DB
.
ResourcesInfo
,
as
:
'resourcesInfos'
,
attributes
:
[
'key'
,
'value'
]
}
]
}
)
return
res
.
sendData
(
datas
)
}
catch
(
error
)
{
next
(
error
);
}
try
{
let
datas
=
await
DB
.
Resources
.
findAll
(
{
where
:
{
del
:
0
},
include
:
[
{
model
:
DB
.
ResourcesInfo
,
as
:
'resourcesInfos'
,
attributes
:
[
'key'
,
'value'
]
}
]
}
)
return
res
.
sendData
(
datas
)
}
catch
(
error
)
{
next
(
error
);
}
}
module
.
exports
=
{
createResource
,
updateResource
,
deleteResource
,
listResource
,
createResource
,
updateResource
,
deleteResource
,
listResource
,
createResourceInfo
,
updateResourceInfo
,
deleteResourceInfo
,
listResourceInfo
,
createResourceInfo
,
updateResourceInfo
,
deleteResourceInfo
,
listResourceInfo
,
saveAllResourceInfo
,
listResourceAll
,
saveAllResourceInfo
,
listResourceAll
,
}
\ 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