main.js 9.83 KB
import Vue from "vue";
import App from "./App.vue";
import router from "./router/index";

//ElementUI  地址:https://element.eleme.io/#/zh-CN/guide/design
import ElementUI from "element-ui";
import "element-ui/lib/theme-chalk/index.css";
Vue.use(ElementUI);

import { Message } from 'element-ui'
// 挂载到$message上  Message.error(txt)
Vue.prototype.$message = Message

// http://momentjs.cn/ 日期处理组件 获取当前月初、月末、时间区间段 等
import moment from "moment"
Vue.prototype.$moment = moment;

//ajax 实现异步请求的技术
import axios from "axios";
// 挂载到vue的全局(原型上),在每个组件都可以使用 ,prototype是固定的,$axios是自定义的
Vue.prototype.$axios = axios;

//dataV  地址:http://datav.jiaminghi.com/
import dataV from "@jiaminghi/data-view";
Vue.use(dataV);

//vue-seamless-scroll简单无缝滚动组件 目前基本上使用少,使用datav轮播居多
//https://chenxuan0000.github.io/vue-seamless-scroll/zh/guide/01-basic.html
import scroll from "vue-seamless-scroll";
Vue.use(scroll);

//echarts  地址:https://echarts.apache.org/zh/index.html this.$echarts(opt)
import echarts from "echarts";
Vue.prototype.$echarts = echarts;


//【线下地址 根据业务修改 电视机上调试需要使用自身电脑真实IP】
Vue.prototype.baseUrlOff = "http://127.0.0.1:6001";

//【根据实际需求 项目可能有多个不同的接口地址、默认值不要改】
Vue.prototype.baseUrlOnLine = window.appConfig.baseUrlOnLine;
Vue.prototype.baseUrlOnLine2 = window.appConfig.baseUrlOnLine2;
Vue.prototype.baseUrlOnLine3 = window.appConfig.baseUrlOnLine3;
Vue.prototype.baseUrlOnLine4 = window.appConfig.baseUrlOnLine4;

Vue.prototype.baseUrlOnLine5 = window.appConfig.baseUrlOnLine5;
Vue.prototype.baseUrlOnLine6 = window.appConfig.baseUrlOnLine6;

// 更新头部时间并刷新页面
let eleHeadTime = null;
String.prototype.ajaxSuccessActionLoad = function (res) {
    let timestamp = res.data["timestamp"]
    if (!timestamp) {
        // Message.error({
        //     message: "接口API 不存在【timestamp】字段属性,请核实接口字段信息",
        //     duration: 5000
        // });
        return;
    }
    let nowDateVal = moment(timestamp).format("YYYY-MM-DD")
    if (nowDateVal.indexOf("1970") > -1) nowDateVal = moment.unix(timestamp).format("YYYY-MM-DD")
    if (eleHeadTime == null) {
        eleHeadTime = document.querySelector('#head_time')
    }
    let val = eleHeadTime.innerText;
    if (val != nowDateVal && val != '') {
        // window.location.reload()
    }
    eleHeadTime.innerText = nowDateVal
};
//ajax 数据返回判断
String.prototype.ajaxSuccessBefore = function (result, logTitle) {
    // "".Log(result, logTitle)
    let isOk = false;
    let txt = "";
    if (typeof result.data.code != "undefined" && result.data.code == 401) {
        txt = `Tokens失效请重新登入<br>地址:【${result.config.url}】`;
        isOk = true;
    } else if (typeof result.data.code != "undefined" && result.data.code != "Success" || result.data.Code == 500) {
        txt = `本页数据请求成功之后存在错误 :【${JSON.stringify(result.data)}
               <br>地址【${result.config.url}
               <br>请求方式【${result.config.method}
               <br>参数【${JSON.stringify(result.config.data || {})}
               <br>httpCode${result.status}
               <br><br>服务器api接口请求数据出现异常,点击任意空白处继续或刷新浏览器,问题频繁出现请联系管理员!`;
        isOk = true;
    }
    if (isOk) {
        Message.error({
            message: txt,
            dangerouslyUseHTMLString: true,// 允许使用 HTML
            duration: 5000
        });
    }
    return isOk;
};

