index.vue 15.5 KB
Newer Older
1 2
<template>
  <div class="uni-system-choose-location">
3 4 5 6 7
    <v-uni-map
      :latitude="latitude"
      :longitude="longitude"
      class="map"
      show-location
Q
qiang 已提交
8 9 10
      :libraries="['places']"
      @updated="getList"
      @regionchange="onRegionChange"
11
    >
Q
qiang 已提交
12 13 14 15
      <div
        class="map-location"
        :style="locationStyle"
      />
16 17
      <div
        class="map-move"
Q
qiang 已提交
18
        @click="moveToLocation"
19 20 21 22 23
      >
        <i>&#xec32;</i>
      </div>
    </v-uni-map>
    <div class="nav">
24 25
      <div
        class="nav-btn back"
Q
qiang 已提交
26
        @click="back"
27 28 29 30 31 32
      >
        <i class="uni-btn-icon">&#xe650;</i>
      </div>
      <div
        class="nav-btn confirm"
        :class="{ disable: !selected }"
Q
qiang 已提交
33
        @click="choose"
34 35 36 37 38 39 40 41 42
      >
        <i class="uni-btn-icon">&#xe651;</i>
      </div>
    </div>
    <div class="menu">
      <div class="search">
        <v-uni-input
          v-model="keyword"
          class="search-input"
Q
qiang 已提交
43
          :placeholder="$$t('uni.chooseLocation.search')"
44
          @focus="searching = true"
Q
qiang 已提交
45
          @input="input"
46 47 48 49 50 51 52 53 54
        />
        <div
          v-if="searching"
          class="search-btn"
          @click="
            searching = false;
            keyword = '';
          "
        >
Q
qiang 已提交
55
          {{ $$t("uni.chooseLocation.cancel") }}
56 57
        </div>
      </div>
58 59 60
      <v-uni-scroll-view
        scroll-y
        class="list"
Q
qiang 已提交
61
        @scrolltolower="loadMore"
62
      >
63 64 65
        <div
          v-if="loading"
          class="list-loading"
66 67 68 69 70 71 72 73
        >
          <i class="uni-loading" />
        </div>
        <div
          v-for="(item, index) in list"
          :key="index"
          class="list-item"
          :class="{ selected: selectedIndex === index }"
Q
qiang 已提交
74 75 76 77 78
          @click="
            selectedIndex = index;
            latitude = item.latitude;
            longitude = item.longitude;
          "
79 80 81 82 83
        >
          <div class="list-item-title">
            {{ item.name }}
          </div>
          <div class="list-item-detail">
84
            {{ item.distance | distance }}{{ item.address }}
85 86 87
          </div>
        </div>
      </v-uni-scroll-view>
88 89 90 91
    </div>
  </div>
</template>
<script>
92 93 94 95 96
import {
  debounce
} from 'uni-shared'
import {
  getJSONP
Q
qiang 已提交
97 98 99 100 101 102 103 104 105
} from '../../../helpers/get-jsonp'
import {
  i18nMixin
} from 'uni-core/helpers/i18n'
import {
  ICON_PATH_TARGET,
  MapType,
  getMapInfo
} from '../../../helpers/location'
106 107

