uniapp getUserProfile 使用方法
为什么要使用 uni.getUserProfile
使用 uniapp 开发微信小程序时之前用的是 uni.getUserInfo,现在由于微信方面接口调整
(https://developers.weixin.qq.com/community/develop/doc/000cacfa20ce88df04cb468bc52801)
现已改为 getUserProfile; 原来的 getUserInfo 接口可获取用户个人信息能力被回收, 还能返回数据但很
多关键数据是空的, 提示授权的弹窗更是没有。所以要使用新的 getUserProfile 接口。
uni.getUserProfile 使用示例
<template>
<view class="login-container">
<button class="btn-login" type="primary" @click="getUserInfo">一键登录</button>
</view>
</template>
<script>
export default {
name: 'my-login',
methods: {
getUserInfo(e) {
uni.getUserProfile({
lang: 'zh_CN',
desc: '用来授权登录该小程序!',
success: (userInfo) => {
uni.$showMsg('登录成功!')
},
fail: () => {
uni.$showMsg('登录失败!')
},
complete: () => {
},
})
},
},
}
</script>
具体步骤
- 可以在一个 button 里绑定一个点击事件;
- 在点击事件处理函数中 调用 uni.getUserProfile
- 在配置对象中添加需要的属性
参考文档
- 微信小程序官方文档
- uniapp 官方文档