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 4c10471d
authored
Oct 12, 2024
by
yixinjj
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
ws新增设备号登录
1 parent
210a462f
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
35 additions
and
9 deletions
src/modules/auth/auth.controller.ts
src/modules/auth/auth.service.ts
src/modules/broadcast/broadcast.module.ts
src/modules/broadcast/broadcast.service.ts
src/modules/device/device.service.ts
src/modules/ws/ws.module.ts
test.http
src/modules/auth/auth.controller.ts
View file @
4c10471
...
@@ -5,6 +5,7 @@ import { RESPONSE_CODE } from '../../common/enums'
...
@@ -5,6 +5,7 @@ import { RESPONSE_CODE } from '../../common/enums'
import
{
ApiTags
}
from
'@nestjs/swagger'
import
{
ApiTags
}
from
'@nestjs/swagger'
import
{
TransformInterceptor
}
from
'../../common/interceptors'
import
{
TransformInterceptor
}
from
'../../common/interceptors'
import
{
DeviceFindOneDto
}
from
'src/dto'
import
{
DeviceFindOneDto
}
from
'src/dto'
import
{
SnDto
}
from
'src/dto/base.dto'
@
Controller
()
@
Controller
()
@
ApiTags
(
'权限管理'
)
@
ApiTags
(
'权限管理'
)
...
@@ -17,10 +18,10 @@ export class AuthController {
...
@@ -17,10 +18,10 @@ export class AuthController {
// 登录测试
// 登录测试
// @UseGuards(AuthGuard('local'))
// @UseGuards(AuthGuard('local'))
@
Post
(
'login'
)
@
Post
(
'login'
)
async
login
(@
Body
()
user
:
DeviceFindOne
Dto
):
Promise
<
CommonTypes
.
IResData
>
{
async
login
(@
Body
()
{
sn
}:
Sn
Dto
):
Promise
<
CommonTypes
.
IResData
>
{
const
result
:
CommonTypes
.
IResData
=
await
this
.
authService
.
validate
User
(
user
)
const
result
:
CommonTypes
.
IResData
=
await
this
.
authService
.
validate
Sn
(
sn
)
if
(
result
.
code
===
RESPONSE_CODE
.
SUCCESS
)
{
if
(
result
.
code
===
RESPONSE_CODE
.
SUCCESS
)
{
const
token
=
this
.
authService
.
getToken
(
user
)
const
token
=
this
.
authService
.
getToken
BySn
({
sn
}
)
result
.
data
.
token
=
token
result
.
data
.
token
=
token
}
}
return
result
return
result
...
...
src/modules/auth/auth.service.ts
View file @
4c10471
...
@@ -2,6 +2,7 @@ import { Inject, Injectable } from '@nestjs/common'
...
@@ -2,6 +2,7 @@ import { Inject, Injectable } from '@nestjs/common'
import
{
JwtService
,
TokenExpiredError
}
from
'@nestjs/jwt'
import
{
JwtService
,
TokenExpiredError
}
from
'@nestjs/jwt'
import
{
DeviceService
}
from
'../device/device.service'
import
{
DeviceService
}
from
'../device/device.service'
import
{
DeviceFindOneDto
}
from
'src/dto'
import
{
DeviceFindOneDto
}
from
'src/dto'
import
{
SnDto
}
from
'src/dto/base.dto'
@
Injectable
()
@
Injectable
()
export
class
AuthService
{
export
class
AuthService
{
...
@@ -15,9 +16,16 @@ export class AuthService {
...
@@ -15,9 +16,16 @@ export class AuthService {
return
await
this
.
deviceService
.
findOne
(
user
)
return
await
this
.
deviceService
.
findOne
(
user
)
}
}
async
validateSn
(
sn
:
string
):
Promise
<
CommonTypes
.
IResData
<
CommonTypes
.
IId
&
CommonTypes
.
ISn
>>
{
return
await
this
.
deviceService
.
findOneBySn
(
sn
)
}
getToken
({
user
,
mobile
}:
DeviceFindOneDto
):
string
{
getToken
({
user
,
mobile
}:
DeviceFindOneDto
):
string
{
return
this
.
jwtService
.
sign
({
user
,
mobile
})
return
this
.
jwtService
.
sign
({
user
,
mobile
})
}
}
getTokenBySn
({
sn
}:
SnDto
):
string
{
return
this
.
jwtService
.
sign
({
sn
})
}
verifyToken
(
token
:
string
)
{
verifyToken
(
token
:
string
)
{
let
res
:
{
isPass
:
boolean
;
error
:
any
;
data
:
any
;
isExpired
:
boolean
}
=
{
isPass
:
true
,
error
:
null
,
data
:
null
,
isExpired
:
false
}
let
res
:
{
isPass
:
boolean
;
error
:
any
;
data
:
any
;
isExpired
:
boolean
}
=
{
isPass
:
true
,
error
:
null
,
data
:
null
,
isExpired
:
false
}
try
{
try
{
...
...
src/modules/broadcast/broadcast.module.ts
View file @
4c10471
...
@@ -2,8 +2,9 @@ import { Module } from '@nestjs/common'
...
@@ -2,8 +2,9 @@ import { Module } from '@nestjs/common'
import
{
BroadcastController
}
from
'./broadcast.controller'
import
{
BroadcastController
}
from
'./broadcast.controller'
import
{
BroadcastService
}
from
'./broadcast.service'
import
{
BroadcastService
}
from
'./broadcast.service'
import
{
HttpModule
}
from
'@nestjs/axios'
;
import
{
HttpModule
}
from
'@nestjs/axios'
;
import
{
WsModule
}
from
'../ws/ws.module'
;
@
Module
({
@
Module
({
imports
:
[
HttpModule
],
imports
:
[
HttpModule
,
WsModule
],
controllers
:
[
BroadcastController
],
controllers
:
[
BroadcastController
],
providers
:
[
BroadcastService
]
providers
:
[
BroadcastService
]
})
})
...
...
src/modules/broadcast/broadcast.service.ts
View file @
4c10471
...
@@ -4,6 +4,7 @@ import { AxiosResponse } from 'axios'
...
@@ -4,6 +4,7 @@ import { AxiosResponse } from 'axios'
import
{
ConfigService
}
from
'nestjs-config'
import
{
ConfigService
}
from
'nestjs-config'
import
{
Command
,
Events
,
RESPONSE_CODE
,
WsMessageType
}
from
'../../common/enums'
import
{
Command
,
Events
,
RESPONSE_CODE
,
WsMessageType
}
from
'../../common/enums'
import
{
EventEmitter2
}
from
'@nestjs/event-emitter'
import
{
EventEmitter2
}
from
'@nestjs/event-emitter'
import
{
WsGateway
}
from
'../ws/ws.gateway'
@
Injectable
()
@
Injectable
()
export
class
BroadcastService
{
export
class
BroadcastService
{
private
webHook
:
string
private
webHook
:
string
...
@@ -11,14 +12,17 @@ export class BroadcastService {
...
@@ -11,14 +12,17 @@ export class BroadcastService {
private
readonly
httpService
:
HttpService
,
private
readonly
httpService
:
HttpService
,
private
readonly
config
:
ConfigService
,
private
readonly
config
:
ConfigService
,
@
Inject
(
EventEmitter2
)
@
Inject
(
EventEmitter2
)
private
readonly
eventEmitter
:
EventEmitter2
private
readonly
eventEmitter
:
EventEmitter2
,
private
readonly
wsGateway
:
WsGateway
)
{
)
{
this
.
webHook
=
this
.
config
.
get
(
'env'
).
AppWebhook
this
.
webHook
=
this
.
config
.
get
(
'env'
).
AppWebhook
}
}
async
send
(
message
:
WsTypes
.
MessageBody
<
Command
,
WsMessageType
>
):
Promise
<
CommonTypes
.
IResData
>
{
async
send
(
message
:
WsTypes
.
MessageBody
<
Command
,
WsMessageType
>
):
Promise
<
CommonTypes
.
IResData
>
{
const
success
=
this
.
eventEmitter
.
emit
(
Events
.
OnMessageToSendClient
,
message
)
// const success = await this.eventEmitter.emitAsync(Events.OnMessageToSendClient, message)
return
{
code
:
success
?
RESPONSE_CODE
.
SUCCESS
:
RESPONSE_CODE
.
NOSUCCESS
,
data
:
{
success
}
}
return
this
.
wsGateway
.
send
(
message
)
// return { code: success ? RESPONSE_CODE.SUCCESS : RESPONSE_CODE.NOSUCCESS, data: { success } }
}
}
// // 广播消息
// // 广播消息
async
onMessage
(
param
:
any
):
Promise
<
CommonTypes
.
IResData
>
{
async
onMessage
(
param
:
any
):
Promise
<
CommonTypes
.
IResData
>
{
...
...
src/modules/device/device.service.ts
View file @
4c10471
...
@@ -100,6 +100,17 @@ export class DeviceService implements OnModuleInit {
...
@@ -100,6 +100,17 @@ export class DeviceService implements OnModuleInit {
return
{
code
:
RESPONSE_CODE
.
SUCCESS
,
data
:
deviceInfo
}
return
{
code
:
RESPONSE_CODE
.
SUCCESS
,
data
:
deviceInfo
}
}
}
async
findOneBySn
(
sn
:
string
):
Promise
<
CommonTypes
.
IResData
<
CommonTypes
.
IId
&
CommonTypes
.
ISn
>>
{
try
{
const
device
:
CommonTypes
.
IId
&
CommonTypes
.
ISn
=
await
this
.
prisma
.
device
.
findFirstOrThrow
({
where
:
{
sn
}
})
return
{
code
:
RESPONSE_CODE
.
SUCCESS
,
data
:
device
}
}
catch
(
error
)
{
return
{
code
:
RESPONSE_CODE
.
NOSUCCESS
,
message
:
'用户验证失败!'
,
error
}
}
}
async
addDevice
(
device
:
Omit
<
DeviceTypes
.
IDeviceSaves
,
'device_id'
>
):
Promise
<
CommonTypes
.
IResData
>
{
async
addDevice
(
device
:
Omit
<
DeviceTypes
.
IDeviceSaves
,
'device_id'
>
):
Promise
<
CommonTypes
.
IResData
>
{
const
{
device_info
,
log_config
,
user_fields
,
sn
}
=
device
const
{
device_info
,
log_config
,
user_fields
,
sn
}
=
device
await
this
.
prisma
.
$transaction
(
async
prisma
=>
{
await
this
.
prisma
.
$transaction
(
async
prisma
=>
{
...
...
src/modules/ws/ws.module.ts
View file @
4c10471
...
@@ -4,6 +4,7 @@ import { WsService } from './ws.service'
...
@@ -4,6 +4,7 @@ import { WsService } from './ws.service'
import
{
AuthModule
}
from
'../auth/auth.module'
import
{
AuthModule
}
from
'../auth/auth.module'
@
Module
({
@
Module
({
imports
:
[
AuthModule
],
imports
:
[
AuthModule
],
providers
:
[
WsGateway
,
WsService
]
providers
:
[
WsGateway
,
WsService
],
exports
:
[
WsGateway
]
})
})
export
class
WsModule
{}
export
class
WsModule
{}
test.http
View file @
4c10471
...
@@ -39,7 +39,7 @@ POST http://localhost:3000/broadcast/cmd HTTP/1.1
...
@@ -39,7 +39,7 @@ POST http://localhost:3000/broadcast/cmd HTTP/1.1
{
{
"event":"private",
"event":"private",
"device_id":"10000
70
",
"device_id":"10000
68
",
"data":{
"data":{
"command":"getLog",
"command":"getLog",
"days":2,
"days":2,
...
...
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