[Requirement] Watermarks are displayed on the system page, but not on the login page (when you log out, the login page will not display watermarks)

1. Create a watermark Js file

/* * @author: @Date: 2021-07-15 14:43:27 * @lasteditTime: 2021-07-15 15:00:27 * @lasteditors: Both Please set LastEditors * @ Description: add watermark * @ FilePath: / huashijc_MeetingSys/SRC/common/warterMark js * /
'use strict'
 
let watermark = {}
 
let setWatermark = (str) = > {
  let id = '1.23452384164.123412415'
 
  if (document.getElementById(id) ! = =null) {
    document.body.removeChild(document.getElementById(id))
  }
 
  let can = document.createElement('canvas')
  can.width = 250
  can.height = 120
 
  let cans = can.getContext('2d')
  cans.rotate(-15 * Math.PI / 150)
  cans.font = '20px Vedana'
  cans.fillStyle = 'rgba (200, 200, 200, 0.20)'
  cans.textAlign = 'left'
  cans.textBaseline = 'Middle'
  cans.fillText(str, can.width / 8, can.height / 2)
 
  let div = document.createElement('div')
  div.id = id
  div.style.pointerEvents = 'none'
  div.style.top = '35px'
  div.style.left = '0px'
  div.style.position = 'fixed'
  div.style.zIndex = '100000'
  div.style.width = document.documentElement.clientWidth + 'px'
  div.style.height = document.documentElement.clientHeight + 'px'
  div.style.background = 'url(' + can.toDataURL('image/png') + ') left top repeat'
  document.body.appendChild(div)
  return id
}
 
// This method can only be called once
watermark.set = (str) = > {
  let id = setWatermark(str)
  setInterval(() = > {
    if (document.getElementById(id) === null) {
      id = setWatermark(str)
    }
  }, 500)
  window.onresize = () = > {
    setWatermark(str)
  }
}

const outWatermark = (id) = > {
    if (document.getElementById(id) ! = =null) {
      const div = document.getElementById(id)
      div.style.display = 'none'
    }
}
watermark.out = () = > {
    const str = '1.23452384164.123412415'
    outWatermark(str)
}
 
export default watermark
Copy the code

2. Import operations

2.1 References in app.vue or other pages

// 1. Import the app. vue file
import Watemark from '@/common/watermark';

computed: {
  userName() {
    const name = this.$store.state.user.name
    return (name && name.length > 0)? name :'User name not obtained'}},mounted() {
  Watermark.set(this.userName)
}

// 2. Reference in other pages
import Watemark from '@/common/watermark';

created() {
  Watermark.set('admin')}Copy the code

2.2 Referenced in the Router Configuration File


const outWatermark = (id) = > {
  if (document.getElementById(id) ! = =null) {
    const div = document.getElementById(id)
    div.style.display = 'none'
  }
}

router.afterEach((to) = > {
	if(to.path == '/'){
		Watermark.out() // Clear the watermark
	}else{
		Watermark.set('User name not obtained') // Set watermark title}});Copy the code