You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
128 lines
3.1 KiB
128 lines
3.1 KiB
import axios from 'axios' |
|
import utils from '@/utils/encode' |
|
const service = axios.create({ |
|
baseURL: 'http://192.168.0.104:48080', |
|
timeout: 5000 |
|
}) |
|
service.interceptors.request.use( |
|
config => { |
|
const env = process.env.NODE_ENV |
|
|
|
// 拦截请求 |
|
uni.showLoading({ |
|
title: '加载中...' |
|
}) |
|
const token = uni.getStorageSync('Authorization') |
|
const openid = uni.getStorageSync('device') |
|
console.log('取出来缓存' ,token,openid) |
|
|
|
config.headers['Authorization'] = token |
|
config.headers['device'] = openid |
|
|
|
if (env === 'production') { |
|
// 生产环境,加密,不输出任何东西 |
|
// 设置jsessionid和token |
|
const JSESSIONID = utils.uuid() |
|
config.headers['JSESSIONID'] = JSESSIONID |
|
config.headers['token'] = utils.bcrypt(JSESSIONID) |
|
if (!notEncrypt && encryptWhite.indexOf(config.url) < 0) { |
|
const data = { // 用于存储加密 |
|
params: '' // 加密后的密文 |
|
} |
|
// 要加密 |
|
data.params = utils.encrypt(JSON.stringify(config.data)) |
|
config.data = data |
|
} |
|
} else { |
|
console.log('请求路径', config.url, '参数加密前', config.data) |
|
} |
|
|
|
return config |
|
}, |
|
error => { |
|
uni.hideLoading() |
|
uni.showToast({ |
|
title: '网络超时', |
|
icon: "none" |
|
}) |
|
return Promise.reject(error) |
|
} |
|
) |
|
|
|
service.interceptors.response.use( |
|
response => { |
|
const res = response.data |
|
const env = process.env.NODE_ENV |
|
|
|
if (env === 'production') { |
|
// 生产环境,进行加密解密,不输出日志 |
|
if (res.encrypt === 1) { |
|
// 加密的数据,需要解密 |
|
const dataParam = JSON.parse(utils.decrypt(res.data)) |
|
res.data = JSON.stringify(dataParam) === '{}' ? null : dataParam |
|
} |
|
} else { |
|
console.log('请求路径', response.config.url, '返回结果未加密', res) |
|
console.log('-------------------------------------------') |
|
} |
|
|
|
uni.hideLoading() |
|
if (res.code != 20000) { |
|
uni.showToast({ |
|
title: res.msg, |
|
icon: "none" |
|
}) |
|
if(res.code===42011){ |
|
console.log(res.code) |
|
uni.navigateTo({ |
|
url:'/pages/login/login', |
|
success: () => { |
|
console.log('跳转成功') |
|
}, |
|
fail: (err) => { |
|
console.log(err) |
|
} |
|
}) |
|
} |
|
} |
|
return res |
|
}, |
|
error => { |
|
uni.hideLoading() |
|
uni.showToast({ |
|
title: '请求失败!', |
|
icon: "none" |
|
}) |
|
return Promise.reject(error) |
|
} |
|
) |
|
|
|
// #ifndef H5 |
|
service.defaults.adapter = function(config) { |
|
return new Promise((resolve, reject) => { |
|
var settle = require('axios/lib/core/settle'); |
|
var buildURL = require('axios/lib/helpers/buildURL'); |
|
uni.request({ |
|
method: config.method.toUpperCase(), |
|
url: config.baseURL + buildURL(config.url, config.params, config.paramsSerializer), |
|
header: config.headers, |
|
data: config.data, |
|
dataType: config.dataType, |
|
responseType: config.responseType, |
|
sslVerify: config.sslVerify, |
|
complete: function complete(response) { |
|
response = { |
|
data: response.data, |
|
status: response.statusCode, |
|
errMsg: response.errMsg, |
|
header: response.header, |
|
config: config |
|
}; |
|
settle(resolve, reject, response); |
|
} |
|
}) |
|
}) |
|
} |
|
// #endif |
|
|
|
export default service
|
|
|