Skip to content

Commit

Permalink
🐞 fix:移除部份 ESLint 警告
Browse files Browse the repository at this point in the history
  • Loading branch information
BlackishGreen33 committed Aug 30, 2024
1 parent 8a6a664 commit c688344
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 95 deletions.
44 changes: 13 additions & 31 deletions src/common/api/handleLogin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, unknown> = {}) => {
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',
Expand All @@ -35,46 +25,38 @@ const handleLogin = async (data: Record<string, unknown> = {}) => {
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 || '登录过程中发生错误',
});
}
};

Expand Down
45 changes: 0 additions & 45 deletions src/common/hooks/useTokenCheck.ts

This file was deleted.

10 changes: 4 additions & 6 deletions src/common/utils/checkToken.ts
Original file line number Diff line number Diff line change
@@ -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;
28 changes: 15 additions & 13 deletions src/common/utils/fetch.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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 {
Expand All @@ -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({
Expand All @@ -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;
}
Expand All @@ -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 {
Expand All @@ -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')) {
Expand All @@ -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;
}
Expand All @@ -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' });
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -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' });
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit c688344

Please sign in to comment.