根據(jù)微信調(diào)整
getUserProfile 接口、getUserInfo 接口在最新的小程序基礎(chǔ)庫 已經(jīng)不能獲取到用戶信息
官方推薦用頭像填寫能力
實現(xiàn)了一版 代碼如下 做個記錄 方便下次找到文章來源地址http://www.zghlxwxcb.cn/news/detail-505712.html
<template>
<view class="containar">
<view class="avatarUrl">
<button type="balanced" open-type="chooseAvatar" @chooseavatar="onChooseavatar">
<image :src="avatarUrl" class="refreshIcon"></image>
</button>
</view>
<view class="userName">
<text>昵稱:</text>
<input type="nickName" class="weui-input" :value="userName" @blur="bindblur" placeholder="請輸入昵稱"
@input="bindinput" />
</view>
<view class="btn">
<view class="btn-sub" @click="onSubmit">保存</view>
</view>
</view>
</template>
<script>
export default {
data() {
return {
avatarUrl: '',
userName: ''
};
},
onLoad(option) {},
methods: {
bindblur(e) {
this.userName = e.detail.value; // 獲取微信昵稱
},
bindinput(e) {
this.userName = e.detail.value; // 獲取微信昵稱
},
onChooseavatar(e) {
let self = this;
let {
avatarUrl
} = e.detail;
this.avatarUrl = avatarUrl
},
onSubmit() {
var that = this
uni.login({
"provider": "weixin",
// "onlyAuthorize": true, // 微信登錄僅請求授權(quán)認證
success: function(event) {
const {
code
} = event
const name = that.userName
// 判斷頭像以及昵稱不為空 再上傳
if (that.userName && that.avatarUrl) {
uni.uploadFile({
url:"",//請求路徑
filePath: that.avatarUrl,
fileType: "image",
name: 'img',
success: uploadFileRes => {
let data=JSON.parse(uploadFileRes.data);
//將個人信息保存到全局
that.$store.dispatch("user/saveUserInfo",data.data);
// 跳轉(zhuǎn)到我的界面
},
fail: (error) => {
uni.showToast({
title: "保存錯誤",
});
}
});
} else {
uni.showToast({
icon: 'none',
title: '請上傳頭像并填寫昵稱'
})
return false;
}
},
fail: function(err) {
// 登錄授權(quán)失敗
// err.code是錯誤碼
}
})
}
}
}
</script>
<style lang="scss" scoped>
.containar {
.avatarUrl {
padding: 80rpx 0 40rpx;
background: #fff;
button {
background: #fff;
line-height: 80rpx;
height: auto;
border: none !important;
width: auto;
// padding: 20rpx 30rpx;
margin: 0;
display: flex;
border: none;
justify-content: center;
align-items: center;
&::after {
border: none;
}
.refreshIcon {
width: 160rpx;
height: 160rpx;
border-radius: 50%;
background-color: #ccc;
}
.jt {
width: 14rpx;
height: 28rpx;
}
}
}
.userName {
background: #fff;
padding: 20rpx 30rpx 80rpx;
display: flex;
align-items: center;
justify-content: center;
.weui-input {
padding-left: 60rpx;
}
}
.btn {
width: 100%;
.btn-sub {
width: 670rpx;
margin: 80rpx auto 0;
height: 90rpx;
background: #DF8585;
border-radius: 45rpx;
line-height: 90rpx;
text-align: center;
font-size: 36rpx;
color: #fff;
}
}
}
</style>
文章來源:http://www.zghlxwxcb.cn/news/detail-505712.html
到了這里,關(guān)于uniapp微信小程序獲取用戶名和頭像最新方式(頭像填寫)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!