@@ -5,89 +5,105 @@ import * as drpy from '../libs/drpyS.js';
5
5
6
6
export default ( fastify , options , done ) => {
7
7
// 动态加载模块并根据 query 执行不同逻辑
8
- fastify . get ( '/api/:module' , async ( request , reply ) => {
9
- const moduleName = request . params . module ;
10
- const query = request . query ; // 获取 query 参数
11
- const modulePath = path . join ( options . jsDir , `${ moduleName } .js` ) ;
12
- if ( ! existsSync ( modulePath ) ) {
13
- reply . status ( 404 ) . send ( { error : `Module ${ moduleName } not found` } ) ;
14
- return ;
15
- }
16
- const protocol = request . protocol ;
17
- const hostname = request . hostname ;
18
- const proxyUrl = `${ protocol } ://${ hostname } ${ request . url } ` . split ( '?' ) [ 0 ] . replace ( '/api/' , '/proxy/' ) + '/?do=js' ;
19
- // console.log(`proxyUrl:${proxyUrl}`);
20
- const env = {
21
- proxyUrl, getProxyUrl : function ( ) {
22
- return proxyUrl
23
- }
24
- } ;
25
- const pg = Number ( query . pg ) || 1 ;
26
- try {
27
- // 根据 query 参数决定执行逻辑
28
- if ( 'play' in query ) {
29
- // 处理播放逻辑
30
- const result = await drpy . play ( modulePath , env , query . flag , query . play ) ;
31
- return reply . send ( result ) ;
8
+ fastify . route ( {
9
+ method : [ 'GET' , 'POST' ] , // 同时支持 GET 和 POST
10
+ url : '/api/:module' ,
11
+ schema : {
12
+ consumes : [ 'application/json' , 'application/x-www-form-urlencoded' ] , // 声明支持的内容类型
13
+ } ,
14
+ handler : async ( request , reply ) => {
15
+ const moduleName = request . params . module ;
16
+ const modulePath = path . join ( options . jsDir , `${ moduleName } .js` ) ;
17
+ if ( ! existsSync ( modulePath ) ) {
18
+ reply . status ( 404 ) . send ( { error : `Module ${ moduleName } not found` } ) ;
19
+ return ;
32
20
}
21
+ // 根据请求方法选择参数来源
22
+ const query = request . method === 'GET' ? request . query : request . body ;
23
+ const protocol = request . protocol ;
24
+ const hostname = request . hostname ;
25
+ const proxyUrl = `${ protocol } ://${ hostname } ${ request . url } ` . split ( '?' ) [ 0 ] . replace ( '/api/' , '/proxy/' ) + '/?do=js' ;
26
+ const publicUrl = `${ protocol } ://${ hostname } /public/` ;
27
+ // console.log(`proxyUrl:${proxyUrl}`);
28
+ const env = {
29
+ proxyUrl, publicUrl, getProxyUrl : function ( ) {
30
+ return proxyUrl
31
+ }
32
+ } ;
33
+ const pg = Number ( query . pg ) || 1 ;
34
+ try {
35
+ // 根据 query 参数决定执行逻辑
36
+ if ( 'play' in query ) {
37
+ // 处理播放逻辑
38
+ const result = await drpy . play ( modulePath , env , query . flag , query . play ) ;
39
+ return reply . send ( result ) ;
40
+ }
33
41
34
- if ( 'ac' in query && 't' in query ) {
35
- let ext = query . ext ;
36
- // console.log('ext:', ext);
37
- let extend = { } ;
38
- if ( ext ) {
39
- try {
40
- extend = JSON . parse ( base64Decode ( ext ) )
41
- } catch ( e ) {
42
- fastify . log . error ( `筛选参数错误:${ e . message } ` ) ;
42
+ if ( 'ac' in query && 't' in query ) {
43
+ let ext = query . ext ;
44
+ // console.log('ext:', ext);
45
+ let extend = { } ;
46
+ if ( ext ) {
47
+ try {
48
+ extend = JSON . parse ( base64Decode ( ext ) )
49
+ } catch ( e ) {
50
+ fastify . log . error ( `筛选参数错误:${ e . message } ` ) ;
51
+ }
43
52
}
53
+ // 分类逻辑
54
+ const result = await drpy . cate ( modulePath , env , query . t , pg , 1 , extend ) ;
55
+ return reply . send ( result ) ;
44
56
}
45
- // 分类逻辑
46
- const result = await drpy . cate ( modulePath , env , query . t , pg , 1 , extend ) ;
47
- return reply . send ( result ) ;
48
- }
49
57
50
- if ( 'ac' in query && 'ids' in query ) {
51
- // 详情逻辑
52
- const result = await drpy . detail ( modulePath , env , query . ids . split ( ',' ) ) ;
53
- return reply . send ( result ) ;
54
- }
58
+ if ( 'ac' in query && 'ids' in query ) {
59
+ // 详情逻辑
60
+ const result = await drpy . detail ( modulePath , env , query . ids . split ( ',' ) ) ;
61
+ return reply . send ( result ) ;
62
+ }
55
63
56
- if ( 'wd' in query ) {
57
- // 搜索逻辑
58
- const quick = 'quick' in query ? query . quick : 0 ;
59
- const result = await drpy . search ( modulePath , env , query . wd , quick , pg ) ;
60
- return reply . send ( result ) ;
61
- }
64
+ if ( 'ac' in query && 'action' in query ) {
65
+ // 处理动作逻辑
66
+ const result = await drpy . action ( modulePath , env , query . action , query . value ) ;
67
+ return reply . send ( result ) ;
68
+ }
62
69
63
- if ( 'refresh' in query ) {
64
- // 强制刷新初始化逻辑
65
- const refreshedObject = await drpy . init ( modulePath , env , true ) ;
66
- return reply . send ( refreshedObject ) ;
67
- }
68
- if ( ! ( 'filter' in query ) ) {
69
- query . filter = 1
70
- }
71
- // 默认逻辑,返回 home + homeVod 接口
72
- const filter = 'filter' in query ? query . filter : 1 ;
73
- const resultHome = await drpy . home ( modulePath , env , filter ) ;
74
- const resultHomeVod = await drpy . homeVod ( modulePath , env ) ;
75
- let result = {
76
- ...resultHome ,
77
- // list: resultHomeVod,
78
- } ;
79
- if ( Array . isArray ( resultHomeVod ) && resultHomeVod . length > 0 ) {
80
- Object . assign ( result , { list : resultHomeVod } )
81
- }
82
70
83
- reply . send ( result ) ;
71
+ if ( 'wd' in query ) {
72
+ // 搜索逻辑
73
+ const quick = 'quick' in query ? query . quick : 0 ;
74
+ const result = await drpy . search ( modulePath , env , query . wd , quick , pg ) ;
75
+ return reply . send ( result ) ;
76
+ }
84
77
85
- } catch ( error ) {
86
- // console.log('Error processing request:', error);
87
- // reply.status(500).send({error: `Failed to process request for module ${moduleName}: ${error.message}`});
78
+ if ( 'refresh' in query ) {
79
+ // 强制刷新初始化逻辑
80
+ const refreshedObject = await drpy . init ( modulePath , env , true ) ;
81
+ return reply . send ( refreshedObject ) ;
82
+ }
83
+ if ( ! ( 'filter' in query ) ) {
84
+ query . filter = 1
85
+ }
86
+ // 默认逻辑,返回 home + homeVod 接口
87
+ const filter = 'filter' in query ? query . filter : 1 ;
88
+ const resultHome = await drpy . home ( modulePath , env , filter ) ;
89
+ const resultHomeVod = await drpy . homeVod ( modulePath , env ) ;
90
+ let result = {
91
+ ...resultHome ,
92
+ // list: resultHomeVod,
93
+ } ;
94
+ if ( Array . isArray ( resultHomeVod ) && resultHomeVod . length > 0 ) {
95
+ Object . assign ( result , { list : resultHomeVod } )
96
+ }
97
+
98
+ reply . send ( result ) ;
88
99
89
- fastify . log . error ( `Error api module ${ moduleName } :${ error . message } ` ) ;
90
- reply . status ( 500 ) . send ( { error : `Failed to process module ${ moduleName } : ${ error . message } ` } ) ;
100
+ } catch ( error ) {
101
+ // console.log('Error processing request:', error);
102
+ // reply.status(500).send({error: `Failed to process request for module ${moduleName}: ${error.message}`});
103
+
104
+ fastify . log . error ( `Error api module ${ moduleName } :${ error . message } ` ) ;
105
+ reply . status ( 500 ) . send ( { error : `Failed to process module ${ moduleName } : ${ error . message } ` } ) ;
106
+ }
91
107
}
92
108
} ) ;
93
109
0 commit comments