From c688344caf317b927ea78c8b35bf60b1cb810b80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A2=A8=E7=B6=A0BG?= Date: Sat, 31 Aug 2024 03:46:12 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9E=20fix=EF=BC=9A=E7=A7=BB=E9=99=A4?= =?UTF-8?q?=E9=83=A8=E4=BB=BD=20ESLint=20=E8=AD=A6=E5=91=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/common/api/handleLogin.ts | 44 +++++++++--------------------- src/common/hooks/useTokenCheck.ts | 45 ------------------------------- src/common/utils/checkToken.ts | 10 +++---- src/common/utils/fetch.ts | 28 ++++++++++--------- 4 files changed, 32 insertions(+), 95 deletions(-) delete mode 100644 src/common/hooks/useTokenCheck.ts diff --git a/src/common/api/handleLogin.ts b/src/common/api/handleLogin.ts index bb4e393..e24077e 100644 --- a/src/common/api/handleLogin.ts +++ b/src/common/api/handleLogin.ts @@ -2,26 +2,16 @@ import Taro from '@tarojs/taro'; const preUrl = 'https://kstack.muxixyz.com'; -interface LoginResponseHeaders { +type LoginResponseHeaders = { 'X-Jwt-Token'?: string; 'X-Refresh-Token'?: string; -} +}; const handleLogin = async (data: Record = {}) => { const header = { 'Content-Type': 'application/json;charset=utf-8', }; - await Taro.setStorage({ - key: 'shortToken', - data: 'shortToken', - }); - - await Taro.setStorage({ - key: 'longToken', - data: 'longToken', - }); - try { const response = await Taro.request({ method: 'POST', @@ -35,46 +25,38 @@ const handleLogin = async (data: Record = {}) => { const longToken = headers['X-Refresh-Token']; if (shortToken && longToken) { - await Taro.setStorage({ + void Taro.setStorage({ key: 'shortToken', data: shortToken.toString(), - success: () => { - console.log('shortToken 设置成功'); - // 方便看情况 log 出 shortToken 后期上线之前删除掉这个 - console.log(shortToken); - }, }); - await Taro.setStorage({ + void Taro.setStorage({ key: 'longToken', data: longToken.toString(), - success: () => { - console.log('longToken 设置成功'); - // 方便看情况 log 出 longToken 后期上线之前删除掉这个 - console.log(longToken); - }, }); - await Taro.switchTab({ + void Taro.switchTab({ url: '/pages/main/index', }); } // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access if (response.data.code !== 0) { - console.log('登陆失败(code 不为 0)'); - await Taro.showToast({ + // 登陆失败(code 不为 0) + void Taro.showToast({ icon: 'error', - // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-unsafe-member-access - title: response.data.msg, + title: '登录失败,请重试', }); } if (!response.statusCode.toString().startsWith('2')) { - throw new Error(`${response.statusCode}`); + throw new Error(`请求失败,状态码:${response.statusCode}`); } } catch (error) { - console.log(error); + void Taro.showToast({ + icon: 'error', + title: (error as Error).message || '登录过程中发生错误', + }); } }; diff --git a/src/common/hooks/useTokenCheck.ts b/src/common/hooks/useTokenCheck.ts deleted file mode 100644 index 7e07333..0000000 --- a/src/common/hooks/useTokenCheck.ts +++ /dev/null @@ -1,45 +0,0 @@ -import Taro from '@tarojs/taro'; - -const useTokenCheck = () => { - const checkToken = () => { - const token = void Taro.getStorageSync('token'); - if (!token) { - void Taro.login({ - success: (res) => { - if (res.code) { - void Taro.request({ - // 改一下 - url: '/api/v1/login', - data: { - code: res.code, - }, - header: { - 'Content-Type': 'application/json', - }, - method: 'POST', - }).then((response) => { - if (response.statusCode === 200) { - // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access - Taro.setStorageSync('token', response.data.data); - - // eslint-disable-next-line no-console - console.log('登录成功!'); - } - }); - } else { - // eslint-disable-next-line no-console - console.error('登录失败!' + res.errMsg); - } - }, - fail: () => { - // eslint-disable-next-line no-console - console.error('登录操作被用户拒绝'); - }, - }); - } - }; - - return checkToken; -}; - -export default useTokenCheck; diff --git a/src/common/utils/checkToken.ts b/src/common/utils/checkToken.ts index b205e44..cbf4e13 100644 --- a/src/common/utils/checkToken.ts +++ b/src/common/utils/checkToken.ts @@ -1,15 +1,13 @@ import Taro from '@tarojs/taro'; -function checkToken() { +const checkToken = () => { const token: string = Taro.getStorageSync('shortToken'); + if (token) { - // eslint-disable-next-line no-console - console.log('Token 有'); void Taro.switchTab({ url: '/pages/main/index' }); } else { - // eslint-disable-next-line no-console - console.log('无token先登陆'); void Taro.redirectTo({ url: '/pages/login/index' }); } -} +}; + export default checkToken; diff --git a/src/common/utils/fetch.ts b/src/common/utils/fetch.ts index 50ca42e..c34360a 100644 --- a/src/common/utils/fetch.ts +++ b/src/common/utils/fetch.ts @@ -1,8 +1,4 @@ -/* eslint-disable no-console */ /* eslint-disable @typescript-eslint/no-unsafe-return */ -/* eslint-disable @typescript-eslint/no-base-to-string */ -/* eslint-disable @typescript-eslint/restrict-template-expressions */ -/* eslint-disable @typescript-eslint/no-unsafe-assignment */ import Taro from '@tarojs/taro'; const preUrl = 'https://kstack.muxixyz.com'; @@ -17,7 +13,7 @@ export async function post(url = '', data = {}, isToken = true) { void Taro.getStorage({ key: 'shortToken', success: (res) => { - const token = res.data; + const token = res.data as string; if (token) { resolve(token); // 如果token存在,解析Promise } else { @@ -26,13 +22,13 @@ export async function post(url = '', data = {}, isToken = true) { } }, fail: (err) => { - reject(new Error(`Failed to get token: ${err}`)); // 存储操作失败时拒绝Promise + reject(new Error(`Failed to get token: ${err as unknown as string}`)); // 存储操作失败时拒绝Promise }, }); }); }; - if (isToken) header['Authorization'] = `Bearer ${await getToken()}`; + if (isToken) header['Authorization'] = `Bearer ${(await getToken()) as string}`; try { const response = await Taro.request({ @@ -53,6 +49,7 @@ export async function post(url = '', data = {}, isToken = true) { return response.data; } catch (error) { + // eslint-disable-next-line no-console console.log('error', error); throw error; } @@ -68,7 +65,8 @@ export async function get(url = '', isToken = true) { void Taro.getStorage({ key: 'shortToken', success: (res) => { - const token = res.data; + const token = res.data as string; + if (token) { resolve(token); // 如果token存在,解析Promise } else { @@ -77,20 +75,19 @@ export async function get(url = '', isToken = true) { } }, fail: (err) => { - reject(new Error(`Failed to get token: ${err}`)); // 存储操作失败时拒绝Promise + reject(new Error(`Failed to get token: ${err as unknown as string}`)); // 存储操作失败时拒绝Promise }, }); }); }; - if (isToken) header['Authorization'] = `Bearer ${await getToken()}`; + if (isToken) header['Authorization'] = `Bearer ${(await getToken()) as string}`; try { const response = await Taro.request({ url: `${preUrl}${url}`, method: 'GET', header, - // redirect: 'follow', }); if (!response.statusCode.toString().startsWith('2')) { @@ -104,6 +101,7 @@ export async function get(url = '', isToken = true) { return response.data; } catch (error) { + // eslint-disable-next-line no-console console.log('error', error); throw error; } @@ -118,7 +116,8 @@ export async function put(url = '', data = {}, isToken = true) { void Taro.getStorage({ key: 'shortToken', success: (res) => { - const token = res.data; + const token = res.data as string; + if (token) header['Authorization'] = token; else { void Taro.navigateTo({ url: '/pages/login/index' }); @@ -146,6 +145,7 @@ export async function put(url = '', data = {}, isToken = true) { return response.data; } catch (error) { + // eslint-disable-next-line no-console console.log('error', error); throw error; } @@ -175,6 +175,7 @@ export async function postPwd(url = '', data = {}, token: string) { return response.data; } catch (error) { + // eslint-disable-next-line no-console console.log('error', error); throw error; } @@ -189,7 +190,7 @@ export async function postLogin(url = '', data = {}, isToken = true) { void Taro.getStorage({ key: 'token', success: (res) => { - const token = res.data; + const token = res.data as string; if (token) header['Authorization'] = token; else { void Taro.navigateTo({ url: '/pages/login/index' }); @@ -217,6 +218,7 @@ export async function postLogin(url = '', data = {}, isToken = true) { return response.header; //返回相应体头部 } catch (error) { + // eslint-disable-next-line no-console console.log('error', error); throw error; }