微信小程序获取用户信息更新解决方案

微信小程序获取用户头像和昵称更新了,以下代码是兼容旧版本微信和新版本微信的解决方案,仅供参考

直接上代码

 

<template>
	<!-- 顶部用户信息显示 -->
	<view class="UserInfo_main">
		<div class="headArea" v-if="isLogin">
			<div class="nameArea" @click="goDimesion">
				<span class="name">{{userInfo.nickname}}</span>
				<span class="coin">{{userInfo.gold||0}}</span>
				<div class="headImgBg">
					<div class="headImgBgWit">
						<image :src="userInfo.avatar" class="headimg">
						</image>
						<!-- <div class="redPoint" v-if="userInfo.rank_level.level">
							<div class="redPointNum">{{userInfo.rank_level.level}}</div>
						</div> -->
					</div>
				</div>
				<div class="levelArea">
					<image :src="imgLevelBg" class="levelAreaBg">
						<span class="levelNum">{{userInfo.rank_level&&userInfo.rank_level.level||1}}</span>
					</image>
				</div>
			</div>
		</div>

		<!-- <div class="headArea"> -->
		<div class="headArea" v-if="!isLogin">
			<div class="nameArea">
				<span class="nameNotLogin flexdis">
					<button class="goLogin" lang="zh_CN" v-if="canIUseGetUserProfile"
						@click="getUserProfile">点击授权登录</button>
					<button class="goLogin" v-if="!canIUseGetUserProfile" open-type="getUserInfo" lang="zh_CN"
						@getuserinfo="onGotUserInfo">点击授权登录</button>
					<image :src="arrow" v-if="showArrow" class="arrowImg">
					</image>
				</span>
				<div class="headImgBg">
					<div class="headImgBgWit">
						<image :src="defaultHeadImg" class="headimg">
						</image>
					</div>
				</div>
			</div>
			<!-- <div class="nameArea">
		  <div class="headImgBg">
		    <div class="headImgBgWit">
		    </div>
		  </div>
		</div>
		<button class="getUserInfoButton" open-type="getUserInfo" lang="zh_CN"  @getuserinfo="onGotUserInfo">点击登录</button> -->
		</div>

	</view>
</template>

<script>
	function compareVersion(v1, v2) {
		v1 = v1.split('.')
		v2 = v2.split('.')
		const len = Math.max(v1.length, v2.length)

		while (v1.length < len) {
			v1.push('0')
		}
		while (v2.length < len) {
			v2.push('0')
		}

		for (let i = 0; i < len; i++) {
			const num1 = parseInt(v1[i])
			const num2 = parseInt(v2[i])

			if (num1 > num2) {
				return 1
			} else if (num1 < num2) {
				return -1
			}
		}

		return 0
	}
	export default {
		props: ['rankInfo', 'showArrow'],
		data() {
			return {
				canIUseGetUserProfile: false,
				isLogin: false,
				userInfo: {},
				imgLevelBg: this.$config.gameImagesUrl + '/imgLevelBg.png',
				arrow: this.$config.gameImagesUrl + '/arrow.png',
				defaultHeadImg: this.$config.gameImagesUrl + '/defaultHeadImg.jpg',
			}
		},
		asynconHide() {},
		async mounted() {
			this.$common.Init.call(this);
			const version = wx.getSystemInfoSync().SDKVersion

			if (compareVersion(version, '2.16.0') >= 0) {
				this.canIUseGetUserProfile = true;
			}
			// if (wx.getUserProfile) this.canIUseGetUserProfile = true;
			console.log('微信基础库版本号',version,',是否兼容wx.getUserProfile', this.canIUseGetUserProfile)

			this.userInfo = await this.getUserInfo()
			this.isLogin = uni.getStorageSync('userInfo') ? true : false
		},
		methods: {
			goDimesion() {
				this.$navTo.navigateTo({
					url: '/pages/dimesion/dimesion'
				})
			},
			getUserProfile() {
				wx.getUserProfile({
					desc: '用于完善会员资料', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
					success: res => {
						console.log('getUserProfile-res', res)
						this.to_login(res)
					},
					fail: err => {
						console.log('err', err)
					}
				})
			},
			// 用户信息登录上传信息给接口
			to_login(data) {
				console.log('用户信息登录上传信息给接口1', data)
				data.userToken = wx.getStorageSync('userToken');
				this.$request(this.$api.user.wechatXcxSetUserData, data).then(async (res) => {
					console.log('登录返回', JSON.stringify(res))
					if (res.code == 0) {
						this.userInfo = await this.getUserInfo()
						this.isLogin = true
					}
				})
			},
			onGotUserInfo(e) {
				var errMsg = e.detail.errMsg
				if (errMsg === 'getUserInfo:ok') {
					var detail = e.detail
					console.log('旧版本-detail', detail)
				    this.to_login(detail)
				}
			},
		},
	}
