餐饮油烟智能监测与监管一体化平台
riku
2026-03-12 723be8e0896fbf7e9456a5defb44911a3d0cbc27
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
163
164
165
166
167
168
169
170
<template>
  <el-form-item :label="placeholder" :prop="prop">
    <el-cascader
      v-bind="$attrs"
      :model-value="formatedValue"
      @change="handleChange"
      :options="locations"
      :placeholder="placeholder"
      :props="optionProps"
      style="width: 320px"
    />
  </el-form-item>
</template>
 
<script>
import { enumLocation } from '@/enum/location';
 
export default {
  props: {
    // 是否在首选项处添加“全部”选项
    allOption: {
      type: Boolean,
      default: true
    },
    // 查询的行政级别,取值1,2,3,4, 5, 6
    level: {
      type: Number,
      default: 4
    },
    // 筛选条件,传入行政区划中文,例如:['上海市','上海市', '金山区']
    filters: {
      type: Array,
      default: () => []
    },
    // 结果返回
    value: Object,
    // 是否默认返回初始选项
    initValue: {
      type: Boolean,
      default: true
    },
    // 能否选择任意一级选项
    checkStrictly: {
      type: Boolean,
      default: true
    },
    prop: {
      type: String,
      default: '_locations'
    }
  },
  emits: ['update:value', 'change'],
  data() {
    return {
      optionProps: {
        checkStrictly: this.checkStrictly,
        expandTrigger: 'hover',
      }
    };
  },
  computed: {
    locations() {
      return enumLocation(this.allOption, this.level, this.filters);
    },
    placeholder() {
      const list = '省/市/区/镇/集/物'.split('/');
      const p = [];
      for (let i = 0; i < this.level; i++) {
        p.push(list[i]);
      }
      return p.join('/');
    },
    formatedValue() {
      return this.optionFormatReverse(this.value);
    }
  },
  methods: {
    handleChange(value) {
      this.$emit('update:value', this.optionFormat(value));
      this.$emit('change', this.optionFormat(value));
    },
    /**
     * 地区选项结果格式化
     */
    optionFormat(val) {
      const res = {
        pCode: null,
        pName: null,
        cCode: null,
        cName: null,
        dCode: null,
        dName: null,
        tCode: null,
        tName: null,
        aCode: null,
        aName: null,
        mCode: null,
        mName: 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];
      }
      if (val.length > 4) {
        res.aCode = val[4][0];
        res.aName = val[4][1];
      }
      if (val.length > 5) {
        res.mCode = val[5][0];
        res.mName = val[5][1];
      }
      return res;
    },
    optionFormatReverse(val) {
      const res = [];
      if (val) {
        if (val.pName) {
          res.push([val.pCode, val.pName]);
        }
        if (val.cName) {
          res.push([val.cCode, val.cName]);
        }
        if (val.dName) {
          res.push([val.dCode, val.dName]);
        }
        if (val.tName) {
          res.push([val.tCode, val.tName]);
        }
        if (val.aName) {
          res.push([val.aCode, val.aName]);
        }
        if (val.mName) {
          res.push([val.mCode, val.mName]);
        }
      }
      return res;
    }
  },
  mounted() {
    if (this.initValue) {
      if (this.checkStrictly) {
        this.handleChange([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.handleChange(f(this.locations[0]));
      }
    }
  }
};
</script>