diff --git a/src/components/common/app-provider.vue b/src/components/common/app-provider.vue index 36a361a..eb9a8cf 100644 --- a/src/components/common/app-provider.vue +++ b/src/components/common/app-provider.vue @@ -30,6 +30,7 @@ const ContextHolder = defineComponent({ + diff --git a/src/components/common/app-watermark.vue b/src/components/common/app-watermark.vue new file mode 100644 index 0000000..dd8488e --- /dev/null +++ b/src/components/common/app-watermark.vue @@ -0,0 +1,37 @@ + + + + + diff --git a/src/hooks/common/watermark.ts b/src/hooks/common/watermark.ts deleted file mode 100644 index 0d31dd3..0000000 --- a/src/hooks/common/watermark.ts +++ /dev/null @@ -1,130 +0,0 @@ -import type { Ref } from 'vue'; -import { ref, shallowRef, unref } from 'vue'; -import { debounce } from '@/utils/common'; - -type Attrs = { - textStyles?: { - font?: string; - fillStyle?: string; - rotate?: number; - width?: number; - height?: number; - }; - styles?: { [key: string]: any }; -}; - -type WatermarkOpts = { - appendEl?: Ref; - id?: string; -}; - -export function useWatermark(opts: WatermarkOpts = {}) { - // const id = `waterMark_${Math.random().toString().slice(-10)}_${+new Date()}` - const appendEl = opts.appendEl || (ref(document.body) as Ref); - const watermarkEl = shallowRef(); - - /** 绘制canvas文字背景图 */ - const createCanvasBase64 = (str: string, attrs: Attrs = {}) => { - const can = document.createElement('canvas'); - const { rotate, font, fillStyle, width = 200, height = 140 } = attrs.textStyles || {}; - Object.assign(can, { width, height }); - const cans = can.getContext('2d'); - if (cans) { - cans.rotate((-(rotate ?? 20) * Math.PI) / 180); - cans.font = font || '12px Vedana'; - cans.fillStyle = fillStyle || 'rgba(200, 200, 200, 0.3)'; - cans.textAlign = 'left'; - cans.textBaseline = 'middle'; - cans.fillText(str, can.width / 10, can.height / 2); - } - return can.toDataURL('image/png'); - }; - - /** 页面随窗口调整更新水印 */ - const updateWatermark = ( - watermarkOpts: { - str?: string; - attrs?: Attrs; - width?: number; - height?: number; - } = {} - ) => { - const el = unref(watermarkEl); - if (!el) return; - if (typeof watermarkOpts.width !== 'undefined') { - el.style.width = `${watermarkOpts.width}px`; - } - if (typeof watermarkOpts.height !== 'undefined') { - el.style.height = `${watermarkOpts.height}px`; - } - if (typeof watermarkOpts.str !== 'undefined') { - el.style.background = `url(${createCanvasBase64(watermarkOpts.str, watermarkOpts.attrs)}) left top repeat`; - } - }; - - /** 绘制水印层 */ - const createWatermark = (str: string, attrs: Attrs = {}) => { - if (watermarkEl.value) { - updateWatermark({ str, attrs }); - return watermarkEl; - } - const div = document.createElement('div'); - watermarkEl.value = div; - if (opts.id) { - const last_el = document.getElementById(opts.id); - if (last_el) { - document.body.removeChild(last_el); - } - div.id = opts.id; - } - Object.assign( - div.style, - { - pointerEvents: 'none', - top: '0px', - left: '0px', - position: 'fixed', - zIndex: '100000' - }, - attrs.styles || {} - ); - const el = unref(appendEl); - if (!el) return watermarkEl; - const { clientHeight: height, clientWidth: width } = el; - updateWatermark({ str, attrs, width, height }); - el.appendChild(div); - return watermarkEl; - }; - - const debounceUpdateResize = debounce( - () => { - const el = unref(appendEl); - if (!el) return; - const { clientHeight: height, clientWidth: width } = el; - updateWatermark({ width, height }); - }, - 30, - false - ); - - /** 对外提供的设置水印方法 */ - const setWatermark = (str: string, attrs: Attrs = {}) => { - createWatermark(str, attrs); - window.addEventListener('resize', debounceUpdateResize); - }; - - /** 清除水印 */ - const clearWatermark = () => { - let domId: HTMLElement | null | undefined = unref(watermarkEl); - if (!domId && opts.id) { - domId = document.getElementById(opts.id); - } - watermarkEl.value = undefined; - const el = unref(appendEl); - if (!el) return; - domId && el.removeChild(domId); - window.removeEventListener('resize', debounceUpdateResize); - }; - - return { setWatermark, clearWatermark }; -} diff --git a/src/layouts/modules/theme-drawer/modules/page-fun.vue b/src/layouts/modules/theme-drawer/modules/page-fun.vue index a63ebcb..7e39d64 100644 --- a/src/layouts/modules/theme-drawer/modules/page-fun.vue +++ b/src/layouts/modules/theme-drawer/modules/page-fun.vue @@ -106,9 +106,6 @@ const isWrapperScrollMode = computed(() => themeStore.layout.scrollMode === 'wra - - - diff --git a/src/store/modules/auth/index.ts b/src/store/modules/auth/index.ts index 975ab80..0777b15 100644 --- a/src/store/modules/auth/index.ts +++ b/src/store/modules/auth/index.ts @@ -10,14 +10,11 @@ import { $t } from '@/locales'; import { roleTypeRecord } from '@/constants/business'; import { useRouteStore } from '../route'; import { useTabStore } from '../tab'; -import { useThemeStore } from '../theme'; import { clearAuthStorage, getToken } from './shared'; export const useAuthStore = defineStore(SetupStoreId.Auth, () => { - const appTitle = import.meta.env.VITE_APP_TITLE || 'Snail Job'; const route = useRoute(); const routeStore = useRouteStore(); - const themeStore = useThemeStore(); const tabStore = useTabStore(); const { toLogin, redirectFromLogin } = useRouterPush(false); const { loading: loginLoading, startLoading, endLoading } = useLoading(); @@ -58,8 +55,6 @@ export const useAuthStore = defineStore(SetupStoreId.Auth, () => { authStore.$reset(); - themeStore.setWatermarkText(appTitle); - if (!route.meta.constant) { await toLogin(); } @@ -143,7 +138,6 @@ export const useAuthStore = defineStore(SetupStoreId.Auth, () => { localStg.set('userInfo', info); localStg.set('userInfo', info); Object.assign(userInfo, info); - themeStore.setWatermarkText(`${userInfo.userName}@${appTitle}`); return true; } diff --git a/src/store/modules/theme/index.ts b/src/store/modules/theme/index.ts index 1dc61bf..2b348b4 100644 --- a/src/store/modules/theme/index.ts +++ b/src/store/modules/theme/index.ts @@ -5,7 +5,6 @@ import { useEventListener, usePreferredColorScheme } from '@vueuse/core'; import { getPaletteColorByNumber } from '@sa/color'; import { SetupStoreId } from '@/enum'; import { localStg } from '@/utils/storage'; -import { useWatermark } from '@/hooks/common/watermark'; import { addThemeVarsToGlobal, createThemeToken, @@ -55,24 +54,13 @@ export const useThemeStore = defineStore(SetupStoreId.Theme, () => { */ const settingsJson = computed(() => JSON.stringify(settings.value)); - /** Watermarks */ - const { setWatermark, clearWatermark } = useWatermark({ id: 'global_watermark_id' }); - - /** 开启水印 */ + /** + * Set theme watermark + * + * @param visible + */ function toggleWatermark(visible: boolean = false) { - visible ? setWatermark(settings.value?.watermark.text) : clearWatermark(); - } - - /** 修改水印文案 */ - function setWatermarkText(text: string) { - if (!text) { - clearWatermark(); - return; - } - if (settings.value.watermark && settings.value.watermark?.visible) { - settings.value.watermark.text = text; - setWatermark(settings.value.watermark.text); - } + settings.value.watermark.visible = visible; } /** Reset store */ @@ -204,15 +192,6 @@ export const useThemeStore = defineStore(SetupStoreId.Theme, () => { }, { immediate: true } ); - - watch( - settings.value?.watermark, - val => { - toggleWatermark(val?.visible); - setWatermarkText(val?.text); - }, - { immediate: true } - ); }); /** On scope dispose */ @@ -233,7 +212,6 @@ export const useThemeStore = defineStore(SetupStoreId.Theme, () => { updateThemeColors, setThemeLayout, setLayoutReverseHorizontalMix, - setWatermarkText, toggleWatermark }; }); diff --git a/src/theme/settings.ts b/src/theme/settings.ts index ceaa2cc..b3e0fae 100644 --- a/src/theme/settings.ts +++ b/src/theme/settings.ts @@ -71,8 +71,7 @@ export const themeSettings: App.Theme.ThemeSetting = { } }, watermark: { - visible: true, - text: import.meta.env.VITE_APP_TITLE || 'Snail Job' + visible: true } }; diff --git a/src/typings/app.d.ts b/src/typings/app.d.ts index 4a1acaa..4e42dc5 100644 --- a/src/typings/app.d.ts +++ b/src/typings/app.d.ts @@ -104,8 +104,6 @@ declare namespace App { watermark: { /** Whether to show the watermark */ visible: boolean; - /** WatermarkText */ - text: string; }; }