验证码展示

This commit is contained in:
Ken 2025-03-11 14:05:05 +08:00
parent 3d378aebe9
commit 45479375d0
3 changed files with 65 additions and 38 deletions

View file

@ -2,30 +2,52 @@ import axios from 'axios'
const api = axios.create({
baseURL: "http://localhost:8002/xiaozhi-esp32-api",
timeout: 10000,
timeout: 30000,
})
// 请求拦截器
api.interceptors.request.use(config => {
const token = localStorage.getItem('access_token')
// 打印请求信息
console.log('请求信息:', {
method: config.method.toUpperCase(),
url: config.baseURL + config.url,
params: config.params,
data: config.data
});
const token = localStorage.getItem('access_token');
if (token) {
config.headers.Authorization = `Bearer ${token}`
config.headers.Authorization = `Bearer ${token}`;
}
return config
return config;
})
// 响应拦截器
api.interceptors.response.use(
response => {
if (response.data.code !== 200) {
// 打印响应信息
console.log('响应数据:', {
status: response.status,
data: response.data,
headers: response.headers
});
if (response.status !== 200) {
return Promise.reject(response.data)
}
return response.data
return response
},
error => {
if (error.response.status === 401) {
// 打印错误信息
console.error('请求错误:', {
status: error.response?.status,
data: error.response?.data,
message: error.message
});
if (error.response?.status === 401) {
localStorage.removeItem('access_token')
window.location.href = '/login'
}

View file

@ -86,36 +86,27 @@ export default {
},
methods: {
async fetchCaptcha() {
try {
this.captchaUuid = Date.now().toString()
try {
// 添加请求地址打印
console.log('请求地址:', api.defaults.baseURL+`/captcha?uuid=${this.captchaUuid}`)
const response = await api.get(`/captcha?uuid=${this.captchaUuid}`, {
responseType: 'blob',
headers: {
'Content-Type': 'application/json;charset=utf8',
'Accept': 'image/gif'
'Content-Type': 'image/gif',
'Pragma': 'No-cache',
'Cache-Control': 'no-cache'
}
})
// 释放之前创建的URL对象
if (this.captchaUrl) {
URL.revokeObjectURL(this.captchaUrl)
}
// 验证图片类型并创建新URL
if (response.data.type === 'image/png') {
this.captchaUrl = URL.createObjectURL(response.data)
console.log('PNG验证码加载成功,UUID:', this.captchaUuid)
} else {
throw new Error(`无效的图片类型: ${response.data.type}`)
});
// 生成新的验证码URL
if (response.data) {
const blob = new Blob([response.data], { type: response.data.type });
this.captchaUrl = URL.createObjectURL(blob);
}
}
catch (error) {
// 清空验证码图片并显示错误
this.captchaUrl = ''
showDanger('验证码加载失败,请重试')
console.error('验证码请求异常:', error)
throw error
console.error('验证码加载异常:', error);
showDanger('验证码加载失败,点击刷新');
}
},

View file

@ -0,0 +1,14 @@
const { defineConfig } = require('@vue/cli-service');
const dotenv = require('dotenv');
dotenv.config();
module.exports = defineConfig({
devServer: {
// Bug 修复:将代理配置为环境变量中定义的 API 基础 URL
proxy: process.env.VUE_APP_API_BASE_URL,
client: {
overlay: false,
},
}
})