location-select.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <template>
  2. <view class="container" @click="playGlobalSound">
  3. <!-- 库位列表 -->
  4. <scroll-view scroll-y class="location-list" :style="{ height: scrollHeight + 'px' }">
  5. <view v-for="(item, index) in locations" :key="index"
  6. :class="['location-item', item === positionCode ? 'location-item-selected' : '']"
  7. hover-class="location-item-hover" @click="selectLocation(item)">
  8. <text>{{ item }}</text>
  9. </view>
  10. </scroll-view>
  11. <!-- 底部搜索框 -->
  12. <view class="fixed-bottom pad-20" style="background: #ffffff;">
  13. <u-search v-model="searchText" placeholder="请输入库位条码" :show-action="false" :clearabled="true"
  14. @search="onSearch" height="40" @click="searchText = ''">
  15. </u-search>
  16. <u-icon name="scan" size="28" color="#19be6b" @click="openScan"></u-icon>
  17. </view>
  18. </view>
  19. </template>
  20. <script setup>
  21. import {
  22. ref,
  23. computed,
  24. onMounted
  25. } from 'vue'
  26. import {
  27. onLoad,
  28. onShow,
  29. onUnload
  30. } from '@dcloudio/uni-app'
  31. // 搜索文本
  32. const searchText = ref('')
  33. // 点击全局音效
  34. function playGlobalSound(){
  35. uni.$u.playClickSound()
  36. }
  37. // 库位列表数据
  38. const locations = ref([])
  39. //根据仓库id获取库位列表
  40. function getLocationListByGodownId(godownId, code = "", type = "") {
  41. uni.$u.http.get('/app/stock/getGodownPosition?godownId=' + godownId + '&code=' + code).then(res => {
  42. if (res.code == 200) {
  43. locations.value = res.data
  44. if (type == 'select') {
  45. setTimeout(() => {
  46. locations.value.forEach(item => {
  47. if (item == code) {
  48. selectLocation(item)
  49. }
  50. })
  51. },500)
  52. }
  53. }
  54. })
  55. }
  56. // 滚动区域高度
  57. const scrollHeight = ref(0)
  58. // 搜索处理
  59. const onSearch = () => {
  60. // 实现搜索逻辑
  61. getLocationListByGodownId(godownId.value, searchText.value)
  62. }
  63. // 打开扫码
  64. const openScan = () => {
  65. // #ifdef APP-PLUS || MP-WEIXIN
  66. uni.scanCode({
  67. success: (res) => {
  68. searchText.value = res.result
  69. onSearch()
  70. },
  71. fail: (err) => {
  72. uni.showToast({
  73. title: '扫码失败',
  74. icon: 'error'
  75. })
  76. }
  77. })
  78. // #endif
  79. // #ifdef H5
  80. uni.showToast({
  81. title: 'H5环境不支持扫码',
  82. icon: 'none'
  83. })
  84. // #endif
  85. }
  86. // 选择库位
  87. const selectLocation = (item) => {
  88. // 也可以使用事件总线或页面参数的方式传值
  89. uni.$emit('updateLocation', item)
  90. // 返回上一页
  91. uni.navigateBack()
  92. }
  93. const godownId = ref('')
  94. const positionCode = ref('')
  95. onLoad((opts) => {
  96. godownId.value = opts.godownId
  97. positionCode.value = opts.positionCode
  98. getLocationListByGodownId(godownId.value)
  99. scrollHeight.value = uni.getSystemInfoSync().windowHeight - 44 - 60
  100. // #ifdef APP-PLUS
  101. uni.$u.useGlobalEvent((e) => {
  102. if (e.barcode) {
  103. searchText.value = e.barcode
  104. getLocationListByGodownId(godownId.value, e.barcode, 'select')
  105. }
  106. })
  107. // #endif
  108. })
  109. onShow(() => {
  110. uni.$u.updateActivePageOnShow()
  111. })
  112. onUnload(() => {
  113. uni.$u.cleanupOnPageUnload();
  114. });
  115. </script>
  116. <style scoped>
  117. .container {
  118. position: relative;
  119. }
  120. .location-list {
  121. padding: 12px 0 60px;
  122. }
  123. .location-item {
  124. background-color: #fff;
  125. padding: 15px 20px;
  126. margin-bottom: 1px;
  127. font-size: 16px;
  128. }
  129. .location-item-selected {
  130. background-color: #e8f5e9;
  131. border-left: 4px solid #19be6b;
  132. }
  133. .location-item-hover {
  134. background-color: #f5f5f5;
  135. }
  136. </style>