Commit b51a1859c5b091e528eabdd27e1d45bb26a2dc3c
1 parent
05ae724c
水印添加
Showing
2 changed files
with
77 additions
and
0 deletions
src/main.js
| ... | ... | @@ -22,6 +22,9 @@ import i18n from './lang' // internationalization |
| 22 | 22 | import selectLoadMore from '@/utils/loadmore.js'; |
| 23 | 23 | import _ from 'lodash' |
| 24 | 24 | |
| 25 | +import watermark from '@/utils/shuiyin' | |
| 26 | + | |
| 27 | +Vue.prototype.$watermark = watermark; | |
| 25 | 28 | Vue.config.productionTip = false |
| 26 | 29 | Vue.prototype.$m = moment |
| 27 | 30 | Vue.use(ElementUI, { |
| ... | ... | @@ -54,6 +57,15 @@ Vue.directive('removeAriaHidden', { |
| 54 | 57 | }); |
| 55 | 58 | } |
| 56 | 59 | }); |
| 60 | + | |
| 61 | +router.afterEach((item) => { | |
| 62 | + let { userName, mobilePhone } = store.state.user.userInfo | |
| 63 | + if (item.path !== '/login') { //判断是否为登录页面,水印的内容可自定义设置 | |
| 64 | + watermark.set(userName, mobilePhone); | |
| 65 | + }else{ | |
| 66 | + watermark.set('','',''); | |
| 67 | + } | |
| 68 | +}) | |
| 57 | 69 | export default new Vue({ |
| 58 | 70 | router, |
| 59 | 71 | store, | ... | ... |
src/utils/shuiyin.js
0 → 100644
| 1 | +let watermark = {} | |
| 2 | + | |
| 3 | +let setWatermark = (str,str2,str3) => { | |
| 4 | + let id = '1.23452384164.123412416'; | |
| 5 | + | |
| 6 | + if (document.getElementById(id) !== null) { | |
| 7 | + document.body.removeChild(document.getElementById(id)); | |
| 8 | + } | |
| 9 | + | |
| 10 | + //创建一个画布 | |
| 11 | + let can = document.createElement('canvas'); | |
| 12 | + //设置画布的长宽,可以通过调节画布的宽高去设置水印分布的密度 | |
| 13 | + can.width = 350; | |
| 14 | + can.height = 160; | |
| 15 | + | |
| 16 | + let cans = can.getContext('2d'); | |
| 17 | + //旋转角度 | |
| 18 | + cans.rotate(-12 * Math.PI / 150); | |
| 19 | + cans.font = '18px Vedana'; | |
| 20 | + //设置填充绘画的颜色、渐变或者模式 | |
| 21 | + cans.fillStyle = 'rgba(200, 200, 200, 0.40)'; | |
| 22 | + //设置文本内容的当前对齐方式 | |
| 23 | + cans.textAlign = 'left'; | |
| 24 | + //设置在绘制文本时使用的当前文本基线 | |
| 25 | + cans.textBaseline = 'Middle'; | |
| 26 | + //在画布上绘制填色的文本(输出的文本,开始绘制文本的X坐标位置,开始绘制文本的Y坐标位置) | |
| 27 | + // cans.fillText(str, can.width / 6, can.height / 2); | |
| 28 | + if(str){ | |
| 29 | + cans.fillText(str, 15, 80) //第一行字体 | |
| 30 | + } | |
| 31 | + if(str2){ | |
| 32 | + cans.fillText(str2, 15, 110) //第二行字体 | |
| 33 | + } | |
| 34 | + if(str3){ | |
| 35 | + cans.fillText(str3, 15, 140)//第三行字体 | |
| 36 | + } | |
| 37 | + | |
| 38 | + let div = document.createElement('div'); | |
| 39 | + div.id = id; | |
| 40 | + div.style.pointerEvents = 'none'; | |
| 41 | + div.style.top = '30px'; | |
| 42 | + div.style.left = '0px'; | |
| 43 | + div.style.position = 'fixed'; | |
| 44 | + div.style.zIndex = '200000'; | |
| 45 | + div.style.width = document.documentElement.clientWidth + 'px'; | |
| 46 | + div.style.height = document.documentElement.clientHeight + 'px'; | |
| 47 | + div.style.background = 'url(' + can.toDataURL('image/png') + ') left top repeat'; | |
| 48 | + document.body.appendChild(div); | |
| 49 | + return id; | |
| 50 | +} | |
| 51 | + | |
| 52 | +// 该方法只允许调用一次 | |
| 53 | +watermark.set = (str,str2,str3) => { | |
| 54 | + let id = setWatermark(str,str2,str3); | |
| 55 | + setInterval(() => { | |
| 56 | + if (document.getElementById(id) === null) { | |
| 57 | + id = setWatermark(str,str2,str3); | |
| 58 | + } | |
| 59 | + }, 500); | |
| 60 | + window.onresize = () => { | |
| 61 | + setWatermark(str,str2,str3); | |
| 62 | + }; | |
| 63 | +} | |
| 64 | + | |
| 65 | +export default watermark; | |
| 0 | 66 | \ No newline at end of file | ... | ... |