</script>

<style lang="scss" scoped>
	
</style>

<p style="color:#2F2F2F;"> 老规矩,先看本节效果图 </p> <div style="text-align:center;color:#2F2F2F;"> <div style="background-color:transparent;"> <div> </div> <div> <img src="https://upload-images.jianshu.io/upload_images/6273713-fce2f4ffa8f92d99.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/418/format/webp" alt="" /></div> </div> </div> <br /><p style="color:#2F2F2F;"> 我们实现这个支付功能完全是借助小程序云开发实现的,不用搭建自己的服务器,不用买域名,不用备案域名,不用支持https。只需要一个简单的云函数,就可以轻松的实现微信小程序支付功能。<br /> 核心代码就下面这些 </p> <br /><div style="text-align:center;color:#2F2F2F;"> <div style="background-color:transparent;"> <div> </div> <div> <img src="https://upload-images.jianshu.io/upload_images/6273713-7433fba3b792bb28.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/922/format/webp" alt="" /></div> </div> </div> 一,创建一个云开发小程序 <p style="color:#2F2F2F;"> 关于如何创建云开发小程序,这里我就不再做具体讲解。不知道怎么创建云开发小程序的同学,可以去翻看我之前的文章,或者看下我录制的视频:<a href="https://links.jianshu.com/go?to=https%3A%2F%2Fedu.csdn.net%2Fcourse%2Fplay%2F9604%2F204528">https://edu.csdn.net/course/play/9604/204528</a> </p> 创建云开发小程序有几点注意的 <p style="color:#2F2F2F;"> 1,一定不要忘记在app.js里初始化云开发环境。 </p> <br /><div style="text-align:center;color:#2F2F2F;"> <div style="background-color:transparent;"> <div> </div> <div> <img src="https://upload-images.jianshu.io/upload_images/6273713-c436567c3368ac74.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp" alt="" /></div> </div> </div> <br /><p style="color:#2F2F2F;"> 2,创建完云函数后,一定要记得上传 </p> 二, 创建支付的云函数 <p style="color:#2F2F2F;"> 1,创建云函数pay </p> <br /><div style="text-align:center;color:#2F2F2F;"> <div style="background-color:transparent;"> <div> </div> <div> <img src="https://upload-images.jianshu.io/upload_images/6273713-32302ade305b8a18.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/564/format/webp" alt="" /></div> </div> </div> <div style="text-align:center;color:#2F2F2F;"> <div style="background-color:transparent;"> <div> </div> <div> <img src="https://upload-images.jianshu.io/upload_images/6273713-8ea47ffa0b4cffca.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/708/format/webp" alt="" /></div> </div> </div> 三,引入三方依赖tenpay <p style="color:#2F2F2F;"> 我们这里引入三方依赖的目的,是创建我们支付时需要的一些参数。我们安装依赖是使用里npm 而npm必须安装node,关于如何安装node,我这里不做讲解,百度一下,网上一大堆。 </p> 1,首先右键pay,然后选择在终端中打开 <div style="text-align:center;color:#2F2F2F;"> <div style="background-color:transparent;"> <div> </div> <div> <img src="https://upload-images.jianshu.io/upload_images/6273713-8881030499ebe5ce.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp" alt="" /></div> </div> </div> 2,我们使用npm来安装这个依赖。 <p style="color:#2F2F2F;"> 在命令行里执行 npm i tenpay </p> <br /><div style="text-align:center;color:#2F2F2F;"> <div style="background-color:transparent;"> <div> </div> <div> <img src="https://upload-images.jianshu.io/upload_images/6273713-c61cb1cb5880c475.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/684/format/webp" alt="" /></div> </div> </div> <br /><div style="text-align:center;color:#2F2F2F;"> <div style="background-color:transparent;"> <div> </div> <div> <img src="https://upload-images.jianshu.io/upload_images/6273713-cd34c63e39e6427f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp" alt="" /></div> </div> </div> <br /><div style="text-align:center;color:#2F2F2F;"> <div style="background-color:transparent;"> <div> </div> <div> <img src="https://upload-images.jianshu.io/upload_images/6273713-768712337485bf67.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp" alt="" /></div> </div> </div> <br /><p style="color:#2F2F2F;"> 安装完成后,我们的pay云函数会多出一个package.json 文件 </p> <br /><div style="text-align:center;color:#2F2F2F;"> <div style="background-color:transparent;"> <div> </div> <div> <img src="https://upload-images.jianshu.io/upload_images/6273713-7e9236d8983ebb21.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/682/format/webp" alt="" /></div> </div> </div> <br /><span style="color:#2F2F2F;">到这里我们的tenpay依赖就安装好了。</span> 四,编写云函数pay <div style="text-align:center;color:#2F2F2F;"> <div style="background-color:transparent;"> <div> </div> <div> <img src="https://upload-images.jianshu.io/upload_images/6273713-cd36f9084fada492.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp" alt="" /></div> </div> </div> <br /><p style="color:#2F2F2F;"> 完整代码如下 </p> <span style="color:#929292;">//云开发实现支付</span> <span style="color:#C678DD;">const</span> cloud = <span style="color:#E6C07B;">require</span>(<span style="color:#98C379;">'wx-server-sdk'</span>) cloud.init() <span style="color:#929292;">//1,引入支付的三方依赖</span> <span style="color:#C678DD;">const</span> tenpay = <span style="color:#E6C07B;">require</span>(<span style="color:#98C379;">'tenpay'</span>); <span style="color:#929292;">//2,配置支付信息</span> <span style="color:#C678DD;">const</span> config = { <span style="color:#D19A66;">appid</span>: <span style="color:#98C379;">'你的小程序appid'</span>, <span style="color:#D19A66;">mchid</span>: <span style="color:#98C379;">'你的微信商户号'</span>, <span style="color:#D19A66;">partnerKey</span>: <span style="color:#98C379;">'微信支付安全密钥'</span>, <span style="color:#D19A66;">notify_url</span>: <span style="color:#98C379;">'支付回调网址,这里可以先随意填一个网址'</span>, <span style="color:#D19A66;">spbill_create_ip</span>: <span style="color:#98C379;">'127.0.0.1'</span> <span style="color:#929292;">//这里填这个就可以</span> }; exports.main = <span style="color:#C678DD;">async</span>(event, context) => { <span style="color:#C678DD;">const</span> wxContext = cloud.getWXContext() <span style="color:#C678DD;">let</span> { orderid, money } = event; <span style="color:#929292;">//3,初始化支付</span> <span style="color:#C678DD;">const</span> api = tenpay.init(config); <span style="color:#C678DD;">let</span> result = <span style="color:#C678DD;">await</span> api.getPayParams({ <span style="color:#D19A66;">out_trade_no</span>: orderid, <span style="color:#D19A66;">body</span>: <span style="color:#98C379;">'商品简单描述'</span>, <span style="color:#D19A66;">total_fee</span>: money, <span style="color:#929292;">//订单金额(分),</span> openid: wxContext.OPENID <span style="color:#929292;">//付款用户的openid</span> }); <span style="color:#C678DD;">return</span> result; } 一定要注意把appid,mchid,partnerKey换成你自己的。 <p style="color:#2F2F2F;"> 到这里我们获取小程序支付所需参数的云函数代码就编写完成了。<br /> 不要忘记上传这个云函数。 </p> <br /><div style="text-align:center;color:#2F2F2F;"> <div style="background-color:transparent;"> <div> </div> <div> <img src="https://upload-images.jianshu.io/upload_images/6273713-ba99ca6fe33401ec.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/992/format/webp" alt="" /></div> </div> </div> <br /><p style="color:#2F2F2F;"> 出现下图就代表上传成功 </p> <br /><div style="text-align:center;color:#2F2F2F;"> <div style="background-color:transparent;"> <div> </div> <div> <img src="https://upload-images.jianshu.io/upload_images/6273713-6133d61bc300dac4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/626/format/webp" alt="" /></div> </div> </div> 五,写一个简单的页面,用来提交订单,调用pay云函数。 <div style="text-align:center;color:#2F2F2F;"> <div style="background-color:transparent;"> <div> </div> <div> <img src="https://upload-images.jianshu.io/upload_images/6273713-ee974aecada48f7c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp" alt="" /></div> </div> </div> <br /><p style="color:#2F2F2F;"> 这个页面很简单,<br /> 1,自己随便编写一个订单号(这个订单号要大于6位)<br /> 2,自己随便填写一个订单价(单位是分)<br /> 3,点击按钮,调用pay云函数。获取支付所需参数。 </p> <p style="color:#2F2F2F;"> 下图是官方支付api所需要的一些必须参数。 </p> <br /><div style="text-align:center;color:#2F2F2F;"> <div style="background-color:transparent;"> <div> </div> <div> <img src="https://upload-images.jianshu.io/upload_images/6273713-2708b7475409199b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp" alt="" /></div> </div> </div> <br /><p style="color:#2F2F2F;"> 下图是我们调用pay云函数获取的参数,和上图所需要的是不是一样。 </p> <br /><div style="text-align:center;color:#2F2F2F;"> <div style="background-color:transparent;"> <div> </div> <div> <img src="https://upload-images.jianshu.io/upload_images/6273713-d94c566dd744f128.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp" alt="" /></div> </div> </div> 六,调用wx.requestPayment实现支付 <p style="color:#2F2F2F;"> 下图是官方的示例代码 </p> <br /><div style="text-align:center;color:#2F2F2F;"> <div style="background-color:transparent;"> <div> </div> <div> <img src="https://upload-images.jianshu.io/upload_images/6273713-00e9315590e4e14c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp" alt="" /></div> </div> </div> <br /><p style="color:#2F2F2F;"> 这里不在做具体讲解了,完整的可以看视频。 </p> 实现效果 1,调起支付键盘 <div style="text-align:center;color:#2F2F2F;"> <div style="background-color:transparent;"> <div> </div> <div> <img src="https://upload-images.jianshu.io/upload_images/6273713-b20becb49e6fd26e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/234/format/webp" alt="" /></div> </div> </div> 2,支付完成 <div style="text-align:center;color:#2F2F2F;"> <div style="background-color:transparent;"> <div> </div> <div> <img src="https://upload-images.jianshu.io/upload_images/6273713-b2a8266fdc83edc3.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/270/format/webp" alt="" /></div> </div> </div> 3,log日志,可以看出不同支付状态的回调 <div style="text-align:center;color:#2F2F2F;"> <div style="background-color:transparent;"> <div> </div> <div> <img src="https://upload-images.jianshu.io/upload_images/6273713-3a1fca73b650742e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp" alt="" /></div> </div> </div> <br /><p style="color:#2F2F2F;"> 上图是支付成功的回调,我们可以在支付成功回调时,改变订单支付状态。 </p> <p style="color:#2F2F2F;"> 下图是支付失败的回调, </p> <br /><div style="text-align:center;color:#2F2F2F;"> <div style="background-color:transparent;"> <div> </div> <div> <img src="https://upload-images.jianshu.io/upload_images/6273713-1b306a9b35b292e0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp" alt="" /></div> </div> </div> <p style="color:#2F2F2F;"> 下图是支付完成的状态。 </p> <br /><div style="text-align:center;color:#2F2F2F;"> <div style="background-color:transparent;"> <div> </div> <div> <img src="https://upload-images.jianshu.io/upload_images/6273713-906f64407be62c4c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp" alt="" /></div> </div> </div> <p style="color:#2F2F2F;"> 到这里我们就轻松的实现了微信小程序的支付功能了。是不是很简单啊,完整的讲解可以看视频。 </p>
相关推荐
<p> <b><span style="background-color:#FFE500;">【超实用课程内容】</span></b> </p> <p> <br /> </p> <p> <br /> </p> <p> 本课程内容包含讲解<span>解读Nginx的基础知识,</span><span>解读Nginx的核心知识、带领学员进行</span>高并发环境下的Nginx性能优化实战,让学生能够快速将所学融合到企业应用中。 </p> <p> <br /> </p> <p style="font-family:Helvetica;color:#3A4151;font-size:14px;background-color:#FFFFFF;"> <b><br /> </b> </p> <p style="font-family:Helvetica;color:#3A4151;font-size:14px;background-color:#FFFFFF;"> <b><span style="background-color:#FFE500;">【课程如何观看?】</span></b> </p> <p style="font-family:Helvetica;color:#3A4151;font-size:14px;background-color:#FFFFFF;"> PC端:<a href="https://edu.csdn.net/course/detail/26277"><span id="__kindeditor_bookmark_start_21__"></span></a><a href="https://edu.csdn.net/course/detail/27216">https://edu.csdn.net/course/detail/27216</a> </p> <p style="font-family:Helvetica;color:#3A4151;font-size:14px;background-color:#FFFFFF;"> 移动端:CSDN 学院APP(注意不是CSDN APP哦) </p> <p style="font-family:Helvetica;color:#3A4151;font-size:14px;background-color:#FFFFFF;"> 本课程为录播课,课程永久有效观看时长,大家可以抓紧时间学习后一起讨论哦~ </p> <p style="font-family:"color:#3A4151;font-size:14px;background-color:#FFFFFF;"> <br /> </p> <p class="ql-long-24357476" style="font-family:"color:#3A4151;font-size:14px;background-color:#FFFFFF;"> <strong><span style="background-color:#FFE500;">【学员专享增值服务】</span></strong> </p> <p class="ql-long-24357476" style="font-family:"color:#3A4151;font-size:14px;background-color:#FFFFFF;"> <b>源码开放</b> </p> <p class="ql-long-24357476" style="font-family:"color:#3A4151;font-size:14px;background-color:#FFFFFF;"> 课件、课程案例代码完全开放给你,你可以根据所学知识,自行修改、优化 </p> <p class="ql-long-24357476" style="font-family:"color:#3A4151;font-size:14px;background-color:#FFFFFF;"> 下载方式:电脑登录<a href="https://edu.csdn.net/course/detail/26277"></a><a href="https://edu.csdn.net/course/detail/27216">https://edu.csdn.net/course/detail/27216</a>,播放页面右侧点击课件进行资料打包下载 </p> <p> <br /> </p> <p> <br /> </p> <p> <br /> </p>
©️2020 CSDN 皮肤主题: 成长之路 设计师:Amelia_0503 返回首页
实付 49.90元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值