From 4ea422369585e84f52b2959af1071f86331c45b7 Mon Sep 17 00:00:00 2001 From: Tony Date: Tue, 26 Mar 2024 14:34:50 +0800 Subject: [PATCH] =?UTF-8?q?fix::=20=E8=AF=B7=E6=B1=82=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../customized/tabWrapper/index.tsx | 6 ++++- src/config/axios/index.ts | 8 +++--- src/config/index.ts | 3 ++- src/pages/login/index.tsx | 25 +++++++++++++------ 4 files changed, 27 insertions(+), 15 deletions(-) diff --git a/src/components/customized/tabWrapper/index.tsx b/src/components/customized/tabWrapper/index.tsx index ff74a9d..b1d17cf 100644 --- a/src/components/customized/tabWrapper/index.tsx +++ b/src/components/customized/tabWrapper/index.tsx @@ -1,5 +1,6 @@ import { View } from "@tarojs/components" import './index.css' +import Taro from "@tarojs/taro" /** * options 为菜单列表 selected为选中值 callback为函数回调 @@ -16,7 +17,10 @@ const TabWrapper = ({ options, selectedId = '', callback }) => { className={`px-3 py-1 ${selectedId === item.id ? 'selected-tab' : ''}`} key={item.id} style={{ color: 'white' }} - onClick={() => callback(item.id)} + onClick={() => { + Taro.vibrateShort({ type: 'light' }) + callback(item.id) + }} >{ item.title } ) }) diff --git a/src/config/axios/index.ts b/src/config/axios/index.ts index b4f0cfc..36bf737 100644 --- a/src/config/axios/index.ts +++ b/src/config/axios/index.ts @@ -7,10 +7,10 @@ export const request = ({ baseUrl = axiosConfig.baseUrl, data = {}, method = 'GET', - header = { - 'Authorization': Taro.getStorageSync('token') - } + header = {}, + isToken = true }): Promise => { + if (isToken) header['Authorization'] = 'Bearer ' + Taro.getStorageSync('token') let requestParams = '?' if (method === 'GET') requestParams += stringify(data, { indices: false }) console.log('requestParams', requestParams); @@ -23,11 +23,9 @@ export const request = ({ method: method as any, header, success: function (res) { - console.log('接口返回', res); resolve(res.data) }, fail: function(err) { - console.error('err', err); reject(err) }, complete: function () { diff --git a/src/config/index.ts b/src/config/index.ts index 47d0c4e..c86cd50 100644 --- a/src/config/index.ts +++ b/src/config/index.ts @@ -1,5 +1,6 @@ export const axiosConfig = { - baseUrl: 'http://117.73.12.97:1606/admin-api/', + // baseUrl: 'https://e61141561w.vicp.fun/admin-api', + baseUrl: 'http://127.0.0.1:48080/admin-api', timeout: 30000, withCredentials: false // 禁用 Cookie 等信息 } diff --git a/src/pages/login/index.tsx b/src/pages/login/index.tsx index bc21806..76f93f7 100644 --- a/src/pages/login/index.tsx +++ b/src/pages/login/index.tsx @@ -2,7 +2,7 @@ import { Button, Image, Input, Text, View } from "@tarojs/components" import loginBg from '../../assets/images/loginBg.png' import questionIcon from '../../assets/images/icons/question-line.png' import Taro from "@tarojs/taro" -import { useState } from "react" +import { useEffect, useState } from "react" import { request } from "../../config/axios" const login = (data, tenantId = '1') => { @@ -10,7 +10,8 @@ const login = (data, tenantId = '1') => { url: '/system/auth/login', method: 'POST', header: { 'Tenant-Id': tenantId }, - data + data, + isToken: false }) } @@ -18,7 +19,8 @@ const getTenantId = (data) => { return request({ url: '/system/tenant/get-id-by-name', method: 'GET', - data + data, + isToken: false }) } @@ -26,13 +28,15 @@ const autoLoginCheck = async () => { const expireTime = Taro.getStorageSync('expiresTime') const nowTime = new Date().valueOf() console.log('expireTime', expireTime); + console.log('nowTime', nowTime); + console.log('expireTime', expireTime - nowTime); if (expireTime) { - if (expireTime - nowTime > 1000 * 60 * 60 * 8) { - // 过期时间大于八小时 + if ((nowTime - expireTime) > 1000 * 60 * 15) { + // 过期时间大于15分钟 Taro.redirectTo({ url: '/basePage/index/index' }) } else { - // 过期时间不足 八小时 + // 过期时间不足 15分钟 const tenantName = Taro.getStorageSync('tenantName') if (!tenantName) return const { data: _tenantId } = await getTenantId({ name: tenantName }) @@ -82,7 +86,11 @@ const Login = () => { Taro.showLoading({ title: '加载中...'}) - const { data: _tenantId } = await getTenantId({ name: tenantName }) + const { data: _tenantId } = await getTenantId({ name: tenantName }).catch(error => { + // 这里不该这样写,因为实机不能访问接口,先这样 + // Taro.setStorageSync('token', 'data.accessToken') + // Taro.redirectTo({ url: '/basePage/index/index' }) + }) if (!_tenantId) return; const { data } = await login({ password, @@ -100,7 +108,8 @@ const Login = () => { Taro.setStorageSync('expiresTime', data.expiresTime) } } - autoLoginCheck() + + useEffect(() => { autoLoginCheck() }, []) return (