export default {
108
  name: 'SystemChooseLocation',
109 110 111 112 113
  filters: {
    distance (distance) {
      if (distance > 100) {
        return `${distance > 1000 ? (distance / 1000).toFixed(1) + 'k' : distance.toFixed(0)}m | `
      } else if (distance > 0) {
Q
qiang 已提交
114
        return '<100m | '
115 116 117 118 119
      } else {
        return ''
      }
    }
  },
Q
qiang 已提交
120
  mixins: [i18nMixin],
121
  data () {
Q
qiang 已提交
122 123 124 125
    const {
      latitude,
      longitude
    } = this.$route.query
126
    return {
Q
qiang 已提交
127 128 129
      latitude: latitude,
      longitude: longitude,
      pageSize: 20,
130
      pageIndex: 1,
Q
qiang 已提交
131 132
      hasNextPage: true,
      nextPage: null,
133 134 135 136
      selectedIndex: -1,
      list: [],
      keyword: '',
      searching: false,
137
      loading: true,
Q
qiang 已提交
138 139
      adcode: '',
      locationStyle: `background-image: url("${ICON_PATH_TARGET}")`
140 141
    }
  },
142 143 144
  computed: {
    selected () {
      return this.list[this.selectedIndex]
145 146 147
    },
    boundary () {
      return this.adcode ? `region(${this.adcode},1,${this.latitude},${this.longitude})` : `nearby(${this.latitude},${this.longitude},5000)`
148
    }
149 150
  },
  created () {
Q
qiang 已提交
151 152 153 154 155
    if (!this.latitude || !this.longitude) {
      this.moveToLocation()
    }
    this.search = debounce(() => {
      this.reset()
156
      if (this.keyword) {
Q
qiang 已提交
157
        this.getList()
158 159 160
      }
    }, 1000)
    this.$watch('searching', val => {
Q
qiang 已提交
161
      this.reset()
162
      if (!val) {
Q
qiang 已提交
163
        this.getList()
164 165
      }
    })
166
  },
167
  methods: {
Q
qiang 已提交
168
    choose () {
169 170
      if (this.selected) {
        UniViewJSBridge.publishHandler('onChooseLocation', Object.assign({}, this.selected))
171 172 173
        getApp().$router.back()
      }
    },
Q
qiang 已提交
174
    back () {
175 176
      UniViewJSBridge.publishHandler('onChooseLocation', null)
      getApp().$router.back()
177
    },
Q
qiang 已提交
178
    moveToLocation () {
179 180
      uni.getLocation({
        type: 'gcj02',
Q
qiang 已提交
181
        success: this.move.bind(this),
182
        fail: () => {
Q
qiang 已提交
183 184 185 186
          // this.move({
          //   latitude: 39.90960456049752,
          //   longitude: 116.3972282409668
          // })
187 188 189
        }
      })
    },
Q
qiang 已提交
190
    onRegionChange ({ detail: { centerLocation } }) {
191 192
      if (centerLocation) {
        // TODO 图钉 icon 动画
Q
qiang 已提交
193
        this.move(centerLocation)
194 195
      }
    },
Q
qiang 已提交
196
    pushData (array) {
197 198 199 200
      array.forEach(item => {
        this.list.push({
          name: item.title,
          address: item.address,
201
          distance: item._distance || item.distance,
202 203 204 205 206
          latitude: item.location.lat,
          longitude: item.location.lng
        })
      })
    },
Q
qiang 已提交
207
    getList () {
208
      this.loading = true
Q
qiang 已提交
209 210 211 212 213
      const mapInfo = getMapInfo()
      if (mapInfo.type === MapType.GOOGLE) {
        if (this.pageIndex > 1 && this.nextPage) {
          this.nextPage()
          return
214
        }
Q
qiang 已提交
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
        const service = new window.google.maps.places.PlacesService(document.createElement('div'))
        service[this.searching ? 'textSearch' : 'nearbySearch']({
          location: {
            lat: this.latitude,
            lng: this.longitude
          },
          query: this.keyword,
          radius: 5000
        }, (results, state, page) => {
          this.loading = false
          if (results && results.length) {
            results.forEach((item) => {
              this.list.push({
                name: item.name || '',
                address: item.vicinity || item.formatted_address || '',
                distance: 0,
                latitude: item.geometry.location.lat(),
                longitude: item.geometry.location.lng()
              })
            })
          }
          if (page) {
            if (!page.hasNextPage) {
              this.hasNextPage = false
            } else {
              this.nextPage = () => {
                page.nextPage()
              }
            }
          }
        })
      } else if (mapInfo.type === MapType.QQ) {
        const url = this.searching ? `https://apis.map.qq.com/ws/place/v1/search?output=jsonp&key=${mapInfo.key}&boundary=${this.boundary}&keyword=${this.keyword}&page_size=${this.pageSize}&page_index=${this.pageIndex}` : `https://apis.map.qq.com/ws/geocoder/v1/?output=jsonp&key=${mapInfo.key}&location=${this.latitude},${this.longitude}&get_poi=1&poi_options=page_size=${this.pageSize};page_index=${this.pageIndex}`
        // TODO 列表加载失败提示
        getJSONP(url, {
          callback: 'callback'
        }, (res) => {
          this.loading = false
          if (this.searching && 'data' in res && res.data.length) {
            this.pushData(res.data)
          } else if ('result' in res) {
            const result = res.result
            this.adcode = result.ad_info ? result.ad_info.adcode : ''
            if (result.pois) {
              this.pushData(result.pois)
            }
            if (this.list.length === this.pageSize * this.pageIndex) {
              this.hasNextPage = false
            }
          }
        }, () => {
          this.loading = false
        })
268 269 270 271
      } else if (mapInfo.type === MapType.AMAP) {
        const self = this

        window.AMap.plugin('AMap.PlaceSearch', function () {
272
          const placeSearch = new window.AMap.PlaceSearch({
273 274 275
            city: '全国',
            pageSize: 10,
            pageIndex: self.pageIndex
276 277 278 279 280 281 282 283 284 285 286 287
          })
          const keyword = self.searching ? self.keyword : ''
          const radius = self.searching ? 50000 : 5000
          placeSearch.searchNearBy(keyword, [self.longitude, self.latitude], radius, function (status, result) {
            if (status === 'error') {
              console.error(result)
            } else if (status === 'no_data') {
              self.hasNextPage = false
            } else {
              self.pushData(result.poiList.pois)
            }
          })
288 289
          self.loading = false
        })
Q
qiang 已提交
290
      }
291
    },
Q
qiang 已提交
292 293
    loadMore () {
      if (!this.loading && this.hasNextPage) {
294
        this.pageIndex++
Q
qiang 已提交
295
        this.getList()
296 297
      }
    },
Q
qiang 已提交
298
    reset () {
299 300
      this.selectedIndex = -1
      this.pageIndex = 1
Q
qiang 已提交
301 302
      this.hasNextPage = true
      this.nextPage = null
303 304
      this.list = []
    },
Q
qiang 已提交
305
    move ({ latitude, longitude }) {
Q
qiang 已提交
306 307 308
      this.latitude = latitude
      this.longitude = longitude
      if (!this.searching) {
Q
qiang 已提交
309 310
        this.reset()
        this.getList()
Q
qiang 已提交
311 312
      }
    },
Q
qiang 已提交
313 314
    input () {
      this.search()
315 316 317 318
    }
  }
}
</script>
319
<style>
320 321 322 323 324 325 326 327 328 329
@font-face {
  font-weight: normal;
  font-style: normal;
  font-family: "unimapbtn";
  src: url("data:application/octet-stream;base64,AAEAAAAKAIAAAwAgT1MvMkLLXiQAAACsAAAAYGNtYXAADe3YAAABDAAAAUJnbHlmzCeOEgAAAlAAAAD4aGVhZBcH/NkAAANIAAAANmhoZWEHvgOiAAADgAAAACRobXR4BAAAAAAAA6QAAAAGbG9jYQB8AAAAAAOsAAAABm1heHABDwBlAAADtAAAACBuYW1laz5x0AAAA9QAAALZcG9zdAEQAAIAAAawAAAAJwAEBAABkAAFAAgCiQLMAAAAjwKJAswAAAHrADIBCAAAAgAFAwAAAAAAAAAAAAAQAAAAAAAAAAAAAABQZkVkAEDsMuwyA4D/gABcA4AAgAAAAAEAAAAAAAAAAAAAACAAAAAAAAMAAAADAAAAHAABAAAAAAA8AAMAAQAAABwABAAgAAAABAAEAAEAAOwy//8AAOwy//8TzwABAAAAAAAAAQYAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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m9udGVsbG8uY29tAAoAQwByAGUAYQB0AGUAZAAgAGIAeQAgAGkAYwBvAG4AZgBvAG4AdAAKAHUAbgBpAGMAaABvAG8AcwBlAGwAbwBjAGEAdABpAG8AbgBSAGUAZwB1AGwAYQByAHUAbgBpAGMAaABvAG8AcwBlAGwAbwBjAGEAdABpAG8AbgB1AG4AaQBjAGgAbwBvAHMAZQBsAG8AYwBhAHQAaQBvAG4AVgBlAHIAcwBpAG8AbgAgADEALgAwAHUAbgBpAGMAaABvAG8AcwBlAGwAbwBjAGEAdABpAG8AbgBHAGUAbgBlAHIAYQB0AGUAZAAgAGIAeQAgAHMAdgBnADIAdAB0AGYAIABmAHIAbwBtACAARgBvAG4AdABlAGwAbABvACAAcAByAG8AagBlAGMAdAAuAGgAdAB0AHAAOgAvAC8AZgBvAG4AdABlAGwAbABvAC4AYwBvAG0AAAAAAgAAAAAAAAAKAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgACAAABAgAA")
    format("truetype");
}

