search-address.vue
4.8 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<template>
<Page name="search-address">
<template #header>
<view style="display: flex; align-items: center">
<view style="flex: 1"><search-bar v-model="searchForm.query" placeholder="请输入需要搜索的地址关键词" auto @confirm="onSearch" /></view>
<view @click="selectLocation" style="width: 50px; padding-left: 10px; display: flex; align-items: center">
<span> <u-icon name="map-fill"></u-icon></span>地图
</view>
</view>
</template>
<template #content>
<template v-if="list.length === 0 && hisList.length > 0">
<view class="search-history">历史地址</view>
<template v-for="(item, index) in hisList">
<zui-cell :key="index" :title="item.address" :label="`${item.cityName}${item.areaName}`" @click="onSelect(item)"></zui-cell>
</template>
</template>
<List ref="list" v-model="list" :api="searchAPI">
<template v-for="(item, index) in list">
<zui-cell :key="index" :title="item.name" :label="`${item.cityName}${item.areaName}${item.address}`" @click="onSelect(item, true)"></zui-cell>
</template>
<template #empty>
<view v-if="hisList.length > 0"></view>
<u-empty v-else icon="https://download.shjiuze.cn/assets/image/empty/list.png" text="暂无数据"></u-empty>
</template>
</List>
</template>
</Page>
</template>
<script>
import { urlParam } from '@/utils/param';
import { formatLngLat } from '@/utils/format-value';
export default {
props: {
getHisFun: Function,
},
data() {
return {
searchForm: {
query: '',
},
list: [],
hisList: [],
lng: '',
lat: '',
};
},
onLoad(options) {
this.lng = options.lng || '';
this.lat = options.lat || '';
this.queryHis(options.type);
},
methods: {
queryHis(type) {
if (type === 'getStartLatestAddress') {
uni.$u.api.freightOrder.getStartLatestAddress({}).then(res => {
this.hisList = res.result || [];
});
}
if (type === 'getEndLatestAddress') {
uni.$u.api.freightOrder.getEndLatestAddress({}).then(res => {
this.hisList = res.result || [];
});
}
},
searchAPI() {
if (!this.searchForm.query) {
return new Promise(resolve => {
resolve({ result: [], totalCount: 0 });
});
}
return new Promise((resolve, reject) => {
let params = {
key: '1b987a62e5685a58cda5933e1bc66044',
keywords: this.searchForm.query,
};
uni.request({
headers: {
'Content-Type': 'application/json;charset=UTF-8',
},
url: encodeURI(`https://restapi.amap.com/v5/place/text${urlParam(params)}`),
method: 'GET',
dataType: 'json',
success: response => {
const data = response.data ?? {};
const pois = data.pois ?? [];
let result = pois.map(item => {
const lnglatArr = `${item.location || ''}`.split(',');
return {
name: item.name,
provinceCode: item.pcode,
provinceName: item.pname,
areaCode: item.adcode,
areaName: item.adname,
cityCode: `${item.adcode}`.substring(0, 4) + '00',
cityName: item.cityname,
address: item.adname === item.address && item.name ? item.name : item.address || item.name,
lng: lnglatArr[0],
lat: lnglatArr[1],
};
});
resolve({ result: result, totalCount: result.length });
},
fail: () => {
uni.showToast({ title: '没有查询到结果', icon: 'none' });
resolve({ result: [], totalCount: 0 });
},
});
});
},
onSelect(item, append = false) {
if (append) {
item = {
...item,
address: (item.provinceName || '') + (item.cityName || '') + (item.areaName || '') + item.address,
};
}
uni.$emit('select-detail', item);
uni.navigateBack();
},
// 地图选点
selectLocation() {
wx.chooseLocation({
latitude: this.lat ? Number(this.lat) : undefined,
longitude: this.lng ? Number(this.lng) : undefined,
})
.then(res => {
return formatLngLat({ lat: res.latitude, lng: res.longitude });
})
.then(location => {
this.onSelect(location, true);
});
},
},
};
</script>
<style lang="scss">
.page-search-address {
&__header {
padding: 16rpx 32rpx !important;
}
.search-history {
padding: 0 32rpx 32rpx 32rpx;
font-weight: 500;
font-size: 32rpx;
}
.zui-cell {
padding: 12rpx 32rpx;
}
.zui-cell__label {
padding-top: 8rpx;
}
}
</style>