您好,小程序模板欢迎您。
小程序模板
当前位置 : 首页> 小程序教程> 小程序vue2和vue3获取微信头像和微信昵称的方法

小程序vue2和vue3获取微信头像和微信昵称的方法

vue2

通过使用"uview-ui": "^1.8.8"版本弹窗更改,open-type="chooseAvatar"一定要写


            
                基本信息
                
                    
                        
                        
                            昵称
                            {{ vuex_user.nickName }}
                        

                    

                

            

        

更改头像的方法,先上传到服务器,获取在服务器的路径,之后获取到更新头像,再更新用户个人资料存到vuex

    chooseAvatar(e) {
                const avatarUrl = e.detail.avatarUrl;
                uni.uploadFile({
                    //请求的url接口地址
                    url: this.$http._config.url + "/common/upload",
                    fileType: "image", //图片类型
                    filePath: avatarUrl, //哪张图片
                    name: "file", //对应接口的文件名
                    success: (res) => {
                        //成功的回调
                        const fileName = JSON.parse(res.data).fileName;
                        let image = this.$http._config.url + fileName
                        this.$http.PUT("/system/user/updateUserAvatorNew", {
                            userId: uni.getStorageSync("vuex_user").userId,
                            avatar: image
                        }).then(res => {
                            uni.showToast({
                                title: "上传成功"
                            })
                            this.$http.GET("/system/user/profile").then(data => {
                                this.$u.vuex("vuex_user", data.data);
                            })
                        }).catch(res => {})
                    },
                    fail: (err) => {
                        //失败的回调
                        //  console.log(err)
                    }
                })
            },

获取昵称type="nickname"


            
                修改昵称
                
                    
                        
                                                             placeholder="请输入新的昵称" />
                        

                        确认修改
                    

                
            
        

    changeNickname(e) {
                this.nickName = e.detail.value;
                // this.$set(this, "personName", e.detail.value);
            },

submitNicknameForm() {
                console.log(this.nickName)
                if (this.nickName.length < 1) {
                    uni.showToast({
                        title: "请输入新昵称",
                        icon: "none"
                    })
                    return;
                }
                this.$http.PUT("/system/user/updateUserNew", {
                    nickName: this.nickName,
                    userId: uni.getStorageSync("vuex_user").userId,
                }).then((res => {
                    this.$u.toast(res.msg);
                    this.$http.GET("/system/user/profile").then(data => {
                        this.$u.vuex("vuex_user", data.data);
                        this.updateNicknamePop = false;
                    })
                }))
            }

vue3

联系客服 意见反馈

签到成功!

已连续签到1天,签到3天将获得积分VIP1天

知道了