Skip to content

Commit

Permalink
🐛 修复网盘登录掉了之后无法重新登录的问题 #210
Browse files Browse the repository at this point in the history
  • Loading branch information
CodFrm committed Jun 28, 2023
1 parent fc6d14d commit 963a6d2
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
14 changes: 10 additions & 4 deletions pkg/filesystem/auth.ts
Expand Up @@ -60,14 +60,15 @@ export type Token = {
createtime: number;
};

export async function AuthVerify(netDiskType: NetDiskType, reapply?: boolean) {
export async function AuthVerify(netDiskType: NetDiskType, invalid?: boolean) {
let token: Token | undefined;
try {
token = JSON.parse(localStorage[`netdisk:token:${netDiskType}`]);
} catch (e) {
// ignore
}
if (reapply || !token || !token.accessToken) {
// token不存在,或者没有accessToken,重新获取
if (!token || !token.accessToken) {
// 强制重新获取token
await NetDisk(netDiskType);
const resp = await GetNetDiskToken(netDiskType);
Expand All @@ -79,15 +80,20 @@ export async function AuthVerify(netDiskType: NetDiskType, reapply?: boolean) {
refreshToken: resp.data.token.refresh_token,
createtime: Date.now(),
};
invalid = false;
localStorage[`netdisk:token:${netDiskType}`] = JSON.stringify(token);
}
if (Date.now() >= token.createtime + 3600000) {
// token过期或者失效
if (Date.now() >= token.createtime + 3600000 || invalid) {
// 大于一小时刷新token
try {
const resp = await RefreshToken(netDiskType, token.refreshToken);
if (resp.code !== 0) {
// 刷新失败删除token
localStorage.removeItem(`netdisk:token:${netDiskType}`);
// 刷新失败,并且标记失效,尝试重新获取token
if (invalid) {
return AuthVerify(netDiskType);
}
return Promise.reject(new Error(resp.msg));
}
token = {
Expand Down
10 changes: 6 additions & 4 deletions pkg/filesystem/baidu/baidu.ts
Expand Up @@ -22,7 +22,7 @@ export default class BaiduFileSystem implements FileSystem {
async verify(): Promise<void> {
const token = await AuthVerify("baidu");
this.accessToken = token;
return Promise.resolve();
return this.list().then();
}

open(file: File): Promise<FileReader> {
Expand Down Expand Up @@ -78,12 +78,14 @@ export default class BaiduFileSystem implements FileSystem {
return fetch(url, config)
.then((data) => data.json())
.then(async (data) => {
if (data.errno === 111) {
await this.verify();
if (data.errno === 111 || data.errno === -6) {
const token = await AuthVerify("baidu", true);
this.accessToken = token;
url = url.replace(/access_token=[^&]+/, `access_token=${token}`);
return fetch(url, config)
.then((data2) => data2.json())
.then((data2) => {
if (data2.errno === 111) {
if (data2.errno === 111 || data2.errno === -6) {
throw new Error(JSON.stringify(data2));
}
return data2;
Expand Down
19 changes: 16 additions & 3 deletions pkg/filesystem/onedrive/onedrive.ts
Expand Up @@ -22,7 +22,7 @@ export default class OneDriveFileSystem implements FileSystem {
async verify(): Promise<void> {
const token = await AuthVerify("onedrive");
this.accessToken = token;
return Promise.resolve();
return this.list().then();
}

open(file: File): Promise<FileReader> {
Expand Down Expand Up @@ -73,7 +73,7 @@ export default class OneDriveFileSystem implements FileSystem {
"@microsoft.graph.conflictBehavior": "replace",
}),
}
).then((data) => {
).then((data: any) => {
if (data.errno) {
throw new Error(JSON.stringify(data));
}
Expand All @@ -85,7 +85,6 @@ export default class OneDriveFileSystem implements FileSystem {
request(url: string, config?: RequestInit, nothen?: boolean) {
config = config || {};
const headers = <Headers>config.headers || new Headers();
// 利用GM函数的匿名实现不发送cookie,因为某些情况cookie会导致-6错误
headers.append(`Authorization`, `Bearer ${this.accessToken}`);
config.headers = headers;
const ret = fetch(url, config);
Expand All @@ -96,6 +95,19 @@ export default class OneDriveFileSystem implements FileSystem {
.then((data) => data.json())
.then(async (data) => {
if (data.error) {
if (data.error.code === "InvalidAuthenticationToken") {
const token = await AuthVerify("onedrive", true);
this.accessToken = token;
headers.set(`Authorization`, `Bearer ${this.accessToken}`);
return fetch(url, config)
.then((retryData) => retryData.json())
.then((retryData) => {
if (retryData.error) {
throw new Error(JSON.stringify(retryData));
}
return data;
});
}
throw new Error(JSON.stringify(data));
}
return data;
Expand Down Expand Up @@ -128,6 +140,7 @@ export default class OneDriveFileSystem implements FileSystem {
return this.request(
`https://graph.microsoft.com/v1.0/me/drive/special/approot:${path}:/children`
).then((data) => {
console.log(data);

Check warning on line 143 in pkg/filesystem/onedrive/onedrive.ts

View workflow job for this annotation

GitHub Actions / Build

Unexpected console statement
const list: File[] = [];
data.value.forEach((val: any) => {
list.push({
Expand Down

0 comments on commit 963a6d2

Please sign in to comment.