123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- /*
- * @Author: your name
- * @Date: 2022-01-06 09:45:17
- * @LastEditTime: 2022-04-21 17:36:42
- * @LastEditors: your name
- * @Description: axios封装
- */
- import axios from 'axios'
- import { MessageBox, Message } from 'element-ui'
- import store from '@/store'
- import { getToken, getUserId, getCodeToken, getLogId, getUserName } from '@/utils/auth'
- import { parseTime } from '@/utils'
- import { Loading } from 'element-ui'
- // create an axios instance
- const service = axios.create({
- //baseURL: baseURL, // url = base url + request url
- baseURL: PLATFROM_CONFIG.gatewayUrl,
- // withCredentials: true, // send cookies when cross-domain requests
- timeout: 30000, // request timeout
- headers: {
- 'Content-Type': 'application/json'
- }
- })
- let loadingInstance,
- flag = false
- // request interceptor
- service.interceptors.request.use(
- config => {
- if (config.istoken) {
- config.headers['appSecret'] = PLATFROM_CONFIG.appSecret
- } else if (getCodeToken() && !config.istoken && config.islogin) {
- config.headers['token'] = getCodeToken()
- } else 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['Token'] = getToken()
- }
- if (config.data && !config.url.includes('/api/fs4a/sendLog')) {
- config.data['OperatorId'] = getUserId()
- }
- flag = true
- if (flag) {
- loadingInstance = Loading.service({
- text: '数据加载中'
- })
- }
- if (config.url.includes('/api/fs4a/sendLog') && config.data.Data[0]?.OperateResult?.Token) {
- config.headers['token'] = config.data.Data[0].OperateResult.Token
- }
- config.metaData = {
- BeginTime: parseTime(new Date(), '{y}-{m}-{d} {h}:{i}:{s}')
- }
- 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 => {
- const res = response.data
- // 特定请求完成后发送日志
- const { metaData, url, Ident, data, msg, islogin } = response.config
- if (!url.includes('/api/fs4a/sendLog') && (Ident || msg)) {
- Object.assign(metaData, {
- LogInfo: typeof data === 'string' ? JSON.parse(data) : data,
- EndTime: parseTime(new Date(), '{y}-{m}-{d} {h}:{i}:{s}'),
- LogType: msg ?? '未定义操作',
- OperateResult: res.message,
- Ident: Ident ?? ''
- })
- const { BeginTime, EndTime, OperateResult } = metaData
- const config = {
- url: '/api/fs4a/sendLog',
- method: 'post',
- data: {
- Data: [metaData],
- BrowserTag: navigator.userAgent,
- OperateIP: returnCitySN.cip,
- Msg: msg ?? '未定义操作',
- Type: '2',
- BeginTime,
- BeginTime2: BeginTime,
- EndTime,
- OperateResult,
- Ident: Ident ?? '',
- AppId: '9'
- }
- }
- if (islogin) {
- if (res.code === 0) {
- config.data.OperatorId = (res.returnData.UserId ?? '').toString()
- config.data.LogId = (res.returnData.LogId ?? '').toString()
- config.data.UserName = (res.returnData.UserName ?? '').toString()
- } else {
- const loginData = typeof data === 'string' ? JSON.parse(data) : data
- config.data.UserName = loginData.LoginName.toString()
- }
- } else {
- config.data.OperatorId = getUserId()
- config.data.LogId = getLogId()
- config.data.UserName = getUserName()
- }
- service(config)
- }
- if (res.success) {
- flag = false
- loadingInstance.close()
- return res
- } else {
- if (res.code == 500) {
- Message({
- message: '身份令牌过期或失效,即将重新登录',
- type: 'error',
- duration: 5 * 1000,
- onClose: () => {
- store.dispatch('tagsView/delAllViews').then(() => { })
- store.dispatch('user/resetToken').then(() => {
- location.reload()
- })
- }
- })
- }
- flag = false
- loadingInstance.close()
- }
- // if the custom code is not 20000, it is judged as an error.
- // if (res.code != 0 || res.code == -1) {
- // Message({
- // message: res.message || 'Error',
- // type: 'error',
- // duration: 5 * 1000
- // })
- // // 50008: Illegal token; 50012: Other clients logged in; 50014: Token expired;
- // if (res.code == 500) {
- // // to re-login
- // Message({
- // message: '身份令牌过期或失效,即将重新登录',
- // type: 'error',
- // duration: 5 * 1000,
- // onClose: () => {
- // store.dispatch('tagsView/delAllViews').then(() => { })
- // store.dispatch('user/resetToken').then(() => {
- // location.reload()
- // })
- // }
- // })
- // }
- // // loadingInstance.close()
- // return Promise.reject(new Error(res.message || 'Error'))
- // } else {
- // // loadingInstance.close()
- // return res
- // }
- },
- error => {
- // console.log('err', error) // for debug
- const des = `${error}`.split(' ').includes('500')
- if (des) {
- Message({
- message: '身份令牌过期或失效,即将重新登录',
- type: 'error',
- duration: 5 * 1000,
- onClose: () => {
- store.dispatch('app/toggleDialog', false)
- store.dispatch('tagsView/delAllViews').then(() => { })
- store.dispatch('user/resetToken').then(() => {
- location.reload()
- })
- }
- })
- // 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()
- // })
- // })
- } else {
- Message({
- message: error.message,
- type: 'error',
- duration: 5 * 1000
- })
- }
- flag = false
- loadingInstance.close()
- return Promise.reject(error)
- }
- )
- export default service
|