addressCom.vue 5.51 KB
<template>
  <el-drawer class="shoppingCartDrawerCon" title="收货地址管理" size="500px" :visible.sync="dialogShow" :direction="direction">
    <glsx-combi-form class="edit-form-container" ref="editFormRef" label-width="100px" default-width="350px" :form-ref="editChildRef" :form-obj="editForm" :form-data="editFormData" :form-rules="editRule">
      <span slot="provincesAndCities">
        <!-- slot-scope="scope" -->
        <el-cascader v-model="editForm.provincesAndCities" size="mini" ref="cascader" placeholder="请选择 省/市/区" style="width:350px" :options="provincesAndCities_options" :props="{ label: 'name', value: 'code', children: 'childrenList' }" />
      </span>
    </glsx-combi-form>
    <div class="footer-btn">
      <el-button @click="submit" type="primary">保存</el-button>
      <el-button @click="dialogShow = false">取消</el-button>
    </div>
  </el-drawer>
</template>

<script>
import { checkPhone as validPhone } from '@/utils/index'
export default {
  name: 'AddressCom',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    addressInfo: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data() {
    function checkPhone(rule, value, callback) {
      if (!validPhone(value)) {
        callback(new Error('请输入正确的手机号码'))
        return false
      }
      callback()
    }
    return {
      direction: 'rtl',
      editFormData: [
        {
          label: '收货人',
          name: 'contactName',
          type: 'input',
          maxlength: 10,
          placeholder: '请输入姓名'
        },
        {
          label: '收货人电话',
          name: 'contactPhone',
          type: 'input',
          maxlength: 20,
          placeholder: '请输入电话号码'
        },
        {
          label: '收货地址',
          name: 'provincesAndCities',
          type: 'other',
          addHtml: 'provincesAndCities'
        },
        {
          label: '',
          name: 'contactAddress',
          type: 'input',
          maxlength: 50,
          placeholder: '请输入收货地址',
          inputType: 'textarea',
          rows: 3,
          style: {
            'padding-left': '104px'
          }
          // tips: '如XX店车主关怀活动'
        }
      ],
      editForm: {
        contactName: '',
        contactPhone: '',
        contactAddress: '',
        provincesAndCities: []
      },
      provincesAndCities_options: [],
      editRule: {
        contactName: [{ required: true, message: '请输入姓名', trigger: 'change' }],
        contactPhone: [
          { required: true, message: '请输入电话号码', trigger: 'change' },
          { validator: checkPhone, trigger: 'blur' }
        ],
        contactAddress: [{ required: true, message: '请输入收货地址', trigger: 'change' }],
        provincesAndCities: [{ required: true, message: '请选择省市区', trigger: 'change' }]
      },
      editChildRef: 'editChildRef'
    }
  },
  computed: {
    dialogShow: {
      get() {
        return this.visible
      },
      set(val) {
        this.$emit('update:visible', val)
      }
    }
  },
  watch: {
    visible(val) {
      if (val) {
        this.getAreaInfo()
        if (this.addressInfo) {
          this.editForm = this.$common.utils.deepClone(this.addressInfo)
          this.editForm.provincesAndCities = this.editForm.areaId ? [String(this.editForm.provinceId), String(this.editForm.cityId), String(this.editForm.areaId)] : [String(this.editForm.provinceId), String(this.editForm.cityId)]
        } else {
          this.editForm = {}
        }
      }
    }
  },
  created() { },
  methods: {
    // 获取省市区信息
    getAreaInfo() {
      this.$apis.sysBase.getAreaInfoApi({ areaType: '2' }).then(res => {
        this.provincesAndCities_options = res.areaList

      })
    },
    getProvincesAndCities() {
      const nodesInfo = this.$refs.cascader.getCheckedNodes()

      if (nodesInfo.length == 0 || Object.prototype.toString.call(nodesInfo[0]) != '[object Object]') {
        return
      }
      this.editForm.provinceId = nodesInfo[0].pathNodes[0].value
      this.editForm.provinceName = nodesInfo[0].pathNodes[0].label
      this.editForm.cityId = nodesInfo[0].pathNodes[1].value
      this.editForm.cityName = nodesInfo[0].pathNodes[1].label
      this.editForm.areaId = nodesInfo[0].pathNodes[2]?.value || ''
      this.editForm.areaName = nodesInfo[0].pathNodes[2]?.label || ''
    },
    submit() {
      console.log(this.editForm.provincesAndCities)
      this.$refs.editFormRef.$refs[this.editChildRef].validate(valid => {
        if (valid) {
          this.getProvincesAndCities()
          const params = this.$common.utils.deepClone(this.editForm)
          delete params.provincesAndCities
          this.$apis.purchaseShopping
            .addressSave(params)
            .then(result => {
              this.$notify({
                type: 'success',
                title: '保存成功'
              })
              this.dialogShow = false
              this.$emit('handleConfirmAddress')
            })
            .catch(err => { })
        }
      })
    },
    navBack() { }
  }
}
</script>

<style lang="scss" scoped>
.shoppingCartDrawerCon {
  // position: relative;
  ::v-deep .el-drawer__header {
    border-bottom: 1px solid #e7eaec;
    margin-bottom: 20px;
    padding-bottom: 10px;
    font-weight: 600;
    font-size: 20px;
  }

  ::v-deep .el-textarea {
    .el-textarea__inner {
      height: auto !important;
    }
  }

  .footer-btn {
    text-align: center;
  }
}
</style>