watermark.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. const VIEW_ID = '1.23452384164.123412415';
  2. export default {
  3. data() {
  4. return {
  5. showCanvas: true,
  6. canvasSize: uni.upx2px(375),
  7. canvasFontSize: uni.upx2px(18),
  8. canvasTextColor: 'rgba(200, 200, 200, 0.60)',
  9. canvasRotate: -30 * Math.PI / 180,
  10. canvasTextAlign: 'left',
  11. canvasTextBaseline: 'middle',
  12. canvasTextFillX: uni.upx2px(-50),
  13. canvasTextFillY: uni.upx2px(170),
  14. watermarkView : undefined,
  15. }
  16. },
  17. watch: {
  18. canvasText(newVal) {
  19. this.updateWatermark()
  20. }
  21. },
  22. methods: {
  23. show() {
  24. // #ifdef APP-PLUS
  25. plus.nativeObj.View.getViewById(VIEW_ID) !== null && plus.nativeObj.View.getViewById(VIEW_ID).show();
  26. // #endif
  27. // #ifdef H5
  28. if (document.getElementById(VIEW_ID)) {
  29. document.getElementById(VIEW_ID).style.visibility = "visible";
  30. }
  31. // #endif
  32. },
  33. hide() {
  34. // #ifdef APP-PLUS
  35. plus.nativeObj.View.getViewById(VIEW_ID) !== null && plus.nativeObj.View.getViewById(VIEW_ID).hide();
  36. // #endif
  37. // #ifdef H5
  38. if (document.getElementById(VIEW_ID)) {
  39. document.getElementById(VIEW_ID).style.visibility = "hidden";
  40. }
  41. // #endif
  42. },
  43. updateWatermark() {
  44. if (!this.userName) throw new Error('userName is Requied!');
  45. // #ifdef APP-PLUS
  46. this.watermarkView && this.watermarkView.reset();
  47. this.drawAppWaterMark(VIEW_ID);
  48. // #endif
  49. // #ifdef H5
  50. this.drawH5WaterMark(VIEW_ID);
  51. // #endif
  52. },
  53. initWatermark() {
  54. if (!this.userName) throw new Error('userName is Requied!');
  55. // #ifdef APP-PLUS
  56. this.drawAppWaterMark(VIEW_ID);
  57. // #endif
  58. // #ifdef H5
  59. this.drawH5WaterMark(VIEW_ID);
  60. // #endif
  61. uni.$off('ly-show-watermar');
  62. uni.$off('ly-hide-watermark');
  63. // 全局监听事件,触发水印显示
  64. uni.$on('ly-show-watermark', () => {
  65. this.show();
  66. });
  67. // 全局监听事件,触发水印隐藏
  68. uni.$on('ly-hide-watermark', () => {
  69. this.hide();
  70. });
  71. },
  72. // #ifdef APP-PLUS
  73. drawAppWaterMark(id) {
  74. plus.nativeObj.View.getViewById(id) && plus.nativeObj.View.getViewById(id).close();
  75. let cans = uni.createCanvasContext('watermarkCanvas');
  76. cans.rotate(this.canvasRotate);
  77. cans.setFontSize(this.canvasFontSize);
  78. cans.setFillStyle(this.canvasTextColor);
  79. cans.setTextAlign(this.canvasTextAlign);
  80. cans.setTextBaseline(this.canvasTextBaseline);
  81. cans.fillText(this.deptName + "-" + this.nickName, this.canvasTextFillX, this.canvasTextFillY);
  82. cans.fillText(this.time, this.canvasTextFillX, this.canvasTextFillY + 20);
  83. cans.draw(false);
  84. this.canvasDrawCallFn(id);
  85. },
  86. // 利用canvas生成水印图片
  87. canvasDrawCallFn(id) {
  88. // 适当的延时确保绘制完毕
  89. setTimeout(() => {
  90. uni.canvasToTempFilePath({
  91. canvasId: "watermarkCanvas",
  92. success: res => {
  93. let viewList = this.getWaterMarkWebview(res);
  94. this.watermarkView = new plus.nativeObj.View(id, {
  95. top: '0',
  96. left: '0',
  97. right: '0',
  98. bottom: '0'
  99. }, viewList);
  100. // 拦截View控件的触屏事件,将事件穿透给下一层view
  101. this.watermarkView.interceptTouchEvent(false);
  102. this.watermarkView.show();
  103. // this.showCanvas = false;
  104. }
  105. });
  106. }, 100);
  107. },
  108. // 创建webview页面水印样式
  109. getWaterMarkWebview(res) {
  110. let sysInfo = uni.getSystemInfoSync(),
  111. row = Math.ceil(sysInfo.windowHeight / this.canvasSize), // 水印排列行数
  112. imgList = [];
  113. for (let i = 0; i < row; i++) {
  114. for (let j = 0; j < 3; j++) {
  115. imgList.push({
  116. tag: 'img',
  117. src: res.tempFilePath,
  118. position: {
  119. top: (this.canvasSize * i) + 'px',
  120. left: (this.canvasSize * j) + 'px',
  121. width: this.canvasSize + 'px',
  122. height: this.canvasSize + 'px'
  123. }
  124. });
  125. }
  126. }
  127. return imgList;
  128. },
  129. // #endif
  130. // #ifdef H5
  131. drawH5WaterMark(id) {
  132. document.getElementById(id) && document.body.removeChild(document.getElementById(id));
  133. let can = document.createElement('canvas');
  134. let cans = can.getContext('2d');
  135. let div = document.createElement('div');
  136. can.width = this.canvasSize;
  137. can.height = this.canvasSize;
  138. cans.rotate(this.canvasRotate);
  139. cans.font = this.canvasFontSize + 'px sans-serif';
  140. cans.fillStyle = this.canvasTextColor;
  141. cans.textAlign = this.canvasTextAlign;
  142. cans.textBaseline = this.canvasTextBaseline;
  143. cans.fillText(this.canvasText, this.canvasTextFillX, this.canvasTextFillY);
  144. div.id = id;
  145. div.style.pointerEvents = 'none';
  146. div.style.top = '0';
  147. div.style.left = '0';
  148. div.style.bottom = '0';
  149. div.style.right = '0';
  150. div.style.position = 'fixed';
  151. div.style.zIndex = '100000';
  152. div.style.background = 'url(' + can.toDataURL('image/png') + ') left top repeat';
  153. document.body.appendChild(div);
  154. }
  155. // #endif
  156. }
  157. }