index.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <template>
  2. <div class="denet-message" v-show="state.list.length > 0">
  3. <template v-for="item in state.list">
  4. <div class="denet-message-area" @click="clickItem(item)" v-if="item.bizType == 2">
  5. <img :src="require('@/assets/img/icon-message-fail.png')" alt />
  6. <span>You were not selected from jerryWang's giveaway... click to see more giveaway!</span>
  7. <div class="denet-message-close" @click.stop="clickClose(item)">
  8. <img :src="require('@/assets/img/icon-message-close.png')" alt />
  9. </div>
  10. </div>
  11. <div class="denet-message-area" @click="clickItem(item)" v-if="item.bizType == 1">
  12. <img :src="require('@/assets/img/icon-message-win.png')" alt />
  13. <span>Congratulations! You won <b class="denet-message-money">{{ item.bizData.lotteryMoney }}
  14. {{ item.bizData.lotteryTokenSymbol }}</b> from jerryWang's giveaway!
  15. 🎉</span>
  16. <div class="denet-message-close" @click.stop="clickClose(item)">
  17. <img :src="require('@/assets/img/icon-message-close.png')" alt />
  18. </div>
  19. </div>
  20. </template>
  21. </div>
  22. </template>
  23. <script setup>
  24. import { onMounted, reactive } from "vue";
  25. let state = reactive({
  26. list: [],
  27. })
  28. // 过5秒消失逻辑
  29. const overTimeClose = () => {
  30. setTimeout(() => {
  31. let now_time = new Date().getTime()
  32. for (let i in state.list) {
  33. if ((now_time - state.list[i].read_time) > 4500) {
  34. state.list.splice(i, 1)
  35. }
  36. }
  37. }, 5000)
  38. }
  39. const clickClose = (item) => {
  40. // 通知已读
  41. for (let i in state.list) {
  42. if (item.id == state.list[i].id) {
  43. state.list.splice(i, 1)
  44. }
  45. }
  46. }
  47. const clickItem = (item) => {
  48. // 跳转详情页
  49. window.open(`https://twitter.com/${item.bizData.twitterAccount}/status/${item.bizData.twitterId}`)
  50. }
  51. // 读取消息
  52. const readMessage = (id = 0) => {
  53. chrome.runtime.sendMessage({
  54. actionType: "CONTENT_HTTP_NET_WORK",
  55. funcName: '通知已读',
  56. data: {
  57. url: '/notice/read',
  58. params: {
  59. noticeId: id
  60. }
  61. }
  62. });
  63. }
  64. chrome.runtime.onMessage.addListener((req, sender, sendResponse) => {
  65. if (req.actionType == 'BACK_UNREAD_MESSAGE') {
  66. let data = req.data.data || []
  67. if (req.data.code == 0 && data.length > 0) {
  68. data.forEach((item) => {
  69. if (state.list.filter((filter_item) => { return filter_item.id == item.id }).length == 0) {
  70. item.bizData = JSON.parse(item.bizData)
  71. item.read_time = new Date().getTime()
  72. state.list.push(item)
  73. }
  74. readMessage(item.id)
  75. })
  76. overTimeClose()
  77. }
  78. }
  79. })
  80. </script>
  81. <style lang="scss" scoped>
  82. .denet-message {
  83. position: fixed;
  84. max-height: 100%;
  85. overflow: hidden;
  86. top: 0;
  87. right: 0;
  88. width: 500px;
  89. z-index: 9999;
  90. &-area {
  91. width: 344px;
  92. background: #FFFFFF;
  93. border-radius: 15px;
  94. min-height: 72px;
  95. position: relative;
  96. margin-top: 22px;
  97. display: flex;
  98. cursor: pointer;
  99. filter: drop-shadow(0px 5px 20px rgba(0, 0, 0, 0.22));
  100. margin-left: 129px;
  101. img:first-child {
  102. width: 40px;
  103. height: 40px;
  104. margin: 16px 14px 0 16px;
  105. }
  106. span {
  107. padding: 14px 26px 14px 0;
  108. font-style: normal;
  109. font-weight: 500;
  110. font-size: 14px;
  111. line-height: 20px;
  112. }
  113. .denet-message-money {
  114. color: #3D7CB4;
  115. }
  116. }
  117. &-area:last-child {
  118. margin-bottom: 50px;
  119. }
  120. &-close {
  121. border-radius: 100px;
  122. width: 24px;
  123. height: 24px;
  124. display: flex;
  125. justify-content: center;
  126. align-items: center;
  127. background: #F2F2F2;
  128. position: absolute;
  129. right: -10px;
  130. top: -10px;
  131. filter: drop-shadow(0px 2px 4px rgba(0, 0, 0, 0.12));
  132. cursor: pointer;
  133. img:first-child {
  134. height: 10px;
  135. width: 10px;
  136. margin: 0;
  137. }
  138. }
  139. }
  140. </style>