From 3a8fc68d193d07fa48c4bdd406c69fe6d92560af Mon Sep 17 00:00:00 2001 From: Pylogmon Date: Sun, 7 Jan 2024 22:53:09 +0800 Subject: [PATCH] refactor: Code Format --- src/window/Config/pages/Backup/index.jsx | 114 ++--- .../Config/pages/Backup/utils/aliyun.jsx | 460 ++++++++++-------- 2 files changed, 281 insertions(+), 293 deletions(-) diff --git a/src/window/Config/pages/Backup/index.jsx b/src/window/Config/pages/Backup/index.jsx index 2e9f2e4c8c..6d3359fa5c 100644 --- a/src/window/Config/pages/Backup/index.jsx +++ b/src/window/Config/pages/Backup/index.jsx @@ -114,96 +114,52 @@ export default function Backup() { } }; - const refreshQrCode = async () => { - const res = await aliyun.qrcode(); - let sid = ''; - if (res.ok) { - const result = res.data; - if (result['qrCodeUrl']) { - setAliyunQrCodeUrl(result['qrCodeUrl']); - } else { - setAliyunQrCodeUrl(''); - toast.error(JSON.stringify(result), { style: toastStyle }); - } - if (result['sid']) { - sid = result['sid']; - } else { - sid = ''; - toast.error(JSON.stringify(result), { style: toastStyle }); - } - } else { - const result = res.data; - if (result['message']) { - toast.error(result['message'], { style: toastStyle }); - } else { - toast.error(JSON.stringify(result), { style: toastStyle }); - } - } - if (refreshTimer) { - clearInterval(refreshTimer); - } - if (sid === '') return; + const pollingStatus = async (sid) => { refreshTimer = setInterval(async () => { - const res = await aliyun.status(sid); - if (res.ok) { - const result = res.data; - if (result['status'] === 'QRCodeExpired') { - clearInterval(refreshTimer); - refreshQrCode(); - } - if (result['status'] === 'LoginSuccess') { - clearInterval(refreshTimer); - toast.success(t('config.backup.login_success'), { style: toastStyle }); - const res = await aliyun.accessToken(result['authCode']); - if (res.ok) { - const result = res.data; - if (result['access_token']) { - setAliyunAccessToken(result['access_token']); - } else { - toast.error(JSON.stringify(res), { style: toastStyle }); - refreshQrCode(); - } - // if (result['refresh_token']) { - // setAliyunRefreshToken(result['refresh_token']); - // } else { - // toast.error(JSON.stringify(res), { style: toastStyle }); - // refreshQrCode(); - // } - await refreshUserInfo(result['access_token']); - } else { - const result = res.data; - if (result['message']) { - toast.error(result['message'], { style: toastStyle }); - } else { - toast.error(JSON.stringify(result), { style: toastStyle }); - } + try { + const { status, code } = await aliyun.status(sid); + switch (status) { + case 'QRCodeExpired': { refreshQrCode(); + break; + } + case 'LoginSuccess': { + clearInterval(refreshTimer); + toast.success(t('config.backup.login_success'), { style: toastStyle }); + const token = await aliyun.accessToken(code); + setAliyunAccessToken(token); + await refreshUserInfo(token); + break; } } - } else { - const result = res.data; - if (result['message']) { - toast.error(result['message'], { style: toastStyle }); - } else { - toast.error(JSON.stringify(result), { style: toastStyle }); - } + } catch (e) { + toast.error(e.toString(), { style: toastStyle }); refreshQrCode(); } }, 2000); }; + const refreshQrCode = async () => { + try { + const { url, sid } = await aliyun.qrcode(); + setAliyunQrCodeUrl(url); + if (refreshTimer) { + clearInterval(refreshTimer); + } + pollingStatus(sid); + } catch (e) { + setAliyunQrCodeUrl(''); + toast.error(e.toString(), { style: toastStyle }); + } + }; + const refreshUserInfo = async (token) => { - const res = await aliyun.userInfo(token); - if (res.ok) { + try { + const info = await aliyun.userInfo(token); setAliyunQrCodeUrl(''); - setAliyunUserInfo(res.data); - } else { - const result = res.data; - if (result['message']) { - toast.error(result['message'], { style: toastStyle }); - } else { - toast.error(JSON.stringify(result), { style: toastStyle }); - } + setAliyunUserInfo(info); + } catch (e) { + toast.error(e.toString(), { style: toastStyle }); setAliyunAccessToken(''); refreshQrCode(); } diff --git a/src/window/Config/pages/Backup/utils/aliyun.jsx b/src/window/Config/pages/Backup/utils/aliyun.jsx index 1d87119585..20fa04187e 100644 --- a/src/window/Config/pages/Backup/utils/aliyun.jsx +++ b/src/window/Config/pages/Backup/utils/aliyun.jsx @@ -9,210 +9,183 @@ export async function backup(token, name) { operate: 'put', path: filePath, }); - const res = await fetch('https://openapi.alipan.com/adrive/v1.0/user/getDriveInfo', { + const drive_id = await driveId(token); + const dir_id = await createDir(token, drive_id); + const { file_id, upload_id, upload_url } = await createFile(token, drive_id, dir_id, name); + await invoke('aliyun', { operate: 'put', path: filePath, url: upload_url }); + await fetch('https://openapi.alipan.com/adrive/v1.0/openFile/complete', { + method: 'POST', + headers: { + Authorization: `Bearer ${token}`, + }, + body: Body.json({ + drive_id, + file_id, + upload_id, + }), + }); +} + +export async function list(token) { + const drive_id = await driveId(token); + const dir_id = await createDir(token, drive_id); + const res = await fetch('https://openapi.alipan.com/adrive/v1.0/openFile/list', { method: 'POST', headers: { Authorization: `Bearer ${token}`, }, + body: Body.json({ + drive_id, + parent_file_id: dir_id, + type: 'file', + order_by: 'name', + }), }); if (res.ok) { const result = res.data; - if (result['default_drive_id']) { - const drive_id = result['default_drive_id']; - const res = await fetch('https://openapi.alipan.com/adrive/v1.0/openFile/create', { - method: 'POST', - headers: { - Authorization: `Bearer ${token}`, - }, - body: Body.json({ - drive_id, - parent_file_id: 'root', - name: 'pot-app', - type: 'folder', - check_name_mode: 'refuse', - }), + if (result['items']) { + return result['items'].map((item) => { + return item['name']; }); - if (res.ok) { - const result = res.data; - if (result['file_id']) { - const parent_file_id = result['file_id']; - const res = await fetch('https://openapi.alipan.com/adrive/v1.0/openFile/create', { - method: 'POST', - headers: { - Authorization: `Bearer ${token}`, - }, - body: Body.json({ - drive_id, - parent_file_id, - name: name, - type: 'file', - check_name_mode: 'refuse', - }), - }); - if (res.ok) { - const result = res.data; - if (result['file_id']) { - const file_id = result['file_id']; - const upload_id = result['upload_id']; - const upload_url = result['part_info_list'][0]['upload_url']; - await invoke('aliyun', { operate: 'put', path: filePath, url: upload_url }); - await fetch('https://openapi.alipan.com/adrive/v1.0/openFile/complete', { - method: 'POST', - headers: { - Authorization: `Bearer ${token}`, - }, - body: Body.json({ - drive_id, - file_id, - upload_id, - }), - }); - } else { - throw new Error(`Can not find file_id: ${JSON.stringify(result)}`); - } - } else { - throw new Error(`Create file Error: ${JSON.stringify(res)}`); - } - } else { - throw new Error(`Can not find file_id: ${JSON.stringify(result)}`); - } - } else { - throw new Error(`Create folder Error: ${JSON.stringify(res)}`); - } } else { - throw new Error(`Can not find default_drive_id: ${JSON.stringify(result)}`); + throw new Error(`Get File List Error: ${JSON.stringify(result)}`); } } else { - throw new Error(`getDriveInfo Error: ${JSON.stringify(res)}`); + const result = res.data; + if (result['message']) { + throw new Error(result['message']); + } else { + throw new Error(`Get accessToken Error: ${JSON.stringify(result)}`); + } } } -export async function list(token) { - const res = await fetch('https://openapi.alipan.com/adrive/v1.0/user/getDriveInfo', { +export async function get(token, name) { + const drive_id = await driveId(token); + const file_id = await getFileByPath(token, drive_id, name); + const url = await getDownloadUrl(token, drive_id, file_id); + await invoke('aliyun', { operate: 'get', path: '', url }); +} + +export async function remove(token, name) { + const drive_id = await driveId(token); + const file_id = await getFileByPath(token, drive_id, name); + const res = await fetch('https://openapi.alipan.com/adrive/v1.0/openFile/delete', { method: 'POST', headers: { Authorization: `Bearer ${token}`, }, + body: Body.json({ + drive_id, + file_id, + }), }); if (res.ok) { + return; + } else { const result = res.data; - if (result['default_drive_id']) { - const drive_id = result['default_drive_id']; - const res = await fetch('https://openapi.alipan.com/adrive/v1.0/openFile/create', { - method: 'POST', - headers: { - Authorization: `Bearer ${token}`, - }, - body: Body.json({ - drive_id, - parent_file_id: 'root', - name: 'pot-app', - type: 'folder', - check_name_mode: 'refuse', - }), - }); - if (res.ok) { - const result = res.data; - if (result['file_id']) { - const file_id = result['file_id']; - const res = await fetch('https://openapi.alipan.com/adrive/v1.0/openFile/list', { - method: 'POST', - headers: { - Authorization: `Bearer ${token}`, - }, - body: Body.json({ - drive_id, - parent_file_id: file_id, - type: 'file', - order_by: 'name', - }), - }); - if (res.ok) { - const result = res.data; - if (result['items']) { - return result['items'].map((item) => { - return item['name']; - }); - } else { - throw new Error(`Get File List Error: ${JSON.stringify(result)}`); - } - } else { - throw new Error(`Get File List Error: ${JSON.stringify(res)}`); - } - } else { - throw new Error(`Can not find file_id: ${JSON.stringify(result)}`); - } - } else { - throw new Error(`Create folder Error: ${JSON.stringify(res)}`); - } + if (result['message']) { + throw new Error(result['message']); } else { - throw new Error(`Can not find default_drive_id: ${JSON.stringify(result)}`); + throw new Error(`Get accessToken Error: ${JSON.stringify(result)}`); } - } else { - throw new Error(`getDriveInfo Error: ${JSON.stringify(res)}`); } } -export async function get(token, name) { - const res = await fetch('https://openapi.alipan.com/adrive/v1.0/user/getDriveInfo', { +export async function qrcode() { + const res = await fetch('https://openapi.alipan.com/oauth/authorize/qrcode', { method: 'POST', + body: Body.json({ + client_id: 'bf56dd2dc03a4d3489e3dda05dd6d466', + scopes: ['user:base', 'file:all:read', 'file:all:write'], + }), + }); + if (res.ok) { + const result = res.data; + if (result['qrCodeUrl'] && result['sid']) { + return { url: result['qrCodeUrl'], sid: result['sid'] }; + } else { + throw new Error(`Can not find qrCodeUrl: ${JSON.stringify(result)}`); + } + } else { + const result = res.data; + if (result['message']) { + throw new Error(result['message']); + } else { + throw new Error(`Get QrCode Error: ${JSON.stringify(result)}`); + } + } +} + +export async function status(sid) { + const res = await fetch(`https://openapi.alipan.com/oauth/qrcode/${sid}/status`); + + if (res.ok) { + const result = res.data; + if (result['status']) { + return { status: result['status'], code: result['authCode'] }; + } else { + throw new Error(`Can not find status: ${JSON.stringify(result)}`); + } + } else { + const result = res.data; + if (result['message']) { + throw new Error(result['message']); + } else { + throw new Error(`Get Status Error: ${JSON.stringify(result)}`); + } + } +} + +export async function userInfo(token) { + const res = await fetch('https://openapi.alipan.com/oauth/users/info', { headers: { Authorization: `Bearer ${token}`, }, }); if (res.ok) { const result = res.data; - if (result['default_drive_id']) { - const drive_id = result['default_drive_id']; - const res = await fetch('https://openapi.alipan.com/adrive/v1.0/openFile/get_by_path', { - method: 'POST', - headers: { - Authorization: `Bearer ${token}`, - }, - body: Body.json({ - drive_id, - file_path: `/pot-app/${name}`, - }), - }); - if (res.ok) { - const result = res.data; - if (result['file_id']) { - const file_id = result['file_id']; - const res = await fetch('https://openapi.alipan.com/adrive/v1.0/openFile/getDownloadUrl', { - method: 'POST', - headers: { - Authorization: `Bearer ${token}`, - }, - body: Body.json({ - drive_id, - file_id, - }), - }); - if (res.ok) { - const result = res.data; - if (result['url']) { - await invoke('aliyun', { operate: 'get', path: '', url: result['url'] }); - } else { - throw new Error(`Get Download Url Error: ${JSON.stringify(result)}`); - } - } else { - throw new Error(`Get Download Url Error: ${JSON.stringify(res)}`); - } - } else { - throw new Error(`Can not find file_id: ${JSON.stringify(result)}`); - } - } else { - throw new Error(`Get file_id Error: ${JSON.stringify(res)}`); - } + if (result['avatar'] && result['name']) { + return { avatar: result['avatar'], name: result['name'] }; } else { - throw new Error(`Can not find default_drive_id: ${JSON.stringify(result)}`); + throw new Error(`Can not find avatar or name: ${JSON.stringify(result)}`); } } else { - throw new Error(`getDriveInfo Error: ${JSON.stringify(res)}`); + const result = res.data; + if (result['message']) { + throw new Error(result['message']); + } else { + throw new Error(`Get UserInfo Error: ${JSON.stringify(result)}`); + } } } -export async function remove(token, name) { +export async function accessToken(code) { + const res = await fetch('https://pot-app.com/api/ali_access_token', { + method: 'POST', + body: Body.json({ + code, + refresh_token: '', + }), + }); + if (res.ok) { + const result = res.data; + if (result['access_token']) { + return result['access_token']; + } else { + throw new Error(`Can not find access_token: ${JSON.stringify(result)}`); + } + } else { + const result = res.data; + if (result['message']) { + throw new Error(result['message']); + } else { + throw new Error(`Get accessToken Error: ${JSON.stringify(result)}`); + } + } +} + +async function driveId(token) { const res = await fetch('https://openapi.alipan.com/adrive/v1.0/user/getDriveInfo', { method: 'POST', headers: { @@ -222,78 +195,137 @@ export async function remove(token, name) { if (res.ok) { const result = res.data; if (result['default_drive_id']) { - const drive_id = result['default_drive_id']; - const res = await fetch('https://openapi.alipan.com/adrive/v1.0/openFile/get_by_path', { - method: 'POST', - headers: { - Authorization: `Bearer ${token}`, - }, - body: Body.json({ - drive_id, - file_path: `/pot-app/${name}`, - }), - }); - if (res.ok) { - const result = res.data; - if (result['file_id']) { - const file_id = result['file_id']; - const res = await fetch('https://openapi.alipan.com/adrive/v1.0/openFile/delete', { - method: 'POST', - headers: { - Authorization: `Bearer ${token}`, - }, - body: Body.json({ - drive_id, - file_id, - }), - }); - if (res.ok) { - return; - } else { - throw new Error(`Delete file Error: ${JSON.stringify(res)}`); - } - } else { - throw new Error(`Can not find file_id: ${JSON.stringify(result)}`); - } - } else { - throw new Error(`Get file_id Error: ${JSON.stringify(res)}`); - } + return result['default_drive_id']; } else { throw new Error(`Can not find default_drive_id: ${JSON.stringify(result)}`); } } else { - throw new Error(`getDriveInfo Error: ${JSON.stringify(res)}`); + const result = res.data; + if (result['message']) { + throw new Error(result['message']); + } else { + throw new Error(`Get accessToken Error: ${JSON.stringify(result)}`); + } } } -export async function qrcode() { - return await fetch('https://openapi.alipan.com/oauth/authorize/qrcode', { +async function createDir(token, drive_id) { + const res = await fetch('https://openapi.alipan.com/adrive/v1.0/openFile/create', { method: 'POST', + headers: { + Authorization: `Bearer ${token}`, + }, body: Body.json({ - client_id: 'bf56dd2dc03a4d3489e3dda05dd6d466', - scopes: ['user:base', 'file:all:read', 'file:all:write'], + drive_id, + parent_file_id: 'root', + name: 'pot-app', + type: 'folder', + check_name_mode: 'refuse', }), }); + if (res.ok) { + const result = res.data; + if (result['file_id']) { + return result['file_id']; + } else { + throw new Error(`Can not find file_id: ${JSON.stringify(result)}`); + } + } else { + const result = res.data; + if (result['message']) { + throw new Error(result['message']); + } else { + throw new Error(`Get accessToken Error: ${JSON.stringify(result)}`); + } + } } -export async function status(sid) { - return await fetch(`https://openapi.alipan.com/oauth/qrcode/${sid}/status`); +async function createFile(token, drive_id, dir_id, name) { + const res = await fetch('https://openapi.alipan.com/adrive/v1.0/openFile/create', { + method: 'POST', + headers: { + Authorization: `Bearer ${token}`, + }, + body: Body.json({ + drive_id, + parent_file_id: dir_id, + name: name, + type: 'file', + check_name_mode: 'refuse', + }), + }); + if (res.ok) { + const result = res.data; + if (result['file_id']) { + const file_id = result['file_id']; + const upload_id = result['upload_id']; + const upload_url = result['part_info_list'][0]['upload_url']; + return { file_id, upload_id, upload_url }; + } else { + throw new Error(`Get file_id Error: ${JSON.stringify(result)}`); + } + } else { + const result = res.data; + if (result['message']) { + throw new Error(result['message']); + } else { + throw new Error(`Get accessToken Error: ${JSON.stringify(result)}`); + } + } } -export async function userInfo(token) { - return await fetch('https://openapi.alipan.com/oauth/users/info', { +async function getFileByPath(token, drive_id, name) { + const res = await fetch('https://openapi.alipan.com/adrive/v1.0/openFile/get_by_path', { + method: 'POST', headers: { Authorization: `Bearer ${token}`, }, + body: Body.json({ + drive_id, + file_path: `/pot-app/${name}`, + }), }); + if (res.ok) { + const result = res.data; + if (result['file_id']) { + return result['file_id']; + } else { + throw new Error(`Can not find file_id: ${JSON.stringify(result)}`); + } + } else { + const result = res.data; + if (result['message']) { + throw new Error(result['message']); + } else { + throw new Error(`Get accessToken Error: ${JSON.stringify(result)}`); + } + } } -export async function accessToken(code) { - return await fetch('https://pot-app.com/api/ali_access_token', { +async function getDownloadUrl(token, drive_id, file_id) { + const res = await fetch('https://openapi.alipan.com/adrive/v1.0/openFile/getDownloadUrl', { method: 'POST', + headers: { + Authorization: `Bearer ${token}`, + }, body: Body.json({ - code, - refresh_token: '', + drive_id, + file_id, }), }); + if (res.ok) { + const result = res.data; + if (result['url']) { + return result['url']; + } else { + throw new Error(`Can not find url: ${JSON.stringify(result)}`); + } + } else { + const result = res.data; + if (result['message']) { + throw new Error(result['message']); + } else { + throw new Error(`Get accessToken Error: ${JSON.stringify(result)}`); + } + } }