File tree 7 files changed +45
-1523
lines changed
scripts/vsh/src/check-dep
7 files changed +45
-1523
lines changed Original file line number Diff line number Diff line change @@ -17,13 +17,7 @@ module.exports = util => {
17
17
return {
18
18
api : {
19
19
'/api/auth/codes' ( req , res ) {
20
- const token = req . get ( `Authorization` )
21
- if ( ! token ) {
22
- return res . status ( 401 ) . json ( useResponseError ( 'UnauthorizedException' , 'Unauthorized Exception' ) )
23
- }
24
- const username = Buffer . from ( token , 'base64' ) . toString ( 'utf8' ) ;
25
-
26
- const codes = MOCK_CODES . find ( ( item ) => item . username === username ) ?. codes ?? [ ] ;
20
+ const codes = MOCK_CODES . find ( ( item ) => item . username === req . username ) ?. codes ?? [ ] ;
27
21
res . json ( useResponseSuccess ( codes ) )
28
22
} ,
29
23
'post /api/auth/login' ( req , res ) {
Original file line number Diff line number Diff line change @@ -17,13 +17,7 @@ module.exports = util => {
17
17
return {
18
18
api : {
19
19
'/api/menu/all' ( req , res ) {
20
- const token = req . get ( `Authorization` )
21
- if ( ! token ) {
22
- return res . status ( 401 ) . json ( useResponseError ( 'UnauthorizedException' , 'Unauthorized Exception' ) )
23
- }
24
- const username = Buffer . from ( token , 'base64' ) . toString ( 'utf8' ) ;
25
-
26
- const menus = MOCK_MENUS . find ( ( item ) => item . username === username ) ?. menus ?? [ ] ;
20
+ const menus = MOCK_MENUS . find ( ( item ) => item . username === req . username ) ?. menus ?? [ ] ;
27
21
res . json ( useResponseSuccess ( menus ) )
28
22
29
23
} ,
Original file line number Diff line number Diff line change @@ -15,13 +15,7 @@ module.exports = util => {
15
15
return {
16
16
api : {
17
17
'/api/user/info' ( req , res ) {
18
- const token = req . get ( `Authorization` )
19
- if ( ! token ) {
20
- return res . status ( 401 ) . json ( useResponseError ( 'UnauthorizedException' , 'Unauthorized Exception' ) )
21
- }
22
- const username = Buffer . from ( token , 'base64' ) . toString ( 'utf8' ) ;
23
-
24
- const user = MOCK_USERS . find ( ( item ) => item . username === username ) ;
18
+ const user = MOCK_USERS . find ( ( item ) => item . username === req . username ) ;
25
19
26
20
const { password : _pwd , ...userInfo } = user ;
27
21
res . json ( useResponseSuccess ( user ) )
Original file line number Diff line number Diff line change
1
+ module . exports = {
2
+ key : `get-user` ,
3
+ main ( ) {
4
+ return {
5
+ useCreated ( app ) {
6
+ app . use ( ( req , res , next ) => {
7
+ try {
8
+ const token = req . get ( `Authorization` ) ;
9
+ const username = Buffer . from ( token , 'base64' ) . toString ( 'utf8' ) ;
10
+ req . username = username ;
11
+ } catch {
12
+ // ...
13
+ }
14
+ next ( ) ;
15
+ } ) ;
16
+ } ,
17
+ } ;
18
+ } ,
19
+ } ;
Original file line number Diff line number Diff line change 3
3
useResponseSuccess,
4
4
useResponseError,
5
5
} = require ( `./util.cjs` )
6
+ const getUser = require ( `./get-user.cjs` )
6
7
7
8
/**
8
9
* 配置说明请参考文档:
@@ -12,10 +13,32 @@ const {
12
13
module . exports = util => {
13
14
return {
14
15
watch : [ `./api/` ] ,
16
+ plugin : [ getUser ] ,
15
17
proxy : {
16
18
'/' : `http://www.httpbin.org/` , // 后端接口主域
17
19
} ,
18
20
api : {
21
+ 'use /' ( req , res , next ) {
22
+ const noCheckList = [
23
+ `/` ,
24
+ `/public` ,
25
+ `/favicon.ico` ,
26
+ `/api/test` ,
27
+ `/api/status` ,
28
+ `/api/auth/login` ,
29
+ ]
30
+ if ( req . path === `/` || noCheckList . some ( item => {
31
+ return item . startsWith ( req . path )
32
+ } ) ) {
33
+ next ( )
34
+ } else {
35
+ const token = req . get ( `Authorization` )
36
+ if ( ! token ) {
37
+ return res . status ( 401 ) . json ( useResponseError ( 'UnauthorizedException' , 'Unauthorized Exception' ) )
38
+ }
39
+ next ( )
40
+ }
41
+ } ,
19
42
// 跳转到接口列表
20
43
'/api' ( req , res ) {
21
44
const url = `http://127.0.0.1:${ globalThis . config . testPort } /#/apiStudio`
You can’t perform that action at this time.
0 commit comments