first
This commit is contained in:
54
src/utils/http.js
Normal file
54
src/utils/http.js
Normal file
@ -0,0 +1,54 @@
|
||||
import axios from "axios";
|
||||
import qs from 'qs'
|
||||
import { Toast } from 'vant'
|
||||
|
||||
console.log('process.env.VUE_APP_BASE_API', process.env.VUE_APP_BASE_API)
|
||||
|
||||
const service = axios.create({
|
||||
// baseURL: process.env.VUE_APP_BASE_API,
|
||||
baseURL: 'https://api1.sino-assist.com/',
|
||||
headers: {
|
||||
Accept: 'application/json'
|
||||
},
|
||||
timeout: 10000
|
||||
})
|
||||
service.interceptors.request.use(
|
||||
config => {
|
||||
config.data = config.contentType ? config.data : qs.stringify(config.data)
|
||||
config.headers['Content-Type'] = config.contentType || 'application/x-www-form-urlencoded'
|
||||
// 在发送请求之前做一些操作,如添加token到请求头
|
||||
const token='eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJzemwiLCJhdWQiOlsic3VwcGxpZXItYXBwIl0sIm5iZiI6MTY5MTU1NzIzNSwidXNlcl9pbmZvIjp7InVzZXJJZCI6NTQ1ODUsInBob25lIjoiMTc2MzAwMzU2NTgiLCJuYW1lIjoi5bCP5a6LIiwic3VwcGxpZXJOYW1lIjoi5LiK5rW35a6J55WFQSIsInN1cHBsaWVySWQiOjExMjgsInVzZXJuYW1lIjoic3psIiwidXNlclR5cGUiOiJBUFAiLCJhdXRob3JpdGllcyI6W119LCJzY29wZSI6WyJhbGwiXSwiaXNzIjoiaHR0cHM6Ly9zaW5vYXNzaXN0LmNvbSIsImV4cCI6MTY5MTY0MzYzNSwiaWF0IjoxNjkxNTU3MjM1fQ.S1AignpcnCxSrm3M8rzhCLYlgyqD2Hv-SPMqPLgqlt4'
|
||||
config.headers['Authorization'] = `${token}`;
|
||||
/* if (getToken()) {
|
||||
config.headers['token'] = getToken()
|
||||
}*/
|
||||
return config
|
||||
},
|
||||
error => {
|
||||
console.log(error)
|
||||
return Promise.reject(error)
|
||||
}
|
||||
)
|
||||
|
||||
service.interceptors.response.use(
|
||||
response => {
|
||||
const res = response.data
|
||||
console.log('rere', res)
|
||||
if (res.code && (res.code != '00' && res.code != '105' )) {
|
||||
Toast(res.msg || 'Error')
|
||||
return Promise.reject(new Error(res.msg || 'Error'))
|
||||
} else {
|
||||
if( res.code === 0 ) {
|
||||
return res.result
|
||||
} else {
|
||||
return res.data
|
||||
}
|
||||
}
|
||||
},
|
||||
error => {
|
||||
console.log('err' + error)
|
||||
Toast(error.message)
|
||||
return Promise.reject(error)
|
||||
}
|
||||
)
|
||||
export default service
|
Reference in New Issue
Block a user