index.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <template>
  2. <view class="book-list" style="padding:0">
  3. <!-- 搜索框 -->
  4. <view class="search-header">
  5. <view class="search-box">
  6. <u-icon style="margin-right:16rpx" name="scan" size="30" color="#ffffff" @click="scanCode"></u-icon>
  7. <u-search height="80rpx" v-model="searchText" placeholder="请输入ISBN" :showAction="false"
  8. :clearabled="true" @search="handleSearch" 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.cover" mode="aspectFill"></image>
  16. <view class="book-details">
  17. <text class="book-title">{{ book.bookName }}</text>
  18. <text class="book-isbn">ISBN: {{ book.isbn }}</text>
  19. <text class="book-price">定价: {{ book.price }}</text>
  20. <text class="book-set">套装: {{ book.suit == 1 ? '是' : '否' }}</text>
  21. </view>
  22. </view>
  23. <view class="book-action">
  24. <u-icon name="edit-pen" size="30" color="#4cd964" @click="editBook(book)"></u-icon>
  25. </view>
  26. </view>
  27. </view>
  28. <feedback-popup ref="showFeedback" @submit="handleFeedbackSubmit"></feedback-popup>
  29. </view>
  30. </template>
  31. <script setup>
  32. import {
  33. ref,
  34. onUnmounted
  35. } from 'vue'
  36. import FeedbackPopup from './components/feedbackPopup.vue';
  37. const showFeedback = ref(null)
  38. function handleFeedbackSubmit(data) {
  39. console.log('反馈数据:', data)
  40. }
  41. // 搜索相关
  42. const searchText = ref('')
  43. const bookList = ref([])
  44. // 处理搜索
  45. async function handleSearch() {
  46. if (!searchText.value) {
  47. uni.showToast({
  48. title: '请输入ISBN',
  49. icon: 'none'
  50. })
  51. return
  52. }
  53. await getSimpleBookInfoByIsbn(searchText.value)
  54. }
  55. // 加载数据
  56. async function getSimpleBookInfoByIsbn(isbn) {
  57. uni.$u.http.get(`/app/book/getSimpleBookInfoByIsbn/${isbn}`).then(res => {
  58. if (res.code == 200) {
  59. let bool = bookList.value.some(v => v.isbn == isbn)
  60. if (!bool) {
  61. bookList.value.push(res.data)
  62. }
  63. } else {
  64. uni.$u.toast(res.msg)
  65. }
  66. })
  67. }
  68. // 扫码
  69. function scanCode() {
  70. uni.scanCode({
  71. scanType: ['barCode'],
  72. success: (res) => {
  73. searchText.value = res.result
  74. getSimpleBookInfoByIsbn(res.result)
  75. },
  76. fail: () => {
  77. uni.showToast({
  78. title: '扫码失败',
  79. icon: 'none'
  80. })
  81. }
  82. })
  83. }
  84. // 编辑图书
  85. function editBook(book) {
  86. // 处理编辑逻辑
  87. showFeedback.value?.open(book)
  88. }
  89. // #ifdef APP-PLUS
  90. const { unregister } = uni.$u.useEventListener((e) => {
  91. searchText.value = e.barcode;
  92. handleSearch();
  93. });
  94. // #endif
  95. onUnmounted(() => {
  96. unregister()
  97. })
  98. </script>
  99. <style lang="scss" scoped>
  100. .search-header {
  101. position: sticky;
  102. top: 0;
  103. z-index: 100;
  104. background-color: rgb(34, 172, 56);
  105. padding: 20rpx;
  106. .search-box {
  107. display: flex;
  108. align-items: center;
  109. border-radius: 8rpx;
  110. padding: 0 20rpx;
  111. }
  112. :deep(.uni-input-placeholder) {
  113. font-size: 32rpx;
  114. }
  115. }
  116. .book-list {
  117. padding: 20rpx;
  118. .book-item {
  119. display: flex;
  120. justify-content: space-between;
  121. align-items: center;
  122. background-color: #ffffff;
  123. border-radius: 12rpx;
  124. padding: 20rpx;
  125. margin-bottom: 20rpx;
  126. box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.04);
  127. .book-info {
  128. display: flex;
  129. flex: 1;
  130. .book-image {
  131. width: 160rpx;
  132. height: 200rpx;
  133. border-radius: 8rpx;
  134. margin-right: 20rpx;
  135. }
  136. .book-details {
  137. display: flex;
  138. flex-direction: column;
  139. justify-content: space-between;
  140. .book-title {
  141. font-size: 32rpx;
  142. font-weight: bold;
  143. color: #333;
  144. }
  145. .book-isbn,
  146. .book-price,
  147. .book-set {
  148. font-size: 26rpx;
  149. color: #666;
  150. margin-top: 8rpx;
  151. }
  152. }
  153. }
  154. .book-action {
  155. padding: 20rpx;
  156. }
  157. }
  158. }
  159. </style>