WebApp【公共组件库】@前端(For Git Submodule)
Tevin
2021-04-08 f969b32736ab60b141d076f7aeaafdbd9dc1c546
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
/**
 * CChinaArea - 表单项,中国地址三级联动
 * @author Tevin
 */
 
<template>
    <view class="c-china-area">
        <picker
            mode="multiSelector"
            :range="range"
            :value="current"
            range-key="label"
            @columnchange="evt=>updateColumns(evt.detail)"
            @change="evt=>handleChange(evt.detail.value)"
        >
            <view @tap="evt=>handleOpen(evt)">
                <AtInput
                    ref="input"
                    :name="itemRes.name"
                    :title="itemRes.label"
                    :required="itemRes.required"
                    :error="itemRes.error"
                    :placeholder="placeholder"
                    :value="selected"
                >
                    <AtIcon :value="loading ? 'loading':'chevron-right'" />
                </AtInput>
            </view>
        </picker>
    </view>
</template>
 
<script>
import { AtInput, AtIcon } from 'taro-ui-vue';
import ChinaLocations from './ChinaLocations';
import './cChinaArea.scss';
 
const { onReady, getLocationTree, getRegionNames } = ChinaLocations;
 
export default {
    name: 'CChinaArea',
    components: {
        AtInput,
        AtIcon,
    },
    props: {
        // 表单数据资源(表单组件内部机制专用)
        itemRes: Object,
        // 当地址获取经纬度时,设置加载中显示状态
        loading: {
            type: Boolean,
            default: false,
        },
        // 占位提示
        placeholder: String,
    },
    data() {
        return {
            range: [[], [], []],
            current: [0, 0, 0],
        };
    },
    computed: {
        selected() {
            const curVal = this.itemRes.formData[this.itemRes.name];
            if (curVal && curVal.length === 3) {
                return getRegionNames(curVal).join(' / ');
            } else {
                return '';
            }
        },
    },
    methods: {
        handleOpen(evt) {
            if (process.env.TARO_ENV === 'h5') {
                if (evt && evt.target.className.indexOf('at-input__title') >= 0) {
                    evt.stopPropagation();
                    evt.preventDefault();
                    return;
                }
            }
            const locationTree = getLocationTree();
            const curVal = this.itemRes.formData[this.itemRes.name];
            const range = [
                locationTree,
                locationTree[0].children,
                locationTree[0].children[0].children,
            ];
            const current = [0, 0, 0];
            // 有值
            if (curVal && curVal.length > 0) {
                // 省
                if (curVal[0]) {
                    const proviceIndex = locationTree.findIndex(
                        provice => provice.value === curVal[0]
                    );
                    if (proviceIndex >= 0) {
                        range[1] = locationTree[proviceIndex].children;
                        range[2] = locationTree[proviceIndex].children[0].children;
                        current[0] = proviceIndex;
                        // 市
                        if (curVal[1]) {
                            const cityIndex = range[1].findIndex(
                                city => city.value === curVal[1]
                            );
                            if (cityIndex >= 0) {
                                range[2] = range[1][cityIndex].children;
                                current[1] = cityIndex;
                            }
                            // 区
                            if (curVal[2]) {
                                const areaIndex = range[2].findIndex(
                                    area => area.value === curVal[2]
                                );
                                if (areaIndex >= 0) {
                                    current[2] = areaIndex;
                                }
                            }
                        }
                    }
                }
            }
            this.range = range;
            this.current = current;
        },
        updateColumns(roll) {
            const locationTree = getLocationTree();
            // 第一列滚动
            if (roll.column === 0) {
                const cities = locationTree[roll.value].children;
                this.range = [this.range[0], cities, cities[0].children];
                this.current = [roll.value, 0, 0];
            }
            // 第二列滚动
            else if (roll.column === 1) {
                const areas = locationTree[this.current[0]].children[roll.value].children;
                this.range = [this.range[0], this.range[1], areas];
                this.current = [this.current[0], roll.value, 0];
            }
            // 第三列滚动
            else if (roll.column === 3) {
                this.current.splice(2, 1, roll.value);
            }
        },
        handleChange(detail) {
            const locationTree = getLocationTree();
            const codes = [];
            const provice = locationTree[detail[0]];
            codes[0] = provice.value;
            const city = provice.children[detail[1]];
            codes[1] = city.value;
            const area = city.children[detail[2]];
            codes[2] = area.value;
            this.itemRes.onChange(codes);
        },
    },
    mounted() {
        if (process.env.TARO_ENV === 'weapp') {
            onReady(() => {
                this.handleOpen();
            });
        }
    },
};
</script>