Liuyi
2024-10-23 9a139cc25da8396c1b257ccf99eb8d2fedfbc94d
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
<script setup>
    import { onMounted, ref } from 'vue'
    import { BASE_URL } from '../../config/baseUrl';
    import { editUserInfoApi,getUserInfo } from '../../api/index.js'
    const isEdit = ref(false)
    const editForm = ref({id:'',userName:'',userPhone:''})
    const userInfo = ref(JSON.parse(uni.getStorageSync('userInfo')))
    // const showHeadImg = ref(BASE_URL + '/upload' + userInfo.value.headImg)
    //修改用户头像
    function changeImg(){
        uni.chooseImage({
            success: async(res) =>{
                 //建立表单,请求参数传表单形式
                 let fileForm = {
                     file1:res.tempFiles,
                     path:'qingyuan'
                 }
                uni.uploadFile({
                    url:BASE_URL+ '/file/upload', 
                    filePath:res.tempFilePaths[0],
                    name: 'file',
                    formData:fileForm,
                    success: async(success) => {
                        console.log('success',success);
                        let dataObj = JSON.parse(success.data)
                        // let headImgStatic =  BASE_URL + '/upload' + dataObj.data.newFileName
                        await editUserInfoApi({id:userInfo.value.id,headImg:dataObj.data.newFileName}).then(async(res) =>{
                            if(res.code == 200){
                                uni.showToast({
                                    title: '修改成功!',
                                    duration: 1000,
                                });
                                await getUserInfo().then((res) =>{
                                    userInfo.value.headImg = res.data.userInfo.headImg
                                    // showHeadImg.value = BASE_URL + '/upload' + userInfo.value.headImg
                                    console.log('resheadImg',userInfo.value.headImg)
                                    
                                    uni.setStorageSync('userInfo',JSON.stringify(userInfo.value))
                                })
                            }else{
                                uni.showToast({
                                    title: '网络错误!请重试',
                                    duration: 1000,
                                    icon:'none'
                                });
                            }
                        })
                    },
                    fail: (err) => {
                        uni.showToast({
                            title: '上传失败,请重试',
                            duration: 2000,
                            icon:'none'
                        });
                        console.log('err',err,res.tempFilePaths[0])
                    }
                });
            }
        });
    }
    //修改用户电话及名称
    function edit(){
        validatePhone.value = true
        validateUserName.value = true
        editForm.value.id = userInfo.value.id
        editForm.value.userName = userInfo.value.userName
        editForm.value.userPhone = userInfo.value.userPhone
        isEdit.value = true
        console.log('userInfo',userInfo.value)
    }
    function cancel(){
        isEdit.value = false
        editForm.vlaue = {id:'',userName:'',userPhone:''}
    }
    //添加校验
    const validateUserName = ref(true)
    function onBlur1(event){
        const nameValue = event.target.value
        const regExp =/^[\u4e00-\u9fa5\\.]+$/
        if(!regExp.test(nameValue)){
            validateUserName.value = false
        }else{
            validateUserName.value = true
        }
    }
    const validatePhone = ref(true)
    function onBlur2(event){
        const nameValue = event.target.value
        const regExp =/^1[3-9]\d{9}$/
        if(!regExp.test(nameValue)){
            validatePhone.value = false
        }else{
            validatePhone.value = true
        }
    }
    function validate(){
        if(validatePhone.value && validateUserName.value){
            return true
        }else{
            return false
        }
    }
    //提交修改表单
    async function submitEdit(){
        if(validate()){
            await editUserInfoApi(editForm.value).then(async(res) =>{
                
                if(res.code == 200){
                    uni.showToast({
                        title: '修改成功!',
                        duration: 2000,
                    });
                    await getUserInfo().then((res) =>{
                        userInfo.value.userName = res.data.userInfo.userName
                        userInfo.value.userPhone = res.data.userInfo.userPhone
                        uni.setStorageSync('userInfo',JSON.stringify(userInfo.value))
                    })
                }else{
                    uni.showToast({
                        title: '网络错误!请重试',
                        duration: 2000,
                        icon:'none'
                    });
                }
                isEdit.value = false
                editForm.vlaue = {id:'',userName:'',userPhone:''}
            })
        }else{
            uni.showToast({
                title:'输入不符合格式',
                icon:'none'
            })
        }
    }
