1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<template>
  <el-form-item :label="placeholder">
    <el-cascader
      v-model="selectedOptions"
      :options="locations"
      :placeholder="placeholder"
      :props="optionProps"
      style="width: 280px"
    />
  </el-form-item>
</template>
 
<script>
import { enumLocation } from '@/enum/location'
 
// 记录选项是否是自定义传入的
let customValue = true
 
export default {
  props: {
    // 是否在首选项处添加“全部”选项
    allOption: {
      type: Boolean,
      default: true
    },
    // 查询的行政级别,取值1,2,3,4
    level: {
      type: Number,
      default: 4
    },
    // 结果返回
    value: Object,
    // 是否默认返回初始选项
    initValue: {
      type: Boolean,
      default: true
    },
    // 能否选择任意一级选项
    checkStrictly: {
      type: Boolean,
      default: true
    }
  },
  emits: ['update:value'],
  data() {
    return {
      locations: enumLocation(this.allOption, this.level),
      selectedOptions: [],
      optionProps: {
        checkStrictly: this.checkStrictly
      }
    }
  },
  computed: {
    placeholder() {
      const list = '省/市/区/镇'.split('/')
      const p = []
      for (let i = 0; i < this.level; i++) {
        p.push(list[i])
      }
      return p.join('/')
    }
  },
  watch: {
    selectedOptions: {
      handler(nVal, oVal) {
        if (nVal != oVal) {
          const res = this.optionFormat(nVal)
          // 标明此时的value更新是组件内部触发
          customValue = false
          this.$emit('update:value', res)
        }
      },
      deep: true
    },
    value: {
      handler(nVal, oVal) {
        // 只有当值是外部传入时,才触发更新
        if (!customValue) {
          customValue = true
          return
        }
        if (nVal != oVal) {
          if (nVal || nVal.length > 0) {
            this.selectedOptions = this.optionFormatReverse(nVal)
          }
        }
      },
      deep: true,
      immediate: true
    }
  },
  methods: {
    /**
     * 地区选项结果格式化
     */
    optionFormat(val) {
      const res = {
        pCode: null,
        pName: null,
        cCode: null,
        cName: null,
        dCode: null,
        dName: null,
        tCode: null,
        tName: null
      }
      if (val.length > 0) {
        res.pCode = val[0][0]
        res.pName = val[0][1]
      }
      if (val.length > 1) {
        res.cCode = val[1][0]
        res.cName = val[1][1]
      }
      if (val.length > 2) {
        res.dCode = val[2][0]
        res.dName = val[2][1]
      }
      if (val.length > 3) {
        res.tCode = val[3][0]
        res.tName = val[3][1]
      }
      return res
    },
    optionFormatReverse(val) {
      const res = []
      if (val.pCode) {
        res.push([val.pCode, val.pName])
      }
      if (val.cCode) {
        res.push([val.cCode, val.cName])
      }
      if (val.dCode) {
        res.push([val.dCode, val.dName])
      }
      if (val.tCode) {
        res.push([val.tCode, val.tName])
      }
      return res
    }
  },
  mounted() {
    if (this.initValue) {
      if (this.checkStrictly) {
        this.selectedOptions = [this.locations[0].value]
      } else {
        const f = (location) => {
          if (location.children && location.children.length > 0) {
            const r = f(location.children[0])
            r.unshift(location.value)
            return r
          } else {
            return [location.value]
          }
        }
        this.selectedOptions = f(this.locations[0])
      }
    }
  }
}
</script>