/** * Vue插件模块 * 提供Vue应用中使用统一登录SDK的能力 */ import { Auth } from '../core/auth'; import { Storage } from '../utils/storage'; import { RouterGuard } from '../guards/router'; /** * Vue插件类 */ export class VuePlugin { /** * 构造函数 * @param storage 存储实例 */ constructor(storage) { this.auth = new Auth(storage); this.routerGuard = new RouterGuard(this.auth); } /** * 安装Vue插件 * @param app Vue构造函数或Vue 3应用实例 * @param options 插件选项 */ install(app, options) { const { config, pluginName = 'unifiedLogin' } = options; // 初始化SDK this.auth.init(config); // 判断是Vue 2还是Vue 3 const isVue3 = typeof app.config !== 'undefined'; if (isVue3) { // Vue 3 // 在全局属性上挂载SDK实例 app.config.globalProperties[`${pluginName}`] = this.auth; app.config.globalProperties.$auth = this.auth; // 兼容简写 // 提供Vue组件内的注入 app.provide(pluginName, this.auth); app.provide('auth', this.auth); // 兼容简写 // 处理路由守卫 app.mixin({ beforeCreate() { // 如果是根组件,添加路由守卫 if (this.$options.router) { const router = this.$options.router; // 添加全局前置守卫 router.beforeEach(this.routerGuard.createVueGuard()); } } }); } else { // Vue 2 // 在Vue实例上挂载SDK实例 app.prototype[`${pluginName}`] = this.auth; app.prototype.$auth = this.auth; // 兼容简写 // 全局混入 app.mixin({ beforeCreate() { // 如果是根组件,添加路由守卫 if (this.$options.router) { const router = this.$options.router; // 添加全局前置守卫 router.beforeEach(this.routerGuard.createVueGuard()); } } }); } } /** * 获取认证实例 * @returns Auth 认证实例 */ getAuth() { return this.auth; } /** * 获取路由守卫实例 * @returns RouterGuard 路由守卫实例 */ getRouterGuard() { return this.routerGuard; } } /** * 创建Vue插件实例 * @param storageType 存储类型 * @returns VuePlugin Vue插件实例 */ export function createVuePlugin(storageType) { const storage = new Storage(storageType); return new VuePlugin(storage); } //# sourceMappingURL=vue.js.map