陶杰
2024-12-10 b07889e22f823fac80a66b503671e170668f4ee6
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
<template>
    <view class="u-page">
 
        <view class="u-demo-block">
            <view class="u-demo-block__content">
                <!-- 注意,如果需要兼容微信小程序,最好通过setRules方法设置rules规则 -->
                <u--form labelPosition="left" :rules="rules" :model="userInfo" ref="subAccountForm"
                    :labelWidth="labelWidth">
                    <u-form-item required label="子账号名称" prop="name" borderBottom>
                        <u--input v-model="userInfo.name" border="none" placeholder="请输入子账号名称" clearable></u--input>
                    </u-form-item>
 
                    <u-form-item required label="联系人" prop="contact" borderBottom>
                        <u--input v-model="userInfo.contact" border="none" placeholder="请输入联系人" clearable></u--input>
                    </u-form-item>
 
                    <u-form-item required label="手机" prop="phone" borderBottom>
                        <u--input type="number" v-model="userInfo.phone" border="none" placeholder="请输入手机号码" clearable></u--input>
 
                    </u-form-item>
                    <u-form-item required label="验证码" prop="smsCode" borderBottom>
                        <u--input type="number" v-model="userInfo.smsCode" border="none" placeholder="请填写验证码" clearable></u--input>
                        <u-button slot="right" @tap="getCode" :text="tips" type="success" size="mini"
                            :disabled="disabled1"></u-button>
                    </u-form-item>
 
                    <u-form-item required label="密码" prop="password" borderBottom>
                        <u--input type="password" v-model="userInfo.password" border="none" placeholder="请输入密码" clearable></u--input>
                    </u-form-item>
                    <u-form-item required label="账号状态" prop="isEnabled" borderBottom>
                        <u-switch v-model="userInfo.isEnabled" size="50"></u-switch>
                    </u-form-item>
                </u--form>
                <u-button type="primary" text="提交" customStyle="margin-top: 50px" @click="submit"></u-button>
                <u-button type="error" text="重置" customStyle="margin-top: 10px" @click="reset"></u-button>
            </view>
        </view>
    </view>
</template>
 
<script>
export default {
    data() {
        return {
            labelWidth: '180',
            tips: '获取验证码',
            disabled1: false,// 校验是否禁用
            timer: null, // 倒计时计时器
            count:60,
            // #ifdef PUB_SUPPLIER
            apitype: 'supplier',
            // #endif
            // #ifdef PUB_PARTNER
            apitype: 'partner',
            // #endif
            // #ifdef PUB_CUSTOMER
            apitype: 'customer',
            // #endif
            userInfo: {
                id: '',
                name: '',
                contact: '',
                phone: '',
                smsCode: '',
                password: '',
                isEnabled: true,
            },
            rules: {
                'name': [{
                    type: 'string',
                    required: true,
                    message: '请输入子账号名称',
                    trigger: ['blur', 'change']
                }
                    // , {
                    //     validator: (rule, value, callback) => {
                    //         return uni.$u.test.chinese(value);
                    //     },
                    //     message: "姓名必须为中文",
                    //     trigger: ["change", "blur"],
                    // }
                ],
                'contact': {
                    type: 'string',
                    required: true,
                    message: '请输入联系人',
                    trigger: ['blur', 'change']
                },
 
 
                'phone': [{
                    type: 'string',
                    required: true,
                    message: '请输入电话号码',
                    trigger: ['blur', 'change']
                }, {
                    pattern: /^1[3-9]\d{9}$/,
                    message: '请输入有效的手机号码',
                    trigger: ['blur', 'change'],
                },
                ],
 
                'smsCode': {
                    type: 'string',
                    required: true,
                    message: '请输入验证码',
                    trigger: ['blur', 'change']
                },
 
                'password': [{
                    type: 'string',
                    required: true,
                    message: '请输入密码',
                    trigger: ['blur', 'change']
                },
                {
                    validator: (rule, value, callback) => {
                        if (!value) {
                            callback(new Error('请输入密码'));
                        } else if (value.length < 8 || value.length > 20) {
                            callback(new Error('密码长度需为 8 到 20 位'));
                        } else {
                            callback(); // 校验通过
                        }
                    },
                    trigger: ['blur', 'change']
                },
                ]
 
            },
 
        }
    },
    onReady() {
        // 如果需要兼容微信小程序,并且校验规则中含有方法等,只能通过setRules方法设置规则
        this.$refs.subAccountForm.setRules(this.rules)
    },
    onUnload() {
        // 组件销毁时清除计时器
        if (this.timer) {
            clearInterval(this.timer);
        }
    },
    onLoad(options) {
        this.userInfo.id = options.id || ''
        if(this.userInfo.id){
            this.getSubAccount()
            // 修改topbar标题
            uni.setNavigationBarTitle({
                title: '子账号修改'
            });
        }
    },
    onShow() {
      this.getList();
    },
    methods: {
 
        async getSubAccount(){
            const {
                code,data
            } = await this.$http.request('get', '/api/supplierSub/getById', {
                params: {
                    id:this.userInfo.id
                }
            })
            if (code == 0) {
                this.userInfo = {
                    ...data
                }
                this.userInfo.password=''
            } else {
                
            }
        },
        validatePhone(phone) {
            const phoneRegex = /^[1][3-9]\d{9}$/; // 简单的中国大陆手机号正则
            return phoneRegex.test(phone);
        },
        async getCode() {
            
            // 校验手机号格式
            if (!this.validatePhone(this.userInfo.phone)) {
                uni.showToast({
                    title: '请输入有效的手机号码',
                    icon: 'none'
                });
                return;
            }
            if (this.disabled1) return; // 防止重复点击
 
            const {
                code
            } = await this.$http.request('post', '/api/sms/send/code', {
                data: {
                    tel: this.userInfo.phone,
                    userType: this.apitype.toLowerCase()
                }
            })
            if (code == 0) {
                uni.$u.toast('验证码已发送');
                this.startCountdown(); // 开始倒计时
            } else {
                
            }
 
            
        },
        startCountdown() {
            this.disabled1 = true; // 禁用按钮
            this.tips = `${this.count}秒后重试`; // 设置按钮文本
 
            this.timer = setInterval(() => {
                if (this.count > 1) {
                    this.count--;
                    this.tips = `${this.count}秒后重试`;
                } else {
                    this.resetCountdown(); // 倒计时结束,重置状态
                }
            }, 1000);
        },
 
 
        resetCountdown() {
            clearInterval(this.timer); // 清除计时器
            this.timer = null;
            this.count = 60; // 重置秒数
            this.tips = '获取验证码'; // 重置按钮文本
            this.disabled1 = false; // 启用按钮
        },
 
        submit() {
            // 如果有错误,会在catch中返回报错信息数组,校验通过则在then中返回true
                this.$refs.subAccountForm.validate().then(async res => {
                    uni.$u.toast('校验通过')
                    const {
                    code
                } = await this.$http.request('post', '/api/supplierSub/addOrUpdate', {
                    data: {
                        ... this.userInfo
                    }
                })
                if (code == 0) {
                    uni.$u.toast('新增子账号成功');
                    uni.navigateTo({
                        url: '/sub_pages/supplier/sub-account/sub-account-list'
                    })
                    
                    
                } else {
                    uni.$u.toast('新增子账号失败')
                }
            }).catch(errors => {
                uni.$u.toast('请填写完整信息')
            })
        },
        reset() {
            this.$refs.subAccountForm.resetFields()
            this.$refs.subAccountForm.clearValidate()
            this.userInfo={
                id: '',
                name: '',
                contact: '',
                phone: '',
                smsCode: '',
                password: '',
                isEnabled: true,
            }
            
        },
 
    },
}
</script>
 
