Files
tougao_web/src/main.js
2026-03-09 16:50:59 +08:00

270 lines
9.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import Vue from 'vue';
import App from './App.vue';
import router from './router';
import ElementUI, { TabPane } from 'element-ui';
import VueI18n from 'vue-i18n';
import { messages } from './components/common/i18n';
import 'element-ui/lib/theme-chalk/index.css'; // 默认主题
import locale from 'element-ui/lib/locale/lang/en.js'; // 中英文
// import './assets/css/theme-green/index.css'; // 浅绿色主题
import './assets/css/icon.css';
import './components/common/directives';
import 'babel-polyfill';
import api from './api/index.js';
import Common from './components/common/common'
import store from './store' // 引入 store
Vue.prototype.$bus = new Vue();
// Vue.prototype.$validateString = function (str) {
// return /^[a-zA-Z\s\u00C0-\u00FF\u0100-\u017F-]+$/.test(str);
// }
//除了中文,数字不能用,其他都可以
Vue.prototype.$validateString = function (str) {
// 匹配规则:不包含数字 (0-9) 且 不包含中文字符 (\u4e00-\u9fa5)
return !/[0-9\u4e00-\u9fa5]/.test(str);
}
window.MathJax = {
tex: {
inlineMath: [['$', '$'], ['\\(', '\\)']], // 行内公式
displayMath: [['$$', '$$'], ['\\[', '\\]']], // 块级公式
},
svg: { fontCache: 'global' }
};
// 监听 DOM 变化,并自动渲染公式
Vue.prototype.$renderMath = function () {
if (window.MathJax && window.MathJax.typeset) {
setTimeout(() => {
window.MathJax.typeset();
}, 100); // 延迟防止 Vue 还未完全渲染
} else {
console.warn('MathJax 未正确加载');
}
};
import VXETable from 'vxe-table'
import 'vxe-table/lib/style.css'
Vue.use(VXETable)
// 引入富文本编辑器
import VueQuillEditor from 'vue-quill-editor'
// 富文本编辑器对应的样式
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
// 注册富文本编辑器组件为全局组件
Vue.use(VueQuillEditor)
async function loadJournalType() {
// 1. 获取并尝试解析缓存数据
const cachedString = localStorage.getItem('journalTypeData');
let parsedData = null;
if (cachedString) {
try {
// 必须先解析 JSON 字符串
parsedData = JSON.parse(cachedString);
} catch (e) {
console.error("缓存数据解析失败,将重新请求 API:", e);
}
}
// 2. 检查解析后的数据是否有效(是数组且长度大于 0
if (parsedData && Array.isArray(parsedData) && parsedData.length > 0) {
// 缓存有效,直接返回缓存数据
return parsedData;
}
// 3. 如果缓存无效或不存在,执行 API 调用
try {
// 第一个 API 调用,使用 await 接收结果
const journalRes = await api.post('api/Articletype/getArticleType', {});
if (journalRes.status === 1) {
// 存储 journal data
localStorage.setItem('journalTypeData', JSON.stringify(journalRes.data.base));
localStorage.setItem('journalTypeDataAll', JSON.stringify([...journalRes.data.base, ...journalRes.data.supplement]));
}
// 第二个 API 调用,串行执行
const medicalRes = await api.post('api/Articletype/getMedicalType', {});
if (medicalRes.status === 1) {
// 存储 medical data
localStorage.setItem('opMedicalListData', JSON.stringify(medicalRes.data));
}
// 4. 返回第一个 API 调用的主要数据
if (journalRes.status === 1) {
return journalRes.data.base;
}
} catch (error) {
// 集中处理 API 调用失败
console.error('Error loading data:', error);
return []; // 失败时返回一个默认的空数组,避免程序崩溃
}
// 如果所有流程都没有返回,默认返回空数组
return [];
}
async function loadJournalList() {
try {
const localData = store.state.journalList;
if (localData && Array.isArray(localData) && localData.length > 0) {
return localData;
}
const res = await api.post('api/Article/getJournal', {});
if (res) {
const journalList = res;
store.commit('setJournalList', journalList);
return journalList;
} else {
return [];
}
} catch (error) {
}
}
// 时间过滤器
// 定义时间过滤器(不含时分秒)
Vue.filter('formatDate', function (originVal) {
const dt = new Date(originVal * 1000)
const y = dt.getFullYear()
const m = (dt.getMonth() + 1 + '').padStart(2, '0')
const d = (dt.getDate() + '').padStart(2, '0')
const hh = (dt.getHours() + '').padStart(2, '0')
const mm = (dt.getMinutes() + '').padStart(2, '0')
const ss = (dt.getSeconds() + '').padStart(2, '0')
return `${y}-${m}-${d}`
})
// 含时分秒
Vue.filter('formatDatehms', function (originVal) {
const dt = new Date(originVal * 1000)
const y = dt.getFullYear()
const m = (dt.getMonth() + 1 + '').padStart(2, '0')
const d = (dt.getDate() + '').padStart(2, '0')
const hh = (dt.getHours() + '').padStart(2, '0')
const mm = (dt.getMinutes() + '').padStart(2, '0')
const ss = (dt.getSeconds() + '').padStart(2, '0')
return `${y}-${m}-${d} ${hh}:${mm}:${ss}`
})
// 引入wps文档编辑
import mammoth from "mammoth";
import commonJS from '@/common/js/commonJS.js'
Vue.prototype.$commonJS = commonJS
Vue.prototype.Common = Common;
// import { getJournalTypeName } from '@/common/js/commonJS.js';
Vue.filter('jtName', function (value) {
return commonJS.getJournalTypeName(value);
});
// 使用 ES Module
import * as echarts from 'echarts';
Vue.prototype.$echarts = echarts
Vue.prototype.$api = api
Vue.config.productionTip = false;
import Editor from "@tinymce/tinymce-vue"; // 默认导入
Vue.component("Editor", Editor);
//自定义组件
const components = {
'common-table': () => import('@/components/page/components/table/table.vue'),
'common-author-comment-editor': () => import('@/components/page/components/Tinymce/AuthorCommentEditor.vue'),
'common-review-article': () => import('@/components/page/components/reviewArticle/index.vue'),
'common-author-article': () => import('@/components/page/components/reviewArticle/author.vue'),
'common-editor-article': () => import('@/components/page/components/EditorArticle/index.vue'),
'common-editor-article-detail': () => import('@/components/page/components/EditorArticle/detail.vue'),
'common-late-x': () => import('@/components/page/components/table/LateX.vue'),
'common-major': () => import('@/components/page/components/major/index.vue'),
'common-major-list': () => import('@/components/page/components/major/list.vue'),
'common-paypal-button': () => import('@/components/page/components/pendingPayment/PayPalButton.vue'),
'common-tiff': () => import('@/components/page/components/table/tiff.vue'),
'common-content': () => import('@/components/page/components/table/content.vue'),
'common-word': () => import('@/components/page/components/table/word.vue'),
'common-comment': () => import('@/components/page/components/table/comment.vue'),
'common-edit-table': () => import('@/components/page/components/table/editTable.vue'),
'common-annotations': () => import('@/components/page/components/table/annotations.vue'),
'common-word-html': () => import('@/components/page/components/table/wordHtml.vue'),
'common-word-html-type-setting': () => import('@/components/page/components/table/wordHtmlTypesetting.vue'),
'common-drag-word': () => import('@/components/page/components/table/dragWord.vue'),
'common-media-link-dialog': () => import('@/components/page/components/table/MediaLinkDialog.vue'),
};
Object.keys(components).forEach(key => {
Vue.component(key, components[key]);
});
Vue.use(VueI18n);
Vue.use(ElementUI, {
size: 'small',
locale
});
const i18n = new VueI18n({
locale: localStorage.getItem('langs') || 'en',
messages
});
//使用钩子函数对路由进行权限跳转
router.beforeEach(async (to, from, next) => {
const currentRoute = to; // 获取当前路由路径,例如 "/home"
if (currentRoute.meta.hideJournal) {
} else {
try {
// 尝试请求接口(即使失败也继续后续逻辑)
await Promise.all([
loadJournalList(),
loadJournalType()
]);
} catch (err) {
// 仅打印错误,不阻断路由
}
}
// 无论接口成功/失败,都执行原有跳转逻辑
document.title = `${to.meta.title} | Traditional Medicine Research`;
const role = localStorage.getItem('U_name');
const userrole = localStorage.getItem('U_status');
if (!role && to.path != '/register' && to.path !== '/submission' && to.path !== '/verification' && to.path !== '/orcidLink' && to.path !== '/img' && to.path !== '/reviewer' && to.path !== '/thanks' && to.path !== '/login' && to.path !== '/refuse' && to.path !== '/managing' && to.path.search(/retrieve/i) < 0) {
next('/login');
} else {
if (navigator.userAgent.indexOf('MSIE') > -1 && to.path === '/editor') {
Vue.prototype.$alert('vue-quill-editor组件不兼容IE10及以下浏览器请使用更高版本的浏览器查看', '浏览器不兼容通知', {
confirmButtonText: '确定'
});
} else {
next();
}
}
});
new Vue({
router,
i18n,
store,
render: h => h(App),
mounted() {
this.$renderMath(); // 页面加载后自动渲染
}
}).$mount('#app');