// const electron = require('electron')
import storage from './storage'

let imgUrlDomain = process.env.VUE_APP_IMG_URL_DOMAIN
let errorCount = 1;

export default {
    // 状态码通用处理
    xhrStatus(res) {
        if (res.status === 997) {
            if(errorCount == 1){
                var v = this;
                this.$layer.alert('当前未登录状态，请登录',{
                    shadeClose: false
                }, function(id){
                    v.$layer.close(id);
                    var imei = storage.getItem('IMEI');
                    window.localStorage.clear();
                    storage.setItem("IMEI", imei);
                    v.$router.replace("/login");
                    errorCount = 1;
                });
                errorCount ++;
            }
        } else if (res.status === 998) {
            if(errorCount == 1){
                var v = this;
                this.$layer.alert('登录超时，请检查网络重新登录', {
                    shadeClose: false
                },function(id){
                    v.$layer.close(id);
                    var imei = storage.getItem('IMEI');
                    window.localStorage.clear();
                    storage.setItem("IMEI", imei);
                    v.$router.replace("/login");
                    errorCount = 1;
                });
                errorCount ++;
            }
        } else if (res.status === 999) {
            if(errorCount == 1){
                var v = this;
                this.$layer.alert(res.msg,{
                    shadeClose: false
                }, function(id){
                    v.$layer.close(id);
                    var imei = storage.getItem('IMEI');
                    window.localStorage.clear();
                    storage.setItem("IMEI", imei);
                    v.$router.replace("/login");
                    errorCount = 1;
                });
                errorCount ++;
            }
        } else {
            this.$toast(res.msg || '错误' + res.status + '，请联系客服')
        }
    },
    // 头像图片路径通配
    setImgUrl(str) {
        if (typeof str !== 'string') {
            return ''
        }
        if (str.length === 0) {
            return ''
        } else {
            if (str.indexOf('../') > -1) {
                // 访问本地路径
                return './images/' + str.split('/')[str.split('/').length - 1]
            } else if ((str.indexOf('http://') > -1) || (str.indexOf('https://') > -1)) {
                return str
            } else {
                // 服务器上的图片路径
                return imgUrlDomain + str
            }
        }
    }
}
