web
2025-07-03 8e5fb21a2bf621291c1ceee396c01e3d42553511
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
<template>
    <view class="setting">
        <Navbar title="用户信息"></Navbar>
        <view class="user">
            <view class="userImg" @click="chooseImg">
                <view class="t">用户头像</view>
                <button class="cell-wrapper" open-type="chooseAvatar" @chooseavatar="chooseImg">
                    <image class="head" :src="userForm.headImg" mode="widthFix"></image>
                </button>
            </view>
            <view class="userName">
                <view class="t">用户昵称</view>
                <input class="input" type="nickname" v-model="userForm.nickName" placeholder="请输入昵称" />
            </view>
            <button class="btn" @click="savaUser">保存</button>
        </view>
    </view>
</template>
 
<script setup>
    import {ref, onMounted} from 'vue'
    import Navbar from '../../components/navbar/navbar.vue'
    import {setUserInfo, getUserInfo} from '@/api/index.js'
    
    const userForm = ref({
        headImg: '',
        nickName: ''
    })
    
    const chooseImg = (e) => {
        if(e.detail.avatarUrl){
            userForm.value.headImg = e.detail.avatarUrl
        }
    }
    
    // 获取用户信息存缓存
    const getUser = () => {
        getUserInfo().then(res => {
            if(res.code == 200){
                uni.setStorageSync('userInfo',JSON.stringify(res.data))
            }
        })
    }
    
    const savaUser = () => {
        setUserInfo(userForm.value).then(res => {
            if(res.code === 200) {
                getUser()
                uni.showToast({
                    title:'修改成功',
                    icon: 'success'
                })
                setTimeout(() => {
                    uni.navigateBack()
                }, 1000)
            }
        })
    }
    
    onMounted(() => {
        const userInfo = JSON.parse(uni.getStorageSync('userInfo'))
        userForm.value = {
            id: userInfo.id,
            headImg: userInfo.headImg,
            nickName: userInfo.nickName
        }
    })
</script>
 
<style scoped lang="scss">
    .user{
        padding: 40rpx;
        box-sizing: border-box;
        .userImg{
            width: 100%;
            padding: 10rpx 20rpx;
            box-sizing: border-box;
            border-radius: 20rpx;
            background-color: #fff;
            display: flex;
            justify-content: space-between;
            align-items: center;
            .cell-wrapper{
                width: 80rpx;
                height: 80rpx;
                border-radius: 50%;
                border: 0;
                margin: 0;
                background-color: #ddd;
            }
            .head{
                width: 80rpx;
                height: 80rpx;
                border-radius: 50%;
                margin-left: -30rpx;
            }
        }
        .userName{
            margin-top: 40rpx;
            width: 100%;
            padding: 30rpx 20rpx;
            box-sizing: border-box;
            border-radius: 20rpx;
            background-color: #fff;
            display: flex;
            justify-content: space-between;
            align-items: center;
            .input{
                width: 200rpx;
                text-align: right;
            }
        }
        .btn{
            margin-top: 100rpx;
            background-color: $uni-primary;
            border-color: $uni-primary;
            color: #fff;
        }
    }
</style>