//创建axios实例
instance = axios.create({
    method: 'post',
    timeout: 30000,
});

// 添加请求拦截器
instance.interceptors.request.use(function (config) {
    // 在发送请求之前做些什么
    nav.bar.show();
    if (config.url == '/upload') {
        nav.tip.show("上传中 ...");
    } else {
        nav.tip.show();
    }
    return config;
}, function (error) {
    // 对请求错误做些什么
    return Promise.reject(error);
});

// 添加响应拦截器
instance.interceptors.response.use(function (response) {
    // 对响应数据做点什么
    nav.tip.close();
    try {//确保服务器正确返回Json
        if(response.data.errors.length > 0){
            console.error(response.data.errors)
        }
        nav.bar.finish();
    }catch (e){
        nav.bar.error();
        response.data = {errors: [{message: '服务器错误'}]};
    }
    return response;
}, function (error) {
    // 对响应错误做点什么,保准化返回结果
    nav.tip.close();
    nav.bar.error();
    const rsp = {errors: []};
    if (!error.response) {
        rsp.errors.push({message: error.message});
    } else {
        switch (error.response.status) {
            case 401:
                rsp.errors.push({message: "未授权，请登录(401)"});
                break
            case 403:
                rsp.errors.push({message: "拒绝访问(403)"});
                break
            case 404:
                rsp.errors.push({message: "请求地址错误(404)"});
                break
            case 408:
                rsp.errors.push({message: "请求超时(408)"});
                break
            case 500:
                rsp.errors.push({message: "服务器内部错误(500)"});
                break
            case 501:
                rsp.errors.push({message: "服务未实现(501)"});
                break
            default:
                rsp.errors.push({message: "请求错误(" + error.response.status + ")"});
                break
        }
    }
    return Promise.reject(rsp);
});
jsonRequest = function (config) {
    return instance.request({
        params: {
            method: config.method
        },
        url: '/ajax',
        headers: {'Content-Type': 'text/plain'},
        data: config.data
    }).then(function (response) {
        return Promise.resolve(response.data);
    }, function (response) {
        return Promise.resolve(response);
    })
};
fileRequest = function (config) {
    return instance.request({
        url: '/upload',
        data: config.data,
        headers: {'Content-Type': 'multipart/form-data'},
        onUploadProgress: function (progressEvent) {
            var complete = (progressEvent.loaded / progressEvent.total * 100 | 0) + '%'
            nav.tip.show("上传中(" + complete + ")")
        }
    }).then(function (response) {
        return Promise.resolve(response.data);
    }, function (response) {
        return Promise.resolve(response);
    })
};
window.ajax = {
    login: function (data) {
        return jsonRequest({
            method: "ajax.login",
            data: data
        })
    },
    nginxStart: function (data) {
        return jsonRequest({
            method: "nginx.start",
            data: data
        })
    },
    nginxStop: function (data) {
        return jsonRequest({
            method: "nginx.stop",
            data: data
        })
    },
    nginxReload: function (data) {
        return jsonRequest({
            method: "nginx.reload",
            data: data
        })
    },
    fileUpload: function (file) {
        var fd = new FormData();
        fd.append("file", file);
        return fileRequest({
            data: fd
        })
    },
    mappingCreate: function (data) {
        return jsonRequest({
            method:"ajax.admin.mapping.create",
            data: JSON.stringify(data),
        })
    },
    mappingDelete: function (data) {
        return jsonRequest({
            method:"ajax.admin.mapping.delete",
            data: JSON.stringify(data),
        })
    },
    mappingUpdate: function (data) {
        return jsonRequest({
            method:"ajax.admin.mapping.update",
            data: JSON.stringify(data),
        })
    },
    serviceLoad: function(data) {
        return jsonRequest({
            method:"ajax.admin.service.load",
            data: JSON.stringify(data),
        })
    },
    configFlush: function(data) {
        return jsonRequest({
            method:"ajax.admin.config.flush",
            data: JSON.stringify(data),
        })
    },
}