From a406e0b3badeb2ffc1374a194e342a021298c759 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=BF=94=E5=AE=87-=E6=97=85=E7=AE=A1=E5=AE=B6?= <153298343@qq.com> Date: Tue, 6 Sep 2022 08:50:24 +0800 Subject: [PATCH] up --- .env.development | 4 +- src/api/user.js | 3 +- src/utils/request.js | 201 ++++++++++++++++++++------------------ src/views/login/index.vue | 49 ++++++---- 4 files changed, 143 insertions(+), 114 deletions(-) diff --git a/.env.development b/.env.development index 56074d9..060f877 100644 --- a/.env.development +++ b/.env.development @@ -2,8 +2,8 @@ ENV = 'development' # base api -VUE_APP_DOMIAN=http://192.168.60.99:9003/ -#VUE_APP_DOMIAN=http://hdcontract.ali251.langye.net/ +V#UE_APP_DOMIAN=http://192.168.60.99:9003/ +VUE_APP_DOMIAN=http://hdcontract.ali251.langye.net/ VUE_APP_BASE_API = '' diff --git a/src/api/user.js b/src/api/user.js index ecb1476..f9dcc72 100644 --- a/src/api/user.js +++ b/src/api/user.js @@ -12,7 +12,8 @@ export function loginOss(data) { return request({ url: '/api/admin/auth/oss-login', method: 'post', - data + data, + noloading:true }) } diff --git a/src/utils/request.js b/src/utils/request.js index 9462e36..f2521e5 100644 --- a/src/utils/request.js +++ b/src/utils/request.js @@ -1,95 +1,108 @@ -import axios from 'axios' -import { MessageBox, Message,Loading } from 'element-ui' -import store from '@/store' -import { getToken } from '@/utils/auth' -let loading; - -// create an axios instance -const service = axios.create({ - baseURL: process.env.VUE_APP_DOMIAN, // url = base url + request url - // withCredentials: true, // send cookies when cross-domain requests - timeout: 5000 // request timeout -}) - -// request interceptor -service.interceptors.request.use( - config => { - // do something before request is sent - loading = Loading.service({ - lock:true, - background:"rgba(0,0,0,0.4)", - text:"正在加载中..." - }) - - if (store.getters.token) { - // let each request carry token - // ['X-Token'] is a custom headers key - // please modify it according to the actual situation - //config.headers['X-Token'] = getToken(); - - config.headers['Authorization'] ="Bearer "+getToken() - } - return config - }, - error => { - // do something with request error - console.log(error) // for debug - return Promise.reject(error) - } -) - -// response interceptor -service.interceptors.response.use( - /** - * If you want to get http information such as headers or status - * Please return response => response - */ - - /** - * Determine the request status by custom code - * Here is just an example - * You can also judge the status by HTTP Status Code - */ - response => { - loading.close() - const res = response.data - - // if the custom code is not 20000, it is judged as an error. - if (res.errcode) { - Message({ - message: res.errmsg || 'Error', - type: 'error', - duration: 5 * 1000 - }) - - // 50008: Illegal token; 50012: Other clients logged in; 50014: Token expired; - if (res.errcode === 50008 || res.errcode === 50012 || res.errcode === 50014) { - // to re-login - MessageBox.confirm('You have been logged out, you can cancel to stay on this page, or log in again', 'Confirm logout', { - confirmButtonText: 'Re-Login', - cancelButtonText: 'Cancel', - type: 'warning' - }).then(() => { - store.dispatch('user/resetToken').then(() => { - location.reload() - }) - }) - } - return Promise.reject(new Error(res.errmsg || 'Error')) - } else { - return res - } - }, - error => { - loading.close() - console.log('err' + error) // for debug - Message({ - message: error.message, - type: 'error', - duration: 5 * 1000 - }) - return Promise.reject(error) - } -) - +import axios from 'axios' +import { + MessageBox, + Message, + Loading +} from 'element-ui' +import store from '@/store' +import { + getToken +} from '@/utils/auth' +let loading; + +// create an axios instance +const service = axios.create({ + baseURL: process.env.VUE_APP_DOMIAN, // url = base url + request url + // withCredentials: true, // send cookies when cross-domain requests + timeout: 500000 // request timeout +}) + +// request interceptor +service.interceptors.request.use( + config => { + + if (!config.noloading) { + // do something before request is sent + loading = Loading.service({ + lock: true, + background: "rgba(0,0,0,0.4)", + text: "正在加载中..." + }) + } + if (store.getters.token) { + // let each request carry token + // ['X-Token'] is a custom headers key + // please modify it according to the actual situation + //config.headers['X-Token'] = getToken(); + + config.headers['Authorization'] = "Bearer " + getToken() + } + return config + }, + error => { + // do something with request error + console.log(error) // for debug + return Promise.reject(error) + } +) + +// response interceptor +service.interceptors.response.use( + /** + * If you want to get http information such as headers or status + * Please return response => response + */ + + /** + * Determine the request status by custom code + * Here is just an example + * You can also judge the status by HTTP Status Code + */ + response => { + if (loading) { + loading.close() + } + const res = response.data + + // if the custom code is not 20000, it is judged as an error. + if (res.errcode) { + Message({ + message: res.errmsg || 'Error', + type: 'error', + duration: 5 * 1000 + }) + + // 50008: Illegal token; 50012: Other clients logged in; 50014: Token expired; + if (res.errcode === 50008 || res.errcode === 50012 || res.errcode === 50014) { + // to re-login + MessageBox.confirm('You have been logged out, you can cancel to stay on this page, or log in again', + 'Confirm logout', { + confirmButtonText: 'Re-Login', + cancelButtonText: 'Cancel', + type: 'warning' + }).then(() => { + store.dispatch('user/resetToken').then(() => { + location.reload() + }) + }) + } + return Promise.reject(new Error(res.errmsg || 'Error')) + } else { + return res + } + }, + error => { + if (loading) { + loading.close() + } + console.log('err' + error) // for debug + Message({ + message: error.message, + type: 'error', + duration: 5 * 1000 + }) + return Promise.reject(error) + } +) + export default service diff --git a/src/views/login/index.vue b/src/views/login/index.vue index c05748a..29e9de6 100644 --- a/src/views/login/index.vue +++ b/src/views/login/index.vue @@ -1,5 +1,5 @@