bubble-tips.vue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <template>
  2. <view class="bubble-tips-container" :style="{top: top, left: left}">
  3. <view class="bubble-content row" v-show="showBubble" v-for="item in currentList" :key="item.id">
  4. <u-image class="bubble-img" width="50rpx" height="50rpx" :src="item.user.avatar" border-radius="50%" />
  5. <view class="xs">
  6. {{item.template}}
  7. </view>
  8. </view>
  9. </view>
  10. </template>
  11. <script>
  12. import {getBubbleLists} from "@/api/app";
  13. import Cache from "@/utils/cache"
  14. export default {
  15. name: "BubbleTips",
  16. props: {
  17. // 是否停止切换
  18. discharge: {
  19. type: Boolean,
  20. default: false
  21. },
  22. top: {
  23. type: String,
  24. default: "40rpx"
  25. },
  26. left: {
  27. type: String,
  28. default: "20rpx"
  29. },
  30. updateTime: {
  31. type: Number,
  32. default: 5 * 60 * 1000
  33. }
  34. },
  35. data() {
  36. return {
  37. index: Cache.get("currentIndex") || 0,
  38. list: [],
  39. currentList: [],
  40. timer: null,
  41. showBubble: false,
  42. }
  43. },
  44. watch: {
  45. index(newValue, oldValue) {
  46. if (this.index - this.list.length >= 0) {
  47. this.showBubble = false;
  48. let timer = setTimeout(() => {
  49. Cache.set("currentIndex", 0)
  50. if (this.timer) {
  51. clearInterval(this.timer)
  52. this.timer = null
  53. }
  54. this.fadeUpBubble();
  55. clearTimeout(timer)
  56. }, 2000)
  57. } else {
  58. if (this.timer) {
  59. clearInterval(this.timer)
  60. this.timer = null
  61. }
  62. this.fadeUpBubble();
  63. return;
  64. }
  65. },
  66. discharge() {
  67. // 为了让活性的页面停止计时器
  68. if (this.discharge) {
  69. // 停止
  70. Cache.set("currentIndex", this.index);
  71. clearInterval(this.timer);
  72. this.timer = null;
  73. return false;
  74. } else {
  75. // 继续切换
  76. let currentInex = Cache.get("currentInex") || this.list.length;
  77. if (currentInex - this.list.length < 0) {
  78. if (this.timer) {
  79. setInterval(this.timer)
  80. this.timer = null;
  81. }
  82. this.fadeUpBubble()
  83. }
  84. }
  85. }
  86. },
  87. methods: {
  88. $getBubbleLists() {
  89. getBubbleLists().then(res => {
  90. if (res) {
  91. this.list = res.data.lists;
  92. var requestTime = res.data.time * 1000;
  93. Cache.set("bubbleList", JSON.stringify(this.list), 300);
  94. Cache.set("requestTime", requestTime);
  95. if (this.timer) {
  96. clearInterval(this.timer);
  97. this.timer = null;
  98. }
  99. this.fadeUpBubble()
  100. }
  101. })
  102. },
  103. fadeUpBubble() {
  104. let requestTime = Cache.get("requestTime");
  105. let currentTime = new Date();
  106. this.showBubble = true;
  107. this.index = Cache.get("currentIndex") || 0;
  108. this.list = Cache.get("bubbleList") ? JSON.parse(Cache.get("bubbleList")) : [];
  109. if (currentTime.getTime() - requestTime >= this.updateTime) {
  110. this.$getBubbleLists();
  111. Cache.set("currentIndex", 0, 300)
  112. return;
  113. }
  114. this.timer = setInterval(() => {
  115. this.currentList = this.list.slice(this.index, this.index + 1);
  116. Cache.set("currentIndex", ++this.index);
  117. }, 4000);
  118. }
  119. },
  120. created() {
  121. var index = Cache.get("currentIndex") || 0;
  122. var requestTime = Cache.get("requestTime");
  123. var currentTime = new Date();
  124. var currentList = Cache.get("bubbleList") ? JSON.parse(Cache.get("bubbleList")) : [];
  125. if (currentList.length <= 0) {
  126. this.$getBubbleLists();
  127. Cache.set("currentIndex", 0)
  128. } else if (index - currentList.length >= 0) {
  129. Cache.set("currentIndex", 0);
  130. if (this.timer) {
  131. clearInterval(this.timer)
  132. this.timer = null
  133. }
  134. this.fadeUpBubble();
  135. } else if (currentTime.getTime() - requestTime >= this.updateTime) {
  136. this.$getBubbleLists();
  137. Cache.set("currentIndex", 0)
  138. } else {
  139. if (this.timer) {
  140. clearInterval(this.timer);
  141. this.timer = null;
  142. }
  143. this.fadeUpBubble()
  144. }
  145. },
  146. onLoad() {},
  147. destroyed() {
  148. if (this.timer) {
  149. clearInterval(this.timer);
  150. this.timer = null
  151. }
  152. }
  153. }
  154. </script>
  155. <style lang="scss">
  156. @import '@/styles/base.scss';
  157. .bubble-tips-container {
  158. position: fixed;
  159. z-index: 98;
  160. .bubble-content {
  161. padding: 4rpx 20rpx 4rpx 10rpx;
  162. background-color: rgba(0, 0, 0, 0.7);
  163. color: white;
  164. border-radius: 120rpx;
  165. .bubble-img {
  166. width: 50rpx;
  167. height: 50rpx;
  168. border-radius: 50%;
  169. margin-right: 10rpx;
  170. }
  171. }
  172. }
  173. </style>