1
1
// Import necessary packages
2
+ const Iconv = require ( 'iconv' ) . Iconv
2
3
const path = require ( 'path' )
3
4
const SrcDir = path . join ( '../../' , './src/' )
4
5
const db = require ( SrcDir + 'db' )
@@ -25,23 +26,41 @@ async function hitokoto (ctx, next) {
25
26
}
26
27
// CheckEncoding
27
28
const encode = ctx . query . encode
29
+ let response
30
+ let js = false
28
31
switch ( encode ) {
29
32
case 'json' :
30
- ctx . body = ret
33
+ response = Buffer . from ( ret )
31
34
break
32
35
case 'text' :
33
- ctx . body = ret . text
36
+ response = Buffer . from ( ret . text )
34
37
break
35
38
case 'js' :
36
39
const select = ctx . query . select ? ctx . query . select : '.hitokoto'
37
- ctx . set ( 'charset ' , 'utf-8' )
38
- ctx . set ( 'Content-Type' , 'text/javascript; charset=utf-8' )
39
- ctx . body = `(function hitokoto(){var hitokoto="${ ret . hitokoto } ";var dom=document.querySelector('${ select } ');Array.isArray(dom)?dom[0].innerText=hitokoto:dom.innerText=hitokoto;})()`
40
+ // ctx.set('Content-Type ', 'text/javascript; charset= utf-8')
41
+ js = true
42
+ response = Buffer . from ( `(function hitokoto(){var hitokoto="${ ret . hitokoto } ";var dom=document.querySelector('${ select } ');Array.isArray(dom)?dom[0].innerText=hitokoto:dom.innerText=hitokoto;})()` )
40
43
break
41
44
default :
42
- ctx . body = ret
45
+ response = Buffer . from ( ret )
43
46
break
44
47
}
48
+ if ( ctx . query && ctx . query . charset && ctx . query . charset . toLocaleLowerCase ( ) === 'gbk' ) {
49
+ if ( js ) {
50
+ ctx . set ( 'Content-Type' , 'text/javascript; charset=gbk' )
51
+ } else {
52
+ ctx . set ( 'Content-Type' , 'application/json; charset=gbk' )
53
+ }
54
+ const iconv = new Iconv ( 'UTF-8' , 'GBK' )
55
+ ctx . body = iconv . convert ( response )
56
+ } else {
57
+ if ( js ) {
58
+ ctx . set ( 'Content-Type' , 'text/javascript; charset=utf-8' )
59
+ } else {
60
+ ctx . set ( 'Content-Type' , 'application/json; charset=utf-8' )
61
+ }
62
+ ctx . body = response
63
+ }
45
64
} else {
46
65
// Not Params or just has callback
47
66
const ret = await hitokoto . findOne ( {
@@ -51,22 +70,41 @@ async function hitokoto (ctx, next) {
51
70
52
71
// CheckEncoding
53
72
const encode = ctx . query . encode
73
+ let response
74
+ let js = false
54
75
switch ( encode ) {
55
76
case 'json' :
56
- ctx . body = ret
77
+ response = Buffer . from ( ret )
57
78
break
58
79
case 'text' :
59
- ctx . body = ret . text
80
+ response = Buffer . from ( ret . text )
60
81
break
61
82
case 'js' :
62
83
const select = ctx . query . select ? ctx . query . select : '.hitokoto'
63
- ctx . set ( 'Content-Type' , 'text/javascript; charset=utf-8' )
64
- ctx . body = `(function hitokoto(){var hitokoto="${ ret . hitokoto } ";var dom=document.querySelector('${ select } ');if(!dom){console.error("请输入正确的选择器值");}Array.isArray(dom)?dom[0].innerText=hitokoto:dom.innerText=hitokoto;})()`
84
+ // ctx.set('Content-Type', 'text/javascript; charset=utf-8')
85
+ js = true
86
+ response = Buffer . from ( `(function hitokoto(){var hitokoto="${ ret . hitokoto } ";var dom=document.querySelector('${ select } ');Array.isArray(dom)?dom[0].innerText=hitokoto:dom.innerText=hitokoto;})()` )
65
87
break
66
88
default :
67
- ctx . body = ret
89
+ response = Buffer . from ( ret )
68
90
break
69
91
}
92
+ if ( ctx . query && ctx . query . charset && ctx . query . charset . toLocaleLowerCase ( ) === 'gbk' ) {
93
+ if ( js ) {
94
+ ctx . set ( 'Content-Type' , 'text/javascript; charset=gbk' )
95
+ } else {
96
+ ctx . set ( 'Content-Type' , 'application/json; charset=gbk' )
97
+ }
98
+ const iconv = new Iconv ( 'UTF-8' , 'GBK' )
99
+ ctx . body = iconv . convert ( response )
100
+ } else {
101
+ if ( js ) {
102
+ ctx . set ( 'Content-Type' , 'text/javascript; charset=utf-8' )
103
+ } else {
104
+ ctx . set ( 'Content-Type' , 'application/json; charset=utf-8' )
105
+ }
106
+ ctx . body = response
107
+ }
70
108
}
71
109
}
72
110
module . exports = hitokoto
0 commit comments