Files
LSM_OIL_SITE_zhongping/utils/request.js
2023-04-26 14:03:20 +08:00

252 lines
6.8 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 utils from '@/utils/encode'
// const env = 'test' /* */
// const env = 'test'// 仅用于测试、
const env = 'production'
//42 洛超 /29 孙志华 /40 阿浩
// 测试日日顺时需要连阿浩
const testUrl = 'http://192.168.23:38080'
// const productionUrl = 'https://www.51xingka.net/oilApp' // 生产,加密 old
// const productionUrl = 'https://www.xingoil.com/adminapi' // 生产,加密 new
// const productionUrl = 'https://www.xingoil.com/adminapi' // 生产,加密 new
const productionUrl = 'http://uat.xingoil.com/adminapi'
// const env = process.env.NODE_ENV // 环境
const service = axios.create({
baseURL: env=='production'?productionUrl:testUrl, //
// baseURL: testUrl, //
timeout: 5000
})
var url = ''
let outTimer = null
service.interceptors.request.use(
config => {
console.log('这里是config',config)
// if(config.url.indexOf('/oil-user/oilCustomerChannelRelation/getByPhone/') != -1){
// uni.hideToast()
// }else{
// 拦截请求
uni.showLoading({
title: '加载中...',
mask:true
})
// }
const token = uni.getStorageSync('Authorization')
const openid = uni.getStorageSync('device')
const openId = uni.getStorageSync('openid')
const unionId = uni.getStorageSync('unionid')
const accountSources = 'LOGIN_MP_WECHAT'
// console.log(unionId)
// 原本为 openid
config.headers['Authorization'] = token
config.headers['imei'] = unionId
config.headers['openId'] = openId
config.headers['dataSources'] = 'MP'
config.headers['accountSources'] = accountSources
if (env === 'production') {
// 生产环境,加密,不输出任何东西
// 设置jsessionid和token
console.log('这里是生产')
const JSESSIONID = utils.uuid()
config.headers['JSESSIONID'] = JSESSIONID
config.headers['token'] = utils.md5Salt(JSESSIONID)
// if ( encryptWhite.indexOf(config.url) < 0) {
// if (!notEncrypt && encryptWhite.indexOf(config.url) < 0) {
// 这是定义在哪的
const data = { // 用于存储加密
params: '' // 加密后的密文
}
// console.log('请求路径', config.url, '参数加密前', config.data)
// 要加密
data.params = utils.encrypt(JSON.stringify(config.data)) //加密
config.data = data
// }
} else {
// console.log('请求路径', config.url, '参数加密前', config.data)
url = config.url
}
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
// const env = 'production' // 测试中的生茶
if (env === 'production') {
// 生产环境,进行加密解密,不输出日志
if (res.encrypt == 1) {
// 加密的数据,需要解密
const dataParam = JSON.parse(utils.decrypt(res.data))
// console.log('解密前的data===========',res)
// console.log('解密后的data===========',dataParam)
// 数据解密 赋值
res.data=JSON.stringify(dataParam) === '{}' ? null : dataParam
// console.log('这里是res解密后的',res.data)
// console.log('生产环境 请求路径', response.config.url, '解密为', res.data)
}
} else {
console.log('请求路径', response.config.url, '返回结果未加密', res)
console.log('-------------------------------------------')
}
uni.hideLoading()
if (res.code != 20000) {
console.log('这里是登录报错处',res)
if(response.config.url.indexOf('/oil-user/oilCustomerChannelRelation/getByPhone/') != -1){
// uni.hideToast()
}else{
uni.showToast({
title: res.msg,
icon: "none"
})
}
if (res.msg === '登录已过期!') {
outTimer = setTimeout(() => {
uni.showModal({
title: '下线提醒',
confirmText: '重新登录',
cancelText: "退出",
content: '您的账号在另一微信下登录了星油云站,请确认是否本加油站工作人员操作',
success: function(res) {
if (res.confirm) {
uni.reLaunch({
url: '/pages/login/boforeLogin/boforeLogin',
success: () => {
console.log('跳转成功')
},
fail: (err) => {
console.log(err)
}
})
} else if (res.cancel) {
uni.reLaunch({
url: '/pages/login/boforeLogin/boforeLogin?type=1',
success: () => {
console.log('跳转成功')
},
fail: (err) => {
console.log(err)
}
})
}
}
});
}, 1000)
}
if(res.code == 42011){
uni.showToast({
title: res.msg,
icon: "none"
})
uni.reLaunch({
url: '/pages/login/boforeLogin/boforeLogin',
success: () => {
console.log('跳转成功')
},
fail: (err) => {
console.log(err)
}
})
}
if(res.code == 42077){
uni.showToast({
title: res.msg,
icon: "none"
})
uni.reLaunch({
url: '/pages/login/boforeLogin/boforeLogin',
success: () => {
console.log('跳转成功')
},
fail: (err) => {
console.log(err)
}
})
}
if (res.code != 20000 && res.msg != '令牌为空,不允许操作' && res.code != 42077 && res.code != 42011 &&res.msg != '您的ip已经更改请重新登录' && res.msg != '登录与操作设备不同') {
uni.showToast({
title: res.msg,
icon: "none"
})
// uni.reLaunch({
// url: '/pages/login/boforeLogin/boforeLogin',
// success: () => {
// console.log('跳转成功')
// },
// fail: (err) => {
// console.log(err)
// }
// })
}
}
return res
},
error => {
uni.hideLoading()
uni.getNetworkType({
success: function(res) {
console.log(res.networkType);
if (res.networkType === 'none') {
uni.showToast({
title: '没有网啦!',
icon: "none"
})
} else {
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