|
const USER_NAME = `[email protected]` |
|
const PASSWORD = `123456` |
|
|
|
let cookie = {} |
|
const json = $persistentStore.read('ssp') |
|
|
|
if (json) { |
|
cookie = JSON.parse(json) |
|
} |
|
|
|
let { value, expires } = cookie |
|
|
|
if (!cookie || !expires || expires < +Date.now()) { |
|
console.log('Cookie expires') |
|
$httpClient.post({ |
|
url: 'https://ssp-net.me/api/v1/passport/auth/login', |
|
headers: { |
|
'content-type': 'application/x-www-form-urlencoded' |
|
}, |
|
body: 'email=' + encodeURIComponent(USER_NAME) + '&password=' + encodeURIComponent(PASSWORD) |
|
}, function (error, response) { |
|
if (!error) { |
|
value = response.headers['Set-Cookie'].split(';')[0] |
|
$persistentStore.write('ssp', JSON.stringify({ value, expires: Date.now() + 7_200_000 })) |
|
getUsage() |
|
} |
|
}) |
|
} else { |
|
getUsage() |
|
} |
|
|
|
function getUsage() { |
|
$httpClient.get({ |
|
'url': 'https://ssp-net.me/api/v1/user/getSubscribe', |
|
headers: { cookie: value } |
|
}, function (error, response, data) { |
|
if (!error) { |
|
const usage = JSON.parse(data) |
|
const used = usage?.data?.u + usage?.data?.d |
|
const total = usage?.data?.transfer_enable |
|
const days = usage?.data?.reset_day |
|
const percentage = 100 * used / total |
|
const remain_days = Math.floor((usage?.data?.expired_at - Date.now() / 1000) / 86400) |
|
const style = percentage < 60 ? 'good' : percentage < 90 ? 'alert' : 'error'; |
|
const content = remain_days > days + 1 ? `${days} 日后重置流量` : `${remain_days} 日后订阅到期`; |
|
const title = `少数派 ${(used / (1<<30)).toFixed(2)} / ${(total / (1<<30)).toFixed(0)} GB` |
|
$done({ title, content, style }); |
|
} |
|
}) |
|
} |