.uni-system-choose-location {
  display: block;
330
  position: absolute;
331 332 333 334 335 336 337
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background: #f8f8f8;
}

338
.uni-system-choose-location .map {
339
  position: absolute;
340
  top: 0;
341 342
  left: 0;
  width: 100%;
343
  height: 300px;
344 345
}

346
.uni-system-choose-location .map-location {
347 348 349 350 351 352 353 354 355 356
  position: absolute;
  left: 50%;
  bottom: 50%;
  width: 32px;
  height: 52px;
  margin-left: -16px;
  cursor: pointer;
  background-size: 100%;
}

357
.uni-system-choose-location .map-move {
358 359 360 361 362 363 364 365 366 367 368 369 370 371
  position: absolute;
  bottom: 50px;
  right: 10px;
  width: 40px;
  height: 40px;
  box-sizing: border-box;
  line-height: 40px;
  background-color: white;
  border-radius: 50%;
  pointer-events: auto;
  cursor: pointer;
  box-shadow: 0px 0 5px 1px rgba(0, 0, 0, 0.3);
}

372
.uni-system-choose-location .map-move > i {
373 374 375 376 377 378 379 380 381 382 383
  display: block;
  width: 100%;
  height: 100%;
  font: normal normal normal 14px/1 "unimapbtn";
  line-height: inherit;
  text-align: center;
  font-size: 24px;
  text-rendering: auto;
  -webkit-font-smoothing: antialiased;
}

