GuidePopup.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <template>
  2. <view class="guide-popup" v-if="visible">
  3. <view class="guide-mask"></view>
  4. <view class="guide-wrap">
  5. <view class="guide-card">
  6. <view v-if="imageLoading" class="guide-loading">
  7. <u-loading-icon mode="circle" color="#FF233C" size="40"></u-loading-icon>
  8. </view>
  9. <template v-else>
  10. <swiper
  11. class="guide-swiper"
  12. :current="guideCurrent"
  13. @change="onGuideSwiperChange"
  14. :indicator-dots="false"
  15. :autoplay="false"
  16. :circular="false"
  17. >
  18. <swiper-item v-for="(item, index) in guideList" :key="'guide-' + index">
  19. <image class="guide-img" :src="item.value" mode="aspectFit" />
  20. </swiper-item>
  21. </swiper>
  22. <view class="guide-dots" v-if="guideList.length > 1">
  23. <view
  24. v-for="(item, index) in guideList"
  25. :key="'guide-dot-' + index"
  26. class="guide-dot"
  27. :class="{ active: guideCurrent === index }"
  28. ></view>
  29. </view>
  30. </template>
  31. </view>
  32. <view v-if="!imageLoading" class="guide-btn" @tap="onGuideAction">{{ isGuideLast ? '立即体验' : '下一页' }}</view>
  33. <view v-if="!imageLoading" class="guide-close" @tap="closeGuide">
  34. <u-icon name="close-circle" color="#fff" size="36"></u-icon>
  35. </view>
  36. </view>
  37. </view>
  38. </template>
  39. <script>
  40. import { getStoreActivityGuide } from '@/api/home.js'
  41. const GUIDE_VIEWED_KEY = 'store_activity_guide_viewed'
  42. export default {
  43. name: 'GuidePopup',
  44. data() {
  45. const guideViewed = !!uni.getStorageSync(GUIDE_VIEWED_KEY)
  46. return {
  47. visible: false,
  48. guideList: [],
  49. guideCurrent: 0,
  50. guideViewed,
  51. imageLoading: false,
  52. imagesReady: false
  53. }
  54. },
  55. created() {
  56. this.syncViewedState()
  57. },
  58. computed: {
  59. isGuideLast() {
  60. return this.guideList.length > 0 && this.guideCurrent >= this.guideList.length - 1
  61. }
  62. },
  63. methods: {
  64. isGuideViewedFlag(viewed) {
  65. return viewed === true || viewed === 1 || viewed === '1' || viewed === 'true'
  66. },
  67. markGuideViewed() {
  68. this.guideViewed = true
  69. this.visible = false
  70. this.imageLoading = false
  71. uni.setStorageSync(GUIDE_VIEWED_KEY, true)
  72. },
  73. syncViewedState() {
  74. if (uni.getStorageSync(GUIDE_VIEWED_KEY) || this.guideViewed) {
  75. this.markGuideViewed()
  76. }
  77. },
  78. check() {
  79. if (!this.utils.checkLoginState()) return
  80. this.syncViewedState()
  81. if (this.guideViewed) return
  82. getStoreActivityGuide().then(res => {
  83. if (res.code != 200) return
  84. const guideList = res.guideData || []
  85. const viewed = this.isGuideViewedFlag(res.viewed)
  86. if (viewed) {
  87. this.markGuideViewed()
  88. return
  89. }
  90. if (uni.getStorageSync(GUIDE_VIEWED_KEY) || !guideList.length) {
  91. this.visible = false
  92. return
  93. }
  94. this.guideList = guideList
  95. const isFirstOpen = !this.visible
  96. if (isFirstOpen) {
  97. this.guideCurrent = 0
  98. }
  99. this.visible = true
  100. if (isFirstOpen || !this.imagesReady) {
  101. this.prepareGuideImages(guideList)
  102. }
  103. })
  104. },
  105. prepareGuideImages(guideList) {
  106. this.imageLoading = true
  107. this.imagesReady = false
  108. this.preloadGuideImages(guideList).then(() => {
  109. this.imagesReady = true
  110. this.imageLoading = false
  111. })
  112. },
  113. preloadGuideImages(guideList) {
  114. const urls = (guideList || []).map(item => item && item.value).filter(Boolean)
  115. if (!urls.length) return Promise.resolve()
  116. return Promise.all(urls.map(url => new Promise(resolve => {
  117. uni.getImageInfo({
  118. src: url,
  119. success: () => resolve(),
  120. fail: () => resolve()
  121. })
  122. })))
  123. },
  124. onGuideSwiperChange(e) {
  125. this.guideCurrent = e.detail.current
  126. },
  127. onGuideAction() {
  128. if (this.isGuideLast) {
  129. this.closeGuide()
  130. } else {
  131. this.guideCurrent++
  132. }
  133. },
  134. closeGuide() {
  135. if (!this.visible) return
  136. this.markGuideViewed()
  137. this.$emit('close')
  138. // viewStoreActivityGuide().catch(() => {})
  139. }
  140. }
  141. }
  142. </script>
  143. <style lang="scss" scoped>
  144. .guide-popup {
  145. position: fixed;
  146. top: 0;
  147. right: 0;
  148. left: 0;
  149. bottom: 0;
  150. z-index: 1001;
  151. display: flex;
  152. justify-content: center;
  153. align-items: center;
  154. }
  155. .guide-mask {
  156. position: absolute;
  157. top: 0;
  158. right: 0;
  159. bottom: 0;
  160. left: 0;
  161. background: rgba(0, 0, 0, 0.55);
  162. }
  163. .guide-wrap {
  164. position: relative;
  165. z-index: 1;
  166. width: 670rpx;
  167. display: flex;
  168. flex-direction: column;
  169. align-items: center;
  170. }
  171. .guide-card {
  172. width: 670rpx;
  173. border-radius: 40rpx;
  174. overflow: hidden;
  175. position: relative;
  176. }
  177. .guide-loading {
  178. width: 670rpx;
  179. height: 880rpx;
  180. display: flex;
  181. align-items: center;
  182. justify-content: center;
  183. background: rgba(255, 255, 255, 0.96);
  184. }
  185. .guide-swiper {
  186. width: 670rpx;
  187. height: 880rpx;
  188. }
  189. .guide-img {
  190. width: 100%;
  191. height: 100%;
  192. display: block;
  193. }
  194. .guide-dots {
  195. position: absolute;
  196. left: 0;
  197. right: 0;
  198. bottom: 24rpx;
  199. display: flex;
  200. justify-content: center;
  201. align-items: center;
  202. gap: 12rpx;
  203. }
  204. .guide-dot {
  205. width: 12rpx;
  206. height: 12rpx;
  207. border-radius: 6rpx;
  208. background: rgba(255, 255, 255, 0.45);
  209. transition: all 0.2s;
  210. }
  211. .guide-dot.active {
  212. width: 28rpx;
  213. background: #fff;
  214. }
  215. .guide-btn {
  216. margin-top: 30rpx;
  217. width: 590rpx;
  218. height: 104rpx;
  219. background: linear-gradient(135deg, #FF5267 0%, #FF233C 100%);
  220. border-radius: 52rpx;
  221. font-family: PingFangSC, PingFang SC;
  222. font-weight: 600;
  223. font-size: 40rpx;
  224. color: #FFFFFF;
  225. line-height: 104rpx;
  226. text-align: center;
  227. }
  228. .guide-close {
  229. width: 60rpx;
  230. height: 60rpx;
  231. margin-top: 30rpx;
  232. display: flex;
  233. align-items: center;
  234. justify-content: center;
  235. }
  236. </style>