|
| 1 | +function Env(name) { |
| 2 | + this.name = name |
| 3 | + this.logs = [] |
| 4 | + this.isSurge = () => 'undefined' !== typeof $httpClient |
| 5 | + this.isQuanX = () => 'undefined' !== typeof $task |
| 6 | + this.log = (...log) => { |
| 7 | + this.logs = [...this.logs, ...log] |
| 8 | + if (log) console.log(log.join('\n')) |
| 9 | + else console.log(this.logs.join('\n')) |
| 10 | + } |
| 11 | + this.msg = (title = this.name, subt = '', desc = '') => { |
| 12 | + if (this.isSurge()) $notification.post(title, subt, desc) |
| 13 | + if (this.isQuanX()) $notify(title, subt, desc) |
| 14 | + this.log('==============📣系统通知📣==============') |
| 15 | + if (title) this.log(title) |
| 16 | + if (subt) this.log(subt) |
| 17 | + if (desc) this.log(desc) |
| 18 | + } |
| 19 | + this.getdata = (key) => { |
| 20 | + if (this.isSurge()) return $persistentStore.read(key) |
| 21 | + if (this.isQuanX()) return $prefs.valueForKey(key) |
| 22 | + } |
| 23 | + this.setdata = (val, key) => { |
| 24 | + if (this.isSurge()) return $persistentStore.write(val, key) |
| 25 | + if (this.isQuanX()) return $prefs.setValueForKey(val, key) |
| 26 | + } |
| 27 | + this.get = (url, callback) => this.send(url, 'GET', callback) |
| 28 | + this.wait = (min, max = min) => (resove) => setTimeout(() => resove(), Math.floor(Math.random() * (max - min + 1) + min)) |
| 29 | + this.post = (url, callback) => this.send(url, 'POST', callback) |
| 30 | + this.send = (url, method, callback) => { |
| 31 | + if (this.isSurge()) { |
| 32 | + const __send = method == 'POST' ? $httpClient.post : $httpClient.get |
| 33 | + __send(url, (error, response, data) => { |
| 34 | + response.body = data |
| 35 | + response.statusCode = response.status |
| 36 | + callback(error, response, data) |
| 37 | + }) |
| 38 | + } |
| 39 | + if (this.isQuanX()) { |
| 40 | + url.method = method |
| 41 | + $task.fetch(url).then( |
| 42 | + (response) => { |
| 43 | + response.status = response.statusCode |
| 44 | + callback(null, response, response.body) |
| 45 | + }, |
| 46 | + (reason) => callback(reason.error, reason, reason) |
| 47 | + ) |
| 48 | + } |
| 49 | + } |
| 50 | + this.done = (value = {}) => $done(value) |
| 51 | +} |
0 commit comments