384
.uni-system-choose-location .nav {
385 386 387 388 389 390 391 392 393 394 395 396 397
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 44px;
  background-color: transparent;
  background-image: linear-gradient(
    to bottom,
    rgba(0, 0, 0, 0.3),
    rgba(0, 0, 0, 0)
  );
}

398
.uni-system-choose-location .nav-btn {
399 400 401 402 403 404 405 406 407 408 409 410 411 412
  position: absolute;
  box-sizing: border-box;
  top: 0;
  left: 0;
  width: 60px;
  height: 44px;
  padding: 6px;
  line-height: 32px;
  font-size: 26px;
  color: white;
  text-align: center;
  cursor: pointer;
}

413
.uni-system-choose-location .nav-btn.confirm {
414 415 416 417
  left: auto;
  right: 0;
}

418
.uni-system-choose-location .nav-btn.disable {
419 420 421
  opacity: 0.4;
}

422
.uni-system-choose-location .nav-btn > .uni-btn-icon {
423 424 425 426 427 428 429
  display: block;
  width: 100%;
  height: 100%;
  line-height: inherit;
  border-radius: 2px;
}

430
.uni-system-choose-location .nav-btn.confirm > .uni-btn-icon {
431 432 433
  background-color: #007aff;
}

434
.uni-system-choose-location .menu {
435 436 437 438 439 440 441 442
  position: absolute;
  top: 300px;
  left: 0;
  width: 100%;
  bottom: 0;
  background-color: white;
}

443
.uni-system-choose-location .search {
444 445 446 447 448 449 450 451 452
  display: flex;
  flex-direction: row;
  height: 50px;
  padding: 8px;
  line-height: 34px;
  box-sizing: border-box;
  background-color: white;
}

453
.uni-system-choose-location .search-input {
454 455 456 457 458 459 460
  flex: 1;
  height: 100%;
  border-radius: 5px;
  padding: 0 5px;
  background: #ebebeb;
}

461
.uni-system-choose-location .search-btn {
Q
qiang 已提交
462
  margin-left: 5px;
463 464 465 466 467
  color: #007aff;
  font-size: 17px;
  text-align: center;
}

468
.uni-system-choose-location .list {
469 470 471 472 473 474 475 476 477
  position: absolute;
  top: 50px;
  left: 0;
  width: 100%;
  bottom: 0;
  padding-bottom: 10px;
  /* background-color: #f6f6f6; */
}

478
.uni-system-choose-location .list-loading {
479 480 481 482 483 484
  display: flex;
  height: 50px;
  justify-content: center;
  align-items: center;
}

485
.uni-system-choose-location .list-item {
486 487 488
  position: relative;
  padding: 10px;
  padding-right: 40px;
Q
qiang 已提交
489
  cursor: pointer;
490 491
}

492
.uni-system-choose-location .list-item.selected::before {
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
  position: absolute;
  top: 50%;
  right: 10px;
  width: 30px;
  height: 30px;
  margin-top: -15px;
  text-align: center;
  content: "\e651";
  font: normal normal normal 14px/1 "unibtn";
  font-size: 24px;
  line-height: 30px;
  color: #007aff;
  text-rendering: auto;
  -webkit-font-smoothing: antialiased;
}

509
.uni-system-choose-location .list-item:not(:last-child)::after {
510 511 512 513 514 515 516 517 518
  position: absolute;
  content: "";
  height: 1px;
  left: 10px;
  bottom: 0;
  width: 100%;
  background-color: #d3d3d3;
}

519
.uni-system-choose-location .list-item-title {
520 521 522 523 524 525
  font-size: 14px;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

526
.uni-system-choose-location .list-item-detail {
527 528 529 530 531 532 533 534
  font-size: 12px;
  color: #808080;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

@media screen and (min-width: 800px) {
535
  .uni-system-choose-location .map {
536 537 538
    top: 0;
    height: 100%;
  }
539
  .uni-system-choose-location .map-move {
540 541 542
    bottom: 10px;
    right: 320px;
  }
543
  .uni-system-choose-location .menu {
544 545 546 547 548 549 550 551 552
    top: 54px;
    left: auto;
    right: 10px;
    width: 300px;
    bottom: 10px;
    max-height: 600px;
    box-shadow: 0px 0 20px 5px rgba(0, 0, 0, 0.3);
  }
}
553
</style>