123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- <template>
- <view class="u-city-locate">
- <up-index-list :indexList="indexList">
- <template #header>
- <view class="u-current-city-wrap">
- <view class="u-current-city-title">定位城市</view>
- <view class="u-current-city-item" @tap="location">
- <view class="u-location-city">{{locationCity}}</view>
- </view>
- </view>
- </template>
- <template :key="index" v-for="(item, index) in cityList">
- <!-- #ifdef APP-NVUE -->
- <up-index-anchor :text="indexList[index]"></up-index-anchor>
- <!-- #endif -->
- <up-index-item>
- <!-- #ifndef APP-NVUE -->
- <up-index-anchor :text="indexList[index]"></up-index-anchor>
- <!-- #endif -->
- <view class="hot-city-list" v-if="index == 0">
- <view class="" v-for="(item1, index1) in item" @tap="selectedCity(item1)">
- <view class="hot-city-item">{{ item1[nameKey] }}</view>
- </view>
- </view>
- <view v-else class="item-list" v-for="(item1, index1) in item" :key="index1">
- <view class="list__item" @tap="selectedCity(item1)">
- <text class="list__item__city-name">{{item1[nameKey]}}</text>
- </view>
- <up-line></up-line>
- </view>
- </up-index-item>
- </template>
- <template #footer>
- <view class="u-safe-area-inset--bottom">
- <text class="list__footer"></text>
- </view>
- </template>
- </up-index-list>
- </view>
- </template>
- <script>
- export default{
- name: 'u-city-locate',
- props:{
- indexList: {
- type: Array,
- default: ['🔥']
- },
- cityList:{
- type: Array,
- default: () => {
- return [
- [{
- name: '北京',
- value: 'beijing'
- },
- {
- name: '上海',
- value: 'shanghai'
- },
- {
- name: '广州',
- value: 'guangzhou'
- },
- {
- name: '深圳',
- value: 'shenzhen'
- },
- {
- name: '杭州',
- value: 'hangzhou'
- }]
- ]
- }
- },
- locationType: {
- type: String,
- default: 'wgs84'
- },
- currentCity: {
- type: String,
- default: ''
- },
- nameKey: {
- type: String,
- default: 'name'
- }
- },
- computed:{
- },
- watch:{
- currentCity(val) {
- this.locationCity = val;
- }
- },
- data(){
- return{
- locationCity: '定位中....'
- }
- },
- emits: ['location-success', 'select-city'],
- methods:{
- // 获取城市
- selectedCity(city){
- this.locationCity = city[this.nameKey];
- this.$emit('select-city', {
- locationCity: this.locationCity
- });
- },
- // 定位操作
- location(){
- let That = this;
- uni.getLocation({
- type: this.locationType,
- geocode:true,
- success(res){
- console.log(res);
- That.locationCity = res.address && res.address.city;
- That.$emit('location-success', {
- ...res,
- locationCity: That.locationCity
- });
- },
- fail(){
- That.locationCity = "定位失败,请点击重试"
- }
- });
- },
- },
- // 页面挂载后进行异步操作
- created(){
- },
- mounted(){
- this.location();
- }
- }
- </script>
- <style lang="scss">
- .list__item {
- padding: 8px 1px;
- }
- .u-current-city-title {
- color: grey;
- margin-bottom: 5px;
- }
- .u-current-city-item {
- height: 30px;
- }
- .hot-city-list {
- display: flex !important;
- flex-direction: row !important;
- padding: 12px 0;
- .hot-city-item {
- padding: 6px 12px;
- margin: 5px;
- border: 1px solid #ededed;
- }
- }
- </style>
|