chenyc
2023-06-04 b8ce0e7f0652aab9d9b0df9c5fbaebd924d31cc2
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
<template>
    <div>
        <van-popup v-model:show="showDizhi" position="bottom" :style="{ height: '100%' }">
            <van-nav-bar
                title="收货地址管理"
                left-text="返回"
                left-arrow
                @click-left="onClickLeft"
            />
            <van-address-list
                v-model="chosenAddressId"
                :list="list"
                default-tag-text="默认"
                @add="onAdd"
                @edit="onEdit"
            />
        </van-popup>
        <van-popup v-model:show="editDizhi" position="bottom" :style="{ height: '100%' }">
            <van-nav-bar
                title="编辑地址"
                left-text="返回"
                left-arrow
                @click-left="onClickLeft2"
            />
            <van-address-edit
                ref="addressEditRef"
                :area-list="areaList"
                show-delete
                show-set-default
                show-search-result
                :address-info="addressInfo"
                :area-columns-placeholder="['请选择', '请选择', '请选择']"
                @save="onSave"
                @delete="onDelete"
            />
        </van-popup>
    </div>
</template>
<script setup lang="ts">
 
    import { Toast } from 'vant'
    import { ref } from 'vue'
    import { areaList } from '@vant/area-data'
    import type { AddressEditInstance } from 'vant'
    import { ajaxPost } from '@/utils/axios'
    import { userInfoStore } from '@/stores/userInfo'
    const userInfo = userInfoStore()
    const emit = defineEmits(['fanhuiFun'])
    const showDizhi=ref(false)
    const editDizhi=ref(false)
    const addressEditRef = ref<AddressEditInstance>()
    const chosenAddressId = ref(1)
    const addressInfo=ref({
        code:'',
        id:0,
        name:'',
        addressDetail:'',
        tel:'',
        city:'',
        county:'',
        areaCode:'',
        address:'',
        isDefault:false
    })
    const list = ref([
        {
            id: 0,
            code:'',
            name:'',
            tel:'',
            province:'',
            city:'',
            county:'',
            areaCode:'',
            address:'',
            isDefault:false
        },
    ])
    const getDefDizhi=(isload:boolean)=>{
        const pasm=`page=0&size=0&wherecondition=patient_code="${userInfo.patient.patientInfo.code}"`
        ajaxPost('/patient/address/list', pasm).then((re: any) => {
            console.log('默认地址')
            console.log(re)
            if (re.list.length===0){
                list.value=[]
                onAdd()
            } else {
                list.value=[]
                re.list.forEach((e:any)=>{
                    list.value.push({
                        id: e.id,
                        code:e.code,
                        name:e.receivePersonName,
                        tel:e.receivePersonMobile,
                        province:'',
                        city:e.addressArea.split(',')[0],
                        county:e.addressArea.split(',')[1],
                        areaCode:e.remark,
                        address:e.patientAddress,
                        isDefault:e.patientIsDefault===1?true:false,
                    })
                    if (isload){
                        if (e.patientIsDefault===1){
                            chosenAddressId.value=(e.id)
                        }
                    }
                })
                console.log(list.value)
            }
 
        })
    }
    // 打开地址列表
    const openShow=(id:number)=>{
        showDizhi.value=true
        chosenAddressId.value=id
        getDefDizhi(false)
    }
    const onClickLeft=()=>{
        showDizhi.value=false
        console.log('返回父级组件',chosenAddressId.value)
        const row=list.value.find((e:any)=> e.id===chosenAddressId.value)
        emit('fanhuiFun',row)
    }
    const onClickLeft2=()=>{
        addressEditRef.value?.setAddressDetail('3434343')
        editDizhi.value=false
    }
    const onAdd = () =>{
        editDizhi.value=true
        console.log('用户信息')
        console.log(userInfo.patient)
        addressInfo.value={
            code:'',
            id:0,
            name:userInfo.patient.patientInfo.patientName,
            addressDetail:userInfo.patient.patientInfo.patientAddress,
            tel:userInfo.patient.patientInfo.patientTelNo,
            city:'',
            county:'',
            areaCode:'',
            address:'',
            isDefault:false
        }
    }
    const onEdit = (item:any, index:number) =>{
        console.log(item,index)
        addressInfo.value={
            code:item.code,
            id:item.id,
            name:item.name,
            addressDetail:item.address,
            tel:item.tel,
            city:item.city,
            county:item.county,
            areaCode:item.areaCode,
            address:'',
            isDefault:item.isDefault
        }
        editDizhi.value=true
    }
    const onSave = (form:any) => {
        const pasm={
            addressArea: [form.city,form.county].toString(),
            id: addressInfo.value.id,
            code:addressInfo.value.code,
            patientAddress: form.addressDetail,
            patientCode: userInfo.patient.patientInfo.code,
            patientIsDefault: form.isDefault===true?1:0,
            receivePersonMobile: form.tel,
            receivePersonName: form.name,
            // 存地址code
            remark: form.areaCode,
        }
        if (pasm.id===0){
            ajaxPost('/patient/address/add', pasm).then((re: any) => {
                console.log('默认地址')
                console.log(re)
                Toast.success('保存成功')
                getDefDizhi(false)
                chosenAddressId.value=re.id
                editDizhi.value=false
            })
        } else {
            ajaxPost('/patient/address/update', pasm).then((re: any) => {
                console.log('默认地址')
                chosenAddressId.value=re.id
                console.log(re)
                Toast.success('保存成功')
                getDefDizhi(false)
                editDizhi.value=false
            })
        }
 
 
 
 
    }
    const onDelete = () => {
        ajaxPost('patient/address/delete','id='+addressInfo.value.id).then(re=>{
            editDizhi.value=false
            console.log(re)
            Toast.success('删除成功!')
        }).finally(()=>{
            getDefDizhi(true)
        })
    }
    defineExpose({
        openShow
    } )
</script>