index.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <template>
  2. <page-scroll ref="pageScrollRef" :loadmore="loadMore" :loading="loading" :empty="isEmpty" emptyText="暂无搜索结果">
  3. <!-- 搜索框 -->
  4. <view class="search-header">
  5. <view class="search-box">
  6. <u-icon name="scan" size="24" color="#ffffff" @click="scanCode"></u-icon>
  7. <u-search v-model="searchText" placeholder="请输入ISBN" :showAction="false" :clearabled="true"
  8. @search="handleSearch" @clear="handleClear" bgColor="#ffffff"></u-search>
  9. </view>
  10. </view>
  11. <!-- 搜索结果列表 -->
  12. <view class="book-list" v-if="bookList.length > 0">
  13. <view class="book-item" v-for="(book, index) in bookList" :key="index">
  14. <view class="book-info">
  15. <image class="book-image" :src="book.coverUrl" mode="aspectFill"></image>
  16. <view class="book-details">
  17. <text class="book-title">{{ book.title }}</text>
  18. <text class="book-isbn">ISBN: {{ book.isbn }}</text>
  19. <text class="book-price">定价: {{ book.price }}</text>
  20. <text class="book-set">套装: {{ book.isSet ? '是' : '否' }}</text>
  21. </view>
  22. </view>
  23. <view class="book-action">
  24. <u-icon name="edit-pen" size="20" color="#4cd964" @click="editBook(book)"></u-icon>
  25. </view>
  26. </view>
  27. </view>
  28. <feedback-popup v-model:show="showFeedback" @submit="handleFeedbackSubmit"></feedback-popup>
  29. </page-scroll>
  30. </template>
  31. <script setup>
  32. import {
  33. ref,
  34. computed
  35. } from 'vue'
  36. import PageScroll from '@/components/pageScroll/index.vue'
  37. import FeedbackPopup from './components/feedbackPopup.vue';
  38. const showFeedback = ref(false)
  39. function handleFeedbackSubmit(data) {
  40. console.log('反馈数据:', data)
  41. }
  42. // 页面滚动组件引用
  43. const pageScrollRef = ref(null)
  44. // 搜索相关
  45. const searchText = ref('')
  46. const loading = ref(false)
  47. const bookList = ref([{
  48. id: 1,
  49. title: '公文写作教程',
  50. isbn: '9787040515555',
  51. price: '49.5',
  52. isSet: false,
  53. coverUrl: 'https://img20.360buyimg.com/da/jfs/t1/141592/25/8861/261559/5f68d8c1E33ed78ab/698ad655bfcfbaed.png'
  54. }])
  55. // 是否为空
  56. const isEmpty = computed(() => {
  57. return !loading.value && bookList.value.length === 0
  58. })
  59. // 处理搜索
  60. async function handleSearch() {
  61. if (!searchText.value) {
  62. uni.showToast({
  63. title: '请输入ISBN',
  64. icon: 'none'
  65. })
  66. return
  67. }
  68. await loadData()
  69. }
  70. // 处理清除
  71. function handleClear() {
  72. bookList.value = []
  73. }
  74. // 加载数据
  75. async function loadData() {
  76. loading.value = true
  77. try {
  78. // 模拟API调用
  79. const res = await mockSearchBooks(searchText.value)
  80. bookList.value = res
  81. } catch (error) {
  82. uni.showToast({
  83. title: '加载失败',
  84. icon: 'none'
  85. })
  86. } finally {
  87. loading.value = false
  88. }
  89. }
  90. // 加载更多
  91. async function loadMore() {
  92. if (loading.value) return
  93. page.value++
  94. await loadData()
  95. }
  96. // 扫码
  97. function scanCode() {
  98. uni.scanCode({
  99. scanType: ['barCode'],
  100. success: (res) => {
  101. searchText.value = res.result
  102. handleSearch()
  103. },
  104. fail: () => {
  105. uni.showToast({
  106. title: '扫码失败',
  107. icon: 'none'
  108. })
  109. }
  110. })
  111. }
  112. // 编辑图书
  113. function editBook(book) {
  114. // 处理编辑逻辑
  115. showFeedback.value = true
  116. }
  117. // 模拟搜索API
  118. function mockSearchBooks(isbn) {
  119. return new Promise((resolve) => {
  120. setTimeout(() => {
  121. resolve([{
  122. id: 1,
  123. title: '公文写作教程',
  124. isbn: '9787040515555',
  125. price: '49.5',
  126. isSet: false,
  127. coverUrl: '/static/book-cover.jpg'
  128. }])
  129. }, 1000)
  130. })
  131. }
  132. </script>
  133. <style lang="scss" scoped>
  134. .search-header {
  135. position: sticky;
  136. top: 0;
  137. z-index: 100;
  138. background-color: #4cd964;
  139. padding: 20rpx;
  140. .search-box {
  141. display: flex;
  142. align-items: center;
  143. border-radius: 8rpx;
  144. padding: 0 20rpx;
  145. }
  146. }
  147. .book-list {
  148. padding: 20rpx;
  149. .book-item {
  150. display: flex;
  151. justify-content: space-between;
  152. align-items: center;
  153. background-color: #ffffff;
  154. border-radius: 12rpx;
  155. padding: 20rpx;
  156. margin-bottom: 20rpx;
  157. box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.04);
  158. .book-info {
  159. display: flex;
  160. flex: 1;
  161. .book-image {
  162. width: 160rpx;
  163. height: 200rpx;
  164. border-radius: 8rpx;
  165. margin-right: 20rpx;
  166. }
  167. .book-details {
  168. display: flex;
  169. flex-direction: column;
  170. justify-content: space-between;
  171. .book-title {
  172. font-size: 32rpx;
  173. font-weight: bold;
  174. color: #333;
  175. }
  176. .book-isbn,
  177. .book-price,
  178. .book-set {
  179. font-size: 26rpx;
  180. color: #666;
  181. margin-top: 8rpx;
  182. }
  183. }
  184. }
  185. .book-action {
  186. padding: 20rpx;
  187. }
  188. }
  189. }
  190. </style>