xuxueyang
2024-07-24 499a94ded67a4c1e763dc6686afe5a2e99faf407
pages/user/address/address.vue
@@ -28,21 +28,12 @@
         </view>
         <view class="v-line"></view>
         <view class="flex">
            <view class="flex" @click="changeDefaultAddress(item,index)">
               <!-- <label class="radio flex" > -->
               <!--       <radio value="r1" :checked="item.defaulted" disabled
                     style="transform:scale(0.6)" v-if="item.defaulted" />
                  <radio value="r1"
                     style="transform:scale(0.6)" :checked="item.defaulted" v-if="!item.defaulted"  @change="changeDefaultAddress(item,index)"/>
                      -->
               <!-- </label> -->
        <!--        @click="changeDefaultAddress(item,index)"-->
        <view class="flex">
               <view class="radio m-t-2 m-r-10" :class="[item.defaulted?'select':'']">
               </view>
               <span class="desc">{{item.defaulted?'默认地址:':'当前地址:'}}{{item.schoolAreaStr||''}}
                  {{item.blockStr||''}}
                  {{item.room||''}}</span>
          <span class="desc">{{ item.defaulted ? '默认地址:' : '当前地址:' }}{{ item.address || '' }}</span>
               <!-- 当前地址/默认地址勾选 -->
            </view>
            <view class="desc del t-red" @click="deleteAddress(item)">
@@ -65,7 +56,8 @@
            this.submitForm['area'] = ''
            
            openAddressForm()
         }">添加收货地址</view>
         }">添加收货地址
    </view>
      <uni-popup ref="popup_form" type="top" :mask-click="false">
         <view class="popup-address-form">
@@ -87,10 +79,20 @@
                  <view class="form-item-label require">
                     请选择收获地址
                  </view>
                  <view class="form-item-value">
                     <input v-model="submitForm.room" placeholder="请选择收获地址" disabled class="form-input"></input>
                     <!-- todo 点击定位 -->
            <view class="m-l-a m-r-0 flex " :class="[!dto['province']?'desc-gray':'']">
              <uni-data-picker :area="true" @change="(e)=>{PickArea(dto,e)}" placeholder=""
                               :localdata="regionDataPlus">
                {{ dto['province'] || '请选择' }}{{ dto['city'] && ('/' + dto['city']) || '' }}{{
                  dto['region'] && ('/' + dto['region']) || ''
                }}
              </uni-data-picker>
              <u-icon class="m-l-a" name="arrow-right"></u-icon>
                  </view>
            <!--            <view class="form-item-value" @click="chooseLocation">-->
            <!--              <input v-model="submitForm.room" placeholder="请选择收获地址" disabled class="form-input"></input>-->
            <!--              &lt;!&ndash;  点击定位 &ndash;&gt;-->
            <!--            </view>-->
               </view>
               <view class="form-item">
                  <view class="form-item-label require">
@@ -106,8 +108,8 @@
                     是否默认
                  </view>
                  <view class="form-item-value">
                     <radio value="r1" :checked="submitForm.defaulted"
                        @click="submitForm.defaulted=!submitForm.defaulted"
              <radio value="r1" :checked="submitForm.isDefault"
                     @click="submitForm.isDefault=!submitForm.isDefault"
                        style="transform:scale(0.6);margin-top: -8rpx;" />
                  </view>
               </view>
@@ -121,6 +123,10 @@
</template>
<script>
// import qqmapwx from '@/plugins/qqmap-wx-jssdk.min.js';
// const lockey = 'VUHBZ-2AMLP-B7AD7-VUQZ7-D4TW5-MFFVN'; //使用在腾讯位置服务申请的key
// const chooseLocation = requirePlugin('chooseLocation');
   export default {
      async onPullDownRefresh() {
         this.page.current = 0
@@ -130,7 +136,7 @@
      async onLoad(options) {
         // this.list = [{}, {}]
         this.source = options.source || ''
         this.listApi = '/api/address/customer/list'
    this.listApi = '/api/address/list'
         await this.getList()
      },
@@ -141,21 +147,102 @@
               name: '',
               tel: '',
               address: '',
               schoolArea: '',
               block: '',
               room: '',
               defaulted: false
        isDefault: false,
        province: '',
        city: '',
        region: '',
            },
      regionDataPlus: [],
         };
      },
      methods: {
    PickArea(item, e) {
      console.log('PickArea', item, e)
      if (e.detail.value) {
        this.dto.province = ''
        this.dto.city = ''
        this.dto.region = ''
        if (e.detail.value.length <= 3) {
          if (!!e.detail.value[0])
            this.dto.province = e.detail.value[0].value
          if (!!e.detail.value[1])
            this.dto.city = e.detail.value[1].value
          if (!!e.detail.value[2])
            this.dto.region = e.detail.value[2].value
        } else {
          //说明有重复的
          var plusnum = e.detail.value.length - 3
          if (!!e.detail.value[plusnum + 0])
            this.dto.province = e.detail.value[plusnum + 0].value
          if (!!e.detail.value[plusnum + 1])
            this.dto.city = e.detail.value[plusnum + 1].value
          if (!!e.detail.value[plusnum + 2])
            this.dto.region = e.detail.value[plusnum + 2].value
        }
        this.$forceUpdate()
      }
    },
    async init_area() {
      const res = await this.$http.request('get', '/api/pub/china/area/json')
      this.regionDataPlus = res.data && JSON.parse(res.data.replaceAll('code', 'value').replaceAll('name',
          'text')) || []
    },
    chooseLocation() {
      //前往接口挑选位置
      const qqmapsdk = new qqmapwx({
        // 使用你在腾讯地图应用生成的key
        key: lockey
      });
      uni.getLocation({
        type: 'wgs84',
        success(res) {
          //得到经纬度
          console.log(res);
          qqmapsdk.reverseGeocoder({
            location: {
              latitude: res.latitude,
              longitude: res.longitude
            },
            //成功后的回调
            success: (r) => {
              // console.log('地址信息', r.result.address_component);
              // result: {location: {lat: 31.26249, lng: 120.63212}, address: "江苏省苏州市吴中区太湖东路288号",…}
              // ad_info: {nation_code: "156", adcode: "320506", phone_area_code: "0512", city_code: "156320500",…}
              // address: "江苏省苏州市吴中区太湖东路288号"
              // address_component: {nation: "中国", province: "江苏省", city: "苏州市", district: "吴中区", street: "太湖东路",…}
              // address_reference: {,…}
              // formatted_addresses: {recommend: "长桥苏州市吴中区人民政府(太湖东路北)", rough: "长桥苏州市吴中区人民政府(太湖东路北)"}
              // location: {lat: 31.26249, lng: 120.63212}
              // status: 0
              tmp.formcodes['LOC_ONE'][tmp.tmp_picker_lock_index].longitude = r
                  .result.location.lng
              tmp.formcodes['LOC_ONE'][tmp.tmp_picker_lock_index].latitude = r
                  .result.location.lat
              tmp.formcodes['LOC_ONE'][tmp.tmp_picker_lock_index].loc_desc = r
                  .result.address
              tmp.formcodes['LOC_ONE'][tmp.tmp_picker_lock_index]._show = false
              tmp.$forceUpdate()
              tmp.tmp_picker_lock_index = -1
            },
            fail: function (res) {
              console.log(res);
              tmp.tmp_picker_lock_index = -1
            },
          });
        }
      });
    },
         async changeDefaultAddress(item, index) {
            if (item.defaulted) {
      if (item.isDefault) {
               return
            }
            this.$nextTick(() => {
               // item.defaulted = false
               // this.$set(item,'defaulted',false)
               this.$message.confirm('确定设置此地址为默认地址吗').then(async res => {
                  this.$message.showLoading()
                  var {
@@ -163,12 +250,12 @@
                  } = await this.$http.request('post', '/api/address/customer/default/' +
                     item.id, {})
                  this.$message.hideLoading()
                  if (code == 0) {
          if (code === 0) {
                     for (var dto of this.list) {
                        dto.defaulted = false
              dto.isDefault = false
                     }
                     this.$message.showToast('设置成功')
                     item.defaulted = true
            item.isDefault = true
                     this.$store.commit('setDefaultAddress', {
                        ...item
                     })
@@ -200,7 +287,7 @@
               code
            } = await this.$http.request('get', '/api/address/customer/delete/' + item.id, {})
            this.$message.hideLoading()
            if (code == 0) {
      if (code === 0) {
               this.$message.showToast('删除成功')
               setTimeout(() => {
                  this.page.current = 1
@@ -209,11 +296,12 @@
            }
         },
         async openAddressForm() {
      this.init_area()
            if (this.submitForm.id) {
               this.$message.showLoading()
               var {
                  data
               } = await this.$http.request('get', '/api/address/customer/get/' + this.submitForm.id, {})
        } = await this.$http.request('get', '/api/address/list/detail/' + this.submitForm.id, {})
               if (data) {
@@ -221,8 +309,6 @@
                     ...this.submitForm,
                     ...data
                  }
                  this.blockIndex = -1
                  this.schoolIndex = -1
               }
               this.$message.hideLoading()
            } else {
@@ -230,14 +316,11 @@
                  name: this.currentInfo && this.currentInfo.nickName || '',
                  tel: this.currentInfo && this.currentInfo.tel || '',
                  address: '',
                  schoolArea: '',
                  block: '',
                  room: '',
                  defaulted: false
          isDefault: false,
          province: '',
          city: '',
          region: '',
               }
               this.schoolIndex = -1
               this.blockIndex = -1
               this.blocks = []
            }
            this.$refs.popup_form.open()
@@ -255,12 +338,12 @@
            this.$message.showLoading()
            const {
               code
            } = await this.$http.request('post', '/api/address/customer/' + (this.submitForm.id ? 'edit' :
               'new'), {
      } = await this.$http.request('post', '/api/address/' + (this.submitForm.id ? 'update' :
          'edit'), {
               data: this.submitForm
            })
            this.$message.hideLoading()
            if (code == 0) {
      if (code === 0) {
               this.$refs.popup_form.close()
               this.$message.showToast(this.submitForm.id ? '修改成功' : '新增成功')