chengkun
2025-08-06 b54e02d98e42ae73071e3c01e59f12671d13d06a
public/static/vue/mixin.js
@@ -1,251 +1,40 @@
var menu_index = $('#menu_index').val();
const { createI18n, useI18n } = VueI18n;
// 通过选项创建 VueI18n 实例
const i18n = createI18n({
    locale: localStorage.getItem('changjiang-park-lang') || 'zh-cn',
    messages,
    fallbackLocale: 'zh-cn',
    legacy: false,
})
const sharedMixin = {
    data() {
        return {
            menu_index: '',
            side_menu_collapse: false,
            default_aside_width: "250px",
            el_aside_width: "340px",
            side_menu_show_type: 1,/////左侧菜单收放状态,1=展开,2=收缩
            breadcrumbList: [],
            menuList: [],///菜单列表
            url: '',
            lastColumn: 100,
            currentLang: localStorage.getItem('lang') || 'zh-cn',
            langPag: {},// 存储加载后的语言包
            languageList: [
                { id: 1, name_loc: '简体中文', symbol: 'zh-cn' },
                { id: 2, name_loc: 'English', symbol: 'en-us' },
                { id: 3, name_loc: 'ar', symbol: 'ar' },
                // { id: 3, name_loc: 'ar', symbol: 'ar' },
            ],
            currentLang: '',
            i18n: i18n.global, // 引入i18n实例
        }
    },
    // 生命周期 - 创建完成(可以访问当前this实例)
    mounted() {
        setTimeout(() => {
            this.observeLastColumnWidth();
        }, 100);
        this.getLanguage();
        if (this.currentLang == 'en-us') {
            this.el_aside_width = this.default_aside_width = "340px"
        }
        else if (this.currentLang == 'zh-cn') {
            this.el_aside_width = this.default_aside_width = "250px";
        }
        else {
            this.el_aside_width = this.default_aside_width = "300px";
        }
    },
    // 创建生命周期
    created() {
        this.getMenu();
        this.changeLang(this.currentLang, false);
        this.currentLang = this.i18n.locale || 'zh-cn';
    },
    // 方法
    methods: {
        getLanguage() {
            const that = this;
            const url = "/supplier/login/getLanguage.html";
            postRequest(url, {}).then((res) => {
                if (res.data.code === 200) {
                    this.languageList = res.data.list
                }
            }).catch((error) => {
            });
        },
        // 异步加载语言包的方法
        async loadLanguage(lang, ifreload) {
            try {
                localStorage.setItem('lang', lang);//设置默认语言
                const response = await fetch(`/static/lang/1/${lang}.json`);
                this.langPag = await response.json();
                this.setLanguage(lang, ifreload);
            } catch (error) {
                // 回退到中文
                const zhResponse = await fetch('/static/lang/1/zh-cn.json');
                this.langPag = await zhResponse.json();
            }
        },
        // 切换语言
        async changeLang(lang, ifreload) {
            if (!['zh-cn', 'en-us', 'ar'].includes(lang)) {
                lang = 'zh-cn';
            }
            this.currentLang = lang;
            await this.loadLanguage(lang, ifreload);
        },
        // 翻译方法
        t(key, value = '') {
            if (value == '') {
                value = key
            }
            return key.split('.').reduce((obj, k) => obj?.[k], this.langPag) || value;
        },
        setLanguage(lang, ifreload) {
            let url = "/supplier/login/setlang.html"
            postRequest(url, { lang: lang }).then(res => {
                if (res.data.code == 200) {
                    if (ifreload) {
                        window.location.reload();
                    }
                }
            });
        },
        /**
         * 高级替换函数
         * @param {string} original - 原始文本
         * @param {string|RegExp} search - 要查找的内容或正则表达式
         * @param {string|function} replacement - 替换内容或替换函数
         * @param {boolean} caseSensitive - 是否区分大小写
         * @returns {string} 替换后的文本
         */
        replaceStr(original, search, replacement, caseSensitive = true) {
            if (!original || !search) return original;
            const flags = caseSensitive ? 'g' : 'gi';
            const regex = typeof search === 'string'
                ? new RegExp(search, flags)
                : search;
            return original.replace(regex, replacement);
        },
        sharedMethod() {
            // 共享的方法逻辑
        },
        /////切换语言/////
        switchLanguage(type) {
            /////提交保存到数据库默认语言类型/////
            /////切换语言//////
            window.switchLanguage(type);
        },
        /////左侧菜单跳转/////
        goto_menu(url) {
            window.location.href = url;
        },
        /////左侧菜单收放状态
        show_side_menu() {
            if (this.side_menu_show_type == 1) {
                /////设置为收缩状态/////
                this.el_aside_width = "auto";
                this.side_menu_show_type = 2;
                this.side_menu_collapse = true;
            } else {
                /////设置为展开状态/////
                this.el_aside_width = this.default_aside_width;
                this.side_menu_show_type = 1;
                this.side_menu_collapse = false;
            }
        },
        getMenu() {
            let that = this;
            let url = "/supplier/index/getMenu.html"
            console.log(menu_index)
            postRequest(url, { menuindex: menu_index }).then(res => {
                if (res.data.code == 200) {
                    that.menuList = res.data.data;
                    that.menu_index = menu_index
                    this.url = new URL(window.location.href)
                    let menus = this.getMenus(that.menuList)
                    let home = [{ title: that.menuList[0].title, menu_url: '/supplier/index/index.html' }]
                    if (menus !== undefined) {
                        if (this.url.pathname !== '/supplier/index/index.html' && this.url.href !== '/supplier/index/index.html') {
                            menus = home.concat(menus)
                        }
                    } else {
                        menus = home
                    }
                    this.breadcrumbList = menus
                }
            });
        },
        getMenus(menuList, arr, z) {
            arr = arr || []
            z = z || 0
            for (let i = 0; i < menuList.length; i++) {
                let item = menuList[i]
                arr[z] = item
                if (menuList[i].menu_url != null && menuList[i].menu_url != '') {
                    if (this.url.pathname === menuList[i].menu_url || this.url.pathname.includes(menuList[i].menu_url.slice(0, -5))) {
                        return arr.slice(0, z + 1)
                    }
                }
                if (menuList[i].children && menuList[i].children.length) {
                    let res = this.getMenus(menuList[i].children, arr, z + 1)
                    if (res) {
                        return res
                    }
                }
            }
        },
        curentTime() {
            var now = new Date();
            var year = now.getFullYear();       // 年
            var month = now.getMonth() + 1;     // 月
            var day = now.getDate();            // 日
            var hh = now.getHours();            // 时
            var mm = now.getMinutes();          // 分
            var ss = now.getSeconds();          // 秒
            var clock = year + "-";
            if (month < 10)
                clock += "0";
            clock += month + "-";
            if (day < 10)
                clock += "0";
            clock += day + " ";
            if (hh < 10)
                clock += "0";
            clock += hh + ":";
            if (mm < 10)
                clock += "0";
            clock += mm + ":";
            if (ss < 10)
                clock += "0";
            clock += ss;
            return clock;
        },
        //获取表格中最后一列的宽度
        observeLastColumnWidth() {
            // 获取表格的 DOM 元素
            if (typeof this.$refs.tableRef == 'undefined') {
                return false;
            }
            try {
                const tableHeader = this.$refs.tableRef.$el.querySelector('.el-table__header-wrapper').querySelector('colgroup');
                if (tableHeader) {
                    // 获取最后一列的 DOM 元素
                    const lastColumn = tableHeader.querySelector('col:last-child');
                    if (lastColumn) {
                        // 创建 ResizeObserver 实例
                        const observer = new ResizeObserver((entries) => {
                            for (const entry of entries) {
                                const columnWidth = entry.contentRect.width; // 获取列宽
                                this.lastColumn = columnWidth;
                            }
                        });
                        // 监听最后一列的宽度变化
                        observer.observe(lastColumn);
                    }
                }
            }
            catch (error) {
                return false;
            }
        },
        //设置最后一列的对齐方式
        alignValue(width) {
            if (this.lastColumn > width) {
                return 'left';
            } else {
                return 'center';
            }
        changeLanguage(newLang) {
            this.i18n.locale = newLang;
            this.i18n.currentLocale = newLang;
            localStorage.setItem('changjiang-park-lang', newLang);
        },
    }
}