diff --git a/src/utils/request.js b/src/utils/request.js index f283b45..d16fa70 100644 --- a/src/utils/request.js +++ b/src/utils/request.js @@ -5,27 +5,18 @@ import { tansParams } from '@/utils/compatible.js'; const pinias = useStore(); -// 创建axios实例 -const service = axios.create({ - // axios中请求配置有baseURL选项,表示请求URL公共部分 - baseURL: import.meta.env.VITE_APP_BASE_API, - // 超时 - timeout: 30000, -}); + +// 设置接口超时时间 +axios.defaults.timeout = 8000; //http request 拦截器 -service.interceptors.request.use( +axios.interceptors.request.use( (config) => { - // 是否需要设置 token - const isToken = (config.headers || {}).isToken === false; // 配置请求头 - if (localStorage.getItem('GCAppToken') && !isToken) { - config.headers = { - 'Content-Type': 'application/json;charset=utf-8', - token: localStorage.getItem('GCAppToken'), - USERNO: localStorage.getItem('userNo'), - }; - } + config.headers = { + 'Content-Type': 'application/json;charset=UTF-8', + Authorization: 'Bearer ' + pinias.token, + }; return config; }, (error) => { @@ -34,22 +25,13 @@ ); //http response 拦截器 -service.interceptors.response.use( +axios.interceptors.response.use( (response) => { // console.log('request success', response); - const { code } = response ? response.data : 'undefined' || ''; if (code == 401) { showFailToast('登录已过期,请重新登录!'); - router.push('/login'); - // if (import.meta.env.MODE == 'development') { - // router.push('/login'); - // } else { - // // window.location.href = 'http://192.168.16.94:8082/appMain/login'; //研发服务器 - // window.location.href = 'https://server1.wh-nf.cn:8876/appMain/login'; //正式环境 - // } - // window.android.startLoginPage(); return; } // axios单独请求接口时,未返回code情况处理 @@ -75,32 +57,31 @@ } ); -export default service; -// // 封装 GET POST 请求并导出 -// export default function request(config) { -// // get请求映射params参数 -// if (config.params) { -// let url = config.url + '?' + tansParams(config.params); -// url = url.slice(0, -1); -// config.url = url; -// } -// const baseURL = import.meta.env.MODE == 'development' ? config.url : '/prod-api' + config.url; -// return new Promise((resolve, reject) => { -// let promise = axios({ -// method: config.method, -// url: baseURL, -// data: config.data, -// header: config.header || {}, -// dataType: 'json', -// timeout: config.timeout || 8000, -// }); -// //处理返回 -// promise -// .then((res) => { -// resolve(res.data); -// }) -// .catch((err) => { -// reject(err); -// }); -// }); -// } +// 封装 GET POST 请求并导出 +export default function request(config) { + // get请求映射params参数 + if (config.params) { + let url = config.url + '?' + tansParams(config.params); + url = url.slice(0, -1); + config.url = url; + } + const baseURL = import.meta.env.MODE == 'development' ? config.url : '/prod-api' + config.url; + return new Promise((resolve, reject) => { + let promise = axios({ + method: config.method, + url: baseURL, + data: config.data, + header: config.header || {}, + dataType: 'json', + timeout: config.timeout || 8000, + }); + //处理返回 + promise + .then((res) => { + resolve(res.data); + }) + .catch((err) => { + reject(err); + }); + }); +} diff --git a/src/utils/request.js b/src/utils/request.js index f283b45..d16fa70 100644 --- a/src/utils/request.js +++ b/src/utils/request.js @@ -5,27 +5,18 @@ import { tansParams } from '@/utils/compatible.js'; const pinias = useStore(); -// 创建axios实例 -const service = axios.create({ - // axios中请求配置有baseURL选项,表示请求URL公共部分 - baseURL: import.meta.env.VITE_APP_BASE_API, - // 超时 - timeout: 30000, -}); + +// 设置接口超时时间 +axios.defaults.timeout = 8000; //http request 拦截器 -service.interceptors.request.use( +axios.interceptors.request.use( (config) => { - // 是否需要设置 token - const isToken = (config.headers || {}).isToken === false; // 配置请求头 - if (localStorage.getItem('GCAppToken') && !isToken) { - config.headers = { - 'Content-Type': 'application/json;charset=utf-8', - token: localStorage.getItem('GCAppToken'), - USERNO: localStorage.getItem('userNo'), - }; - } + config.headers = { + 'Content-Type': 'application/json;charset=UTF-8', + Authorization: 'Bearer ' + pinias.token, + }; return config; }, (error) => { @@ -34,22 +25,13 @@ ); //http response 拦截器 -service.interceptors.response.use( +axios.interceptors.response.use( (response) => { // console.log('request success', response); - const { code } = response ? response.data : 'undefined' || ''; if (code == 401) { showFailToast('登录已过期,请重新登录!'); - router.push('/login'); - // if (import.meta.env.MODE == 'development') { - // router.push('/login'); - // } else { - // // window.location.href = 'http://192.168.16.94:8082/appMain/login'; //研发服务器 - // window.location.href = 'https://server1.wh-nf.cn:8876/appMain/login'; //正式环境 - // } - // window.android.startLoginPage(); return; } // axios单独请求接口时,未返回code情况处理 @@ -75,32 +57,31 @@ } ); -export default service; -// // 封装 GET POST 请求并导出 -// export default function request(config) { -// // get请求映射params参数 -// if (config.params) { -// let url = config.url + '?' + tansParams(config.params); -// url = url.slice(0, -1); -// config.url = url; -// } -// const baseURL = import.meta.env.MODE == 'development' ? config.url : '/prod-api' + config.url; -// return new Promise((resolve, reject) => { -// let promise = axios({ -// method: config.method, -// url: baseURL, -// data: config.data, -// header: config.header || {}, -// dataType: 'json', -// timeout: config.timeout || 8000, -// }); -// //处理返回 -// promise -// .then((res) => { -// resolve(res.data); -// }) -// .catch((err) => { -// reject(err); -// }); -// }); -// } +// 封装 GET POST 请求并导出 +export default function request(config) { + // get请求映射params参数 + if (config.params) { + let url = config.url + '?' + tansParams(config.params); + url = url.slice(0, -1); + config.url = url; + } + const baseURL = import.meta.env.MODE == 'development' ? config.url : '/prod-api' + config.url; + return new Promise((resolve, reject) => { + let promise = axios({ + method: config.method, + url: baseURL, + data: config.data, + header: config.header || {}, + dataType: 'json', + timeout: config.timeout || 8000, + }); + //处理返回 + promise + .then((res) => { + resolve(res.data); + }) + .catch((err) => { + reject(err); + }); + }); +} diff --git a/src/views/login.vue b/src/views/login.vue index eb7a8b3..0870d8c 100644 --- a/src/views/login.vue +++ b/src/views/login.vue @@ -92,9 +92,9 @@ }; let res = await appLogin(params); - if (res && res.data.code == 200) { + if (res && res.code == 200) { allData.loginLoading = false; - const { access_token } = res.data; + const { access_token } = res; pinias.$patch((state) => { state.token = access_token; }); @@ -119,9 +119,9 @@ // 获取验证码 const getCode = async () => { let res = await getCodeImg(); - if (res?.data.code === 200) { - codeUrl.value = 'data:image/gif;base64,' + res.data.img; - allData.uuid = res.data.uuid; + if (res?.code === 200) { + codeUrl.value = 'data:image/gif;base64,' + res.img; + allData.uuid = res.uuid; } };