MyWishesView.vue 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <template>
  2. <div class="my-wishes-page with-bottom-nav">
  3. <van-nav-bar title="我的愿望" />
  4. <div v-if="!userStore.isLoggedIn" class="no-login">
  5. <van-empty description="请先登录查看你的愿望">
  6. <van-button type="primary" round to="/login">去登录</van-button>
  7. </van-empty>
  8. </div>
  9. <template v-else>
  10. <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
  11. <van-list
  12. v-model:loading="loading"
  13. :finished="finished"
  14. finished-text="没有更多了"
  15. @load="loadWishes"
  16. >
  17. <wish-card
  18. v-for="wish in wishes"
  19. :key="wish.id"
  20. :wish="wish"
  21. @click="goWish(wish.id)"
  22. />
  23. </van-list>
  24. </van-pull-refresh>
  25. <van-empty v-if="!loading && wishes.length === 0" description="你还没有许过愿望哦">
  26. <van-button type="primary" round to="/">去许愿</van-button>
  27. </van-empty>
  28. </template>
  29. </div>
  30. </template>
  31. <script setup lang="ts">
  32. import { ref } from 'vue'
  33. import { useRouter } from 'vue-router'
  34. import { useUserStore } from '@/stores/user'
  35. import { fetchMyWishes } from '@/api/wish'
  36. import WishCard from '@/components/WishCard.vue'
  37. import type { Wish } from '@/mock/data'
  38. const router = useRouter()
  39. const userStore = useUserStore()
  40. const wishes = ref<Wish[]>([])
  41. const loading = ref(false)
  42. const finished = ref(false)
  43. const refreshing = ref(false)
  44. let page = 1
  45. async function loadWishes() {
  46. loading.value = true
  47. try {
  48. const result = await fetchMyWishes(page, 20)
  49. if (refreshing.value) {
  50. wishes.value = result.list
  51. } else {
  52. wishes.value = [...wishes.value, ...result.list]
  53. }
  54. finished.value = result.list.length < 20
  55. page++
  56. } catch {
  57. finished.value = true
  58. } finally {
  59. loading.value = false
  60. refreshing.value = false
  61. }
  62. }
  63. async function onRefresh() {
  64. refreshing.value = true
  65. page = 1
  66. finished.value = false
  67. await loadWishes()
  68. }
  69. function goWish(id: number) {
  70. router.push(`/wish/${id}`)
  71. }
  72. </script>
  73. <style scoped>
  74. .my-wishes-page {
  75. min-height: 100vh;
  76. background: #f7f8fa;
  77. }
  78. .no-login {
  79. padding-top: 60px;
  80. }
  81. </style>