popup.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <template>
  2. <uni-popup ref="popupRef" type="center">
  3. <view class="">
  4. <view class="popup-box" :style="{'height': swiperHeight + 'rpx'}">
  5. <u-swiper :list="swiperList" :height="swiperHeight" bg-color="transparent"
  6. :mode="swiperList && swiperList.length > 1 ?'round': 'none'" @click="openSwiper"></u-swiper>
  7. </view>
  8. <view class="cancel-btn">
  9. <image @click.stop="cancelPopup(true)" src="./cancel-btn.png" mode="aspectFit"></image>
  10. </view>
  11. </view>
  12. </uni-popup>
  13. </template>
  14. <script>
  15. import {
  16. getBannerList
  17. } from "@/api/government.js"
  18. export default {
  19. props: {
  20. pattern: {
  21. type: Number,
  22. default: 0
  23. }
  24. },
  25. data() {
  26. return {
  27. StorageName: 'adHistory',
  28. swiperHeight: 775,
  29. swiperList: [],
  30. popupTyle: ['', false, false],
  31. openNum: 0,
  32. }
  33. },
  34. created() {
  35. },
  36. methods: {
  37. getSwiper(newNum) {
  38. const status = this.popupTyle[newNum];
  39. // if (status === true) return
  40. const adInfo = {}
  41. const key = newNum === 1 ? this.$keys.ZW_TanChuang : this.$keys.SH_TanChuang;
  42. getBannerList(key).then(res => {
  43. const data = res.data || []
  44. this.swiperList = data.map(el => {
  45. el.image = this.$getImgPath(el.image)
  46. const t = new Date(el.updateTime).getTime()
  47. adInfo[el.adId] = t
  48. return el
  49. });
  50. this.getNewAD(this.swiperList, key, adInfo)
  51. // this.openNum = newNum;
  52. }).finally(() => {})
  53. },
  54. getNewAD(adList, key, adInfo) {
  55. if (adList && adList.length > 0) {
  56. // 1. 获取缓存的广告历史信息
  57. let adHistory = {}
  58. try {
  59. adHistory = uni.getStorageSync(this.StorageName) || {};
  60. // console.log('adHistory = ', adHistory, uni.getStorageSync(this.StorageName))
  61. } catch (e) {
  62. // error
  63. };
  64. // 2. 读取当前模块的广告历史信息
  65. const History = adHistory[key];
  66. if (History) {
  67. // 4. 存在历史信息
  68. try {
  69. const adInfoKeys = Object.keys(adInfo) || [];
  70. adInfoKeys.forEach(el => {
  71. // 历史记录不存在当前ID,或者当前ID的值不等于历史ID的值,则存在新的广告数据
  72. if (!History[el] || adInfo[el] !== History[el]) {
  73. this.$refs.popupRef.open()
  74. throw new Error('匹配不到数据了')
  75. }
  76. })
  77. } catch (e) {
  78. //TODO handle the exception
  79. // console.log('adInfoKeys', e)
  80. }
  81. } else {
  82. // 3. 不存在历史信息, 或者当前广告数据比历史数据多
  83. this.$refs.popupRef.open()
  84. };
  85. // 缓存当前的广告信息
  86. adHistory[key] = adInfo
  87. uni.setStorage({
  88. key: this.StorageName,
  89. data: adHistory,
  90. success: function() {
  91. // console.log('success adHistory');
  92. }
  93. });
  94. } else {
  95. this.$refs.popupRef.close()
  96. }
  97. },
  98. openSwiper(e) {
  99. this.cancelPopup(true)
  100. this.$openPage(this.swiperList[e])
  101. },
  102. cancelPopup(status = false) {
  103. try {
  104. if (status) {
  105. this.popupTyle[this.openNum] = status
  106. }
  107. this.swiperList = []
  108. this.$refs.popupRef.close()
  109. } catch {}
  110. }
  111. },
  112. mounted() {
  113. },
  114. watch: {
  115. pattern: {
  116. handler: function(newNum, oldNum) {
  117. if ([1, 2].includes(newNum)) {
  118. this.cancelPopup()
  119. this.getSwiper(newNum)
  120. }
  121. },
  122. immediate: true
  123. }
  124. }
  125. }
  126. </script>
  127. <style lang="scss" scoped>
  128. .cancel-btn {
  129. text-align: center;
  130. padding-top: 40rpx;
  131. image {
  132. width: 60rpx;
  133. height: 60rpx;
  134. }
  135. }
  136. .popup-box {
  137. width: 544rpx;
  138. }
  139. </style>