图书馆小程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

127 lines
3.0 KiB

1 week ago
  1. <template>
  2. <view class="container">
  3. <!-- 扫码按钮 -->
  4. <button type="primary" @click="scanCode" class="scan-btn">
  5. 扫码识别图书
  6. </button>
  7. <!-- 图书信息展示 -->
  8. <view class="book-info" v-if="bookData.title">
  9. <image :src="bookData.image" class="book-cover" mode="widthFix"></image>
  10. <view class="book-title">{{ bookData.title }}</view>
  11. <view class="book-desc">作者{{ bookData.author }}</view>
  12. <view class="book-desc">出版社{{ bookData.publisher }}</view>
  13. <view class="book-desc">出版日期{{ bookData.pubdate }}</view>
  14. <view class="book-desc">ISBN{{ isbn }}</view>
  15. </view>
  16. <!-- 无数据提示 -->
  17. <view class="empty" v-else>
  18. 请点击按钮扫描图书条形码
  19. </view>
  20. </view>
  21. </template>
  22. <script>
  23. export default {
  24. data() {
  25. return {
  26. isbn: '', // 扫码得到的ISBN
  27. bookData: {} // 图书信息
  28. }
  29. },
  30. methods: {
  31. // 调用微信扫码
  32. async scanCode() {
  33. try {
  34. // 只允许扫条形码(图书ISBN)
  35. const res = await uni.scanCode({
  36. onlyFromCamera: true,
  37. scanType: ['barCode']
  38. })
  39. // res.result 就是扫码得到的ISBN编码
  40. // this.isbn = res.result
  41. this.isbn = '9787020167319'
  42. console.log('扫码结果:', this.isbn)
  43. // 根据ISBN查询图书信息
  44. this.getBookInfo()
  45. } catch (err) {
  46. uni.showToast({
  47. title: '扫码失败',
  48. icon: 'none'
  49. })
  50. }
  51. },
  52. // 查询图书信息(免费公开接口)
  53. async getBookInfo() {
  54. uni.showLoading({ title: '查询中...' })
  55. try {
  56. this.bookData = {
  57. title: "活着",
  58. author: "余华",
  59. publisher: "人民文学出版社",
  60. pubdate: "2012-08-01",
  61. image: "https://qiniu.aiyxlib.com/1606124577077"
  62. };
  63. uni.hideLoading();
  64. uni.showToast({ title: "查询成功" });
  65. // const res = await uni.request({
  66. // url: 'https://api.jike.xyz/situ/book/isbn/' + this.isbn,
  67. // method: 'GET'
  68. // })
  69. // if (res.data.code === 200) {
  70. // this.bookData = res.data.data
  71. // uni.showToast({ title: '查询成功' })
  72. // } else {
  73. // uni.showToast({ title: '未查询到图书信息', icon: 'none' })
  74. // }
  75. } catch (err) {
  76. uni.showToast({ title: '查询失败', icon: 'none' })
  77. } finally {
  78. uni.hideLoading()
  79. }
  80. }
  81. }
  82. }
  83. </script>
  84. <style scoped>
  85. .container {
  86. padding: 40rpx;
  87. }
  88. .scan-btn {
  89. margin-bottom: 40rpx;
  90. }
  91. .book-info {
  92. display: flex;
  93. flex-direction: column;
  94. align-items: center;
  95. }
  96. .book-cover {
  97. width: 300rpx;
  98. height: 420rpx;
  99. margin-bottom: 20rpx;
  100. border: 1rpx solid #eee;
  101. }
  102. .book-title {
  103. font-size: 36rpx;
  104. font-weight: bold;
  105. margin-bottom: 15rpx;
  106. }
  107. .book-desc {
  108. font-size: 28rpx;
  109. color: #666;
  110. margin-bottom: 10rpx;
  111. }
  112. .empty {
  113. text-align: center;
  114. color: #999;
  115. margin-top: 100rpx;
  116. }
  117. </style>