|
| 1 | +const DUOKAN_COOKIE_KEY = 'duokan_cookie' |
| 2 | +const DUOKAN_DEVICE_ID_KEY = 'duokan_device_id' |
| 3 | +const API_HOST = 'https://www.duokan.com' |
| 4 | +const TASK_NAME = '多看阅读' |
| 5 | + |
| 6 | +let $util = init() |
| 7 | + |
| 8 | +;(async () => { |
| 9 | + let cookieVal = $util.getdata(DUOKAN_COOKIE_KEY) |
| 10 | + let deviceId = $util.getdata(DUOKAN_DEVICE_ID_KEY) |
| 11 | + if (!cookieVal || !deviceId) { |
| 12 | + $util.msg(TASK_NAME, '⚠️ 请先获取 Cookie') |
| 13 | + $util.done({}) |
| 14 | + return |
| 15 | + } |
| 16 | + await checkin(cookieVal, deviceId).then(() => { |
| 17 | + $util.done({}) |
| 18 | + }) |
| 19 | +})() |
| 20 | + |
| 21 | +function checkin(cookieVal, deviceId) { |
| 22 | + return new Promise((resolve, reject) => { |
| 23 | + let options = { |
| 24 | + url: `${API_HOST}/checkin/v0/checkin`, |
| 25 | + headers: { |
| 26 | + 'User-Agent': |
| 27 | + 'Mozilla/5.0 (iPhone; CPU iPhone OS 13_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148', |
| 28 | + Cookie: cookieVal |
| 29 | + }, |
| 30 | + body: signature(deviceId) |
| 31 | + } |
| 32 | + $util.post(options, (error, response, data) => { |
| 33 | + if (error) { |
| 34 | + $util.log(`签到失败,error:${error}`) |
| 35 | + $util.msg(TASK_NAME, '⚠️ 签到失败,详情请查看日志') |
| 36 | + resolve() |
| 37 | + return |
| 38 | + } |
| 39 | + let result = JSON.parse(data) |
| 40 | + if (result && result.result === 0 && result.data) { |
| 41 | + $util.log(`签到成功,response: ${data}`) |
| 42 | + let subtitle = `签到成功,已连续签到 ${result.data.today} 天` |
| 43 | + let body = '' |
| 44 | + if (result.data.gift && Array.isArray(result.data.gift) && result.data.gift.length > 0) { |
| 45 | + body = result.data.gift.reduce((prev, cur) => { |
| 46 | + return (prev += `获得 ${cur.value} 个${cur.name} \n`) |
| 47 | + }, '') |
| 48 | + } |
| 49 | + $util.msg(TASK_NAME, subtitle, body) |
| 50 | + resolve() |
| 51 | + } else { |
| 52 | + $util.log(`签到失败,response: ${data}`) |
| 53 | + $util.msg(TASK_NAME, `⚠️ 签到失败,${result.msg}`) |
| 54 | + resolve() |
| 55 | + } |
| 56 | + }) |
| 57 | + }) |
| 58 | +} |
| 59 | + |
| 60 | +function signature(deviceId) { |
| 61 | + let t = parseInt(new Date().getTime() / 1000) |
| 62 | + let c = 0 |
| 63 | + for (char of `${deviceId}&${t}`) { |
| 64 | + c = (c * 131 + char.charCodeAt(0)) % 65536 |
| 65 | + } |
| 66 | + return `_t=${t}&_c=${c}` |
| 67 | +} |
| 68 | + |
| 69 | +function init() { |
| 70 | + isSurge = () => { |
| 71 | + return undefined !== this.$httpClient |
| 72 | + } |
| 73 | + isQuanX = () => { |
| 74 | + return undefined !== this.$task |
| 75 | + } |
| 76 | + getdata = (key) => { |
| 77 | + if (isSurge()) return $persistentStore.read(key) |
| 78 | + if (isQuanX()) return $prefs.valueForKey(key) |
| 79 | + } |
| 80 | + setdata = (key, val) => { |
| 81 | + if (isSurge()) return $persistentStore.write(key, val) |
| 82 | + if (isQuanX()) return $prefs.setValueForKey(key, val) |
| 83 | + } |
| 84 | + msg = (title, subtitle = '', body = '') => { |
| 85 | + if (isSurge()) $notification.post(title, subtitle, body) |
| 86 | + if (isQuanX()) $notify(title, subtitle, body) |
| 87 | + } |
| 88 | + log = (msg) => { |
| 89 | + console.log(`${msg}\n`) |
| 90 | + } |
| 91 | + get = (options, callback) => { |
| 92 | + if (isQuanX()) { |
| 93 | + if (typeof options == 'string') options = { url: options } |
| 94 | + options['method'] = 'GET' |
| 95 | + return $task.fetch(options).then( |
| 96 | + (response) => { |
| 97 | + response['status'] = response.statusCode |
| 98 | + callback(null, response, response.body) |
| 99 | + }, |
| 100 | + (reason) => callback(reason.error, null, null) |
| 101 | + ) |
| 102 | + } |
| 103 | + if (isSurge()) return $httpClient.get(options, callback) |
| 104 | + } |
| 105 | + post = (options, callback) => { |
| 106 | + if (isQuanX()) { |
| 107 | + if (typeof options == 'string') options = { url: options } |
| 108 | + options['method'] = 'POST' |
| 109 | + $task.fetch(options).then( |
| 110 | + (response) => { |
| 111 | + response['status'] = response.statusCode |
| 112 | + callback(null, response, response.body) |
| 113 | + }, |
| 114 | + (reason) => callback(reason.error, null, null) |
| 115 | + ) |
| 116 | + } |
| 117 | + if (isSurge()) $httpClient.post(options, callback) |
| 118 | + } |
| 119 | + done = (value = {}) => { |
| 120 | + $done(value) |
| 121 | + } |
| 122 | + return { isSurge, isQuanX, msg, log, getdata, setdata, get, post, done } |
| 123 | +} |
0 commit comments