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 './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 }</View>
)
})

View File

@ -7,10 +7,10 @@ export const request = ({
baseUrl = axiosConfig.baseUrl,
data = {},
method = 'GET',
header = {
'Authorization': Taro.getStorageSync('token')
}
header = {},
isToken = true
}): Promise<any> => {
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 () {

View File

@ -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 等信息
}

View File

@ -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 (
<View className="w-full h-[100vh] relative">
<Image src={loginBg} className="w-full h-full" />