fix:: 请求模块

This commit is contained in:
Tony 2024-03-26 14:34:50 +08:00
parent 8c4e9a416a
commit 4ea4223695
4 changed files with 27 additions and 15 deletions

View File

@ -1,5 +1,6 @@
import { View } from "@tarojs/components" import { View } from "@tarojs/components"
import './index.css' import './index.css'
import Taro from "@tarojs/taro"
/** /**
* options selected为选中值 callback为函数回调 * options selected为选中值 callback为函数回调
@ -16,7 +17,10 @@ const TabWrapper = ({ options, selectedId = '', callback }) => {
className={`px-3 py-1 ${selectedId === item.id ? 'selected-tab' : ''}`} className={`px-3 py-1 ${selectedId === item.id ? 'selected-tab' : ''}`}
key={item.id} key={item.id}
style={{ color: 'white' }} style={{ color: 'white' }}
onClick={() => callback(item.id)} onClick={() => {
Taro.vibrateShort({ type: 'light' })
callback(item.id)
}}
>{ item.title }</View> >{ item.title }</View>
) )
}) })

View File

@ -7,10 +7,10 @@ export const request = ({
baseUrl = axiosConfig.baseUrl, baseUrl = axiosConfig.baseUrl,
data = {}, data = {},
method = 'GET', method = 'GET',
header = { header = {},
'Authorization': Taro.getStorageSync('token') isToken = true
}
}): Promise<any> => { }): Promise<any> => {
if (isToken) header['Authorization'] = 'Bearer ' + Taro.getStorageSync('token')
let requestParams = '?' let requestParams = '?'
if (method === 'GET') requestParams += stringify(data, { indices: false }) if (method === 'GET') requestParams += stringify(data, { indices: false })
console.log('requestParams', requestParams); console.log('requestParams', requestParams);
@ -23,11 +23,9 @@ export const request = ({
method: method as any, method: method as any,
header, header,
success: function (res) { success: function (res) {
console.log('接口返回', res);
resolve(res.data) resolve(res.data)
}, },
fail: function(err) { fail: function(err) {
console.error('err', err);
reject(err) reject(err)
}, },
complete: function () { complete: function () {

View File

@ -1,5 +1,6 @@
export const axiosConfig = { 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, timeout: 30000,
withCredentials: false // 禁用 Cookie 等信息 withCredentials: false // 禁用 Cookie 等信息
} }

View File

@ -2,7 +2,7 @@ import { Button, Image, Input, Text, View } from "@tarojs/components"
import loginBg from '../../assets/images/loginBg.png' import loginBg from '../../assets/images/loginBg.png'
import questionIcon from '../../assets/images/icons/question-line.png' import questionIcon from '../../assets/images/icons/question-line.png'
import Taro from "@tarojs/taro" import Taro from "@tarojs/taro"
import { useState } from "react" import { useEffect, useState } from "react"
import { request } from "../../config/axios" import { request } from "../../config/axios"
const login = (data, tenantId = '1') => { const login = (data, tenantId = '1') => {
@ -10,7 +10,8 @@ const login = (data, tenantId = '1') => {
url: '/system/auth/login', url: '/system/auth/login',
method: 'POST', method: 'POST',
header: { 'Tenant-Id': tenantId }, header: { 'Tenant-Id': tenantId },
data data,
isToken: false
}) })
} }
@ -18,7 +19,8 @@ const getTenantId = (data) => {
return request({ return request({
url: '/system/tenant/get-id-by-name', url: '/system/tenant/get-id-by-name',
method: 'GET', method: 'GET',
data data,
isToken: false
}) })
} }
@ -26,13 +28,15 @@ const autoLoginCheck = async () => {
const expireTime = Taro.getStorageSync('expiresTime') const expireTime = Taro.getStorageSync('expiresTime')
const nowTime = new Date().valueOf() const nowTime = new Date().valueOf()
console.log('expireTime', expireTime); console.log('expireTime', expireTime);
console.log('nowTime', nowTime);
console.log('expireTime', expireTime - nowTime);
if (expireTime) { if (expireTime) {
if (expireTime - nowTime > 1000 * 60 * 60 * 8) { if ((nowTime - expireTime) > 1000 * 60 * 15) {
// 过期时间大于八小时 // 过期时间大于15分钟
Taro.redirectTo({ url: '/basePage/index/index' }) Taro.redirectTo({ url: '/basePage/index/index' })
} else { } else {
// 过期时间不足 八小时 // 过期时间不足 15分钟
const tenantName = Taro.getStorageSync('tenantName') const tenantName = Taro.getStorageSync('tenantName')
if (!tenantName) return if (!tenantName) return
const { data: _tenantId } = await getTenantId({ name: tenantName }) const { data: _tenantId } = await getTenantId({ name: tenantName })
@ -82,7 +86,11 @@ const Login = () => {
Taro.showLoading({ title: '加载中...'}) 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; if (!_tenantId) return;
const { data } = await login({ const { data } = await login({
password, password,
@ -100,7 +108,8 @@ const Login = () => {
Taro.setStorageSync('expiresTime', data.expiresTime) Taro.setStorageSync('expiresTime', data.expiresTime)
} }
} }
autoLoginCheck()
useEffect(() => { autoLoginCheck() }, [])
return ( return (
<View className="w-full h-[100vh] relative"> <View className="w-full h-[100vh] relative">
<Image src={loginBg} className="w-full h-full" /> <Image src={loginBg} className="w-full h-full" />