location-select.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <template>
  2. <view class="container">
  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. } from '@dcloudio/uni-app'
  30. // 搜索文本
  31. const searchText = ref('')
  32. // 库位列表数据
  33. const locations = ref([])
  34. //根据仓库id获取库位列表
  35. function getLocationListByGodownId(godownId, code = "", type = "") {
  36. uni.$u.http.get('/app/stock/getGodownPosition?godownId=' + godownId + '&code=' + code).then(res => {
  37. if (res.code == 200) {
  38. locations.value = res.data
  39. if (type == 'select') {
  40. setTimeout(() => {
  41. locations.value.forEach(item => {
  42. if (item == code) {
  43. selectLocation(item)
  44. }
  45. })
  46. },500)
  47. }
  48. }
  49. })
  50. }
  51. // 滚动区域高度
  52. const scrollHeight = ref(0)
  53. // 搜索处理
  54. const onSearch = () => {
  55. // 实现搜索逻辑
  56. getLocationListByGodownId(godownId.value, searchText.value)
  57. }
  58. // 打开扫码
  59. const openScan = () => {
  60. // #ifdef APP-PLUS || MP-WEIXIN
  61. uni.scanCode({
  62. success: (res) => {
  63. searchText.value = res.result
  64. onSearch()
  65. },
  66. fail: (err) => {
  67. uni.showToast({
  68. title: '扫码失败',
  69. icon: 'error'
  70. })
  71. }
  72. })
  73. // #endif
  74. // #ifdef H5
  75. uni.showToast({
  76. title: 'H5环境不支持扫码',
  77. icon: 'none'
  78. })
  79. // #endif
  80. }
  81. // 选择库位
  82. const selectLocation = (item) => {
  83. // 也可以使用事件总线或页面参数的方式传值
  84. uni.$emit('updateLocation', item)
  85. // 返回上一页
  86. uni.navigateBack()
  87. }
  88. const godownId = ref('')
  89. const positionCode = ref('')
  90. onLoad((opts) => {
  91. godownId.value = opts.godownId
  92. positionCode.value = opts.positionCode
  93. getLocationListByGodownId(godownId.value)
  94. scrollHeight.value = uni.getSystemInfoSync().windowHeight - 44 - 60
  95. // #ifdef APP-PLUS
  96. uni.$u.useGlobalEvent((e) => {
  97. if (e.barcode) {
  98. searchText.value = e.barcode
  99. getLocationListByGodownId(godownId.value, e.barcode, 'select')
  100. }
  101. })
  102. // #endif
  103. })
  104. onShow(() => {
  105. uni.$u.updateActivePageOnShow()
  106. })
  107. </script>
  108. <style scoped>
  109. .container {
  110. position: relative;
  111. }
  112. .location-list {
  113. padding: 12px 0 60px;
  114. }
  115. .location-item {
  116. background-color: #fff;
  117. padding: 15px 20px;
  118. margin-bottom: 1px;
  119. font-size: 16px;
  120. }
  121. .location-item-selected {
  122. background-color: #e8f5e9;
  123. border-left: 4px solid #19be6b;
  124. }
  125. .location-item-hover {
  126. background-color: #f5f5f5;
  127. }
  128. </style>