index.vue 3.9 KB

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