+
80
-

uniapp如何获取微信和抖音小程序的用户图像及昵称?

uniapp如何获取微信和抖音小程序的用户图像及昵称?


网友回复

+
0
-

1、微信小程序之前的getuserprofile已经不能使用,微信官方的解释是:

800_auto

现在只能使用新方案:

获取微信图像

<button type="balanced" open-type="chooseAvatar" @chooseavatar="onChooseavatar">点击获取图像</button>

获取昵称

<input type="nickName" class="weui-input" :value="userName" @blur="bindblur" placeholder="请输入昵称" @input="bindinput" />

新的获取微信图像与昵称示例代码:

<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, // 微信登录仅请求授权认证
					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);
									// 跳转到我的界面
								},
								fail: (error) => {
									uni.showToast({
										title: "保存错误",
									});
								}
							});
						} else {
							uni.showToast({
								icon: 'none',
								title: '请上传头像并填写昵称'
							})
							return false;
						}
					},
					fail: function(err) {
						// 登录授权失败  
						// 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>

2、抖音小程序获取参考官方文档:https://developer.open-douyin.com/docs/resource/zh-CN/dop/develop/openapi/account-management/get-account-open-info

+
0
-

抖音小程序还可以使用getUserProfile

  tt.getUserProfile({
      success: (res) => {
        console.log('tt.getUserProfile success,获取的用户信息:', res);

        this.setData({
          userInfo: res.userInfo,
        });
      },

      fail(err) {
        console.log('tt.getUserProfile failed', err.errMsg);

        tt.showModal({
          title: '获取用户信息失败',
          content: JSON.stringify(err),
          showCancel: false,
        });
      },

      complete() {
        console.log('tt.getUserProfile completed');
      },
    });

我知道答案,我要回答