MCPcopy Create free account
hub / github.com/badyun/AShare /

Class

utils/cloud.js:3–126  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1const superagent = require('superagent');
2const db = require('./db');
3module.exports = class {
4 constructor(id) {
5 this.info = db('account').find({ id: id }).value()
6 }
7
8 // 登录
9 async login(refresh_token) {
10 try {
11 if (!refresh_token) {
12 let s = await superagent.post('https://auth.aliyundrive.com/v2/account/token')
13 .send({
14 refresh_token: this.info.refresh_token,
15 grant_type: "refresh_token"
16 });
17
18 this.info = db('account').find({ id: this.info.id }).assign(s.body).write();
19 return this.info
20 } else {
21 let s = await superagent.post('https://auth.aliyundrive.com/v2/account/token')
22 .send({
23 refresh_token: refresh_token,
24 grant_type: "refresh_token"
25 });
26 let info = db('account').find({ user_id: s.body.user_id }).value()
27 if (!info) {
28 info = db('account').insert(s.body).write()
29 } else {
30 info = db('account').find({ user_id: s.body.user_id }).assign(s.body).write()
31 }
32 this.info = info
33 return this.info
34 }
35
36 } catch (error) {
37 // console.log(error)
38 return Promise.reject('refresh_token存在问题')
39 }
40
41 }
42
43 // 请求
44 async http(url, params = {}) {
45 return new Promise(async (resolve, reject) => {
46 try {
47 let s = await superagent.post(url)
48 .set({
49 authorization: 'Bearer ' + this.info.access_token,
50 referer: 'https://www.aliyundrive.com/',
51 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36'
52 })
53 .send(params)
54 resolve(s.body)
55 } catch (error) {
56 if (error.status == 401) {
57 await this.login()
58 return this.http(url, params)
59 } else {
60 reject(error.response.body.message)

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected