yj
2024-12-05 ac3234c308e86f20cc63465573f321561ee00690
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<template>
    <!--地址picker-->
    <view :status="checkStatus" v-if="lotusAddressData.visible" class="lotus-address-mask">
        <view class="lotus-address-box">
            <view class="lotus-address-action">
                <text @click.stop="cancelPicker" class="lotus-address-action-cancel">取消</text>
                <text @click.stop="chosedVal" class="lotus-address-action-affirm">确认</text>
            </view>
            <view class="lotus-address-picker-box">
                <!--省-->
                <scroll-view scroll-y :scroll-into-view="'pid'+pChoseIndex" class="lotus-address-picker-box-item">
                    <view @tap="clickPicker(0,pIndex,pItem);" :id="'pid'+pIndex" :class="pIndex === pChoseIndex?'lotus-address-picker lotus-address-picker2':'lotus-address-picker'"  v-for="(pItem,pIndex) in province" :key="pIndex">{{pItem}}</view>
                </scroll-view>
                <!--市-->
                <scroll-view scroll-y :scroll-into-view="'cid'+cChoseIndex" class="lotus-address-picker-box-item">
                    <view @tap="clickPicker(1,cIndex,cItem);" :id="'cid'+cIndex" :class="cIndex === cChoseIndex?'lotus-address-picker lotus-address-picker2':'lotus-address-picker'" v-for="(cItem,cIndex) in city" :key="cIndex">{{cItem}}</view>
                </scroll-view>
                <!--区-->
                <scroll-view scroll-y :scroll-into-view="'tid'+tChoseIndex" class="lotus-address-picker-box-item">
                    <view @tap="clickPicker(2,tIndex,tItem);" :id="'tid'+tIndex" :class="tIndex === tChoseIndex?'lotus-address-picker lotus-address-picker2':'lotus-address-picker'" v-for="(tItem,tIndex) in town" :key="tIndex">{{tItem}}</view>
                </scroll-view>
                <!--区END-->
            </view>
        </view>
    </view>
    <!--地址picker END-->
</template>
 
<script>
    import {lotusAddressJson} from  "./Winglau14-lotusAddress.js";
    export default {
        props:['lotusAddressData'],
        data() {
            return {
                visible: false,
                province:[],
                city:[],
                town:[],
                provinceName:'',
                cityName:'',
                townName:'',
                type:0,//0新增1编辑
                pChoseIndex:-1,
                cChoseIndex:-1,
                tChoseIndex:-1,
            };
        },
        methods:{
            //取消
            cancelPicker(){
                const provinceCode = this.getTarId(this.provinceName);
                const cityCode = this.getTarId(this.cityName);
                const townCode = this.getTarId(this.townName);
                this.$emit("choseVal",{
                    provice:this.provinceName,
                    provinceCode,
                    city:this.cityName,
                    cityCode,
                    town:this.townName,
                    townCode,
                    isChose:0,
                    visible:false
                });
            },
            //获取最后选择的省市区的值
            chosedVal() {
                this.type = 1;
                const provinceCode = this.getTarId(this.provinceName);
                const cityCode = this.getTarId(this.cityName);
                const townCode = this.getTarId(this.townName);
                this.$emit("choseVal",{
                    provice:this.provinceName,
                    provinceCode,
                    city:this.cityName,
                    cityCode,
                    town:this.townName,
                    townCode,
                    isChose:1,
                    visible:false
                });
            },
            //获取省市区value
            getTarId(name,type){
                let id = 0;
                const _this = this;
                lotusAddressJson.map((item,index)=>{
                    if(item.name === name){
                        id = item.value;
                    }
                });
                return id;
            },
            //获取市数据
            getCityArr(parentId){
                let city = [];
                
                lotusAddressJson.map((item,index)=>{
                    if(item.parent === parentId){
                        city.push(item.name);
                    }
                });
                return city;
            },
            //获取区数据
            getTownArr(parentId){
                let town = [];
                lotusAddressJson.map((item,index)=>{
                    if(index>34&&item.parent === parentId){
                        town.push(item.name);
                    }
                });
                return town;
            },
            //初始化数据
            initFn(){
                console.log(1);
                lotusAddressJson.map((item,index)=>{
                    if(index<=34){
                        this.province.push(item.name);
                    }
                });
                //已选择省市区,高亮显示对应选择省市区
                const p = this._props.lotusAddressData.provinceName;
                const c = this._props.lotusAddressData.cityName;
                const t = this._props.lotusAddressData.townName;
                if(p){
                    this.pChoseIndex = this.getTarIndex(this.province,p);
                }
                if(p&&c){
                    const pid = this.getTarId(p);
                    this.city = this.getCityArr(pid);
                    this.cChoseIndex = this.getTarIndex(this.city,c);
                }
                if(p&&c&&t){
                    const cid= this.getTarId(c);
                    this.town = this.getTownArr(cid);
                    this.tChoseIndex = this.getTarIndex(this.town,t);
                }
            },
            //获取已选省市区
            getChosedData(){
                const pid = this.getTarId(this.provinceName,'provice');
                this.city = this.getCityArr(pid);
                const cid= this.getTarId(this.cityName,'city');
                this.town = this.getTownArr(cid);
                //已选省市区获取对应index
                if(this.provinceName){
                    this.pChoseIndex = this.getTarIndex(this.province,this.provinceName);
                }
                if(this.cityName){
                    this.cChoseIndex = this.getTarIndex(this.city,this.cityName);
                }
                if(this.townName){
                    this.tChoseIndex = this.getTarIndex(this.town,this.townName);
                }
            },
            //选择省市区交互
            clickPicker(type,index,name){
                //省
                if(type === 0){
                    this.pChoseIndex = index;
                    this.provinceName = name;
                    this.cChoseIndex = -1;
                    this.tChoseIndex = -1;
                    this.cityName = '';
                    this.townName = '';
                }
                //市
                if(type ===1){
                    this.cChoseIndex = index;
                    this.cityName = name;
                    this.tChoseIndex = -1;
                    this.townName = '';
                }
                //区
                if(type === 2){
                    this.tChoseIndex = index;
                    this.townName = name;
                }
                //获取省市区数据
                this.getChosedData();
            },
            //获取已选省市区index
            getTarIndex(arr,tarName){
                let cIndex = 0;
                arr.map((item,index)=>{
                    if(item === tarName){
                        cIndex = index;
                    }
                });
                return cIndex;
            }
        },
        created() {
            this.provinceName = this._props.lotusAddressData.provinceName;
            this.cityName = this._props.lotusAddressData.cityName;
            this.townName = this._props.lotusAddressData.townName;
        },
        computed:{
            checkStatus(){
                let t = null;
                const _this = this;
                if(!_this.visible){
                    _this.initFn();
                    _this.visible = _this._props.lotusAddressData.visible;
                    t = _this.visible;
                }
                return t;
            }
        }
    }
</script>
 
<style >
@import "./Winglau14-lotusAddress.css";
</style>