</script>
<template>
    <view class="container">
        <navbar title = '用户资料'></navbar>
        <view class="content">
            <view class="info-box">
                <view class="info-up">
                    <view class="info-img">
                        <text>头像</text>
                        <view class="img-box" @click="changeImg">
                            <image v-if="userInfo.headImg" :src="BASE_URL + '/upload' + userInfo.headImg" alt=''></image>
                            <image v-else src='../../static/images/index/head.png' alt=''></image>
                            <image src='../../static/images/other/more.png' alt=''></image>
                        </view>
                    </view>
                    <view class="info-name">
                        <text>用户名称</text>
                        <view>
                            <view v-if="isEdit">
                                <input v-model="editForm.userName" @blur="onBlur1" placeholder="请输入新用户名"/>
                                <text class="validate-text" v-if="!validateUserName">请输入中文或" . "符号</text>
                            </view>
                            <text v-else class="info-value">{{userInfo.userName}}</text>
                        </view>
                    </view>
                </view>
                <view class="info-list">
                    <view class="item">
                        <text>会员卡号</text>
                        <text v-if="userInfo.waterCardNumber">{{userInfo.waterCardNumber}}</text>
                        <text v-else>未绑定会员卡</text>
                    </view>
                    <view class="item">
                        <text>联系方式</text>
                        <view class="item-change">
                            <view v-if="isEdit">
                                <input v-model="editForm.userPhone" @blur="onBlur2" placeholder="请输入新联系方式"/>
                                <text v-if="!validatePhone">请输入正确的手机号格式</text>
                            </view>
                            <text v-if="userInfo.userPhone && !isEdit">{{userInfo.userPhone}}</text>
                            <text v-if="!userInfo.userPhone && !isEdit">未添加联系方式</text>
                            <!-- <image class="edit"src="../../static/images//other/more.png" alt=''></image> -->
                        </view>
                    </view>
                </view>
            </view>
            <view v-if="!isEdit" @click="edit()" class="subBtn">修改</view>
            <view v-if="isEdit" @click="cancel()" class="btn btn1">取消</view>
            <view v-if="isEdit" @click="submitEdit()" class="btn btn2">确认修改</view>
        </view>
    </view>
</template>
 
<style lang="scss">
    .container{
        width: 100%;
        height: 100vh;
        .content{
            width: 100%;
            height:calc(100vh - 176rpx);
            background:linear-gradient(to top,#FFFFFF,#E8EFFF);  
            padding-top:36rpx;
            position: relative;
            box-sizing: border-box;
            .info-value{
                font-size:32rpx;
                color: #777777;
            }
            // .edit{
            //     margin-left:15rpx;
            //     width: 20rpx;
            //     height:28rpx;
            // }
            .info-box{
                width: 100%;
                //该页面公用input样式
                input{
                    border-radius:5rpx;
                    border-bottom: 1px solid rgba(83, 131, 203, 0.5);
                    // border:1px solid rgba(101, 156, 244, 0.2);
                    padding:5rpx 10rpx;
                    // box-shadow: 0 0 12rpx 1rpx rgba(101, 156, 244, 0.2);
                    color: #777777;
                    height:50rpx;
                    line-height:50rpx;
                    width:300rpx;
                }
                .info-up{
                    height:308rpx;
                    width:100%;
                    background: #FFFFFF;
                    padding:32rpx;
                    display: flex;
                    justify-content:space-between;
                    align-items:center;
                    flex-direction: column;
                    box-sizing: border-box;
                    font-size: 32rpx;
                    color: #343434;
                    .info-img{
                        width:100%;
                        display: flex;
                        justify-content:space-between;
                        align-items:center;
                        .img-box{
                            display: flex;
                            align-items: center;
                            image:first-child{
                                width:100rpx;
                                height:100rpx;
                                border-radius: 50rpx;
                                margin-right:10rpx;
                            }
                            image:last-child{
                                width:30rpx;
                                height:36rpx;
                                border-radius: 50rpx;
                            }
                        }
                    }
                    .info-name{
                        width:100%;
                        display: flex;
                        justify-content:space-between;
                        align-items:center;
                        .validate-text{
                            color:#f56c6c;
                            font-size:24rpx;
                        }
                    }
                }
                .info-list{
                    width:100%;
                    height:252rpx;
                    display: flex;
                    flex-direction:column;
                    justify-content:space-between;
                    align-items: center;
                    margin-top:20rpx;
                    .item{
                        width:100%;
                        height: 116rpx;
                        background-color: #FFFFFF;
                        display: flex;
                        justify-content: space-between;
                        align-items: center;
                        padding:0 32rpx;
                        box-sizing: border-box;
                        text:last-child{
                            color: #777777;
                        }
                        .item-change{
                            height: 116rpx;
                            display: flex;
                            justify-content:flex-end;
                            align-items: center;
                            view{
                                display: flex;
                                flex-direction: column;
                                text{
                                    color:#f56c6c;
                                    font-size:24rpx;
                                }
                            }
                            text{
                                color: #777777;
                            }
                        }
                    }
                }
            }
            .subBtn{
                width: 686rpx;
                height: 98rpx;
                background:#5EA1FA;
                border-radius:50rpx;
                text-align: center;
                line-height: 98rpx;
                letter-spacing:3rpx;
                font-weight: 300;
                font-size: 36rpx;
                color: #FFFFFF;
                position: absolute;
                bottom:100rpx;
                left:32rpx;
            }
            .btn{
                    width: 200rpx;
                    height: 70rpx;
                    border-radius: 32rpx;
                    text-align: center;
                    line-height: 70rpx;
                    letter-spacing:3rpx;
                    font-weight: 300;
                    font-size: 36rpx;
                    position: absolute;
                    bottom:100rpx;
            }
            .btn1{
                color:#5EA1FA;
                background:#FFFFFF;
                position: absolute;
                left:130rpx;
                border:2rpx solid #5EA1FA;
            }
            .btn2{
                color: #FFFFFF;
                background:#5EA1FA;
                position: absolute;
                right:130rpx;
            }
        }
    }
</style>