/*
 * ajax封装:
 * 1. 引入文件
 * 2. new Vue().ajax.get(url,data,fn,ojson), 或 new Vue().ajax.post(url,data,fn,ojson)
 *     url: 需要获取数据的文件地址 (string)
 *     data: 需要发送的信息 (可省略) (obj)
 *     fn: 获取信息后的回调函数,接收到的返回值为data (function)
 *     ojson: 是否需要转换为json格式 (可省略,如果async有值，必须站位'') (设置为 "json")
 *     async:同步异步标记 true or false 默认true 可不传
 *
 * 3. new Vue().ajax.get().cancel(): 取消异步请求
 * 4. new Vue().ajax.json(str): 可转化json格式字符串
 **/
Vue.prototype.ajax = {
    ajaxStatus: true,
    sendAjax: function(url, data, success, cache, alone, async, type, dataType, loading, error) {
        var loads = loading == false ? false : true; // 是否有loading
        var type = type || 'post'; //请求类型
        var dataType = dataType || 'json'; //接收数据类型
        var async = async == false ? false : true; //异步请求
        var alone = alone || false; //独立提交（一次有效的提交）
        var cache = cache || false; //浏览器历史缓存
        var error = error || function(data) {
            var message = ""
            if (data.status == 404) {
                message = "请求失败，请求未找到"
            } else if (data.status == 503 || data.status == 504) {
                message = "请求失败，服务器内部错误"
            } else if (data.status == 502 && data.responseText == '表空间内存快满了,请相关服务人员维护人员做好措施！') {
                loading.close()
                return false;
            }
            if (!((data.status >= 200 && data.status < 300) || data.status == 304)) {
                var mes = "";
                if (mes) {
                    mes = '请求错误代码【' + data.status + "】，" + message
                } else {
                    mes = "请求错误代码【" + data.status + "】";
                }
                Vue.prototype.$alert(mes, '提示', {
                    confirmButtonText: '确定',
                    callback: action => {
                        if (loads) {
                            loading.close()
                        }
                    }
                });
            }
            if (loads) {
                loading.close()
            }
        };
        var flag = false;
        if (jQuery.type(data) == "string") {
            if (data.indexOf("[") > -1) {
                flag = true
            }
        }
        if (data.zsDjxh == undefined || data.zsDjxh == null) {
            data.zsDjxh = sessionStorage.getItem("zsDjxh")
        }
        if (data.djxh == undefined || data.djxh == null) {
            data.djxh = sessionStorage.getItem("djxh")
        }
        var option = {
            'headers': flag ? {
                'Accept': 'application/json',
                'Content-Type': 'application/json',
                "Encryption": sessionStorage.getItem("authorization_sbzj2")
            } : {
                "Encryption": sessionStorage.getItem("authorization_sbzj2")
            },
            'url': url,
            'data': data,
            'timeout': config.ajaxTimeOut, //超时时间设置，单位毫秒
            'type': type,
            'dataType': dataType,
            'async': async,
            'success': function(data) {
                if (data.returnCode == "99") {
                    var lastFlag = url.split('/')[url.split('/').length - 1]
                    if (lastFlag != "saveZzsLdczlx") {
                        Vue.prototype.$alert(data.returnMessage, '提示', {
                            confirmButtonText: '确定',
                            callback: action => {
                                if (loads) {
                                    loading.close()
                                }
                            }
                        });
                    } else {
                        if (loads) {
                            loading.close()
                        }
                    }
                } else if (data.returnCode == "-2") {
                    Vue.prototype.$alert(data.returnMessage, '提示', {
                        confirmButtonText: '确定',
                        callback: action => {
                            window.close();
                        }
                    });
                } else {
                    if (loads) {
                        loading.close()
                    }
                    success(data)
                }
            },
            'error': error,
            'jsonpCallback': 'jsonp' + (new Date()).valueOf().toString().substr(-4),
            'beforeSend': function(request) {
                if (flag) {
                    request.setRequestHeader("Content-Type", "application/json");
                }
            },
        }
        if (loads) {
            var loading = Vue.prototype.$loading({
                lock: true, //lock的修改符--默认是false
                /*  text: '拼命加载中...',//显示在加载图标下方的加载文案*/
                spinner: 'el-loading-spinner', //自定义加载图标类名
                fullscreen: true,
                background: 'rgba(0, 0, 0, 0)', //遮罩层颜色
            });
        }

        $.ajax(option); // 调用
    },

    /*  submitAjax:function (form, success, cache, alone) {
      cache = cache || true;
      var form = $(form);
      var url = form.attr('action');
      var data = form.serialize();
        this.sendAjax(url, data, success,dataType, cache, alone, false, 'post',dataType);
    },*/

    post: function(url, data, success, dataType, async, loading, cache, alone) {
        this.sendAjax(url, data, success, cache, alone, async, 'post', dataType, loading);
    },

    // ajax提交(get方式提交)
    get: function(url, success, dataType, cache, alone) {
        this.sendAjax(url, {}, success, cache, alone, false, 'get', dataType);
    },

    // jsonp跨域请求(get方式提交)
    jsonp: function(url, success, cache, alone) {
        sendAjax(url, {}, success, cache, alone, false, 'get', 'jsonp');
    },
}