场景

  • 微信小程序,获取不到用户信息,也不弹窗
  • 新版如何获取用户手机号
  • wx.getUserProfile获取不到信息,不弹窗

解决

昵称、性别、头像、都是可以自定义的,所以是不是从微信获取的意义不大。

先wx.login进行登录, 之后可在可在个人中心或设置等页面使用头像昵称填写能力让用户完善个人资料。

其次,如果需要绑定手机号,可以根据标识,后续提示引导用户需要绑定手机号。或者点击按钮微信授权手机号【需要马内】:https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/getPhoneNumber.html


其他参考

uniapp登录: https://uniapp.dcloud.net.cn/api/plugins/login.html

【不推荐】uniapp一键登录,获取手机号+ unicloud云函数,需要马内: https://doc.dcloud.net.cn/uniCloud/uni-login/price.html

新版>2.27.1和新发布小程序,wx.将不弹窗,也获取不到用户信息了,而且获取手机号要马内。详见: https://developers.weixin.qq.com/community/develop/doc/00022c683e8a80b29bed2142b56c01 但是可以通过wx.login直接获取到openid, 微信小程序获取头像填写,昵称填写 https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/userProfile.html

从一开始的getUserInfo改成getUserProfile,再改到直接不能获取信息了,那这个接口还有什么用,根本就没调用必要,直接wx.login拿个code登陆就完事了

新版uniapp微信小程序授权登录,获取用户信息手机号_html

新版uniapp微信小程序授权登录,获取用户信息手机号_微信_02

微信获取手机号,已经收费了,详见下面👇: https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/getPhoneNumber.html

旧版本微信获取手机号,需要先wx.login: https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/deprecatedGetPhoneNumber.html

推荐新版本,无需wx.login:

uniapp示例代码

<template>
	<view class="content">
		<button @click="login()">微信登录</button>
		<button type="default" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">获取手机号</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				title: '塔滋米',
				sessionKey: ''
			}
		},
		onLoad() {

		},
		methods: {
			getPhoneNumber(e) {
				const accountInfo = uni.getAccountInfoSync();
				const appid = accountInfo.miniProgram.appId;
				console.log(e.detail.code) // 动态令牌
				uni.request({
					url: 'http://172.20.10.4:9993/getWxMpPhone',
					method: 'POST',
					dataType: 'json',
					data: {
						code: e.detail.code,
						appid: appid
					},
					success(res) {
						console.log("phone", res.data.data)
					}
				})
			},
			async login() {
				let that = this
				const accountInfo = uni.getAccountInfoSync();
				const appid = accountInfo.miniProgram.appId;
				await uni.login({
					onlyAuthorize: true,
					success: function(response) { // 用微信登录的话就要去微信开发工具
						console.log(response) //这里打印就说明接口调用成功了,然后看message login :ok
						//微信登录就完了,后面就是获取用户信息
						uni.request({
							url: 'http://172.20.10.4:9993/xcxLogin',
							method: 'POST',
							dataType: 'json',
							data: {
								code: response.code,
								appid: appid
							},
							success(res) {
								console.log("sessionkey", JSON.parse(res.data.data.token)
									.sessionKey)
								that.sessionKey = JSON.parse(res.data.data.token).sessionKey
							}
						})

					}
				})
				// 此步骤,调不调用,没意义了
				await uni.getUserProfile({
					desc: '测试用例',
					success: function(res) {
						console.log("getUserProfile", res)
						uni.request({
							url: 'http://172.20.10.4:9993/getWxMpUserInfo',
							method: 'POST',
							dataType: 'json',
							data: {
								rawData: res.rawData,
								signature: res.signature,
								encryptedData: res.encryptedData,
								iv: res.iv,
								sessionKey: that.sessionKey,
								appid: appid
							},
							success(resc) {
								console.log("登录成功", resc)
							}
						})
					}
				})

			}
		}
	}
</script>

<style>

</style>