AdBanner.vue 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. <template>
  2. <view class="ad-banner" :style="{ '--font-scale': fontScale }">
  3. <!-- Empty state when no data -->
  4. <view v-if="!hasItems" class="ad-banner-empty">
  5. <text class="ad-banner-empty-icon">📢</text>
  6. <text class="ad-banner-empty-text">暂无推荐内容</text>
  7. </view>
  8. <!-- Swiper carousel -->
  9. <swiper
  10. v-else
  11. class="ad-banner-swiper"
  12. :indicator-dots="totalSlides > 1"
  13. :active-index="activeIndex"
  14. @change="onSwiperChange"
  15. autoplay
  16. :interval="autoPlayInterval"
  17. :duration="swiperDuration"
  18. circular
  19. @click="onSlideClick"
  20. >
  21. <swiper-item
  22. v-for="(item, index) in displayItems"
  23. :key="item.id"
  24. :data-index="index"
  25. >
  26. <view
  27. class="ad-banner-slide"
  28. :data-index="index"
  29. @click="onSlideClick(index)"
  30. >
  31. <!-- 封面图 -->
  32. <image
  33. class="ad-banner-cover"
  34. :src="getCoverImage(item)"
  35. mode="aspectFill"
  36. />
  37. <!-- 叠加层:标签 + 标题 -->
  38. <view class="ad-banner-overlay">
  39. <!-- 标签 -->
  40. <view
  41. class="ad-banner-tag"
  42. :class="item.type === 'product' ? 'tag-product' : 'tag-activity'"
  43. >
  44. {{ item.type === 'product' ? '🛍️ 商品' : '🔥 活动' }}
  45. </view>
  46. <!-- 标题 -->
  47. <view class="ad-banner-title" v-html="getTitleHtml(item)" />
  48. </view>
  49. </view>
  50. </swiper-item>
  51. </swiper>
  52. <!-- 指示点 -->
  53. <view v-if="totalSlides > 1" class="ad-banner-dots">
  54. <view
  55. class="ad-banner-dot"
  56. v-for="(dot, i) in totalSlides"
  57. :key="i"
  58. :class="{ active: activeIndex === i }"
  59. ></view>
  60. </view>
  61. </view>
  62. </template>
  63. <script>
  64. import { parseDate } from '@/utils/format.js'
  65. import { getHomeBanners } from '@/utils/api.js'
  66. import fontScaleMixin from '@/mixins/font-scale.js'
  67. export default {
  68. mixins: [fontScaleMixin],
  69. props: {
  70. /** 推荐活动列表(后备数据) */
  71. activities: {
  72. type: Array,
  73. default: function() { return [] }
  74. },
  75. /** 推荐商品列表(后备数据) */
  76. products: {
  77. type: Array,
  78. default: function() { return [] }
  79. },
  80. /** 自动播放间隔(毫秒) */
  81. autoPlayInterval: {
  82. type: Number,
  83. default: 3000
  84. },
  85. /** Swiper 切换动画时长(毫秒) */
  86. swiperDuration: {
  87. type: Number,
  88. default: 500
  89. }
  90. },
  91. data() {
  92. return {
  93. // 合并后的项目列表,交替排列:商品、活动、商品、活动...
  94. displayItems: [],
  95. activeIndex: 0,
  96. totalSlides: 0,
  97. loading: false
  98. }
  99. },
  100. computed: {
  101. /** 是否有内容 */
  102. hasItems: function() {
  103. return this.displayItems && this.displayItems.length > 0
  104. }
  105. },
  106. mounted: function() {
  107. this.loadBanners()
  108. },
  109. methods: {
  110. /** 优先从配置接口加载广告,失败则回退到 props */
  111. loadBanners: function() {
  112. var self = this
  113. self.loading = true
  114. getHomeBanners().then(function(res) {
  115. self.loading = false
  116. if (res && res.code === 200 && res.data && res.data.length > 0) {
  117. self.fromConfig(res.data)
  118. } else {
  119. self.fromProps()
  120. }
  121. }).catch(function() {
  122. self.loading = false
  123. self.fromProps()
  124. })
  125. },
  126. /** 使用配置接口返回的数据构建 displayItems */
  127. fromConfig: function(banners) {
  128. var items = banners.map(function(b) {
  129. return {
  130. id: b.id,
  131. type: b.bannerType === 'product' ? 'product' : 'activity',
  132. name: b.title,
  133. title: b.title,
  134. coverImage: b.coverImage || b.cover_image,
  135. refId: b.refId || b.ref_id,
  136. linkUrl: b.linkUrl || b.link_url,
  137. source: b
  138. }
  139. }.bind(this))
  140. this.displayItems = items
  141. this.totalSlides = items.length
  142. this.activeIndex = 0
  143. },
  144. /** 使用 props(activities + products)构建 displayItems(后备逻辑) */
  145. fromProps: function() {
  146. this.mergeItems()
  147. },
  148. /** 获取合并后的项目列表(后备逻辑) */
  149. mergeItems: function() {
  150. var productItems = (this.products || []).slice(0, 4).map(function(p) {
  151. return {
  152. id: p.id || p._id,
  153. type: 'product',
  154. name: p.name || '未知商品',
  155. coverImage: p.coverImage || p.image,
  156. price: p.price,
  157. priceLabel: p.priceLabel,
  158. source: p
  159. }
  160. }.bind(this))
  161. var activityItems = (this.activities || []).slice(0, 4).map(function(a) {
  162. var startTime = a.startTime
  163. var d
  164. try {
  165. d = parseDate(startTime)
  166. if (d) {
  167. var month = d.getMonth() + 1
  168. var day = d.getDate()
  169. var hour = d.getHours() < 10 ? '0' + d.getHours() : d.getHours()
  170. var minute = d.getMinutes() < 10 ? '0' + d.getMinutes() : d.getMinutes()
  171. startTime = month + '月' + day + '日 ' + hour + ':' + minute
  172. }
  173. } catch (e) {
  174. startTime = String(startTime)
  175. }
  176. return {
  177. id: a.id || a._id,
  178. type: 'activity',
  179. title: a.title || '未知活动',
  180. coverImage: a.coverImage || a.image,
  181. price: a.price,
  182. priceLabel: a.priceLabel,
  183. startTime: startTime,
  184. status: a.status,
  185. source: a
  186. }
  187. }.bind(this))
  188. // 交替合并:商品、活动、商品、活动...
  189. var merged = []
  190. var pi = 0, ai = 0
  191. var toggle = 0
  192. while ((pi < productItems.length || ai < activityItems.length) && merged.length < 8) {
  193. if (toggle % 2 === 0 && pi < productItems.length) {
  194. merged.push(productItems[pi++])
  195. } else if (ai < activityItems.length) {
  196. merged.push(activityItems[ai++])
  197. } else if (pi < productItems.length) {
  198. merged.push(productItems[pi++])
  199. }
  200. toggle++
  201. }
  202. this.displayItems = merged
  203. this.totalSlides = merged.length
  204. this.activeIndex = 0
  205. },
  206. /** 获取封面图片URL */
  207. getCoverImage: function(item) {
  208. var img = item.coverImage || item.image
  209. if (!img) return '/static/default-activity.png'
  210. var s = String(img).trim()
  211. if (!s || s === 'null' || s === '/null' || s === 'undefined') return '/static/default-activity.png'
  212. if (s.indexOf('http://') === 0 || s.indexOf('https://') === 0) return s
  213. return config.API_BASE_URL + (s.charAt(0) === '/' ? s : '/' + s)
  214. },
  215. /** 获取标题 HTML(防止 AI 腔,使用直陈) */
  216. getTitleHtml: function(item) {
  217. if (item.type === 'product') {
  218. return (item.name || '').substring(0, 12)
  219. } else {
  220. return (item.title || '').substring(0, 12)
  221. }
  222. },
  223. /** swiper 变化回调 */
  224. onSwiperChange: function(e) {
  225. this.activeIndex = e.detail.current
  226. },
  227. /** 点击轮播项 */
  228. onSlideClick: function(index) {
  229. if (isNaN(index) || index < 0 || index >= this.displayItems.length) return
  230. var item = this.displayItems[index]
  231. if (!item) return
  232. // 自定义链接(link_url)直接跳转,否则按类型路由
  233. if (item.linkUrl) {
  234. if (item.linkUrl.indexOf('http://') === 0 || item.linkUrl.indexOf('https://') === 0) {
  235. window.location.href = item.linkUrl
  236. return
  237. }
  238. uni.navigateTo({ url: item.linkUrl })
  239. return
  240. }
  241. if (item.type === 'product') {
  242. uni.navigateTo({
  243. url: '/pages/discover-detail/product-detail/product-detail?id=' + item.refId
  244. })
  245. } else if (item.type === 'activity') {
  246. uni.navigateTo({
  247. url: '/pages/activity/activity-detail/activity-detail?id=' + item.refId
  248. })
  249. }
  250. },
  251. },
  252. }
  253. </script>
  254. <style scoped lang="scss">
  255. /* ===== 广告轮播组件 ===== */
  256. .ad-banner {
  257. width: 100%;
  258. margin: 0;
  259. padding: 0;
  260. overflow: hidden;
  261. }
  262. .ad-banner-empty {
  263. display: flex;
  264. flex-direction: column;
  265. align-items: center;
  266. padding: 60rpx $uni-space-page;
  267. color: $facade-text-body;
  268. }
  269. .ad-banner-empty-icon {
  270. font-size: calc(48rpx * var(--font-scale, 1));
  271. margin-bottom: 12rpx;
  272. }
  273. .ad-banner-empty-text {
  274. font-size: calc(24rpx * var(--font-scale, 1));
  275. }
  276. .ad-banner-swiper {
  277. width: 100%;
  278. }
  279. /* 幻灯片卡片 */
  280. .ad-banner-slide {
  281. position: relative;
  282. width: 100%;
  283. /* 高度保持与封面图比例一致 */
  284. height: 320rpx;
  285. }
  286. /* 封面图 - 统一宽高比 16:9 -> 约320rpx高 */
  287. .ad-banner-cover {
  288. width: 100%;
  289. height: 100%;
  290. display: block;
  291. background: linear-gradient(135deg, $dim-body 0%, $dim-wealth 100%);
  292. border-radius: 16rpx;
  293. }
  294. /* 叠加层 */
  295. .ad-banner-overlay {
  296. position: absolute;
  297. bottom: 0;
  298. left: 0;
  299. right: 0;
  300. padding: 24rpx 24rpx 32rpx;
  301. }
  302. .ad-banner-tag {
  303. display: inline-block;
  304. padding: 4rpx 12rpx;
  305. border-radius: 24rpx;
  306. font-size: calc(20rpx * var(--font-scale, 1));
  307. font-weight: 700;
  308. margin-bottom: 12rpx;
  309. text-transform: uppercase;
  310. letter-spacing: 1rpx;
  311. }
  312. .tag-product {
  313. background: rgba(255, 255, 255, 0.9);
  314. color: $brand-primary;
  315. }
  316. .tag-activity {
  317. background: rgba(255, 255, 255, 0.9);
  318. color: $brand-primary-dark;
  319. }
  320. /* 标题 - 使用 v-html 防止 AI 腔,直陈具体文本 */
  321. .ad-banner-title {
  322. font-size: calc(32rpx * var(--font-scale, 1));
  323. font-weight: 700;
  324. color: $app-surface;
  325. text-shadow: 0 1rpx 4rpx rgba(0,0,0,0.4);
  326. line-height: 1.3;
  327. min-height: 48rpx;
  328. display: flex;
  329. align-items: center;
  330. -webkit-line-clamp: 2;
  331. -webkit-box-orient: vertical;
  332. overflow: hidden;
  333. }
  334. /* 指示点 */
  335. .ad-banner-dots {
  336. display: flex;
  337. justify-content: center;
  338. align-items: center;
  339. margin-top: 12rpx;
  340. gap: 8rpx;
  341. }
  342. .ad-banner-dot {
  343. width: 8rpx;
  344. height: 8rpx;
  345. border-radius: 50%;
  346. background: rgba(255,255,255,0.5);
  347. transition: width 0.3s ease, background-color 0.3s ease;
  348. }
  349. .ad-banner-dot.active {
  350. width: 24rpx;
  351. background: $brand-primary;
  352. }
  353. </style>