From 9ae23571be12e92670e3720bc591763212de936a Mon Sep 17 00:00:00 2001 From: Ken Date: Wed, 12 Mar 2025 11:23:35 +0800 Subject: [PATCH] =?UTF-8?q?web=E9=AA=8C=E8=AF=81=E7=A0=81=E7=99=BB?= =?UTF-8?q?=E5=BD=95=E6=B3=A8=E5=86=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main/manager-web/src/apis/api.js | 2 +- main/manager-web/src/apis/httpRequest.js | 44 +++++++++++++++-- main/manager-web/src/apis/module/user.js | 19 ++++++++ main/manager-web/src/apis/request.js | 58 ---------------------- main/manager-web/src/views/login.vue | 62 ++++++++++-------------- main/manager-web/src/views/register.vue | 28 +++++------ 6 files changed, 96 insertions(+), 117 deletions(-) delete mode 100644 main/manager-web/src/apis/request.js diff --git a/main/manager-web/src/apis/api.js b/main/manager-web/src/apis/api.js index 93fe826b2..d9765a71a 100755 --- a/main/manager-web/src/apis/api.js +++ b/main/manager-web/src/apis/api.js @@ -5,7 +5,7 @@ import user from './module/user.js' * 在开发阶段,如果地址写的是相对路径,请与vue.config.js的devServer配置相结合,方便跨域请求 * */ -const DEV_API_SERVICE = 'https://apifoxmock.com/m1/5931378-5618560-default' +const DEV_API_SERVICE = 'http://localhost:8002/xiaozhi-esp32-api' /** * 根据开发环境返回接口url diff --git a/main/manager-web/src/apis/httpRequest.js b/main/manager-web/src/apis/httpRequest.js index aeddd1881..b61dd378e 100755 --- a/main/manager-web/src/apis/httpRequest.js +++ b/main/manager-web/src/apis/httpRequest.js @@ -23,21 +23,42 @@ function sendRequest() { _data: {}, _header: {'content-type': 'application/json; charset=utf-8'}, _url: '', + _responseType: undefined, // 新增响应类型字段 'send'() { this._header.token = localStorage.getItem(Constant.STORAGE_KEY.TOKEN) + + // 打印请求信息 + console.log('请求发送:', { + url: this._url, + method: this._method, + params: this._data, + headers: this._header, + responseType: this._responseType + }); + fly.request(this._url, this._data, { method: this._method, - headers: this._header + headers: this._header, + responseType: this._responseType }).then((res) => { - const error = httpHandlerError(res, this._failCallback) + // 打印成功响应 + + console.log('请求成功:', { + status: res.status, + data: this._responseType === 'blob' ? + `Blob(${res.data.type}, ${res.data.size} bytes)` : res.data, + headers: res.headers + }); + const error = httpHandlerError(res, this._failCallback); if (error) { return } + if (this._sucCallback) { this._sucCallback(res) } }).catch((res) => { - console.log(1111, res) + // 打印失败响应 httpHandlerError(res, this._failCallback) }) return this @@ -75,6 +96,11 @@ function sendRequest() { }, 'async'(flag) { this.async = flag + }, + // 新增类型设置方法 + 'type'(responseType) { + this._responseType = responseType; + return this; } } } @@ -84,13 +110,21 @@ function sendRequest() { * callBack 回调函数 * errTip 自定义错误信息 */ +// 在错误处理函数中添加日志 function httpHandlerError(info, callBack) { + console.log('响应处理:', { + status: info.status, + code: info.data?.code, + message: info.data?.msg + }); + /** 请求成功,退出该函数 可以根据项目需求来判断是否请求成功。这里判断的是status为200的时候是成功 */ let networkError = false if (info.status === 200) { - if (info.data.code === 'success' || info.data.code === 0) { + + if (info.data.code === 'success' || info.data.code === 0 || info.data.code === undefined) { return networkError - } else if (info.data.code === 401) { + }else if (info.data.code === 401) { goToPage(Constant.PAGE.LOGIN, true) return true } else { diff --git a/main/manager-web/src/apis/module/user.js b/main/manager-web/src/apis/module/user.js index 7a6815b2b..cf38c82e7 100755 --- a/main/manager-web/src/apis/module/user.js +++ b/main/manager-web/src/apis/module/user.js @@ -58,4 +58,23 @@ export default { }); }).send() }, + getCaptcha(uuid, callback) { + + RequestService.sendRequest() + .url(`${getServiceUrl()}/api/v1/user/captcha?uuid=${uuid}`) + .method('GET') + .type('blob') + .header({ + 'Content-Type': 'image/gif', + 'Pragma': 'No-cache', + 'Cache-Control': 'no-cache' + }) + .success((res) => { + RequestService.clearRequestTime(); + callback(res); + }) + .fail((err) => { // 添加错误参数 + + }).send() + }, } diff --git a/main/manager-web/src/apis/request.js b/main/manager-web/src/apis/request.js deleted file mode 100644 index 7a3054f2c..000000000 --- a/main/manager-web/src/apis/request.js +++ /dev/null @@ -1,58 +0,0 @@ -import axios from 'axios' - -const api = axios.create({ - baseURL: "http://localhost:8002/xiaozhi-esp32-api", - timeout: 30000, - - -}) - -// 请求拦截器 -api.interceptors.request.use(config => { - // 打印请求信息 - 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}`; - } - return config; -}) - -// 响应拦截器 -api.interceptors.response.use( - response => { - // 打印响应信息 - console.log('响应数据:', { - status: response.status, - data: response.data, - headers: response.headers - }); - - if (response.status !== 200) { - return Promise.reject(response.data) - } - return response - }, - error => { - // 打印错误信息 - 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' - } - return Promise.reject(error) - } -) - -export default api \ No newline at end of file diff --git a/main/manager-web/src/views/login.vue b/main/manager-web/src/views/login.vue index d5b2a3f4c..f95d67d05 100644 --- a/main/manager-web/src/views/login.vue +++ b/main/manager-web/src/views/login.vue @@ -63,9 +63,9 @@