验证码展示

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({ const api = axios.create({
baseURL: "http://localhost:8002/xiaozhi-esp32-api", baseURL: "http://localhost:8002/xiaozhi-esp32-api",
timeout: 10000, timeout: 30000,
}) })
// 请求拦截器 // 请求拦截器
api.interceptors.request.use(config => { 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) { if (token) {
config.headers.Authorization = `Bearer ${token}` config.headers.Authorization = `Bearer ${token}`;
} }
return config return config;
}) })
// 响应拦截器 // 响应拦截器
api.interceptors.response.use( api.interceptors.response.use(
response => { 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 Promise.reject(response.data)
} }
return response.data return response
}, },
error => { 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') localStorage.removeItem('access_token')
window.location.href = '/login' window.location.href = '/login'
} }

View file

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

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,
},
}
})