/**
|
* Created by c.y on 2018/3/16.
|
* Updated by 汤彦祖 on 2018/4/11
|
* 采用axios发送请求,这是对请求的全局处理
|
* 在获取数据时应当防止用户操作,打开loding
|
*/
|
import Vue from 'vue';
|
import {AjaxPlugin, md5} from 'vux';
|
import sign from '../tool/loginSign';
|
import store from '../store/index';
|
|
Vue.use(AjaxPlugin);
|
|
// 创建实例
|
const Service = Vue.http.create({
|
timeout: 1000 * 6, // 6秒超时
|
headers: {'Content-Type': 'application/json; charset=UTF-8'}
|
});
|
|
// 全局拦截器设置
|
Service.interceptors.request.use(config => {
|
// Do something before request is sent
|
// ================begin ===================
|
let clientInfo = window.localStorage.getItem('newClientInfo');
|
let clicentPara = JSON.parse(clientInfo);
|
//sysType为2是EC,C端为1
|
clicentPara.sysType = 1;
|
store.commit('UPDATE_LOADING',{isLoading:true});
|
if (config.method === 'post') { // post 封装数据
|
if (typeof (config.data) !== 'object') {
|
config.data = {};
|
}
|
for (let attr in clicentPara) {
|
config.data[attr] = clicentPara[attr];
|
}
|
} else if (config.method === 'get') { // get 封装数据
|
if (config.params === undefined) {
|
config.params = {};
|
}
|
for (let attr in clicentPara) {
|
config.params[attr] = clicentPara[attr];
|
}
|
config.params['mblNo'] = config.mblNo;
|
}
|
let _newSid = window.sessionStorage.getItem('newSid');
|
// 验签 用于所有没有登录并传递有电话号码的接口验签
|
if (_newSid === null) {
|
if (config.method === 'post') {
|
let _configstr = JSON.stringify(config.data);
|
const _indexNum = _configstr.indexOf('mblNo');
|
if (_indexNum !== -1) {
|
let _strmd = 'jttech_ver_code_sign_75a7_';
|
_strmd += config.data.mblNo;
|
let _url = config.url.split('/');
|
_strmd += '_' + _url[_url.length - 1];
|
config.data['anonySign'] = md5(_strmd);
|
}
|
} else {
|
let _configstr = JSON.stringify(config.params);
|
const _indexNum = _configstr.indexOf('mblNo');
|
if (_indexNum !== -1) {
|
let _strmd = 'jttech_ver_code_sign_75a7_';
|
_strmd += config.params.mblNo;
|
let _url = config.url.split('/');
|
_strmd += '_' + _url[_url.length - 1];
|
config.params['anonySign'] = md5(_strmd);
|
}
|
}
|
} else {
|
// 参数签名
|
// 必须由http或https开头的请求,才会进行自动签名组装
|
if (config.params) {
|
config.params = sign.signForFormDataOrUrl (config.params, true, _newSid);
|
} else if (config.data) {
|
config.data = sign.signForFormDataOrUrl (config.data, false, _newSid);
|
} else {
|
if (config.method === 'GET') {
|
config.params = sign.signForFormDataOrUrl (config.data, false, _newSid);
|
} else {
|
config.data = sign.signForFormDataOrUrl (config.data, false, _newSid);
|
}
|
}
|
|
|
}
|
// ================end ===================
|
return config;
|
}, (error) => {
|
store.commit('UPDATE_LOADING',{isLoading:false});
|
return Promise.reject(error);
|
});
|
|
// response统一处理操作
|
Service.interceptors.response.use(res => {
|
store.commit('UPDATE_LOADING',{isLoading:false});
|
return res.data;
|
}, (error) => {
|
store.commit('UPDATE_LOADING',{isLoading:false});
|
return Promise.reject(error);
|
});
|
|
export default Service;
|