ann0707
2018-08-16 c9bc8ec61cff4076132f6396d99d383a2cdf5a03
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/**
 * 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;