Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
node
/
app-admin
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit c1df7f76
authored
Sep 24, 2024
by
yixinjj
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
处理文件路径
1 parent
6f0cee25
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
79 additions
and
28 deletions
src/common/pipes/arguments.pipe.ts
src/modules/config/config.controller.ts
src/modules/config/config.service.ts
src/modules/device/device.controller.ts
src/modules/file/file.controller.ts
src/modules/file/file.service.ts
test.http
src/common/pipes/arguments.pipe.ts
View file @
c1df7f7
import
{
PipeTransform
,
Injectable
}
from
'@nestjs/common'
import
{
PipeTransform
,
Injectable
,
ArgumentMetadata
,
BadRequestException
}
from
'@nestjs/common'
@
Injectable
()
export
class
PagesPipe
implements
PipeTransform
<
CommonTypes
.
IPageIn
>
{
async
transform
(
value
:
CommonTypes
.
IPageIn
)
{
const
{
pageIndex
=
1
,
pageSize
=
10
}
=
value
const
{
pageIndex
=
1
,
pageSize
=
10
}
=
value
const
skip
=
(
pageIndex
-
1
)
*
pageSize
value
.
skip
=
+
skip
<
0
?
0
:
skip
value
.
take
=
+
pageSize
return
value
}
}
@
Injectable
()
export
class
FilePathPipe
implements
PipeTransform
<
Express
.
Multer
.
File
>
{
async
transform
(
file
:
Express
.
Multer
.
File
)
{
file
.
path
=
file
.
path
.
split
(
process
.
env
.
STATIC_PATH
).
pop
().
replace
(
/
\\
/g
,
'/'
)
return
file
}
}
@
Injectable
()
export
class
ParseDeviceIdPipe
implements
PipeTransform
<
any
,
any
>
{
transform
(
value
:
any
,
metadata
:
ArgumentMetadata
):
any
{
// 创建一个新对象,以避免直接修改原始值
const
transformedValue
=
{
...
value
}
// 检查 device_id 是否存在并且是字符串类型
if
(
typeof
transformedValue
.
device_id
===
'string'
)
{
const
deviceId
=
parseInt
(
transformedValue
.
device_id
,
10
)
// 检查转换后的值是否是有效的数字
if
(
isNaN
(
deviceId
))
{
throw
new
BadRequestException
(
'device_id must be a valid number'
)
}
// 更新 device_id 字段为数字类型
transformedValue
.
device_id
=
deviceId
}
// 返回转换后的值,其中 device_id 是数字类型,其他字段保持不变
return
transformedValue
}
}
@
Injectable
()
export
class
ParseIdPipe
implements
PipeTransform
<
any
,
any
>
{
transform
(
value
:
any
,
metadata
:
ArgumentMetadata
):
any
{
// 创建一个新对象,以避免直接修改原始值
const
transformedValue
=
{
...
value
}
// 检查 id 是否存在并且是字符串类型
if
(
typeof
transformedValue
.
id
===
'string'
)
{
const
id
=
parseInt
(
transformedValue
.
id
,
10
)
// 检查转换后的值是否是有效的数字
if
(
isNaN
(
id
))
{
throw
new
BadRequestException
(
'id must be a valid number'
)
}
// 更新 id 字段为数字类型
transformedValue
.
id
=
id
}
// 返回转换后的值,其中 id 是数字类型,其他字段保持不变
return
transformedValue
}
}
src/modules/config/config.controller.ts
View file @
c1df7f7
...
...
@@ -2,7 +2,7 @@ import { Controller, Post, Body, UseInterceptors, Get, UsePipes, Param, Query }
import
{
ConfigService
}
from
'./config.service'
import
{
TransformInterceptor
}
from
'../../common/interceptors'
import
{
LogConfigDto
}
from
'../../dto/config.dto'
import
{
PagesPipe
,
ValidationPipe
}
from
'../../common/pipes'
import
{
PagesPipe
,
ParseDeviceIdPipe
,
ValidationPipe
}
from
'../../common/pipes'
import
{
ApiOperation
,
ApiTags
}
from
'@nestjs/swagger'
@
ApiTags
(
'设备配置信息'
)
...
...
@@ -14,6 +14,7 @@ export class ConfigController {
@
Get
()
@
UsePipes
(
new
PagesPipe
())
@
UsePipes
(
new
ParseDeviceIdPipe
())
@
ApiOperation
({
summary
:
'根据设备号获取配置'
})
async
findAll
(@
Query
()
params
:
LogConfigDto
):
Promise
<
CommonTypes
.
IResData
<
ConfigTypes
.
LogConfigResponse
[]
>>
{
return
this
.
configService
.
findAll
(
params
)
...
...
src/modules/config/config.service.ts
View file @
c1df7f7
...
...
@@ -9,7 +9,7 @@ export class ConfigService {
)
{}
async
findAll
({
device_id
,
skip
,
take
}:
ConfigTypes
.
LogConfigQuery
):
Promise
<
CommonTypes
.
IResData
<
ConfigTypes
.
LogConfigResponse
[]
>>
{
const
where
:
any
=
{
device_id
:
+
device_id
device_id
}
const
result
:
ConfigTypes
.
LogConfigResult
[]
=
await
this
.
prisma
.
log_config
.
findMany
({
skip
,
...
...
src/modules/device/device.controller.ts
View file @
c1df7f7
...
...
@@ -98,14 +98,15 @@ export class DeviceController {
* @returns 一个Promise,成功时解析为包含CommonTypes.IResponseBase接口的CommonTypes.IResData对象
*/
@
Post
(
'/offlineAll'
)
@
ApiOperation
({
summary
:
'修改所有设备状态为离线'
})
@
OnEvent
(
Events
.
OnOfflineAll
)
@
ApiOperation
({
summary
:
'修改所有设备状态为离线'
})
async
resetAllDeviceStatus
():
Promise
<
void
|
CommonTypes
.
IResponseBase
>
{
return
await
this
.
deviceService
.
resetAllDeviceStatus
()
}
@
Get
(
'/task'
)
@
OnEvent
(
Events
.
OnTaskStart
)
@
ApiOperation
({
summary
:
'日任务'
})
async
task
():
Promise
<
void
|
CommonTypes
.
IResData
>
{
return
await
this
.
deviceService
.
removeFilesTask
()
}
...
...
src/modules/file/file.controller.ts
View file @
c1df7f7
...
...
@@ -7,7 +7,7 @@ import { UploadFile } from '../../common/decorators'
import
{
DocTypes
}
from
'../../common/enums'
import
{
FileDeleteDto
,
FileQueryDto
}
from
'../../dto'
import
{
ApiOperation
,
ApiTags
}
from
'@nestjs/swagger'
import
{
Pages
Pipe
}
from
'../../common/pipes'
import
{
FilePathPipe
,
PagesPipe
,
ParseDeviceIdPipe
,
ParseId
Pipe
}
from
'../../common/pipes'
@
ApiTags
(
'文件管理'
)
@
UsePipes
(
new
ValidationPipe
())
@
Controller
(
'file'
)
...
...
@@ -27,12 +27,13 @@ export class FileController {
* @returns 返回上传的文件对象
*/
@
Post
(
'upload'
)
@
UsePipes
(
new
FilePathPipe
())
@
ApiOperation
({
summary
:
'文件上传'
})
@
UploadFile
(
'file'
,
{
fileFilter
:
FileMimeTypeFilter
(
'text/plain'
),
limits
:
{
fileSize
:
1024
*
1024
*
100
}
})
upload
(@
UploadedFile
()
file
:
Express
.
Multer
.
File
,
@
Req
()
req
:
Request
):
FileTypes
.
FileUpload
<
DocTypes
>
{
const
{
fieldname
,
encoding
,
destination
,
stream
,
buffer
,
...
o
}
=
file
const
{
fieldname
,
encoding
,
destination
,
stream
,
buffer
,
path
,
...
o
}
=
file
const
bodyData
:
Pick
<
FileTypes
.
FileUpload
<
DocTypes
>
,
'device_id'
|
'type'
>
=
{
device_id
:
Number
(
req
.
body
.
deviceId
),
type
:
req
.
body
.
type
}
const
f
:
FileTypes
.
FileUpload
<
DocTypes
>
=
Object
.
assign
({
created_at
:
new
Date
()
},
bodyData
,
o
)
const
f
:
FileTypes
.
FileUpload
<
DocTypes
>
=
Object
.
assign
({
created_at
:
new
Date
()
,
path
:
`
${
req
.
protocol
}
://
${
req
.
get
(
'Host'
)}${
path
}
`
},
bodyData
,
o
)
this
.
fileService
.
save
(
f
)
return
f
}
...
...
@@ -44,8 +45,9 @@ export class FileController {
* @returns 返回该设备下所有指定类型的文件
*/
@
Get
(
'list'
)
@
ApiOperation
({
summary
:
'获取文件列表'
})
@
UsePipes
(
new
PagesPipe
())
@
UsePipes
(
new
ParseDeviceIdPipe
())
@
ApiOperation
({
summary
:
'获取文件列表'
})
async
findAll
(@
Query
()
params
:
FileQueryDto
):
Promise
<
CommonTypes
.
IResData
<
FileTypes
.
IFile
<
DocTypes
|
string
>
[]
>>
{
return
await
this
.
fileService
.
findAll
(
params
)
}
...
...
@@ -57,9 +59,10 @@ export class FileController {
* @returns 返回删除结果
*/
@
Get
(
'delete'
)
@
UsePipes
(
new
ParseIdPipe
())
@
ApiOperation
({
summary
:
'根据ID删除文件'
})
async
remove
(@
Query
()
{
id
}:
FileDeleteDto
):
Promise
<
CommonTypes
.
IResData
>
{
return
await
this
.
fileService
.
remove
(
+
id
)
return
await
this
.
fileService
.
remove
(
id
as
number
)
}
/**
...
...
src/modules/file/file.service.ts
View file @
c1df7f7
...
...
@@ -28,7 +28,7 @@ export class FileService {
*/
async
findAll
({
device_id
,
type
,
filename
,
skip
,
take
}:
FileTypes
.
FindeFiles
<
DocTypes
>
):
Promise
<
CommonTypes
.
IResData
<
FileTypes
.
IFile
<
DocTypes
|
string
>
[]
>>
{
const
where
=
{
device_id
:
device_id
?
+
device_id
:
undefined
,
device_id
,
type
,
originalname
:
{
contains
:
filename
...
...
test.http
View file @
c1df7f7
...
...
@@ -34,23 +34,23 @@
#
POST http://localhost:3000/broadcast/cmd HTTP/1.1
#
content-type: application/json
#
{
#
"event":"private",
# "device_id":1000001
,
#
"data":{
#
"command":"getStorage",
#
"payload":{
# "key":"userInfo
"
#
}
#
}
#
}
http://localhost:3000/file/list?device_id=1000000
# http://localhost:3000/config?device_id=
2
POST http://localhost:3000/broadcast/cmd HTTP/1.1
content-type: application/json
{
"event":"private",
"device_id":1000000
,
"data":{
"command":"getStorage",
"payload":{
"key":"recordList
"
}
}
}
#
http://localhost:3000/file/list?device_id=1000000
# http://localhost:3000/config?device_id=
1000000
# http://localhost:3000/file/delete?id=50
# POST http://localhost:3000/config/add HTTP/1.1
# content-type: application/json
...
...
Write
Preview
Markdown
is supported
Attach a file
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 post a comment