code-storage.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <template>
  2. <view class="container">
  3. <view class="main-content">
  4. <view class="input-group">
  5. <u-input :customStyle="customStyle" :placeholder-style="placeholderStyle" v-model="form.isbn"
  6. placeholder="扫描/输入ISBN" border="surround" />
  7. <u-button :customStyle="customStyle" type="info" color="#a4adb3" @click="handleBarcode(form.isbn)"
  8. text="确定" />
  9. </view>
  10. </view>
  11. <view class="fixed-bottom">
  12. <u-button size="large" type="success" @click="scanCode" text="扫码" class="scan-button" />
  13. </view>
  14. </view>
  15. </template>
  16. <script setup>
  17. import {
  18. reactive,
  19. ref,
  20. onUnmounted
  21. } from 'vue';
  22. import {
  23. onLoad,
  24. onShow
  25. } from '@dcloudio/uni-app'
  26. const placeholderStyle = "font-size:32rpx"
  27. const customStyle = reactive({
  28. height: '90rpx'
  29. })
  30. const form = ref({
  31. "isbn": ""
  32. })
  33. function scanCode() {
  34. uni.scanCode({
  35. success: (res) => {
  36. form.value.isbn = res.result;
  37. handleBarcode(res.result)
  38. },
  39. fail: (err) => {
  40. uni.$u.toast('扫码失败')
  41. }
  42. });
  43. }
  44. //isbn正则校验是否符合
  45. function checkIsbn(isbn) {
  46. const isbn13Regex = /^(?:97[89]-?\d{1,5}-?\d{1,7}-?\d{1,6}-?\d)$/;
  47. if (isbn13Regex.test(isbn)) {
  48. return true;
  49. }
  50. return false;
  51. }
  52. function handleBarcode(code) {
  53. if (!code) return uni.$u.toast('请输入ISBN')
  54. // 验证ISBN格式
  55. if (!checkIsbn(code)) {
  56. return uni.$u.ttsModule.speak('不是正确的ISBN码')
  57. }
  58. // 这里可以添加ISBN查询逻辑
  59. uni.$u.http.post('/activation/bookActivationInfo/queryBookBasicByIsbn/' + code).then(res => {
  60. if (res.code == 200) {
  61. // 处理查询成功的逻辑
  62. uni.navigateTo({
  63. url: '/pages/index/wms/code-storage-add?isbn=' + code
  64. })
  65. form.value.isbn = ""
  66. // 传递书籍信息到下一页
  67. uni.setStorageSync('bookInfo', res.data)
  68. } else {
  69. uni.$u.toast('未找到该书籍')
  70. }
  71. })
  72. }
  73. // #ifdef APP-PLUS
  74. const { unregister } = uni.$u.useEventListener((e) => {
  75. form.value.isbn = e.barcode
  76. handleBarcode(e.barcode)
  77. });
  78. // #endif
  79. onUnmounted(() => {
  80. // #ifdef APP-PLUS
  81. unregister();
  82. // #endif
  83. });
  84. </script>
  85. <style lang="scss" scoped>
  86. .main-content {
  87. padding: 20px;
  88. gap: 20px;
  89. }
  90. .input-group {
  91. display: flex;
  92. gap: 10px;
  93. margin-bottom: 30rpx;
  94. :deep(.u-button) {
  95. width: 160rpx
  96. }
  97. .u-input {
  98. background-color: #fff;
  99. }
  100. }
  101. .scan-button {
  102. width: 100%;
  103. }
  104. </style>