11function Env ( name , opts ) {
2+ class Http {
3+ constructor ( env ) {
4+ this . env = env
5+ }
6+
7+ send ( opts , method = 'GET' ) {
8+ opts = typeof opts === 'string' ? { url : opts } : opts
9+ let sender = this . get
10+ if ( method === 'POST' ) {
11+ sender = this . post
12+ }
13+ return new Promise ( ( resolve , reject ) => {
14+ sender . call ( this , opts , ( err , resp , body ) => {
15+ if ( err ) reject ( err )
16+ else resolve ( resp )
17+ } )
18+ } )
19+ }
20+
21+ get ( opts ) {
22+ return this . send . call ( this . env , opts )
23+ }
24+
25+ post ( opts ) {
26+ return this . send . call ( this . env , opts , 'POST' )
27+ }
28+ }
29+
230 return new ( class {
331 constructor ( name , opts ) {
432 this . name = name
33+ this . http = new Http ( this )
534 this . data = null
635 this . dataFile = 'box.dat'
736 this . logs = [ ]
@@ -28,6 +57,41 @@ function Env(name, opts) {
2857 return 'undefined' !== typeof $loon
2958 }
3059
60+ toObj ( str , defaultValue = null ) {
61+ try {
62+ return JSON . parse ( str )
63+ } catch {
64+ return defaultValue
65+ }
66+ }
67+
68+ toStr ( obj , defaultValue = null ) {
69+ try {
70+ return JSON . stringify ( obj )
71+ } catch {
72+ return defaultValue
73+ }
74+ }
75+
76+ getjson ( key , defaultValue ) {
77+ let json = defaultValue
78+ const val = this . getdata ( key )
79+ if ( val ) {
80+ try {
81+ json = JSON . parse ( this . getdata ( key ) )
82+ } catch { }
83+ }
84+ return json
85+ }
86+
87+ setjson ( val , key ) {
88+ try {
89+ return this . setdata ( JSON . stringify ( val ) , key )
90+ } catch {
91+ return false
92+ }
93+ }
94+
3195 getScript ( url ) {
3296 return new Promise ( ( resolve ) => {
3397 this . get ( { url } , ( err , resp , body ) => resolve ( body ) )
@@ -50,6 +114,7 @@ function Env(name, opts) {
50114 this . post ( opts , ( err , resp , body ) => resolve ( body ) )
51115 } ) . catch ( ( e ) => this . logErr ( e ) )
52116 }
117+
53118 loaddata ( ) {
54119 if ( this . isNode ( ) ) {
55120 this . fs = this . fs ? this . fs : require ( 'fs' )
@@ -103,7 +168,11 @@ function Env(name, opts) {
103168 lodash_set ( obj , path , value ) {
104169 if ( Object ( obj ) !== obj ) return obj
105170 if ( ! Array . isArray ( path ) ) path = path . toString ( ) . match ( / [ ^ . [ \] ] + / g) || [ ]
106- path . slice ( 0 , - 1 ) . reduce ( ( a , c , i ) => ( Object ( a [ c ] ) === a [ c ] ? a [ c ] : ( a [ c ] = Math . abs ( path [ i + 1 ] ) >> 0 === + path [ i + 1 ] ? [ ] : { } ) ) , obj ) [ path [ path . length - 1 ] ] = value
171+ path
172+ . slice ( 0 , - 1 )
173+ . reduce ( ( a , c , i ) => ( Object ( a [ c ] ) === a [ c ] ? a [ c ] : ( a [ c ] = Math . abs ( path [ i + 1 ] ) >> 0 === + path [ i + 1 ] ? [ ] : { } ) ) , obj ) [
174+ path [ path . length - 1 ]
175+ ] = value
107176 return obj
108177 }
109178
@@ -285,7 +354,9 @@ function Env(name, opts) {
285354 'S' : new Date ( ) . getMilliseconds ( )
286355 }
287356 if ( / ( y + ) / . test ( fmt ) ) fmt = fmt . replace ( RegExp . $1 , ( new Date ( ) . getFullYear ( ) + '' ) . substr ( 4 - RegExp . $1 . length ) )
288- for ( let k in o ) if ( new RegExp ( '(' + k + ')' ) . test ( fmt ) ) fmt = fmt . replace ( RegExp . $1 , RegExp . $1 . length == 1 ? o [ k ] : ( '00' + o [ k ] ) . substr ( ( '' + o [ k ] ) . length ) )
357+ for ( let k in o )
358+ if ( new RegExp ( '(' + k + ')' ) . test ( fmt ) )
359+ fmt = fmt . replace ( RegExp . $1 , RegExp . $1 . length == 1 ? o [ k ] : ( '00' + o [ k ] ) . substr ( ( '' + o [ k ] ) . length ) )
289360 return fmt
290361 }
291362
0 commit comments