index.vue 3.9 KB

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