main.js 7.83 KB
/* eslint-disable eqeqeq, node/prefer-global/process */

import { BrowserTracing } from "@sentry/tracing";
// 接入sentry
import * as Sentry from "@sentry/vue";
import { Request } from "glsk-toolkit";
// Vue
import Vue from "vue";
// import { GLCombiForm } from "glsx-components" // 权限按钮 (根据权限动态创建)
import VueLazyload from "vue-lazyload";

import { config } from "@/common/baseConfig";
import developConfig from "@/common/baseConfig.develop.json";
import { installVueMapApi } from "@/common/installVueMapApi";
import notice from "@/common/notice";
import {
	mergeConfigTarget,
	parseConfigCenterResponse,
} from "@/common/parseConfigCenterResponse";
import util from "@/libs/util.js";
// 针对template模板中使用可选链操作符
import chaining from "@/plugin/chaining";
// 核心插件
import d2Admin from "@/plugin/d2admin";
// store
import store from "@/store/index";
// import axios from 'axios'
// import i18n from './i18n'
import App from "./App";
// import D2Crud from '@d2-projects/d2-crud'
// 菜单和路由设置
import router from "./router";
import "default-passive-events";
import "element-ui/lib/theme-chalk/index.css";

installVueMapApi(Vue);
Vue.use(VueLazyload, {
	// 懒加载默认加载图片
	loading:
		"https://img02.glsx.com.cn/weapp/resource/saas/dj/images/img_loading.png",
	attempt: 3, // 尝试加载次数
});
// 实例化
process.env.NODE_ENV === "production"
&& process.env.VUE_APP_SIMPLE_SENTRY === "true"
&& Sentry.init({
	Vue,
	dsn: "https://39bc80d3bb0a419a8d7d9bf5a39678e3@sentry.glsx.com.cn/24",
	// 与 vue.config 中 SentryWebpackPlugin.release 一致,sourcemap 才能正确解析栈
	release: process.env.VUE_APP_VERSION,
	// dsn: 'http://6225fb8e73fb453a8455a9371f3517da@192.168.3.115:9000/4',
	integrations: [
		new BrowserTracing({
			routingInstrumentation: Sentry.vueRouterInstrumentation(router),
			tracingOrigins: ["localhost", "my-site-url.com", /^\//],
		}),
	],
	// Set tracesSampleRate to 1.0 to capture 100%
	// of transactions for performance monitoring.
	// We recommend adjusting this value in production
	tracesSampleRate: 1.0,
	logErrors: true,
	// 解决火狐浏览器 无法捕获错误的问题
	beforeSend(event, hint) {
		const isNonErrorException
			= event.exception.values[0].value.startsWith(
				"Non-Error exception captured",
			)
			|| hint.originalException.message.startsWith(
				"Non-Error exception captured",
			);
		if (isNonErrorException) {
			// We want to ignore those kind of errors
			return null;
		}
		return event;
	},
	// "send-default-pii": true
});

// d2Admin 已导入
// import ElementUI from 'element-ui'
// Vue.use(ElementUI)

// 核心插件
Vue.use(d2Admin);
Vue.use(chaining);
// Vue.use(GLCombiForm)
Vue.prototype.notice = notice;

const isProd = process.env.NODE_ENV === "production";
// console.log(isProd)
if (!isProd) {
	Vue.config.devtools = true;
}

Vue.prototype.$EventBus = new Vue();

Vue.directive("preventReClick", {
	bind: (el, binding) => {
		let time = binding.value; // 防抖时间
		if (!time) {
			// 用户若不设置防抖时间,则默认1s
			time = 1000;
		}
		let fun;
		el.addEventListener(
			"click",
			(event) => {
				if (!fun) {
					// 第一次执行
					fun = setTimeout(() => {
						fun = null;
					}, time);
				} else {
					// 直接取消当前事件
					event && event.stopImmediatePropagation();
				}
			},
			true,
		);
	},
});
const baseConfig = config;
let configCenterLoaded = false;

// 如果要改地址, 优先去 .env.development 中配置
// 如果要创建自己的配置  可以 创建.env.development.local 文件.  该文件已被 git忽略, 不会上传, 优先级比.env.development更高, 满足本地开发的需求.
// 生产环境和测试环境用的都是 .env文件里面的内容. 不会串
// 这样配置, 不会再出现 改了地址 , 上传到git 甚至 上到测试环境 的情况
// 唯一需要注意的是, 改了 .env.development.local 文件的内容, 需要重新 npm run serve

/**
 * 另: 在本地开发时,优先级是 .env.development.local > .env.development > .env.production > .env
 * 以 VUE_APP_A  这个变量为例
 * 如果 .env.development.local 文件没有, 就会取 .env.development 中的, 以此类推直到.env, 都没有  则返回 undefined
 * 如果 所有 文件都有这个变量,  优先取 .env.development.local的
 * 在生产环境中:
 * 只取 .env.production  .env 两个文件中的.
 */

const configCenterUrl
	= process.env.VUE_APP_CONFIG_CENTER_URL
		|| "https://config-center-backend.glsx.com.cn/glsxAdmin/config?moduleUrl=dj-car-life-system-config";
console.log("configCenterUrl", configCenterUrl);
// 使用fetch 解决axios提前实例化导致sign里面的secret不能通过配置中心动态获取
fetch(configCenterUrl)
	.then(
		(response) => {
			// 处理http响应
			return response.json();
		},
		(error) => {
			// 处理错误
			console.log(error);
		},
	)
	.then((response) => {
		console.log("response", response?.code, response);
		const parsed = parseConfigCenterResponse(response);
		window.$gscApi = new Request({
			...response.gsc,
			// baseURL:'http://192.168.3.145:9898/gsc-vcp-web-facade',
			// baseURL: 'http://gsc.test.glsx.com.cn/gsc-vcp-web-facade',
			session: {
				requiresSession: true,
				sessionId: _ => util.cookies.get("token"),
			},
			events: {
				error: (error, config) => {
					Vue.prototype.$message.error(error.msg);
				},
				logout: (response, config) => {
					store.dispatch("d2admin/user/clear");
					util.cookies.remove("token");
					router.replace("/login");
					Vue.prototype.$message.error("登录超时,请重新登录");
				},
			},
		});
		if (parsed) {
			configCenterLoaded = true;
			mergeConfigTarget(baseConfig, parsed);
			console.log("config center loaded", baseConfig.env, baseConfig.axios?.baseURL);
		} else {
			const { code } = response || {};
			if (code == "200") {
				console.log(response?.data);
			}
			if (response && typeof response === "object") {
				mergeConfigTarget(baseConfig, response);
			}
			window.$fetchConfig = response;
			console.log(response);
			window.$gscApi = new Request({
				...response.gsc,
				// baseURL:'http://192.168.3.145:9898/gsc-vcp-web-facade',
				// baseURL: 'http://gsc.test.glsx.com.cn/gsc-vcp-web-facade',
				session: {
					requiresSession: true,
					sessionId: _ => util.cookies.get("token"),
				},
				events: {
					error: (error, config) => {
						Vue.prototype.$message.error(error.msg);
					},
					logout: (response, config) => {
						store.dispatch("d2admin/user/clear");
						util.cookies.remove("token");
						router.replace("/login");
						Vue.prototype.$message.error("登录超时,请重新登录");
					},
				},
			});
			// console.log(window.$gscApi, "========");
		}
	})
	.finally(() => {
		init();
	});
function applyDevelopAxiosConfig() {
	if (configCenterLoaded && process.env.VUE_APP_USE_DEVELOP_AXIOS !== "true") {
		return;
	}
	if (process.env.NODE_ENV === "development" && developConfig?.axios) {
		baseConfig.axios = {
			...baseConfig.axios,
			...developConfig.axios,
		};
		if (developConfig.env) {
			baseConfig.env = developConfig.env;
		}
		console.log("develop axios fallback", baseConfig.env, baseConfig.axios?.baseURL);
	}
}

function init() {
	applyDevelopAxiosConfig();
	new Vue({
		router,
		store,
		// i18n,
		render: (h) => {
			// baseConfig.axios.baseURL = 'http://dj.test.glsx.com.cn/rop-dj-smartcarlife/router'
			// data
			// console.log(data)
			Vue.prototype.baseConfig = baseConfig;
			return h(App);
		},
		created() {
			// 系统配置
			Vue.config.devtools = true;
			// 展示系统信息
			this.$store.commit("d2admin/releases/versionShow");
			// 用户登录后从数据库加载一系列的设置
			this.$store.dispatch("d2admin/account/load");
			// 获取并记录用户 UA
			this.$store.commit("d2admin/ua/get");
			// 初始化全屏监听
			this.$store.dispatch("d2admin/fullscreen/listen");
		},
		mounted() {},
	}).$mount("#app");
}
export { baseConfig };