smart-admin/rc-busness/config/request.js
2022-01-06 03:25:11 +00:00

272 lines
9.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import axios from 'axios'
// import Message from '@ele/component/Message'
const instance = axios.create({
// baseURL: apiPrefix,
// baseURL: 'http://192.168.1.111:7005/',
// baseURL: 'http://kbptestservice.keyunzhihui.com/', //40
baseURL: 'http://47.96.75.242:10086/royalcanin/royalcanin/', //52
// baseURL: 'http://192.168.1.52:8080',
// baseURL: 'http://192.168.1.60:8400',
// baseURL: '',
// baseURL: video,
// baseURL: 'http://192.168.1.68:8848/',
// baseURL: 'http://192.168.1.40:8080',
// baseURL: 'http://192.168.33.52:8080',
// baseURL: 'http://192.168.1.68:8580/',
// 媒体增删查改
// videoURL: 'http://192.168.1.115:7005/',
// baseURL: 'http://192.168.33.52:8080/',
// baseURL: 'http://192.168.1.83:8080',
// baseURL:'http://172.19.0.9',
// withCredentials: true, // send cookies when cross-domain requests
timeout: 60000 // request timeout
})
instance.interceptors.request.use(
config => {
//登录状态下socket断连时除登出外中断一切请求
// if (store.state.user.id && !store.state.socket.online && config.url !== '/account/logout') {
// Message.error('请等待与服务器重新连接')
// return Promise.reject('')
// }
// header添加token
// if (store.state.user.token) {
// config.headers['X-Token'] = store.state.user.token
// }
if (localStorage.getItem('Authorization')) {
config.headers.Authorization = localStorage.getItem('Authorization');
// config.headers.Authorization ='Bearer eyJhbGciOiJIUzUxMiJ9.eyJyb2xlQ29kZXMiOlsiUk9MRV9SOCIsIlJPTEVfUjE5IiwiUk9MRV9SMTgiLCJST0xFX1I0IiwiUk9MRV9SMTQiLCJST0xFX1IxMSIsIlJPTEVfWiszMiIsIlJPTEVfUjEwIiwiUk9MRV9SMTMiLCJST0xFX1IxMiJdLCJqdGkiOiIxIiwic3ViIjoiYWRtaW4iLCJleHAiOjE2MzI4MTQ1MDN9.M2VWwhih-T8AD1AwYYskH9y-SoOZtHk-qf4PV4Hdb7J6L4Nd7hxKw4JTibGNREjNsmTR1BwMPTDhVgioBSnDfQ'
}
// config.headers.Authorization ='Bearer eyJhbGciOiJIUzUxMiJ9.eyJyb2xlQ29kZXMiOlsiUk9MRV9SOCIsIlJPTEVfUjE5IiwiUk9MRV9SMTgiLCJST0xFX1I0IiwiUk9MRV9SMTQiLCJST0xFX1IxMSIsIlJPTEVfWiszMiIsIlJPTEVfUjEwIiwiUk9MRV9SMTMiLCJST0xFX1IxMiJdLCJqdGkiOiIxIiwic3ViIjoiYWRtaW4iLCJleHAiOjE2MzI4MTQ1MDN9.M2VWwhih-T8AD1AwYYskH9y-SoOZtHk-qf4PV4Hdb7J6L4Nd7hxKw4JTibGNREjNsmTR1BwMPTDhVgioBSnDfQ'
return config
},
error => Promise.reject(error)
)
instance.interceptors.response.use(
response => {
const res = response.data, { responseType = 'json' } = response.config
console.log(res.errorCode);
if (res.errorCode === 6001) {
console.log('66666');
localStorage.removeItem("Authorization");
removeToken("Token")
res && Notification.error({
title: '错误',
message: res.data.errorMessage || '请求错误,请稍后重试'
})
return Promise.reject(res)
}
if (res.errorCode === 6002) {
console.log('6002');
localStorage.removeItem("Authorization");
removeToken("Token")
res && Notification.error({
title: '错误',
message: res.data.errorMessage || '账号或密码错误,请重试'
})
return Promise.reject(res)
}
if (res.errorCode === 1001) {
console.log('1001');
localStorage.removeItem("Authorization");
removeToken("Token")
window.location.href='/#/login'
res && Notification.error({
title: '错误',
message: res.data.errorMessage || '请求错误,请稍后重试'
})
return Promise.reject(res)
}
if (res.errorCode === 1000) {
localStorage.removeItem("Authorization");
removeToken("Token")
localStorage.removeItem("elecloud");
console.log('---这是状态码')
window.location.href='/#/login'
// res && Notification.error({
// title: '错误',
// message: res.data.errorMessage || '请求错误,请稍后重试'
// })
return Promise.reject(res)
}
if (res.errorCode === 1002) {
localStorage.removeItem("Authorization");
removeToken("Token")
window.location.href='/#/login'
res && Notification.error({
title: '错误',
message: res.data.errorMessage || '请求错误,请稍后重试'
})
return Promise.reject(res)
}
//当返回类型非{status,data,msg}的接口请求时不使用status来判断请求是否成功
if ((!('status' in res)) || (res.status === 200)) {
//当返回类型为json时返回response.data
return responseType === 'json' ? res : response
}
//服务器异常
if (res.status === 500) {
// Message.error(res.msg || '操作失败')
return Promise.reject(res.msg)
}
//未登录
// if (res.status === 6001) {
// if (store.state.user.Authorization) return Promise.reject()
// return MessageBox.alert('请登录后重试', {
// type: 'warning',
// beforeClose: (action, instance, done) => {
// store.dispatch('user/logout').then(done)
// }
// })
// }
//没有权限
if (res.status === 403) {
// Message.error(res.msg || '没有权限进行该操作')
return Promise.reject(res.msg)
}
if (res.status === 400) {
// Message.error(res.msg || '没有权限进行该操作')
return Promise.reject(res)
}
// if(res.)
//其他错误
// Message.error(res.msg || '接口有误')
return Promise.reject(res)
},
error => {
// console.log('error',error.response);
if (axios.isCancel(error)) return
error && Notification.error({
title: '错误',
// message: '请求错误,请稍后重试'
message: error.response.data.message || '请求错误,请稍后重试'
})
return Promise.reject(error)
}
)
class Api {
/**
* 数据接口定义
* @param url 请求url不带参数
* @param arg 对传入参数的处理方法返回值将作为axios[get,post]的第二个参数
* @param chain 形参为请求返回的promise
* @param method 请求方法小写get、post...
*/
constructor(url, arg, chain, method) {
this.url = url
this.arg = arg
this.chain = chain
this.method = method
}
request(...args) {
const params = this.arg ? this.arg(...args) : undefined
const promise = instance[this.method](this.url, params).catch(e => console.error(e)
)
return this.chain ? this.chain(promise) : promise
}
}
class ApiJson {
/**
* 数据接口定义
* @param url 请求url不带参数
* @param arg 对传入参数的处理方法返回值将作为axios[get,post]的第二个参数
* @param chain 形参为请求返回的promise
* @param method 请求方法小写get、post...
* @param header 请求方法头部信息Json
*/
constructor(url, arg, chain, method, header) {
this.url = url
this.arg = arg
this.chain = chain
this.method = method
this.header = header
}
request(...args) {
const params = this.arg ? this.arg(...args) : undefined
const promise = instance[this.method](this.url, params, { headers: { 'Content-Type': 'application/json' } }).catch(e => console.error(e))
return this.chain ? this.chain(promise) : promise
}
}
export class PostApi extends Api {
constructor(url, arg, chain) {
if (!arg) arg = data => data
super(url, arg, chain, 'post')
}
}
export class GetApi extends Api {
constructor(url, arg, chain) {
super(url, arg, chain, 'get')
}
}
export class DeleteApi extends Api {
constructor(url, arg, chain) {
super(url, arg, chain, 'delete')
}
}
export class PatchApi extends Api {
constructor(url, arg, chain) {
if (!arg) arg = data => data
super(url, arg, chain, 'patch')
}
}
export class PutApi extends Api {
constructor(url, arg, chain) {
if (!arg) arg = data => data
super(url, arg, chain, 'put')
}
}
export class PostJsonApi extends ApiJson {
constructor(url, arg, chain) {
if (!arg) arg = data => data
let header = { headers: { 'Content-Type': 'application/json' } }
super(url, arg, chain, 'post', header)
}
}
export class DeleteJsonApi extends ApiJson {
constructor(url, arg, chain) {
super(url, arg, chain, 'delete')
}
}
export default instance