guoshilong
2022-10-14 d8e0af2ce345f3b40581ad05c596f965ae729c01
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
<template>
    <picker
        mode="multiSelector"
        :value="multiIndex"
        :range="multiArray"
        @change="handleValueChange"
        @columnchange="handleColumnChange"
    >
        <slot></slot>
    </picker>
</template>
 
<script>
const CHINA_REGIONS = require('./regions.json');
export default {
    props: {
        defaultRegions: {
            type: Array,
            default() {
                return [];
            }
        },
        defaultRegionCode: {
            type: String
        },
        defaultRegion: [String, Array],
        value: [String, Array]
    },
    data() {
        return {
            cityArr: CHINA_REGIONS[0].childs,
            districtArr: CHINA_REGIONS[0].childs[0].childs,
            multiIndex: [0, 0, 0],
            isInitMultiArray: true
        };
    },
    watch: {
        defaultRegion: {
            handler(region, oldRegion) {
                if (Array.isArray(region)) {
                    // 避免传的是字面量的时候重复触发
                    oldRegion = oldRegion || [];
                    if (region.join('') !== oldRegion.join('')) {
                        this.handleDefaultRegion(region);
                    }
                } else if (region && region.length == 6) {
                    this.handleDefaultRegion(region);
                } else {
                    console.warn('defaultRegion非有效格式');
                }
            },
            immediate: true
        }
    },
    computed: {
        multiArray() {
            return this.pickedArr.map(arr => arr.map(item => item.name));
        },
        pickedArr() {
            // 进行初始化
            if (this.isInitMultiArray) {
                return [
                    CHINA_REGIONS,
                    CHINA_REGIONS[0].childs,
                    CHINA_REGIONS[0].childs[0].childs
                ];
            }
            return [CHINA_REGIONS, this.cityArr, this.districtArr];
        }
    },
    methods: {
        handleColumnChange(e) {
            // console.log(e);
            this.isInitMultiArray = false;
            const that = this;
            let col = e.detail.column;
            let row = e.detail.value;
            that.multiIndex[col] = row;
            try {
                switch (col) {
                    case 0:
                        if (CHINA_REGIONS[that.multiIndex[0]].childs.length == 0) {
                            that.cityArr = that.districtArr = [
                                CHINA_REGIONS[that.multiIndex[0]]
                            ];
                            break;
                        }
                        that.cityArr = CHINA_REGIONS[that.multiIndex[0]].childs;
                        that.districtArr =
                            CHINA_REGIONS[that.multiIndex[0]].childs[
                                that.multiIndex[1]
                            ].childs;
                        break;
                    case 1:
                        that.districtArr =
                            CHINA_REGIONS[that.multiIndex[0]].childs[
                                that.multiIndex[1]
                            ].childs;
                        break;
                    case 2:
                        break;
                }
            } catch (e) {
                // console.log(e);
                that.districtArr = CHINA_REGIONS[that.multiIndex[0]].childs[0].childs;
            }
        },
        handleValueChange(e) {
            // 结构赋值
            let [index0, index1, index2] = e.detail.value;
            let [arr0, arr1, arr2] = this.pickedArr;
            let address = [arr0[index0], arr1[index1], arr2[index2]];
            // console.log(address);
            this.$emit('getRegion', address);
            this.$emit(
                'input',
                `${address[0].name}${address[1].name}${address[2].name}`
            );
        },
        handleDefaultRegion(region) {
            const isCode = !Array.isArray(region);
            this.isInitMultiArray = false;
            let children = CHINA_REGIONS;
            for (let i = 0; i < 3; i++) {
                for (let j = 0; j < children.length; j++) {
                    let condition = isCode
                        ? children[j].code == region.slice(0, (i + 1) * 2)
                        : children[j].name.includes(region[i]);
                    if (condition) {
                        // 匹配成功进行赋值
                        // console.log(i,j,children.length-1);
                        children = children[j].childs;
                        if (i == 0) {
                            this.cityArr = children;
                        } else if (i == 1) {
                            this.districtArr = children;
                        }
                        this.$set(this.multiIndex, i, j);
                        // console.log(this.multiIndex);
                        break;
                    } else {
                        // 首次匹配失败就用默认的初始化
                        // console.log(i,j,children.length-1);
                        if (i == 0 && j == children.length - 1) {
                            this.isInitMultiArray = true;
                        }
                    }
                }
            }
        }
    }
};
</script>