陶杰
2024-12-18 99730c8890388fc2131c2b38146d966e7ef33d17
pages/user/address/address.vue
@@ -1,8 +1,8 @@
<template>
   <!-- 收货地址列表 -->
   <view class="p15 container-address">
      <!-- <view class="search-container m-t-12 flex">
         <view class="flex1 input">
      <view class="search-container m-t-12 flex">
         <view class="flex1 input" @click="selectWxAddress('input')">
            <u-input placeholder="小区名/店铺名/写字楼/街道名等" disabled v-model="search_flow">
               <template slot="suffix">
                  <uni-icons color="#20613D" type="search" size="24"></uni-icons>
@@ -12,19 +12,29 @@
      </view>
      <view class="m-t-12 ">
         <view class="desc-gray">当前位置</view>
         <view class="flex">
            <view class="flex1"></view>
            <view @click="selectWxAddress('input')">
               <image src="https://hmy-flower.oss-cn-shanghai.aliyuncs.com/a2/a248385d4fe748ca81ee058ea5bf8c47icon-address.jpg"></image>
               重新定位
         <view class="flex current-address">
            <view class="flex1">
               {{ addressDesc || (address&&address.id?address.address: '选择地址')  }}
            </view>
            <view class="flex" @click="changeCurrentAddress">
               <view>
                  <image style="width: 30rpx;height: 30rpx;margin-top: 6rpx;margin-right: 10rpx;" class="m-t-2"
                     src="https://hmy-flower.oss-cn-shanghai.aliyuncs.com/a2/a248385d4fe748ca81ee058ea5bf8c47icon-address.jpg">
                  </image>
               </view>
               <view>重新定位</view>
            </view>
         </view>
      </view>
       -->
      <view class="m-t-12 ">
         <view class="desc-gray">我的收货地址</view>
      </view>
      <no-data v-if="!list||list.length==0" style="width: 100%;"></no-data>
      <!-- 循环 -->
      <view v-for="(item,index) of list" :key="index" class="location-each">
      <view v-for="(item,index) of list" :key="index" class="location-each m-t-12">
         <view class="flex">
            <view class="container-info">
               <view class="flex">
@@ -67,14 +77,17 @@
            <view class="desc del t-green m-l-10" v-if="source==='select'" @click="selectAddress(item)">
               选择此地址
            </view>
            <view class="desc del t-green m-l-10" v-if="source==='home'" @click="selectAddress(item)">
               按此地址定位
            </view>
         </view>
      </view>
      <view style="height: 92rpx;width: 690rpx;">
         &nbsp;
      </view>
      <view class="button-green button-fixed-bottom" style="width: 690rpx;line-height: 92rpx;height: 92rpx;"
         @click="addAddress">添加收货地址
      <view class="button-green button-fixed-bottom" v-if="currentInfo.id"
         style="width: 690rpx;line-height: 92rpx;height: 92rpx;" @click="addAddress">添加收货地址
      </view>
      <uni-popup ref="popup_form" type="top" :mask-click="false">
@@ -174,7 +187,12 @@
         // this.list = [{}, {}]
         this.source = options.source || ''
         this.listApi = '/api/address/list'
         await this.getList()
         if (!this.currentInfo.id) {
         } else {
            await this.getList()
         }
      },
      onShow() {
@@ -211,6 +229,12 @@
            address: state => {
               return state.defaultaddress || {}
            },
            addressDesc: state => {
               return state.addressDesc || ''
            },
            // search_flow() {
            //    return this.addressDesc || ''
            // }
         }),
      },
