zhangzengfei
2021-06-07 a002d27f6329cec2c3aeafa28f8d4ffd2cbf901b
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
<template>
    <view style="padding: 20px;flex: 1;">
        <view class="uni-header">
            <view class="uni-group">
                <view class="uni-title">修改密码</view>
            </view>
        </view>
        <uni-forms ref="form" validateTrigger="bind" :rules="rules" @submit="submit">
            <uni-forms-item label="旧密码" name="oldPassword" labelWidth="85">
                <input class="uni-input-border" type="password" placeholder="旧密码" @blur="binddata('oldPassword',$event.detail.value)" />
            </uni-forms-item>
 
            <uni-forms-item label="新密码" name="newPassword" labelWidth="85">
                <input class="uni-input-border" :password="showPassword" placeholder="新密码" @blur="binddata('newPassword',$event.detail.value)" />
                <text class="uni-icon-password-eye pointer" :class="[!showPassword ? 'uni-eye-active' : '']" @click="changePassword">&#xe568;</text>
            </uni-forms-item>
 
            <uni-forms-item label="确认新密码" name="passwordConfirmation" labelWidth="85" :errorMessage="errorMessage">
                <input @confirm="confirmForm('passwordConfirmation',$event.detail.value)" class="uni-input-border" :password="showPasswordAgain"
                 placeholder="确认新密码" @blur="binddata('passwordConfirmation',$event.detail.value)" />
                <text class="uni-icon-password-eye pointer" :class="[!showPasswordAgain ? 'uni-eye-active' : '']" @click="changePasswordAgain">&#xe568;</text>
            </uni-forms-item>
            <view class="uni-button-group pointer">
                <button class="uni-button uni-button-full" type="primary" @click="submitForm">保存</button>
            </view>
        </uni-forms>
    </view>
</template>
 
<script>
    import {
        mapState,
        mapMutations
    } from 'vuex'
    export default {
        data() {
            return {
                showPassword: true,
                showPasswordAgain: true,
                errorMessage: '',
                password: {
                    oldPassword: '',
                    newPassword: '',
                    passwordConfirmation: ''
                },
                rules: {
                    oldPassword: {
                        rules: [{
                            required: true,
                            errorMessage: '请输入旧密码'
                        }]
                    },
                    newPassword: {
                        rules: [{
                                required: true,
                                errorMessage: '请输入新密码'
                            },
                            {
                                minLength: 6,
                                errorMessage: '密码长度最小{minLength}个字符'
                            }
                        ]
                    },
                    passwordConfirmation: {
                        rules: [{
                                required: true,
                                errorMessage: '请确认新密码'
                            },
                            {
                                minLength: 6,
                                errorMessage: '密码长度最小{minLength}个字符'
                            }
                        ]
                    }
                }
            }
        },
        props: {
            hasBackButton: {
                type: Boolean,
                default: false
            },
            isPhone: {
                type: Boolean,
                default: true
            }
        },
        computed: {
            ...mapState('user', ['userInfo'])
        },
        methods: {
            ...mapMutations(['logout']),
            submit(event) {
                const {
                    errors,
                    value
                } = event.detail
                if (errors) return
                if (value.newPassword !== value.passwordConfirmation) {
                    this.errorMessage = '两次输入密码不相同'
                    return
                }
                this.save(value)
            },
            confirmForm(name, value) {
                this.binddata(name, value)
                this.submitForm()
            },
            submitForm() {
                this.errorMessage = ''
                this.$refs.form.submit()
            },
            save(formData) {
                let that = this
                uni.showLoading()
                uniCloud.callFunction({
                    name: 'user-center',
                    data: {
                        action: 'updatePwd',
                        params: {
                            ...formData
                        }
                    },
                    success: (res) => {
                        uni.hideLoading()
                        if (res.result.code === 0) {
                            uni.showModal({
                                title: '提示',
                                content: res.result.msg,
                                showCancel: false,
                                success: (res) => {
                                    if (res.confirm) {
                                        that.logout();
                                        uni.removeStorageSync('uni_id_token')
                                        uni.removeStorageSync('username')
                                        uni.reLaunch({
                                            url: '/pages/login/login'
                                        })
                                    }
                                }
                            });
                        } else {
                            uni.showToast({
                                title: res.result.msg,
                                icon: 'none',
                                duration: 2000
                            })
                        }
                    },
                    fail: (e) => {
                        uni.hideLoading()
                        uni.showModal({
                            content: '修改密码失败',
                            showCancel: false
                        })
                    }
                })
            },
            changePassword: function() {
                this.showPassword = !this.showPassword;
            },
            changePasswordAgain: function() {
                this.showPasswordAgain = !this.showPasswordAgain;
            }
        }
    }
</script>
 
 
<style>
    /* 标题栏 */
    .uni-header {
        padding: 0 15px;
        display: flex;
        height: 55px;
        align-items: center;
        justify-content: space-between;
        border-bottom: 1px #f5f5f5 solid;
    }
 
    .uni-title {
        margin-right: 10px;
        font-size: 16px;
        font-weight: 500;
        color: #333;
    }
 
    .uni-group {
        display: flex;
        align-items: center;
        justify-content: center;
        word-break: keep-all;
    }
 
    /* 容器 */
    .uni-container {
        padding: 15px;
        box-sizing: border-box;
    }
 
    /* 按钮样式 */
    .uni-button-group {
        margin-top: 30px;
        display: flex;
        align-items: center;
        justify-content: center;
    }
 
    .pointer {
        cursor: pointer;
    }
 
    .uni-input-border,
    .uni-textarea-border {
        width: 100%;
        font-size: 14px;
        color: #666;
        border: 1px #e5e5e5 solid;
        border-radius: 5px;
        box-sizing: border-box;
    }
 
    .uni-input-border {
        padding: 0 10px;
        height: 35px;
 
    }
 
    .uni-icon-password-eye {
        position: absolute;
        right: 8px;
        top: 6px;
        font-family: uniicons;
        font-size: 20px;
        font-weight: normal;
        font-style: normal;
        width: 24px;
        height: 24px;
        line-height: 24px;
        color: #999999;
    }
 
    .uni-eye-active {
        color: #007AFF;
    }
 
    .uni-button {
        padding: 10px 20px;
        font-size: 14px;
        border-radius: 4px;
        line-height: 1;
        margin: 0;
        box-sizing: border-box;
        overflow: initial;
    }
 
 
    .uni-button-full {
        width: 100%;
    }
</style>