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.
214 lines
4.6 KiB
214 lines
4.6 KiB
import Vue from 'vue'
|
|
import axios from 'axios'
|
|
import qs from 'qs'
|
|
import md5 from 'js-md5';
|
|
import constant from './constant.js';
|
|
|
|
let baseURL = '';
|
|
// #ifdef H5
|
|
baseURL = constant.H5_BASE_URL
|
|
// #endif
|
|
// #ifdef APP-PLUS
|
|
baseURL = constant.BASE_URL
|
|
// #endif
|
|
const service = axios.create({
|
|
withCredentials: true,
|
|
crossDomain: true,
|
|
baseURL: baseURL,
|
|
timeout: 160000
|
|
})
|
|
|
|
// request拦截器,在请求之前做一些处理
|
|
service.interceptors.request.use(
|
|
config => {
|
|
const language = uni.getStorageSync("language")|| 'en_US';
|
|
console.log('语言', language)
|
|
if (constant.SHOW_DIALOG) {
|
|
uni.showLoading({
|
|
title: 'loading',
|
|
mask: true
|
|
})
|
|
}
|
|
|
|
if (config.url.indexOf('market') > -1) {
|
|
// #ifdef H5
|
|
config.baseURL = constant.H5_MARKET_URL;
|
|
// #endif
|
|
// #ifdef APP-PLUS
|
|
config.baseURL = constant.MARKET_URL
|
|
// #endif
|
|
|
|
}
|
|
|
|
const ticket = uni.getStorageSync('ticket')
|
|
if (ticket) {
|
|
if(!config.data){
|
|
config.data = {};
|
|
}
|
|
config.data['ticket'] = ticket
|
|
}
|
|
if (Vue.prototype.$showDialog) {
|
|
uni.showLoading({
|
|
title: 'loading',
|
|
mask: true
|
|
})
|
|
}
|
|
// if (store.state.token) {
|
|
// // 给请求头添加user-token
|
|
// config.headers["user-token"] = store.state.token;
|
|
// }
|
|
//const ticket = Vue.prototype.$store.state.ticket
|
|
|
|
|
|
let dataStr = [];
|
|
if (config.method == 'get') {
|
|
|
|
if (config.url.split('?').length > 1) {
|
|
dataStr = config.url.split('?')[1].split('&');
|
|
}
|
|
|
|
}
|
|
|
|
if (config.method == 'post') {
|
|
dataStr = decodeURIComponent(qs.stringify(config.data)).split('&');
|
|
}
|
|
//console.log(config.url,111111,config.data,dataStr)
|
|
config.data = {};
|
|
dataStr.forEach(function(e) {
|
|
config.data[e.split('=')[0]] = e.split('=')[1];
|
|
});
|
|
|
|
|
|
var keys = [];
|
|
let sign = '';
|
|
|
|
var t = Date.now() - 2901;
|
|
|
|
config.data['lang'] = language;
|
|
for (var k in config.data)
|
|
keys.push(k);
|
|
keys.sort(function(a, b) {
|
|
return a < b ? -1 : 1;
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
keys.forEach(function(e) {
|
|
|
|
if (config.data[e] || config.data[e] == '') {
|
|
sign += e + '=' + config.data[e] + '&'
|
|
}
|
|
|
|
});
|
|
|
|
|
|
var key = "key=" + constant.KEY;
|
|
|
|
|
|
console.log(sign + key)
|
|
sign = md5(sign + key);
|
|
if (config.method == 'post') {
|
|
config.data['sign'] = sign
|
|
|
|
config.data = qs.stringify(config.data).replace("timestamp=&", "")
|
|
|
|
|
|
}
|
|
if (config.method == 'get') {
|
|
|
|
config.url = config.url + '&sign=' + sign
|
|
}
|
|
|
|
return config;
|
|
},
|
|
error => {
|
|
console.error(error); // for debug
|
|
uni.hideLoading();
|
|
return Promise.reject(error);
|
|
}
|
|
);
|
|
|
|
//配置成功后的拦截器
|
|
service.interceptors.response.use(res => {
|
|
|
|
uni.hideLoading()
|
|
|
|
res=res.data;
|
|
console.log('数据',res)
|
|
// console.log(res.data)
|
|
// console.log(res.data.data[0].nameAlias)
|
|
if (res.code===0||res.success) {
|
|
return res.data;
|
|
}
|
|
|
|
uni.showToast({
|
|
title: res.errMsg,
|
|
icon: 'none',
|
|
duration: 1500
|
|
})
|
|
if (res.errCode === 'SYS.0006') {
|
|
uni.setStorageSync('ticket',null);
|
|
uni.setStorageSync('logInfo',null);
|
|
uni.reLaunch({
|
|
url: '/pages/login/index'
|
|
})
|
|
// #ifdef H5
|
|
var url = location.href;
|
|
if (url.indexOf('pages') > -1) {
|
|
uni.setStorage({
|
|
key: 'url',
|
|
data: url,
|
|
success: function() {
|
|
console.log('success')
|
|
}
|
|
});
|
|
} else {
|
|
uni.removeStorageSync('url')
|
|
}
|
|
// #endif
|
|
}
|
|
uni.$emit('refreshQrCode')
|
|
return Promise.reject(res.errMsg);
|
|
}, error => {
|
|
uni.hideLoading();
|
|
return Promise.reject(error)
|
|
})
|
|
|
|
|
|
axios.defaults.adapter = function(config) { //自己定义个适配器,用来适配uniapp的语法
|
|
return new Promise((resolve, reject) => {
|
|
// console.log(config)
|
|
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) {
|
|
//console.log("执行完成:",config.baseURL + buildURL(config.url, config.params, config.paramsSerializer),response.data)
|
|
response = {
|
|
data: response.data,
|
|
status: response.statusCode,
|
|
errMsg: response.errMsg,
|
|
header: response.header,
|
|
config: config
|
|
};
|
|
|
|
settle(resolve, reject, response);
|
|
},
|
|
fail: res => {
|
|
uni.showLoading({
|
|
title: res,
|
|
mask: true
|
|
})
|
|
}
|
|
})
|
|
})
|
|
}
|
|
export default service
|
|
|