code-storage.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <template>
  2. <view class="container">
  3. <view class="main-content">
  4. <view class="input-group">
  5. <u-input :customStyle="customStyle" :placeholder-style="placeholderStyle"
  6. v-model="repository.repositoryName" placeholder="请选择仓库" readonly border="surround" />
  7. <u-button :customStyle="customStyle" type="info" color="#a4adb3" @click="selectRepository"
  8. text="选择" />
  9. </view>
  10. <view class="input-group">
  11. <u-input :customStyle="customStyle" :placeholder-style="placeholderStyle" v-model="form.isbn"
  12. placeholder="扫描/输入ISBN" border="surround" clearable />
  13. <u-button :customStyle="customStyle" type="info" color="#a4adb3" @click="handleBarcode(form.isbn)"
  14. text="查询" />
  15. </view>
  16. </view>
  17. <view class="fixed-bottom">
  18. <u-button size="large" type="success" @click="scanCode" text="扫码" class="scan-button" />
  19. </view>
  20. </view>
  21. </template>
  22. <script setup>
  23. import {
  24. reactive,
  25. ref,
  26. onMounted,
  27. onUnmounted
  28. } from 'vue';
  29. import {
  30. onShow
  31. } from '@dcloudio/uni-app'
  32. const STORAGE_KEY = 'codeStorageLastRepository'
  33. const placeholderStyle = "font-size:32rpx"
  34. const customStyle = reactive({
  35. height: '90rpx'
  36. })
  37. const repository = ref({
  38. id: '',
  39. repositoryName: ''
  40. })
  41. const form = ref({
  42. isbn: ""
  43. })
  44. let barcodeLock = false
  45. let lastBarcode = ''
  46. let lastBarcodeAt = 0
  47. const BARCODE_DEBOUNCE_MS = 2000
  48. function loadLastRepository() {
  49. const lastRepository = uni.getStorageSync(STORAGE_KEY)
  50. if (lastRepository) {
  51. repository.value = {
  52. id: lastRepository.id || '',
  53. repositoryName: lastRepository.repositoryName || ''
  54. }
  55. }
  56. }
  57. function selectRepository() {
  58. uni.navigateTo({
  59. url: '/pages/index/wms/warehouse-select?type=repository&repositoryId=' + (repository.value.id || ''),
  60. fail: (err) => {
  61. console.error('跳转仓库选择页失败', err)
  62. uni.$u.toast('打开仓库选择页失败')
  63. }
  64. })
  65. }
  66. function scanCode() {
  67. if (!repository.value.id) {
  68. return uni.$u.toast('请先选择仓库')
  69. }
  70. uni.scanCode({
  71. success: (res) => {
  72. form.value.isbn = res.result;
  73. handleBarcode(res.result)
  74. },
  75. fail: () => {
  76. uni.$u.toast('扫码失败')
  77. }
  78. });
  79. }
  80. function checkIsbn(isbn) {
  81. const isbn13Regex = /^(?:97[89]-?\d{1,5}-?\d{1,7}-?\d{1,6}-?\d)$/;
  82. return isbn13Regex.test(isbn);
  83. }
  84. function handleBarcode(code) {
  85. if (!repository.value.id) {
  86. return uni.$u.toast('请先选择仓库')
  87. }
  88. if (!code) return uni.$u.toast('请输入ISBN')
  89. const now = Date.now()
  90. if (barcodeLock) return
  91. if (code === lastBarcode && now - lastBarcodeAt < BARCODE_DEBOUNCE_MS) return
  92. if (!checkIsbn(code)) {
  93. return uni.$u.ttsModule.speak('不是正确的ISBN码')
  94. }
  95. barcodeLock = true
  96. uni.$u.http.post('/activation/bookActivationInfo/queryBookBasicByIsbn/' + code).then(res => {
  97. if (res.code == 200 && res.data) {
  98. uni.setStorageSync('bookInfo', res.data)
  99. uni.setStorageSync('codeStorageCurrentRepository', {
  100. id: repository.value.id,
  101. repositoryName: repository.value.repositoryName
  102. })
  103. uni.navigateTo({
  104. url: '/pages/index/wms/code-storage-add?isbn=' + encodeURIComponent(code)
  105. })
  106. form.value.isbn = ""
  107. lastBarcode = code
  108. lastBarcodeAt = Date.now()
  109. } else {
  110. uni.$u.toast(res.msg)
  111. uni.$u.ttsModule.speak(res.msg)
  112. }
  113. }).finally(() => {
  114. barcodeLock = false
  115. })
  116. }
  117. onMounted(() => {
  118. loadLastRepository()
  119. uni.$on('updateRepository', (data) => {
  120. repository.value = {
  121. id: data.id,
  122. repositoryName: data.repositoryName
  123. }
  124. uni.setStorageSync(STORAGE_KEY, {
  125. id: data.id,
  126. repositoryName: data.repositoryName
  127. })
  128. })
  129. })
  130. onShow(() => {
  131. loadLastRepository()
  132. })
  133. // #ifdef APP-PLUS
  134. const { unregister } = uni.$u.useEventListener((e) => {
  135. form.value.isbn = e.barcode
  136. handleBarcode(e.barcode)
  137. });
  138. // #endif
  139. onUnmounted(() => {
  140. uni.$off('updateRepository')
  141. // #ifdef APP-PLUS
  142. unregister();
  143. // #endif
  144. });
  145. </script>
  146. <style lang="scss" scoped>
  147. .main-content {
  148. padding: 20px;
  149. gap: 20px;
  150. }
  151. .input-group {
  152. display: flex;
  153. gap: 10px;
  154. margin-bottom: 30rpx;
  155. :deep(.u-button) {
  156. width: 160rpx
  157. }
  158. .u-input {
  159. background-color: #fff;
  160. }
  161. }
  162. .scan-button {
  163. width: 100%;
  164. }
  165. </style>