122 lines
2.9 KiB
JavaScript
122 lines
2.9 KiB
JavaScript
import store from "../store/index.js";
|
|
import utils from "../utils/utils.js";
|
|
|
|
import { baseUrl } from "./api.js";
|
|
|
|
const HOST = location && location.origin
|
|
export default () => {
|
|
let readBaseUrl = process.env.bcUrl
|
|
// 初始化请求配置
|
|
uni.$u.http.setConfig((config) => {
|
|
if(readBaseUrl ){
|
|
config.baseURL = readBaseUrl; // 测试地址
|
|
}
|
|
|
|
// #ifdef APP-PLUS
|
|
config.baseURL = baseUrl; // 测试地址
|
|
// #endif
|
|
return config
|
|
});
|
|
|
|
|
|
// 请求拦截
|
|
uni.$u.http.interceptors.request.use((config) => { // 可使用async await 做异步操作
|
|
|
|
|
|
if(!config.baseURL){
|
|
config.url = '/'+config.url
|
|
}
|
|
|
|
config.data = config.data || {};
|
|
// config.baseURL = 'https://ai.agrimedia.cn/'; // 不使用请求代理 上线记得注释掉 ***********************************************************
|
|
// config.baseURL = 'http://test.sc2.agrimedia.cn';
|
|
|
|
let apiToken = store.getters['api/getApiToken']
|
|
let serverTime = (parseInt(Date.now() /1000) - store.getters['api/getServerTime'])
|
|
|
|
config.header.systemType = 0;
|
|
config.header.ApiToken = apiToken
|
|
// config.header.systemSn = utils.getSunmiSerial()
|
|
config.header.timestamp = serverTime
|
|
let ob = (config.method == 'GET'? config.params : config.data)
|
|
const custom = config.custom.hideLoading || false
|
|
if(!custom){
|
|
// uni.showLoading({
|
|
// mask: true,
|
|
// title:"加载中..."
|
|
// });
|
|
}
|
|
|
|
return config
|
|
}, config => { // 可使用async await 做异步操作
|
|
return Promise.reject(config)
|
|
})
|
|
|
|
// 响应拦截
|
|
uni.$u.http.interceptors.response.use((response) => {
|
|
|
|
uni.hideLoading({});
|
|
|
|
/* 对响应成功做点什么 可使用async await 做异步操作*/
|
|
const data = response.data
|
|
const custom = response.config?.custom
|
|
|
|
if (data.code !== 200) {
|
|
if (data.code === 401 || data.code === 1005) {
|
|
uni.showToast({
|
|
mask: true,
|
|
title:data.msg,
|
|
icon:'none'
|
|
});
|
|
|
|
// setTimeout(e => {
|
|
// utils.goPath('/pages/login/login');
|
|
// },1000)
|
|
} else if (data.code === 400) {
|
|
uni.showToast({
|
|
mask: true,
|
|
title: data.msg,
|
|
icon: 'none'
|
|
})
|
|
} else if (data.code === 0) {
|
|
uni.showToast({
|
|
mask: true,
|
|
title: data.msg,
|
|
icon: 'none'
|
|
})
|
|
}else if (data.code === 7776) {
|
|
store.commit('api/setActiceDevice', {})
|
|
}
|
|
|
|
if (custom.toast !== false) {
|
|
uni.$u.toast(data.msg)
|
|
}
|
|
|
|
if (data.code == 7777) {
|
|
store.commit('api/setApiToken','')
|
|
uni.showToast({
|
|
mask: true,
|
|
title: '请重新登录',
|
|
icon: 'none'
|
|
})
|
|
setTimeout(() => {
|
|
uni.reLaunch({
|
|
url: '/pages/login/login'
|
|
})
|
|
}, 1000)
|
|
}
|
|
|
|
console.log(response.config.url)
|
|
return Promise.reject(data)
|
|
}
|
|
return data.data === undefined ? {} : data.data;
|
|
}, (response) => {
|
|
uni.showToast({
|
|
mask: true,
|
|
title: '网络断开',
|
|
icon: 'none'
|
|
})
|
|
return Promise.reject(response)
|
|
})
|
|
}
|