宜呗小程序--微信小程序
Lzk
2025-08-05 b7bcaf556aa2424e808b6e6426ab22df9cfe11c1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<template>
  <view class="container">
    <uv-input
        style="border: none;padding: 0;width: 100%;flex: 1"
        v-model="addressKeyword"
        placeholder="支持地址联想"
        @input="handleInput"
        @focus="suggestionsVisible = true"
    />
    <view v-if="suggestionsVisible && suggestions.length > 0" class="suggestions-list">
      <view
          v-for="(item, index) in suggestions"
          :key="index"
          class="suggestion-item"
          @click="selectAddress(item)"
      >
        {{ item.name }}
      </view>
    </view>
 
  </view>
</template>
<script>
import debounce from '@/uni_modules/uv-ui-tools/libs/function/debounce.js';
import {searchAddress} from "@/api/modules/user";
 
export default {
  data() {
    return {
      addressKeyword: '', // 输入框内容
      suggestions: [], // 地址联想列表
      suggestionsVisible: false, // 是否显示联想列表
    };
  },
  methods: {
    handleInput(){
      debounce(()=>{
        searchAddress({'city':'海口市',query:this.addressKeyword}).then(res=>{
          this.suggestions = res;
        })
 
      })
 
    },
 
    // 选择地址
    selectAddress(item) {
      this.addressKeyword = item.name+' '+item.address;
      console.log(addressKeyword);
      this.suggestionsVisible = false;
      uni.showToast({
        title: `已选择:${item.name}`,
        icon: 'none',
      });
      // 触发事件或更新其他相关数据
      this.$emit('select', item);
    },
  },
  watch: {
    addressKeyword(newVal) {
      if (!newVal) this.suggestionsVisible = false;
    },
  },
};
</script>
<style scoped>
.container {
  position: relative;
}
 
.suggestions-list {
  position: absolute;
  background: white;
  z-index: 999999999999;
  top: 44px;
  border: 1px solid #eee;
  border-radius: 4px;
  max-height: 160px;
  overflow-y: auto;
}
 
.suggestion-item {
  padding: 10px;
  border-bottom: 1px solid #eee;
  font-size: 14px;
  position: relative;
  z-index: 9999999999;
}
 
.suggestion-item:last-child {
  border-bottom: none;
}
 
 
</style>