@@ -230,25 +254,36 @@
            },
            regionDataPlus: [],
            list: [],
            search_flow: '',
         };
      },
      methods: {
         selectWxAddress(source) {
            let that = this
            if (source === 'form') {
            {
               //表单选择了地址
               wx.chooseLocation({
                  success: location => {
                     console.log('success loc', location)
                     console.log('success loc', location,source)
                     // address: "江苏省苏州市吴中区太湖东路288号"
                     // errMsg: "chooseLocation:ok"
                     // latitude: 31.26249
                     // longitude: 120.63212
                     // name: "苏州市吴中区人民政府"
                     that.submitForm.address = (location.address || '') + (location.name || '')
                     that.submitForm.latitude = location.latitude || 0
                     that.submitForm.longitude = location.longitude || 0
                     if (source === 'form') {
                        // (location.address || '') +
                        that.submitForm.address = (location.name || location.address || '')
                        that.submitForm.latitude = location.latitude || 0
                        that.submitForm.longitude = location.longitude || 0
                     } else if (source === 'input') {
                        // (location.address || '') +
                        // that.search_flow =   (location.name || '')
                        that.$store.commit('setAddressDesc', (location.name || location.address || ''))
                        that.$forceUpdate()
                        that.backpage()
                     }
                     // if (!that.submitForm.province && location.provinceName) {
                     //    that.submitForm.province = location.provinceName
                     //    if (!that.submitForm.city && location.cityName) {
@@ -320,6 +355,54 @@
            this.submitForm['isDefault'] = false
            this.openAddressForm()
         },
         changeCurrentAddress() {
            // if(!this.addressDesc){
            // }
            let that = this
            this.$message.showLoading()
            uni.getLocation({
               type: 'gcj02',
               geocode: true,
               success: async (res) => {
                  //  this.location = {
                  // latitude: res.latitude,
                  // longitude: res.longitude,
                  // speed: res.speed,
                  // accuracy: res.accuracy,
                  // address: res.address,
                  //  };
                  const {
                     code,data
                  } = await that.$http.request('get', '/api/pub/customer/home/address/parse', {
                     data: {},
                     params: {
                        // https://apis.map.qq.com/ws/geocoder/v1/?location=39.984154,116.307490&key=[你的key]&get_poi=1
                        location:`${res.latitude},${res.longitude}`
                     }
                  })
                  console.log('定位成功:', data);
                  if(data){
                     var address = data.result&&data.result.address || ''
                     that.$store.commit('setAddressDesc', (address || ''))
                     that.$forceUpdate()
                     that.backpage()
                  }
               },
               fail: (err) => {
                  that.error = err;
                  console.error('定位失败:', err);
               },
               complete() {
                  that.$message.hideLoading()
               }
            });
         },
         PickArea(item, e) {
            console.log('PickArea', item, e)
            if (e.detail.value) {
@@ -356,13 +439,16 @@
            }
         },
         async init_area() {
            if (this.regionDataPlus && this.regionDataPlus.length > 0) {
            // debugger;
            // if (this.regionDataPlus && this.regionDataPlus.length > 0) {
            } else {
               var a = this.$storage.getItem('cache_area')
            // } else {
               // var a = this.$storage.getItem('cache_area')
               var a =false
               if (a) {
                  this.regionDataPlus = JSON.parse(a) || []
               } else {
                  await this.$http.request('get', '/api/pub/china/area/json/refresh')
                  const res = await this.$http.request('get', '/api/pub/china/area/json')
                  // console.log('area', JSON.parse(res.data))
                  this.regionDataPlus = res.data && JSON.parse(res.data.replaceAll('code', 'value').replaceAll(
@@ -370,7 +456,7 @@
                     'text')) || []
                  this.$storage.setItem('cache_area', JSON.stringify(this.regionDataPlus))
               }
            }
            // }
         },
         chooseLocation() {
@@ -449,6 +535,7 @@
                     this.$store.commit('setDefaultAddress', {
                        ...item
                     })
                  }
               }, err => {
@@ -463,11 +550,20 @@
            })
         },
         async selectAddress(item) {
            await this.$message.confirm('确定选择此地址作为收货地址吗')
            //携带,然后返回
            this.$store.commit('setDefaultAddress', {
               ...item
            })
            if(this.source==='home'){
               this.$store.commit('setAddressDesc', '')
               this.$store.commit('setDefaultAddress', {
                  ...item
               })
            }else{
               await this.$message.confirm('确定选择此地址作为收货地址吗')
               this.$store.commit('setDefaultAddress', {
                  ...item
               })
            }
            this.backpage()
         },
         async deleteAddress(item) {
@@ -577,6 +673,14 @@
<style lang="scss" scoped>
   .container-address {
      .current-address {
         padding: 20rpx;
         background-color: #FFFFFF;
         border-radius: 16rpx;
         margin-top: 16rpx;
         margin-bottom: 20rpx;
      }
      .popup-address-form {
         background: #FFFFFF;
         border-radius: 16rpx;