warehouse-select.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <template>
  2. <view class="container" @click="playGlobalSound">
  3. <view class="search-area">
  4. <u-search v-model="searchText" placeholder="请输入仓库名称" :show-action="false" :clearabled="true"
  5. @change="onSearch" height="40" placeholder-style="font-size:16px"></u-search>
  6. </view>
  7. <view class="warehouse-list">
  8. <view v-for="(item, index) in displayList" :key="item.id"
  9. :class="['warehouse-item', { 'warehouse-item-active': item.id == selectedId }]"
  10. hover-class="warehouse-item-hover" @click="selectWarehouse(item)">
  11. <text>{{ index + 1 }}.{{ getItemName(item) }}</text>
  12. </view>
  13. <view v-if="isRepositoryMode && !loading && displayList.length === 0" class="empty-tip">暂无仓库数据</view>
  14. </view>
  15. </view>
  16. </template>
  17. <script setup>
  18. import { ref, computed } from 'vue'
  19. import { onLoad } from '@dcloudio/uni-app'
  20. const STORAGE_KEY = 'codeStorageLastRepository'
  21. function playGlobalSound() {
  22. uni.$u.playClickSound()
  23. }
  24. const searchText = ref('')
  25. const warehouses = ref([])
  26. const loading = ref(false)
  27. const selectedId = ref()
  28. const pageType = ref('godown')
  29. const isRepositoryMode = computed(() => pageType.value === 'repository')
  30. const displayList = computed(() => {
  31. if (!isRepositoryMode.value) {
  32. return warehouses.value
  33. }
  34. const keyword = searchText.value.trim()
  35. if (!keyword) return warehouses.value
  36. return warehouses.value.filter(item => getItemName(item)?.includes(keyword))
  37. })
  38. function getItemName(item) {
  39. return isRepositoryMode.value ? item.repositoryName : item.godownName
  40. }
  41. function getGodownListByName(name = '') {
  42. uni.$u.http.post('/app/appUser/searchGodown?name=' + name).then(res => {
  43. if (res.code == 200) {
  44. warehouses.value = res.data || []
  45. }
  46. })
  47. }
  48. function getRepositoryList() {
  49. loading.value = true
  50. uni.$u.http.get('/activation/bookActivationInfo/getRepositoryList').then(res => {
  51. if (res.code == 200) {
  52. warehouses.value = res.data || []
  53. } else {
  54. uni.$u.toast(res.msg || '获取仓库列表失败')
  55. }
  56. }).finally(() => {
  57. loading.value = false
  58. })
  59. }
  60. function loadList() {
  61. if (isRepositoryMode.value) {
  62. getRepositoryList()
  63. } else {
  64. getGodownListByName(searchText.value)
  65. }
  66. }
  67. function onSearch() {
  68. if (!isRepositoryMode.value) {
  69. getGodownListByName(searchText.value)
  70. }
  71. }
  72. function selectWarehouse(item) {
  73. if (isRepositoryMode.value) {
  74. uni.$emit('updateRepository', item)
  75. uni.setStorageSync(STORAGE_KEY, {
  76. id: item.id,
  77. repositoryName: item.repositoryName
  78. })
  79. } else {
  80. uni.$emit('updateWarehouse', item)
  81. }
  82. uni.navigateBack()
  83. }
  84. onLoad((options) => {
  85. if (options.type === 'repository') {
  86. pageType.value = 'repository'
  87. }
  88. if (options.repositoryId) {
  89. selectedId.value = options.repositoryId
  90. } else if (options.godownId) {
  91. selectedId.value = options.godownId
  92. }
  93. loadList()
  94. })
  95. </script>
  96. <style scoped>
  97. .search-area {
  98. padding: 12px;
  99. background-color: #fff;
  100. }
  101. .warehouse-list {
  102. margin-top: 12px;
  103. }
  104. .warehouse-item {
  105. background-color: #fff;
  106. padding: 15px;
  107. margin-bottom: 1px;
  108. font-size: 16px;
  109. }
  110. .warehouse-item-active {
  111. background-color: #e1f3ff;
  112. color: #2979ff;
  113. }
  114. .warehouse-item-hover {
  115. background-color: #f5f5f5;
  116. }
  117. .empty-tip {
  118. text-align: center;
  119. padding: 40px 0;
  120. color: #999;
  121. font-size: 14px;
  122. }
  123. </style>