cshenfu
2024-06-27 58aa6e282bb343503be3cb198948328ea3ed7b79
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
<template>
    <view class="page_bg">
        <u-form :model="form" ref="uForm" :label-style="labelS" :error-type="['toast']">
            <u-form-item label="姓名:" prop="name">
                <u-input v-model="form.name" placeholder="请输入姓名" />
            </u-form-item>
            <u-form-item label="电话:" prop="phoneNumber">
                <u-input v-model="form.phoneNumber" placeholder="请输入电话" />
            </u-form-item>
            <u-form-item label="地区:" prop="labelShow">
                <u-input type="select" :select-open="show" v-model="labelShow" placeholder="请选择地区" @click="show = true">
                </u-input>
            </u-form-item>
            <u-form-item prop="detail">
                <u-input v-model="form.detail" type="textarea" placeholder="详细地址:如小区、楼栋、门牌号" />
            </u-form-item>
            <u-form-item label="设为默认地址" label-width="50%" class="default_address">
                <u-switch slot="right" size="40" v-model="switchVal" active-color="#FDD303"></u-switch>
            </u-form-item>
        </u-form>
 
        <u-toast ref="uToast" />
        <u-picker mode="region" :default-region="[form.province, form.city, form.area]" v-model="show"
            @confirm="selectAddress"></u-picker>
 
        <u-button class="save_btn" shape="circle" :ripple="true" ripple-bg-color="#444" @tap="send">
            确认
        </u-button>
    </view>
</template>
 
<script>
    var that; // 当前页面对象
    var vk; // vk依赖
    export default {
        data() {
            return {
                labelS: {
                    fontWeight: 'bold'
                },
                // 获得的信息
                fun: 'add',
                addressId: '',
 
                // 表单
                form: {
                    name: '',
                    phoneNumber: '',
                    province: '北京市',
                    city: '市辖区',
                    area: '东城区',
                    detail: ''
                },
                rules: {
                    name: [{
                        required: true,
                        message: '请输入姓名',
                        trigger: ['blur']
                    }],
                    phoneNumber: [{
                        required: true,
                        message: '请输入手机号',
                        trigger: ['blur']
                    }, {
                        validator: (rule, value, callback) => {
                            return this.$u.test.mobile(value);
                        },
                        message: '请输入正确的手机号',
                        trigger: ['blur'],
                    }],
                    detail: [{
                        required: true,
                        message: '请输入详细地址',
                        trigger: ['blur']
                    }],
                },
                // 仅显示文本
                labelShow: '',
                // 是否设为默认地址
                switchVal: false,
 
                show: false
            }
        },
        // 监听 - 页面每次【加载时】执行(如:前进)
        onLoad(options = {}) {
            that = this;
            vk = that.vk;
            that.init(options)
        },
        // 监听 - 页面【首次渲染完成时】执行。注意如果渲染速度快,会在页面进入动画完成前触发
        onReady() {
            this.$refs.uForm.setRules(this.rules);
        },
        // 监听 - 页面每次【显示时】执行(如:前进和返回) (页面每次出现在屏幕上都触发,包括从下级页面点返回露出当前页面)
        onShow() {},
        // 监听 - 页面每次【隐藏时】执行(如:返回)
        onHide() {},
        // 函数
        methods: {
            // 页面数据初始化函数
            init(options) {
                this.fun = options.type
                uni.setNavigationBarTitle({
                    title: options.type == 'edit' ? '编辑地址' : '新增地址'
                });
                if (options.type == 'edit') {
                    this.getAddressInfo(options.id)
                    this.addressId = options.id
                }
            },
            // 获取地址信息
            getAddressInfo(id) {
                vk.callFunction({
                    url: 'client/address/kh/getDetail',
                    title: '请求中...',
                    data: { id },
                    success: function(res) {
                        that.form.name = res.data.name
                        that.form.phoneNumber = res.data.phone_number
                        that.form.province = res.data.province
                        that.form.city = res.data.city
                        that.form.area = res.data.area
                        that.form.detail = res.data.detail
                        that.switchVal = res.data.is_default == 0 ? false : true
                        that.labelShow = res.data.province + '-' + res.data.city + '-' + res.data.area
                    }
                });
            },
            // 完成地区选择
            selectAddress(e) {
                this.form.province = e.province.label
                this.form.city = e.city.label
                this.form.area = e.area.label
                this.labelShow = e.province.label + '-' + e.city.label + '-' + e.area.label
            },
            // 提交
            send() {
                this.$refs.uForm.validate(valid => {
                    if (!valid) return
                    if (that.labelShow == '') {
                        that.$refs.uToast.show({
                            title: '请选择地址',
                            type: 'default'
                        })
                        return
                    }
                    that.addInfo()
                })
            },
            // 添加
            addInfo() {
                vk.callFunction({
                    url: that.fun == 'add' ? 'client/address/kh/add' : 'client/address/kh/update',
                    title: '请求中...',
                    data: {
                        name: that.form.name,
                        phone_number: that.form.phoneNumber,
                        province: that.form.province,
                        city: that.form.city,
                        area: that.form.area,
                        detail: that.form.detail,
                        is_default: that.switchVal ? 1 : 0,
                        address_id: that.addressId,
                        need_user_info: true
                    },
                    success: function(res) {
                        if (res.code == 0) {
                            that.$refs.uToast.show({
                                title: '修改地址成功',
                                type: 'success'
                            })
                            setTimeout(() => {
                                uni.$emit('addressUpdate', { msg: '页面更新' })
                                uni.navigateBack({
                                    delta: 1
                                })
                            }, 1000)
                        }
                    }
                })
            }
        },
    }
</script>
 
<style lang="scss" scoped>
    .page_bg {
        min-height: 100vh;
        background: #F4F6F9;
        padding-top: 2rpx;
 
        .u-form-item {
            padding: 21rpx 20rpx;
            background-color: #FFFFFF;
        }
 
        .default_address {
            margin-top: 21rpx;
        }
    }
 
    /deep/ .u-column-item {
        font-size: 28rpx;
    }
 
    .save_btn {
        background-color: #333333;
        color: #FFFFFF;
        height: 70rpx;
        width: 88%;
        margin-top: 400rpx;
    }
</style>