当前位置:   article > 正文

【Vue】Vue项目,根据登录用户给所有页面添加水印_watermark-dom

watermark-dom

最近做了一个需求,给项目中所有的页面加上“当前登录人”的水印,找了一些资料,总结了两种可用方案,已在项目中测试有效。

第一种方案:watermark-dom 插件
1、安装命令

npm install watermark-dom --save


2、在App.vue文件中

  1. import watermark from 'watermark-dom'
  2. export default {
  3.   name: 'App',
  4.   watch: { //切换路由重新加载
  5.     $route (to, from) {
  6.       const dataS = JSON.parse(sessionStorage.getItem("user"))
  7.       let nameS = dataS.userName
  8.       setTimeout(() => {
  9.         watermark.init({ watermark_txt: nameS })
  10.       }, 1000)
  11.     }
  12.   },
  13.   mounted () {
  14.     setTimeout(() => {
  15.       //在缓存中获取当前登录人信息
  16.       const dataS = JSON.parse(sessionStorage.getItem("user"))
  17.       let nameS = dataS.userName
  18.       watermark.init({ watermark_txt: nameS })
  19.     }, 1000)
  20.   }
  21. }


通过以上两步,就可以实现在所有页面中添加水印。

第二种方案:在网上找的手写Js
1、新建 waterMark.js 文件

  1. 'use strict'
  2. const watermark = {}
  3. const setWatermark = (str, container) => {
  4.   const id = '1.23452384164.123412415'
  5.   if (container === undefined) {
  6.     return
  7.   }
  8.  
  9.   // 查看页面上有没有,如果有则删除
  10.   if (document.getElementById(id) !== null) {
  11.     const childelement = document.getElementById(id)
  12.     childelement.parentNode.removeChild(childelement)
  13.   }
  14.   var containerWidth = container.offsetWidth // 获取父容器宽
  15.   var containerHeight = container.offsetHeight // 获取父容器高
  16.   container.style.position = 'relative' // 设置布局为相对布局
  17.   // 创建canvas元素(先制作一块背景图)
  18.   const can = document.createElement('canvas')
  19.   can.width = 260 // 设置每一块的宽度
  20.   can.height = 260 // 高度
  21.   const cans = can.getContext('2d') // 获取canvas画布
  22.   cans.rotate(-20 * Math.PI / 180) // 逆时针旋转π/9  
  23.   cans.font = '24px Vedana' // 设置字体
  24.   cans.fillStyle = 'rgba(0, 0, 0, 0.1)' // 设置字体的颜色
  25.   cans.textAlign = 'left' // 文本对齐方式
  26.   cans.textBaseline = 'Middle' // 文本基线
  27.   cans.fillText(str, 0, 4 * can.height / 5) // 绘制文字
  28.   // 创建一个div元素
  29.   const div = document.createElement('div')
  30.   div.id = id // 设置id
  31.   div.style.pointerEvents = 'none' // 取消所有事件
  32.   div.style.top = '0px'
  33.   div.style.left = '0px'
  34.   div.style.position = 'absolute'
  35.   div.style.zIndex = '100000'
  36.   div.style.width = containerWidth + 'px'
  37.   div.style.height = containerHeight + 'px'
  38.   div.style.background = 'url(' + can.toDataURL('image/png') + ') left top repeat'
  39.   container.appendChild(div) // 追加到页面
  40.   return id
  41. }
  42. // 该方法只允许调用一次
  43. watermark.set = (str, container) => {
  44.   let id = setWatermark(str, container)
  45.   setInterval(() => {
  46.     if (document.getElementById(id) === null) {
  47.       id = setWatermark(str, container)
  48.     }
  49.   }, 500)
  50.   // 监听页面大小的变化
  51.   window.onresize = () => {
  52.     setWatermark(str, container)
  53.   }
  54. }
  55. export default watermark


2、 在App.vue文件中

  1. <template>
  2.   <div id="app" ref="containerS"> //ref是必须的
  3.   </div>
  4. </template>
  5. import Watermark from './utils/waterMark.js'
  6.   watch:{
  7.     $route(to,from){
  8.       const dataS = JSON.parse(sessionStorage.getItem("user"))
  9.       let nameS = dataS.userName
  10.       this.$nextTick(function(){
  11.         Watermark.set(nameS, this.$refs.containerS);
  12.       })
  13.     }
  14.   },
  15.   mounted(){
  16.     const dataS = JSON.parse(sessionStorage.getItem("user"))
  17.     let nameS = dataS.userName
  18.     this.$nextTick(function(){
  19.       Watermark.set(nameS, this.$refs.containerS);
  20.     })
  21.   },


注意:方式和第一种方案类似,但是需要在元素上加 ref

第二种方案,也可以达到效果。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/480384
推荐阅读
相关标签
  

闽ICP备14008679号