search-address.vue
7.11 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<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, index)"></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, index)"></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';
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) {
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 => {
uni.request({
url: 'https://restapi.amap.com/v3/geocode/regeo', //逆地理编码接口地址。
data: {
key: '1b987a62e5685a58cda5933e1bc66044',
location: res.longitude + ',' + res.latitude,
},
success: response => {
console.info(response);
const regeocode = response?.data?.regeocode || {};
const addressComponent = regeocode?.addressComponent || {};
const addressInfo = this.getAddress(response?.data);
let location = {
lng: res.longitude || '',
lat: res.latitude || '',
provinceCode: addressComponent.pcode,
provinceName: addressComponent.pname,
areaCode: addressComponent.adcode,
areaName: addressComponent.district,
address: addressInfo.address,
};
this.onSelect(location);
},
});
// let location = {
// lng: res.longitude || '',
// lat: res.latitude || '',
// provinceCode: '',
// provinceName: '',
// cityCode: '',
// cityName: '',
// areaCode: '',
// areaName: '',
// address: res.address || '',
// };
// this.onSelect(location);
});
// uni.$once('webview-select', location => {
// this.onSelect(location);
// });
// const url = 'https://zeyi-tms-product.oss-cn-hangzhou.aliyuncs.com/webview/location-wx-freight.html';
// const params = {
// lat: this.lat ? Number(this.lat) : '',
// lng: this.lng ? Number(this.lng) : '',
// };
// uni.navigateTo({ url: `/pages/common/webview?url=${url}&query=${JSON.stringify(params)}` });
},
// 格式化地址
getAddress(geo) {
const { regeocode = {} } = geo || {};
const { addressComponent = {} } = regeocode || {};
const { province, city, district, township, streetNumber } = addressComponent || {};
const { street, number } = streetNumber || {};
const formattedAddress = regeocode.formatted_address || regeocode.formattedAddress || '';
let amapAddress = formattedAddress.replace(province, '');
amapAddress = amapAddress.replace(city, '');
amapAddress = amapAddress.replace(district, '');
amapAddress = amapAddress.replace(township, '');
amapAddress = amapAddress.replace(street, '');
amapAddress = amapAddress.replace(number, '');
return {
formattedAddress,
fullAddress: `${province}${city}${district}${township}${street}${number}`,
address: `${township}${street}${number}${amapAddress}`,
amapAddress,
adcode: addressComponent.adcode,
township,
};
},
},
};
</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>