Skip to content

Commit 0bc7dec

Browse files
committed
fix: 定位搜索地区体验优化
1 parent 5fe1953 commit 0bc7dec

File tree

2 files changed

+25
-21
lines changed

2 files changed

+25
-21
lines changed

resources/spa/src/page/Location.vue

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@
55
leave-active-class="animated slideOutRight"
66
>
77
<div class="m-box-model m-pos-f p-location">
8-
<SearchBar
9-
v-model="keyword"
10-
:back="goBack"
11-
/>
8+
<SearchBar v-model="keyword" :back="goBack" />
129

1310
<main>
1411
<div v-if="showHot">
@@ -44,10 +41,7 @@
4441
</ul>
4542
</div>
4643
</div>
47-
<div
48-
v-else
49-
class="m-box-model"
50-
>
44+
<div v-else class="m-box-model">
5145
<div
5246
v-for="(city, index) in cities"
5347
:key="`search-${city}-${index}`"
@@ -63,22 +57,16 @@
6357
</template>
6458

6559
<script>
66-
import SearchBar from '@/components/common/SearchBar.vue'
67-
import _ from 'lodash'
60+
import { parseSearchTree } from '@/util/location'
6861
import * as api from '@/api/bootstrappers.js'
62+
import SearchBar from '@/components/common/SearchBar.vue'
6963
7064
export default {
7165
name: 'Location',
7266
components: { SearchBar },
7367
props: {
74-
show: {
75-
type: Boolean,
76-
default: true,
77-
},
78-
isComponent: {
79-
type: Boolean,
80-
default: false,
81-
},
68+
show: { type: Boolean, default: true },
69+
isComponent: { type: Boolean, default: false },
8270
},
8371
data () {
8472
return {
@@ -142,6 +130,7 @@ export default {
142130
},
143131
methods: {
144132
goBack () {
133+
this.keyword = ''
145134
this.isComponent
146135
? this.$emit('close', this.currentPos)
147136
: this.$router.go(-1)
@@ -165,12 +154,12 @@ export default {
165154
})
166155
: []
167156
},
168-
searchCityByName: _.debounce(function () {
157+
searchCityByName () {
169158
api.searchCityByName(this.keyword).then(({ data = [] }) => {
170159
this.originCities = data
171160
this.cities = this.formatCities(data)
172161
})
173-
}, 450),
162+
},
174163
getCurrentPosition () {
175164
this.loading = true
176165
this.hotPos = null
@@ -204,7 +193,7 @@ export default {
204193
const city = this.cities[index].split('').pop()
205194
api.getGeo(city.replace(/[\s\uFEFF\xA0]+/g, '')).then(data => {
206195
this.loading = false
207-
data.label = this.originCities[index].tree.name
196+
data.label = parseSearchTree(this.originCities[index].tree)
208197
this.currentPos = data
209198
this.$nextTick(this.goBack)
210199
})

resources/spa/src/util/location.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,21 @@ function switchError (error) {
2121
return message
2222
}
2323

24+
/**
25+
* 将搜索结果的树结构展平
26+
* @param {Object} tree 地区搜索树
27+
* @param {number} level 展平级别 如 level = 2 搜索成都市时返回 “四川省 成都市”
28+
*/
29+
export const parseSearchTree = (tree = {}, level = 2) => {
30+
let result = tree.name
31+
let p = tree.parent
32+
while (p) {
33+
result = `${p.name} ${result}`
34+
p = p.parent
35+
}
36+
return result.split(' ').slice(0, level).join(' ')
37+
}
38+
2439
export default {
2540
getCurrentPosition () {
2641
return new Promise((resolve, reject) => {

0 commit comments

Comments
 (0)