<style lang="scss">
.u-page {
    background-color: #f7f8fa;
    fonst-size: 14px;
    padding: 15px 15px 40px 15px;
 
    .u-demo-block {
        flex: 1;
        margin-bottom: 23px
    }
}
 
.u-page2 {
    background-color: #f7f8fa;
    padding: 15px 15px 40px 15px;
 
    .u-demo-block {
        flex: 1;
        margin-bottom: 23px
    }
 
    .u-demo-block__title {
        font-size: 14px;
        color: #8f9ca2;
        margin-bottom: 8px;
        display: flex;
        flex-direction: row
    }
 
    .u-demo-block__content {
        display: flex;
        flex-direction: column
    }
 
    .image-icon {
        width: 30rpx;
        height: 30rpx;
    }
 
    .u-button--plain.data-v-3bf2dba7 {
        background-color: #00BCD4 !important;
    }
 
    .u-button--plain.u-button--primary.data-v-3bf2dba7 {
        color: #FFFFFF;
    }
 
    .u-button--primary.data-v-3bf2dba7 {
        color: #fff;
        border-color: #00BCD4 !important;
        border-width: 1px;
        border-style: solid;
        border-radius: 10rpx;
        width: 200rpx;
    }
 
    .u-demo-block {
        padding: 10rpx;
        background-color: #FFFFFF;
        border-radius: 50rpx;
        background-color: #00AF68;
        margin: 20rpx;
 
        .tixian {
            margin-top: 30rpx;
            margin-bottom: 30rpx;
        }
    }
 
    .u-demo-block-2 {
        margin: 30rpx;
        padding: 10rpx;
        background-color: #FFFFFF;
        border-radius: 10rpx;
    }
 
    .title {
        text-align: center;
        font-size: 30rpx;
        color: #909399;
        margin: 10rpx;
    }
 
    .grid-text-white {
        color: white;
    }
 
    .grid-text {
        font-size: 14px;
        color: #909399;
        padding: 10rpx 0 20rpx 0rpx;
        /* #ifndef APP-PLUS */
        box-sizing: border-box;
        /* #endif */
    }
 
    .statis_val {
        font-size: 16px;
        color: black;
        // font-weight: bold;
    }
 
    .margin-10 {
        margin: 10rpx;
    }
}
</style>