zoukankan      html  css  js  c++  java
  • 1111

    import axios from 'axios';
    
    let env = process.env.NODE_ENV;
    var needLogin = "";
    let root = '';
    var header='';
    
    if (env === 'development') {
        console.log("api");
        root = '';
    } else if (env === 'production') {
        console.log("pro");
        root = '';
    } else {
        throw '请检查process.env.NODE_ENV的值,是否符合这些值之一:development,production';
    }
    
    Date.prototype.format = function (fmt) {
        var o = {
            "y+": this.getFullYear(),
            "M+": this.getMonth() + 1, //月份 
            "d+": this.getDate(), //日 
            "H+": this.getHours(), //小时 
            "m+": this.getMinutes(), //分 
            "s+": this.getSeconds() //秒 
        };
        if (!fmt) {
            fmt = 'yyyy-MM-dd HH:mm:ss';
        }
        if (/(y+)/.test(fmt))
            fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
        for (var k in o) {
            if (new RegExp("(" + k + ")").test(fmt)) {
                fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
            }
        }
        return fmt;
    }
    
    // 自定义判断元素类型JS
    function toType(obj) {
        return ({}).toString.call(obj).match(/s([a-zA-Z]+)/)[1].toLowerCase();
    }
    
    // 参数过滤函数
    function filterNull(o) {
        for (var key in o) {
            if (o[key] === null) {
                delete o[key];
            }
            if (toType(o[key]) === 'string') {
                o[key] = o[key].trim();
            } else if (toType(o[key]) === 'date') {
                o[key] = (o[key].format());
            } else if (toType(o[key]) === 'object') {
                o[key] = filterNull(o[key]);
            } else if (toType(o[key]) === 'array') {
                o[key] = filterNull(o[key]);
            }
        }
        return o;
    }
    
    function apiAxios(method, url, params, success, failure, authFail) {
        console.log('url:' + url);
        if (params) {
            params = filterNull(params);
        }
    
        var base = "";
        if (url.indexOf(".html") != -1) {
            base = "";
        } else {
            base = root;
        }
    
        axios({
            method: method,
            url: url,
            data: method === 'POST' || method === 'PUT' || method === 'DELETE' ? params : null,
            params: method === 'GET' ? params : null,
            baseURL: base,
            withCredentials: true
        }).then(function (res) {
            if (res.status >= 200 && res.status <= 210) {
                if (success) {
                    success(res);
                }
            } else {
                //不走
                // window.alert('error: ' + JSON.stringify(res.data));
            }
        }).catch(function (err) {
            let res = err.response;
            if (err && res) {
                console.log(res.status);
                if (res.status == 504) {
                    alert("服务器连接失败!请检查您的网络或服务器!!");
                    return;
                } else if (res.status == 401) {
                    if (authFail) {
                        localStorage.setItem('login', '');
                        authFail(res);
                    } else {
                        // var msg = "客户端未登录请重新登录!";
                        // if (confirm(msg) == true) {
                        showHeaderSignin();
                        // sessionStorage.setItem('needLogin', '1');
                        // window.location.href = window.location.href.split(host)[0] + host;
                        // let argu = {needLogin:true};
                        // this.$router.push({
                        //     name: 'tzdIndex',
                        //     params: argu
                        // });
                        // window.location.reload();
                        // }
                        return;
                    }
                } else if(res.status == 750){
                    var host = window.location.host;
                    window.location.href = window.location.href.split(host)[0] + host;
                }
    
                if (failure) {
                    failure(res);
                } else {
                    window.alert(res.data);
                }
            } else {
                if(authFail){
                    localStorage.setItem('login', '');
                    authFail(res);
                }else{
                    window.alert(err);
                }
            }
            // else if (err && !res) {
            //     // 跨域也会出现此提示
            //     var msg = "客户端未登录请重新登录(如非登录超时请反馈给技术)!";
            //     if (confirm(msg) == true) {
    
            //         Cookies.remove('user');
            //         Cookies.remove('password');
            //         Cookies.remove('access');
    
            //         window.location.reload();
    
            //     }
    
            // }
        });
    }
    
    // 返回在vue模板中的调用接口
    export default {
        get: function (url, params, success, failure,authFail) {
            return apiAxios('GET', url, params, success, failure,authFail);
        },
        post: function (url, params, success, failure) {
            return apiAxios('POST', url, params, success, failure);
        },
        put: function (url, params, success, failure) {
            return apiAxios('PUT', url, params, success, failure);
        },
        delete: function (url, params, success, failure) {
            return apiAxios('DELETE', url, params, success, failure);
        },
        initHeader: function (headerModel) {
            header = headerModel;
            if (needLogin && needLogin == "1") {
                header.showSignin();
            }
        }
    };
    
    function showHeaderSignin() {
        if (header) {
            if(localStorage.getItem('login') == "logining"){
                alert.eduToast('登录已过期请重新登录!',2000);
            }
            localStorage.setItem('login', '');
            header.showSignin();
        } else {
            localStorage.setItem('login', '');
            needLogin = "1";
        }
    }
  • 相关阅读:
    起步X5 UI模型使用的新的JAVASCRIPT UI库 DHTMLX (简称DHX)
    行车记录仪
    牛客网——华为机试(题9:提取不重复的整数)(Java)
    牛客网——华为机试(题8:合并表记录)(Java)
    牛客网——华为机试(题7:取近似值)(Java)
    牛客网——华为机试(题6:质数因子)(Java)
    牛客网——华为机试(题5:进制转换)(Java)
    牛客网——华为机试(题4:字符串分隔)(Java)
    牛客网——华为机试(题2:计算字符个数)(Java)
    牛客网——华为机试(题1:字符串最后一个单词的长度)(Java)
  • 原文地址:https://www.cnblogs.com/fqh123/p/10167055.html
Copyright © 2011-2022 走看看