uniapp获取微信手机号码

本文介绍了如何在uniapp中集成微信登录功能,详细讲解了获取微信授权并获取用户手机号码的步骤,包括配置微信开发者工具、调用相应API及处理回调等关键环节。
<template>
	<view>
		<!-- bindgetphonenumber -->
		<button type="primary" open-type="getUserInfo" @click="getuserinfo" withCredentials="true">微信登录</button>
		<button open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">获取电话号码</button>

	</view>
</template>

<script>
	export default {
		data() {
			return {

			};
		},
		onLoad: function() {
			uni.login({
				success: function(res) {
					// 获取code
					// console.log(JSON.stringify(res));
				}
			});
		},

		methods: {
			getPhoneNumber: function(e) {
				console.log(e);
				if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {

				} else {

				}

				// 				console.log(JSON.stringify(e.encryptedData));
				// 				console.log(JSON.stringify(e.iv));
			},
			getUserInfo: function(loginType, cb) {
				var that = this
				if (this.globalData.userInfo) {
					typeof cb == "function" && cb(this.globalData.userInfo, true);
				} else {
					//1.调用登录接口
					wx.login({
						success: function() {
							wx.getUserInfo({
								success: function(res) {
									that.globalData.userInfo = res.userInfo;
									typeof cb == "function" && cb(that.globalData.userInfo, true);
								},
								fail: function() {
									//2.第一次登陆不强制授权,直接返回
									if (loginType == 0) {
										typeof cb == "function" && cb(that.globalData.userInfo, false);
									} else {
										//3.授权友好提示
										wx.showModal({
											title: '提示',
											content: "您还未授权登陆,部分功能将不能使用,是否重新授权?",
											showCancel: true,
											cancelText: "否",
											confirmText: "是",
											success: function(res) {
												//4.确认授权调用wx.openSetting
												if (res.confirm) {
													if (wx.openSetting) { //当前微信的版本 ,是否支持openSetting
														wx.openSetting({
															success: (res) => {
																if (res.authSetting["scope.userInfo"]) { //如果用户重新同意了授权登录
																	wx.getUserInfo({
																		success: function(res) {
																			that.globalData.userInfo = res.userInfo;
																			typeof cb == "function" && cb(that.globalData.userInfo, true);
																		}
																	})
																} else { //用户还是拒绝
																	typeof cb == "function" && cb(that.globalData.userInfo, false);
																}
															},
															fail: function() { //调用失败,授权登录不成功
																typeof cb == "function" && cb(that.globalData.userInfo, false);
															}
														})
													} else {
														typeof cb == "function" && cb(that.globalData.userInfo, false);
													}
												} else {
													typeof cb == "function" && cb(that.globalData.userInfo, false);
												}
											}
										})
									}
								}
							})
						}
					})
				}
			}
		}
	}
</script>

<style>

</style>

 

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值