//ajax 错误提示 ElementUI 
//其他页面使用案例: this.$alert(msg, "提示", { dangerouslyUseHTMLString: true })
String.prototype.ajaxError = function (result, url) {
    try {
        // 安全处理result参数
        if (!result) {
            let txt = `本页中存在错误
                       <br>URL${url}
                       <br>错误信息【参数异常】
                       <br><br>请联系管理员! <br>`;
            Message.error({
                message: txt,
                dangerouslyUseHTMLString: true,
                duration: 5000
            });
            return false;
        }

        var msg = result.toString();
        if (msg.indexOf("SyntaxError") > -1) {
            let txt = `本页中存在JavaScript语法错误,请开发者仔细核实!
                       <br><br>错误信息【${msg}
                       <br><br>URL${url}】`;
            Message.error({
                message: txt,
                dangerouslyUseHTMLString: true,
                duration: 5000
            });
            return false;
        }
    } catch (error) {
        // console.error('ajaxError处理异常:', error);
        let txt = `本页中存在错误
                   <br>URL${url}
                   <br>错误信息【处理异常】
                   <br><br>请联系管理员! <br>`;
        Message.error({
            message: txt,
            dangerouslyUseHTMLString: true,
            duration: 5000
        });
        return false;
    }

    try {
        var types = typeof result.code;
        const status = types == "undefined" ? 501 : (result.response ? result.response.status : 501);
        var txt = `本页中存在错误
                   <br>URL${url}
                   <br>httpCode${status},${result.message || '未知错误'} 
                   <br><br>您访问的接口资源可能不存在,或者不支持跨域,请检查Url地址是否正确, 使用postman核实一下,反复出现请联系接口管理员! <br>`;
        if (typeof result.message != "undefined") {
            "".Log(result, "JS内部方法异常")
            "".Log(txt, "JS错误信息")
            if (status == 501) {
                txt += "<h2 style='color: red;'>【JS内部错误!】</h2>";
            }
            Message.error({
                message: txt,
                dangerouslyUseHTMLString: true,// 允许使用 HTML
                duration: 5000
            });
            return false;
        }

        "".Log(result, "ajaxError")

        switch (status) {
            case 404:
                txt += "<h2 style='color: red;'>【您访问的接口资源不存在,请检查Url地址是否正确!】</h2>";
                break;
            case 500:
                txt += "<h2 style='color: red;'>【服务器内存API出现异常,反复出现请联系接口管理员!】</h2>";
                break;
            case 0:
                txt +=
                    "<h2 style='color: red;'>【您访问的接口不支持跨域或者接口资源不存在,请联系接口管理员!】</h2>";
                break;
            default:
                txt +=
                    "<h2 style='color: red;'>【您访问的接口资源可能不存在,或者不支持跨域,请检查Url地址是否正确,反复出现请联系接口管理员!】</h2>";
                break;
        }
        if (status != 501) {
            if (result.code == "ECONNABORTED" && result.message && result.message.indexOf("timeout") != -1) {
                txt +=
                    "<h2 style='color: red;'>【服务器响应超时,反复出现请联系接口管理员优化接口响应时间!】</h2>";
            }
        }
        if (status == 501) {
            txt += "<h2 style='color: red;'>【JS内部错误!】</h2>";
        }
        "".Log(txt, "ajaxError")
        Message.error({
            message: txt,
            dangerouslyUseHTMLString: true,// 允许使用 HTML
            duration: 5000
        });
    } catch (error) {
        // console.error('ajaxError处理异常:', error);
        let txt = `本页中存在错误
                   <br>URL${url}
                   <br>错误信息【处理异常】
                   <br><br>请联系管理员! <br>`;
        Message.error({
            message: txt,
            dangerouslyUseHTMLString: true,
            duration: 5000
        });
    }
};

//【发布】 编译的时候改成 false
Vue.config.productionTip = false;

//【baseOnLineOrOff true 线上 false 线上】
window.baseOnLineOrOff = true
if (process.env.NODE_ENV === "development") window.baseOnLineOrOff = false

// 全局异常处理
window.onerror = function (message, source, lineno, colno, error) {
    console.error('全局异常:', {
        message,
        source,
        lineno,
        colno,
        error
    });
    // 可以在这里添加错误上报逻辑
    return true; // 阻止默认处理
};

// 全局未处理的Promise rejection处理
window.addEventListener('unhandledrejection', function (event) {
    console.error('未处理的Promise rejection:', event.reason);
    event.preventDefault();
});

// 内存监控和自动清理机制
function monitorMemory() {
    if (performance && performance.memory) {
        const memory = performance.memory;
        const usedMemory = memory.usedJSHeapSize / 1024 / 1024; // MB
        const totalMemory = memory.totalJSHeapSize / 1024 / 1024; // MB
        const memoryUsage = (usedMemory / totalMemory) * 100;

        // console.log(`内存使用情况: ${usedMemory.toFixed(2)}MB / ${totalMemory.toFixed(2)}MB (${memoryUsage.toFixed(2)}%)`);

        // 当内存使用超过80%时,执行清理操作
        if (memoryUsage > 80) {
            // console.log('内存使用过高,执行清理操作');
            // 强制垃圾回收(如果可用)
            if (window.gc) {
                window.gc();
                // console.log('执行垃圾回收');
            }
            // 可以在这里添加其他清理逻辑
        }
    }
}

// 每5分钟检查一次内存使用情况
setInterval(monitorMemory, 300000); // 5分钟

new Vue({
    router,
    render: h => h(App),
}).$mount("#app");