couponList.vue 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. <script lang="ts" setup>
  2. import { usePagination, useRequest, actionDelegationMiddleware } from 'alova/client'
  3. import { storeToRefs } from 'pinia'
  4. import { reactive, ref, watch } from 'vue'
  5. import { getCategoryList, getNewCouponList } from '@/api/home'
  6. import CustomNavigationBar from '@/components/CustomNavigationBar.vue'
  7. import ExchangeCoupon from '@/components/exchangeCoupon.vue'
  8. import TemplateCoupon from "@/components/templateCoupon.vue";
  9. import MescrollUni from '@/components/mescroll.vue'
  10. import MescrollEmpty from '@/custom-components/custom-mescroll/components/mescroll-empty.vue'
  11. import { useAreaStore } from '@/store/area'
  12. import { getImageUrl } from '@/utils/imageUtil'
  13. import { safeAreaInsets } from '@/utils'
  14. import { useTokenStore } from '@/store/token'
  15. import { toLoginPage } from '@/utils/toLoginPage'
  16. const tokenStore = useTokenStore()
  17. const { hasLogin } = storeToRefs(tokenStore)
  18. definePage({
  19. style: {
  20. navigationBarTitleText: '优惠券',
  21. navigationStyle: 'custom',
  22. },
  23. })
  24. const areaStore = useAreaStore()
  25. const { currentCity } = storeToRefs(areaStore)
  26. const topSafeAreaHeight = safeAreaInsets?.top || 0
  27. const activeIndex = ref('0')
  28. const categoryType = ref('0');
  29. const couponType = ref('2')
  30. const searchValue = ref('')
  31. const showLoading = ref(false)
  32. const isSearching = ref(false)
  33. let mescroll = null
  34. const { send, data, pageCount, loading, page, pageSize, isLastPage, refresh, reload, onSuccess, onError } = usePagination((page, pageSize) => getNewCouponList({
  35. pageNo: page,
  36. pageSize,
  37. name: searchValue.value === '' ? '' : searchValue.value,
  38. relatedId: categoryType.value,
  39. type: couponType.value === '0' ? '' : couponType.value,
  40. country: currentCity.value?.value === 'all' ? '' : currentCity.value.value,
  41. }), {
  42. initialPage: 1,
  43. initialPageSize: 10,
  44. append: true,
  45. preloadNextPage: false,
  46. preloadPreviousPage: false,
  47. total: response => response?.total || 0,
  48. data: response => response?.records || [],
  49. immediate: false,
  50. middleware: actionDelegationMiddleware('GetCouponList')
  51. })
  52. const { send: sendCategoryList, data: categoryList, loading: categoryLoading } = useRequest(getCategoryList, {
  53. initialData: [
  54. {
  55. name: '按摩',
  56. value: '0',
  57. }
  58. ],
  59. immediate: false,
  60. })
  61. watch(() => data.value, (newVal) => {
  62. if (newVal.length === 0 && !loading.value) {
  63. mescroll.showEmpty()
  64. }
  65. else {
  66. mescroll.removeEmpty()
  67. }
  68. })
  69. // watch(() => searchValue.value, () => {
  70. // // 判断当前是否是搜索状态
  71. // if (searchValue.value !== '') {
  72. // isSearching.value = true
  73. // } else {
  74. // isSearching.value = false
  75. // }
  76. // })
  77. // ============ mescroll 配置 ============
  78. const downOption = reactive({
  79. use: true,
  80. auto: false,
  81. bgColor: '#f8f8fa',
  82. textColor: '#666',
  83. textInOffset: '下拉刷新',
  84. textOutOffset: '释放刷新',
  85. textLoading: '刷新中...',
  86. offset: 80,
  87. })
  88. const upOption = computed(() => ({
  89. use: true,
  90. auto: false,
  91. noMoreSize: 6,
  92. isBounce: true,
  93. page: {
  94. num: page.value,
  95. size: pageSize.value,
  96. },
  97. empty: {
  98. use: true,
  99. tip: '暂无优惠券',
  100. // btnText: '刷新试试',
  101. icon: '/static/images/mescroll-empty1.png',
  102. fixed: false,
  103. },
  104. textNoMore: '--- 已经到底了 ---',
  105. toTop: {
  106. src: '/static/images/mescroll-totop.png',
  107. offset: 1000,
  108. bottom: 120,
  109. },
  110. // 开启虚拟列表优化(大数据量时)
  111. virtual: {
  112. use: false,
  113. rowHeight: 300,
  114. }
  115. }))
  116. function mescrollInit(ref) {
  117. mescroll = ref
  118. }
  119. async function downCallback() {
  120. await refresh()
  121. }
  122. async function upCallback() {
  123. if (isLastPage.value) {
  124. console.log(mescroll)
  125. mescroll.endSuccess(0, false)
  126. return
  127. }
  128. page.value++
  129. }
  130. onSuccess(() => {
  131. let isNextPage = isLastPage.value
  132. // 此处处理alova isLastPage默认值为true,首次请求出现的无法加载下一页问题
  133. if (pageCount.value > page.value && isLastPage.value) {
  134. isNextPage = false
  135. }
  136. // mescroll.setPageNum(page.value - 1)
  137. mescroll.endSuccess(data.value.length, !isNextPage)
  138. })
  139. onError(() => {
  140. console.log('请求失败')
  141. mescroll.showEmpty()
  142. mescroll.endErr()
  143. })
  144. onLoad(async () => {
  145. await sendCategoryList()
  146. await send()
  147. })
  148. onHide(() => {
  149. areaStore.resetCurrentCity();
  150. })
  151. onShow(async () => {
  152. // 登录后重新获取分类列表
  153. if (hasLogin.value) {
  154. couponType.value = '2'
  155. // 重置分类类型
  156. categoryType.value = '0'
  157. searchValue.value = ''
  158. await sendCategoryList()
  159. }
  160. // 重新获取优惠券数据
  161. await send()
  162. })
  163. onUnload(() => {
  164. areaStore.resetCurrentCity();
  165. })
  166. // #ifdef MP-WEIXIN
  167. async function login() {
  168. const currentPage = getCurrentPages()[0]
  169. const redirectUrl = `/${currentPage.route}`
  170. toLoginPage({ queryString: `?redirect=${encodeURIComponent(redirectUrl)}` })
  171. }
  172. // #endif
  173. async function tabChange(item) {
  174. // 切换tab时,重置上拉加载
  175. // mescroll.resetUpScroll(true)
  176. // mescroll.showDownScroll()
  177. categoryType.value = item.value
  178. mescroll.endUpScroll(false)
  179. mescroll.scrollTo(0, 0)
  180. showLoading.value = true
  181. await reload()
  182. showLoading.value = false
  183. }
  184. async function search() {
  185. // if (activeIndex.value !== '0') {
  186. // activeIndex.value = '0'
  187. // }
  188. // categoryType.value = ''
  189. // if (couponType.value !== '0') {
  190. // couponType.value = '0'
  191. // }
  192. mescroll.endUpScroll(false)
  193. mescroll.scrollTo(0, 0)
  194. showLoading.value = true
  195. // 判断当前是否是搜索状态
  196. if (searchValue.value !== '') {
  197. isSearching.value = true
  198. } else {
  199. isSearching.value = false
  200. }
  201. await reload()
  202. showLoading.value = false
  203. }
  204. async function clear() {
  205. searchValue.value = ''
  206. mescroll.endUpScroll(false)
  207. mescroll.scrollTo(0, 0)
  208. showLoading.value = true
  209. await reload()
  210. isSearching.value = false
  211. showLoading.value = false
  212. }
  213. function searchChange(value) {
  214. searchValue.value = value
  215. }
  216. function regionSelection() {
  217. uni.navigateTo({
  218. url: '/pages-A/regionSelection/index',
  219. })
  220. }
  221. async function couponTypeChange(type) {
  222. couponType.value = type
  223. activeIndex.value = '0'
  224. mescroll.endUpScroll(false)
  225. mescroll.scrollTo(0, 0)
  226. showLoading.value = true
  227. await reload()
  228. showLoading.value = false
  229. }
  230. </script>
  231. <template>
  232. <view class="invite-container">
  233. <view class="invite-content">
  234. <!-- <view class="invite-content"
  235. :style="{ marginTop: `calc(${topSafeAreaHeight}px + 88rpx)`, height: `calc(100vh - ${topSafeAreaHeight}px - 108rpx)`}"> -->
  236. <!-- coupon-tabs -->
  237. <view
  238. class="coupon-types h-42rpx bg-#ffffff pr-10rpx pl-10rpx flex flex-row flex-nowrap justify-between items-center gap-34rpx">
  239. <view :class="{ 'coupon_type_active': couponType === '2' }"
  240. @click="couponTypeChange('2')">
  241. 折扣券
  242. </view>
  243. <view :class="{ 'coupon_type_active': couponType === '3' }"
  244. @click="couponTypeChange('3')">
  245. 满减券
  246. </view>
  247. <view :class="{ 'coupon_type_active': couponType === '4' }"
  248. @click="couponTypeChange('4')">
  249. 兑换券
  250. </view>
  251. </view>
  252. <!-- search -->
  253. <view class="invite-content-search bg-white">
  254. <view class="content-search-prefix" @tap="regionSelection">
  255. <up-icon size="12" style="margin-top: 2rpx;" name="map" />
  256. <text>
  257. {{ currentCity.name }}
  258. </text>
  259. </view>
  260. <up-input style="height: 100%; padding-left: 24rpx; font-size: 24rpx;" v-model="searchValue" border="none"
  261. class="invite-content-search-input" placeholder="请输入券名称" :clearable="true" @confirm="search"
  262. @clear="clear" />
  263. <view class="search-button">
  264. <up-button type="text" @tap="search">
  265. 搜索
  266. </up-button>
  267. </view>
  268. </view>
  269. <!-- category-tabs -->
  270. <view class="tabs-container flex">
  271. <!-- <view :class="[!isSearching ? 'expanded' : 'collapsed']" class="tabs-container flex"> -->
  272. <up-tabs v-model:current="activeIndex" :scrollable="true" class="tabs-content" lineWidth="0" :list="categoryList"
  273. :active-style="{ color: '#ffffff', backgroundColor: '#FF5365', borderRadius: '999rpx', padding: '0 16rpx', lineHeight: '44rpx', zIndex: 2 }"
  274. :inactive-style="{ backgroundColor: '#F5F5F5', borderRadius: '999rpx', padding: '0 16rpx', lineHeight: '44rpx' }"
  275. item-style="height: 44rpx; padding: 0 20rpx 0 0" style="width: 100%;" @change="tabChange" />
  276. <view class="tabs-mask"/>
  277. </view>
  278. <!-- coupon-list -->
  279. <view v-if="!hasLogin" class="empty-container">
  280. <view class="custom-empty"
  281. :style="{
  282. backgroundImage: `url(${getImageUrl('@img/index/login_bg.png')})`,
  283. backgroundSize: 'cover',
  284. backgroundPosition: 'center'
  285. }"/>
  286. <view class="login-btn" @click="login">去登录</view>
  287. </view>
  288. <view v-else class="invite-content-list">
  289. <!-- :style="{ height: `calc(100vh - ${topSafeAreaHeight}px - 70rpx - 88rpx - 20rpx - 176rpx)` }"> -->
  290. <mescroll-uni id="mescrollContainer" :down="downOption" :up="upOption" :fixed="false" offset="70"
  291. @init="mescrollInit" @down="downCallback" @up="upCallback" @emptyclick="reload">
  292. <template v-if="isSearching">
  293. <view v-for="(item, index) in data" :key="index">
  294. <!-- <view v-if="Array.isArray(item.data)" class="text-34rpx text-#333333 border">{{ item.name }}
  295. </view> -->
  296. <view v-for="coupon in item.data" :key="item.templateId" class="invite-content-item">
  297. <!-- <invite-item :data="item" /> -->
  298. <template v-if="coupon.type === '1'">
  299. <ExchangeCoupon :coupon="coupon" />
  300. </template>
  301. <template v-else>
  302. <TemplateCoupon :coupon="coupon" />
  303. </template>
  304. </view>
  305. </view>
  306. </template>
  307. <template v-else>
  308. <view v-for="item in data" :key="item.templateId" class="invite-content-item">
  309. <!-- <invite-item :data="item" /> -->
  310. <!-- 优惠券类型:4-兑换券,2-折扣券,3-满减券 -->
  311. <template v-if="item.type == '4'">
  312. <ExchangeCoupon :coupon="item" />
  313. </template>
  314. <template v-else>
  315. <TemplateCoupon :coupon="item" />
  316. </template>
  317. </view>
  318. </template>
  319. <!-- 滚动区域内的loading遮罩 -->
  320. <view v-if="showLoading && loading" class="loading-mask">
  321. <view class="loading-spinner" />
  322. <text class="loading-text">加载中...</text>
  323. </view>
  324. </mescroll-uni>
  325. </view>
  326. </view>
  327. </view>
  328. </template>
  329. <style lang="scss" scoped>
  330. .invite-container {
  331. padding: 0 20rpx 20rpx;
  332. margin-bottom: 20rpx;
  333. background: linear-gradient( 180deg, #FF8180 0%, rgba(255,127,92,0.49) 30%);
  334. border-radius: 0 0 32rpx 32rpx;
  335. .invite-content {
  336. // max-height: 1130rpx;
  337. display: flex;
  338. flex-direction: column;
  339. border-radius: 32rpx 32rpx 32rpx 32rpx;
  340. background-color: #ffffff;
  341. padding: 24rpx 20rpx 20rpx;
  342. .coupon-types {
  343. view {
  344. width: 94rpx;
  345. line-height: 42rpx;
  346. text-align: center;
  347. font-weight: 400;
  348. font-size: 30rpx;
  349. color: rgba(0,0,0,0.5);
  350. position: relative;
  351. }
  352. .coupon_type_active {
  353. font-weight: 600 !important;
  354. font-size: 30rpx;
  355. color: #333333;
  356. z-index: 1;
  357. // 添加伪元素作为下划线
  358. &:after {
  359. content: '';
  360. position: absolute;
  361. bottom: 6rpx;
  362. left: 0;
  363. width: 100%;
  364. height: 10rpx;
  365. background-color: #FF5666;
  366. z-index: -1;
  367. }
  368. }
  369. }
  370. .invite-content-search {
  371. padding: 0 8rpx 0 24rpx;
  372. margin: 31rpx 0 24rpx;
  373. display: flex;
  374. flex-direction: row;
  375. flex-wrap: nowrap;
  376. align-items: center;
  377. height: 64rpx;
  378. background: #F0F0F0;
  379. border-radius: 999rpx;
  380. .content-search-prefix {
  381. min-width: 127rpx;
  382. // border-top-left-radius: 10rpx;
  383. // border-bottom-left-radius: 10rpx;
  384. display: flex;
  385. justify-content: center;
  386. align-items: center;
  387. position: relative;
  388. text {
  389. font-weight: 400;
  390. font-size: 26rpx;
  391. color: #333333;
  392. text-overflow: ellipsis;
  393. overflow: hidden;
  394. white-space: nowrap;
  395. max-width: 200rpx;
  396. margin: 0 23rpx 0 1rpx;
  397. }
  398. &:after {
  399. content: '';
  400. position: absolute;
  401. right: 0;
  402. top: 50%;
  403. transform: translateY(-50%);
  404. width: 1rpx;
  405. height: 20rpx;
  406. background: #333333;
  407. }
  408. }
  409. .invite-content-search-input {
  410. height: 60rpx;
  411. border-radius: 0;
  412. }
  413. .search-button {
  414. display: flex;
  415. justify-content: center;
  416. align-items: center;
  417. width: 100rpx;
  418. height: 44rpx;
  419. background: #FF5365;
  420. border-radius: 999rpx 999rpx 999rpx 999rpx;
  421. font-weight: 400;
  422. font-size: 26rpx;
  423. color: #FFFFFF;
  424. }
  425. }
  426. .tabs-container {
  427. height: 46rpx;
  428. display: flex;
  429. flex-direction: row;
  430. justify-content: start;
  431. align-items: center;
  432. position: relative;
  433. margin-bottom: 20rpx;
  434. overflow-x: auto; /* 添加横向滚动 */
  435. white-space: nowrap; /* 防止标签换行 */
  436. .tabs-content {
  437. line-height: 46rpx;
  438. font-weight: 400;
  439. font-size: 26rpx;
  440. color: #333333;
  441. }
  442. .tabs-mask {
  443. position: absolute;
  444. top: 0;
  445. right: 0;
  446. z-index: 1;
  447. width: 159rpx;
  448. height: 44rpx;
  449. background: linear-gradient(135deg,rgba(255, 85, 101, 0) 0%, #FFFFFF 100%, );
  450. pointer-events: none;
  451. }
  452. }
  453. /* 收起状态 */
  454. .tabs-container.collapsed {
  455. opacity: 0;
  456. max-height: 0;
  457. }
  458. /* 展开状态 */
  459. .tabs-container.expanded {
  460. opacity: 1;
  461. max-height: 48rpx;
  462. }
  463. .invite-content-list {
  464. display: flex;
  465. flex-direction: column;
  466. position: relative;
  467. #mescrollContainer {
  468. height: 100%;
  469. }
  470. .invite-content-item {
  471. margin-bottom: 20rpx;
  472. }
  473. // loading遮罩样式
  474. .loading-mask {
  475. position: absolute;
  476. top: 0;
  477. left: 0;
  478. right: 0;
  479. bottom: 0;
  480. background-color: rgba(255, 255, 255, 0.9);
  481. display: flex;
  482. flex-direction: column;
  483. justify-content: center;
  484. align-items: center;
  485. z-index: 999;
  486. }
  487. .loading-spinner {
  488. width: 40rpx;
  489. height: 40rpx;
  490. border: 4rpx solid #e0e0e0;
  491. border-top-color: #ed6b66;
  492. border-radius: 50%;
  493. animation: spin 1s linear infinite;
  494. }
  495. .loading-text {
  496. margin-top: 16rpx;
  497. font-size: 24rpx;
  498. color: #666;
  499. }
  500. @keyframes spin {
  501. to {
  502. transform: rotate(360deg);
  503. }
  504. }
  505. }
  506. }
  507. .accordion-enter-active,
  508. .accordion-leave-active {
  509. transition: all 0.4s ease;
  510. overflow: hidden;
  511. }
  512. .accordion-enter-from,
  513. .accordion-leave-to {
  514. max-height: 0;
  515. opacity: 0;
  516. transform: translateY(-10rpx);
  517. }
  518. .accordion-enter-to,
  519. .accordion-leave-from {
  520. max-height: 88rpx;
  521. opacity: 1;
  522. transform: translateY(0);
  523. }
  524. }
  525. /* 自定义 up-empty 图标大小 */
  526. .empty-container {
  527. width: 100%;
  528. display: flex;
  529. flex-direction: column;
  530. align-items: center;
  531. gap: 13rpx;
  532. margin: 20rpx 0 35rpx;
  533. .custom-empty {
  534. width: 160rpx;
  535. height: 140rpx;
  536. }
  537. .login-btn {
  538. display: flex;
  539. justify-content: center;
  540. align-items: center;
  541. width: 102rpx;
  542. height: 44rpx;
  543. background: #FF5365;
  544. border-radius: 999rpx;
  545. font-weight: 400;
  546. font-size: 26rpx;
  547. color: #FFFFFF;
  548. }
  549. }
  550. ::v-deep {
  551. .uni-input-placeholder {
  552. font-weight: 400;
  553. font-size: 24rpx !important;
  554. color: #808080;
  555. }
  556. .mescroll-empty {
  557. padding-top: 150rpx;
  558. }
  559. }
  560. </style>