Watermark.vue 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <template>
  2. <view class="watermark" >
  3. <view class="watermark-box">
  4. <text class="text" v-for="(item, index) in count" :key="index" >{{localUserInfo.nickName || localUserInfo.userName || '' }}</text>
  5. </view>
  6. </view>
  7. </template>
  8. <script>
  9. export default {
  10. name: 'Watermark',
  11. props: {
  12. userInfo: {
  13. type: Object,
  14. default: null
  15. }
  16. },
  17. data() {
  18. return {
  19. count:40,
  20. localUserInfo: this.userInfo || this.getUserInfo()
  21. };
  22. },
  23. watch: {
  24. // 监听props中的userInfo变化,当变化时更新本地userInfo
  25. userInfo: {
  26. handler(newVal) {
  27. this.localUserInfo = newVal || this.getUserInfo();
  28. },
  29. deep: true
  30. }
  31. },
  32. onShow() {
  33. // 如果没有通过props传递userInfo,则从本地存储获取
  34. if (!this.userInfo) {
  35. this.localUserInfo = this.getUserInfo();
  36. }
  37. },
  38. onLoad() {
  39. // 如果没有通过props传递userInfo,则从本地存储获取
  40. if (!this.userInfo) {
  41. this.localUserInfo = this.getUserInfo();
  42. }
  43. },
  44. methods: {
  45. getUserInfo() {
  46. try {
  47. const userInfo = uni.getStorageSync('userInfo');
  48. if (userInfo) {
  49. // 尝试解析JSON字符串
  50. return userInfo|| {};
  51. }
  52. return {};
  53. } catch (error) {
  54. console.error('获取用户信息失败:', error);
  55. return {};
  56. }
  57. }
  58. }
  59. }
  60. </script>
  61. <style scoped>
  62. .watermark {
  63. position: fixed;
  64. top: 0;
  65. left: 0;
  66. width: 100vw;
  67. height: 100vh;
  68. pointer-events: none;
  69. z-index: 999999;
  70. overflow: hidden;
  71. font-size: 40rpx;
  72. }
  73. .watermark-box {
  74. width: 200%;
  75. height: 150%;
  76. display: flex;
  77. flex-wrap: wrap;
  78. justify-content: space-between;
  79. align-items: flex-start;
  80. transform: rotate(-20deg) translate(-25%, -25%);
  81. color: rgba(196, 196, 196, 0.5);
  82. padding: 0;
  83. }
  84. .text {
  85. margin: 20rpx 15rpx;
  86. font-weight: bold;
  87. flex: 0 0 calc(20% - 30rpx);
  88. text-align: center;
  89. }
  90. </style>