uniapp 微信小程序 获取openid,手机号进行登录,配合后端

在这里插入图片描述

流程:登录注册功能,通过uni.getUserProfile获取wxcode,通过wxcode传给后端获取openid,sessionkey,unionid。
通过<u-button type="success" open-type="getPhoneNumber" @getphonenumber="decryptPhoneNumber">一键登录</u-button>,
传到后端这些参数:
'session_key': $this.openidData.sessionKey,
'encryptedData': e.detail.encryptedData,
'iv': e.detail.iv
获取手机号,
再将手机号,unionid,openid传到后端进行登录
<template>
	<view class="page">
		<!-- 自定义头部 navbar -->
		<u-navbar back-icon-name="arrow-left" title=" " :custom-back="navbarBack"
			:background="{ backgroundColor: 'rgba(0,0,0,0)' }" :border-bottom="false"
			:is-back="pageStatus != 'home'"></u-navbar>
		<!-- logo -->
		<view class="logo-wrap" v-if="pageStatus == 'home' || pageStatus == 'getWxRole'">
			<image class="logo"
				src="xxxxxxxxxxxxxx/static/login/login_logo.png"></image>
			<view class="app-name">欢迎登录</view>
			<view class="app-title">弘云艺术中心</view>
		</view>

		<!-- 默认登录页面显示 -->
		<view style="width: 100%;" v-if="pageStatus == 'home'">
			<view class="text-area" @click="loginByWx">手机号快捷登录</view>
		</view>

		<!-- 微信授权登录,获取用户信息 -->
		<view style="width: 100%;" v-if="pageStatus == 'getWxRole'">
			<view class="info">为提供优质服务,需要获取你的以下信息 :</view>
			<view class="public">
				<view class="public-dot"></view>
				<view class="public-text">你的公开信息(头像、昵称等)</view>
			</view>
			<view class="text-enter" @click="getWxLoginRole">授权进入</view>
		</view>

		<!-- 微信登录小程序,再授权获取用户信息之后 -->
		<view class="loginWx" v-if="pageStatus == 'loginByWx'">
			<view class="title">欢迎使用 弘云艺术中心</view>
			<view class="desc">立即登录享受优质服务</view>
			<view class="avatar">
				<image mode="aspectFill" :src="wxLoginUserInfo.userInfo.avatarUrl"></image>
			</view>
			<u-button type="success" open-type="getPhoneNumber" @getphonenumber="decryptPhoneNumber">一键登录</u-button>
		</view>

		<!-- 协议勾选 -->
		<view class="agree" v-if="pageStatus == 'home' || pageStatus == 'getWxRole' ">
			<view class="agree_img" @click="changeAgreeFlag" v-show="!agree_flag">
				<image src="xxxxxxxxxxx/static/mine/check.png">
				</image>
			</view>
			<view class="agree_img" @click="changeAgreeFlag" v-show="agree_flag">
				<image src="xxxxxxxxxxxxxxxxx/static/operate/checked.png">
				</image>
			</view>
			<view class="agree_text">
				登录代表您已同意
				<span>《用户服务协议》《隐私权政策》</span>
			</view>
		</view>
	</view>
</template>

<script>
	import FunUniappTheme from '@/theme.scss';
	export default {
		data() {
			return {
				// 页面状态,默认为 home
				// getWxRole:获取微信权限页,loginByWx:微信授权登录页
				// loginByCode:手机号+验证码登录页,loginByPwd:手机号+密码登录
				// forgetPwd:找回密码页面,resetPwd,重置密码页面
				pageStatus: 'home',
				// 协议是否勾选
				agree_flag: false,
				// 微信授权的用户信息
				wxLoginUserInfo: {},
				// openid
				openId: '',
				// 系统内用户信息
				userInfo: {},
				//openid
				openidData: {}
			};
		},
		onShow() {
		},
		onLoad(ops) {
			if (ops && ops.interceptcard && ops.interceptcard === "true") {
				uni.showToast({
					title: '购买商品,请先登录注册。',
					icon: 'none',
					duration: 4000,
				});
			}
			if (ops && ops.goods_id_detail) {
				console.log(ops.goods_id_detail, '二维码商品详情')
				uni.setStorageSync('goods_id_detail', ops.goods_id_detail)
			}
			console.log(FunUniappTheme);
		},
		onReady() {
		},
		methods: {
			// 勾选/取消用于协议
			changeAgreeFlag() {
				this.agree_flag = !this.agree_flag;
			},

			// 微信登录
			loginByWx() {
				if (!this.agree_flag) {
					uni.showToast({
						icon: 'none',
						title: '请先阅读并勾选协议'
					});
					return;
				}
				this.getUserInfoByWx(() => {
					this.getWxCode(code => {
						let $this = this;
						console.log('执行了吗', code)
						uni.request({
							url: "xxxxxxxxxxxxxx/mobile/index.php?m=user&c=indexapi&a=oauth2",
							method: 'POST',
							header: {
								'Content-Type': 'application/x-www-form-urlencoded'
							},
							data: {
								'wxcode': code,
								'parent_unionid': uni.getStorageSync('unionid')
							},
							success(res) {

								$this.openidData = res.data.data;
								console.log(res.data.data, 9999, $this.openidData);
								uni.setStorageSync('OPEN_ID', res.data.data.openid)
							}
						});
					});
				});
			},

			// 自定义navbar的返回方法
			navbarBack() {
				if (this.pageStatus == 'getWxRole' || this.pageStatus == 'loginByWx' ) {
					this.pageStatus = 'home';
				} else if (this.pageStatus == 'forgetPwd' || this.pageStatus == 'resetPwd') {
					this.pageStatus = 'loginByPwd';
				} else if (this.pageStatus == 'loginByPwd') {
					this.pageStatus = 'loginByCode';
				}
			},

			// 微信授权
			getWxLoginRole() {
				if (!this.agree_flag) {
					uni.showToast({
						icon: 'none',
						title: '请先阅读并勾选协议'
					});
					return;
				}
				this.getUserInfoByWx(() => {
					this.getWxCode(code => {
						let $this = this;
						console.log('执行了吗', code)
						uni.request({
							url: "xxxxxxxxxxxxxxx/mobile/index.php?m=user&c=indexapi&a=oauth2",
							method: 'POST',
							header: {
								'Content-Type': 'application/x-www-form-urlencoded'
							},
							data: {
								'wxcode': code,
								'parent_unionid': uni.getStorageSync('unionid')
							},
							success(res) {


								$this.openidData = res.data.data;
								console.log(res.data.data, 9999, $this.openidData);
								uni.setStorageSync('OPEN_ID', res.data.data.openid)
							}
						});
					});
				});
			},
			//个人中心
			userinfo() {
				uni.request({
					url: "xxxxxxxxxxxxxindex.php?m=user&c=indexapi&a=userinfo",
					method: 'POST',
					header: {
						'Content-Type': 'application/x-www-form-urlencoded'
					},
					data: {
						'open_id': uni.getStorageSync('OPEN_ID')
					},
					success(res) {
						console.log(res.data.data, '个人中心222');
						uni.setStorageSync('USER_ID', res.data.data.info.user_id)
						uni.setStorageSync('user_rank', res.data.data.info.user_rank)
						if (uni.getStorageSync('goods_id_detail')) {
							uni.reLaunch({
								url: '/pages-mall/pages/goods/detail?goods_id_detail=' + uni
									.getStorageSync('goods_id_detail')
							});
						} else {
							uni.reLaunch({
								url: '/pages/home/index'
							});
						}
					}
				});
			},

			// 基于uni的获取code方法
			getWxCode(callBack) {
				uni.login({
					provider: 'weixin',
					onlyAuthorize: true,
					success: loginRes => {
						console.log(loginRes, '获取信息')
						callBack(loginRes.code);
					},
					fail(e) {}
				});
			},

			// 获取openId
			getOpenId(code, callBack) {
				this.$u.api.getOpenId(code).then(res => {
					// uni.setStorageSync('OPEN_ID', res.openid);
					callBack(res);
				});
			},

			// 获取个人信息
			getUserInfoByWx(callBack) {
				let $this = this;
				uni.getUserProfile({
					desc: '获取你的昵称、头像',
					success: function(data) {
						console.log(data, '用户信息')
						uni.setStorageSync({
							key: 'APP_WX_USERINFO',
							data: data
						});
						$this.wxLoginUserInfo = data;
						$this.pageStatus = 'loginByWx';
						callBack();
					},
					fail: function(error) {}
				});
			},

			// 获取手机号回调
			decryptPhoneNumber(e) {
				let $this = this;
				console.log(e.detail, 9898)
				uni.request({
					url: "xxxxxxxxxxxxxxx/index.php?m=user&c=indexapi&a=decryptwechatdata",
					method: 'POST',
					header: {
						'Content-Type': 'application/x-www-form-urlencoded'
					},
					data: {
						'session_key': $this.openidData.sessionKey,
						'encryptedData': e.detail.encryptedData,
						'iv': e.detail.iv
					},
					success(res) {
						console.log(res.data, '手机号');
						uni.request({
							url: "xxxxxxxxxxxxxxxxxxxxxx/index.php?m=oauth&c=indexapi&a=bind_register",
							method: 'POST',
							header: {
								'Content-Type': 'application/x-www-form-urlencoded'
							},
							data: {
								'mobile': res.data.data.phoneNumber,
								'unionid': $this.openidData.unionid,
								'openid': $this.openidData.openid
							},
							success(res) {
								console.log(res.data, '登录成功');
								if (res.data.code === 0) {
									$this.userinfo()
									uni.setStorageSync('IS_LOGIN', true);
									uni.setStorageSync('fh_type',0)
									console.log(uni.getStorageSync('IS_LOGIN'), '成功了?', uni.getStorageSync(
										'USER_ID'))
								}
							}
						});
					}
				});
			},

			// 微信小程序手机号登录
			loginByOpenId() {
				const openid = uni.getStorageSync('OPEN_ID');
				this.$u.api.loginByOpenId(openid).then(res => {
					this.userInfo = res;
				});
			},
		}
	};
</script>

<style lang="scss" scoped>
	.page {
		padding: 0 40rpx;
		background-color: $app-theme-bg-color;
	}

	.logo-wrap {
		display: flex;
		// justify-content: center;
		padding-top: 100rpx;
		padding-bottom: 52rpx;
		flex-wrap: wrap;

		.logo {
			height: 168rpx;
			width: 166rpx;
			margin-left: 76rpx;
			margin-top: 50rpx;
		}

		.app-name {
			width: 100%;
			padding-top: 80rpx;
			margin-left: 78rpx;
			font-size: 38rpx;
			font-weight: 600;
			color: #333333;
			line-height: 52rpx;
		}

		.app-title {
			width: 100%;
			padding-top: 16rpx;
			margin-left: 78rpx;
			font-size: 38rpx;
			font-weight: 600;
			color: #333333;
			line-height: 52rpx;
		}
	}

	.text-area {
		width: 100%;
		height: 96rpx;
		background: #19be6b;
		font-weight: 400;
		border-radius: 56rpx;
		color: $app-theme-text-white-color;
		line-height: 16px;
		font-size: 32rpx;
		text-align: center;
		line-height: 96rpx;
		/* background: url(../../static/img/logo_newhope.png); */
	}

	.info {
		font-size: 28rpx;
		font-weight: 400;
		color: $app-theme-text-black-color;
		line-height: 28rpx;
		margin-top: 180rpx;
	}

	.public {
		display: flex;
		align-items: center;
		margin-top: 30rpx;

		.public-dot {
			width: 6rpx;
			height: 6rpx;
			background: #999999;
			margin-right: 10rpx;
		}

		.public-text {
			font-size: 24rpx;
			font-weight: 400;
			color: #8f92a1;
			line-height: 24rpx;
		}
	}

	.text-enter {
		width: 100%;
		margin-top: 50rpx;
		height: 96rpx;
		background: $app-theme-color;
		font-weight: 400;
		border-radius: 56rpx;
		color: #ffffff;
		line-height: 16px;
		font-size: 32rpx;
		text-align: center;
		line-height: 96rpx;
	}

	.head {
		margin-top: 70rpx;
		width: 100%;
		display: flex;
		justify-content: space-between;
	}

	.back {
		width: 40rpx;
		height: 40rpx;
	}
	.agree {
		position: fixed;
		left: 50%;
		transform: translate(-50%, 0);
		bottom: 66rpx;
		width: 100vw;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.agree_img image {
		width: 32rpx;
		height: 32rpx;
		display: flex;
		align-items: center;
	}

	.agree_text {
		margin-left: 8rpx;
		font-size: 24rpx;
		font-weight: 400;
		color: #8f92a1;
		line-height: 24rpx;
		display: flex;
		align-items: center;

		span {
			color: $app-theme-color;
		}
	}

	.loginWx {
		width: 100%;
		padding-top: 64rpx;

		.title {
			text-align: left;
			font-size: 22px;
			font-family: PingFang-SC-Regular, PingFang-SC;
			font-weight: 400;
			color: #171717;
			line-height: 30px;
			margin-bottom: 12rpx;
		}

		.desc {
			text-align: left;
			font-size: 14px;
			font-family: PingFang-SC-Regular, PingFang-SC;
			font-weight: 400;
			color: #8f92a1;
			line-height: 14px;
		}

		.avatar {
			width: 160rpx;
			height: 160rpx;
			overflow: hidden;
			border-radius: 50%;
			display: flex;
			align-items: center;
			justify-content: center;
			margin: 128rpx auto 160rpx auto;

			image {
				width: 100%;
				height: 100%;
			}
		}
	}
</style>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/577220.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

Spark-机器学习(5)分类学习之朴素贝叶斯算法

在之前的文章中&#xff0c;我们学习了回归中的逻辑回归&#xff0c;并带来简单案例&#xff0c;学习用法&#xff0c;并带来了简单案例。想了解的朋友可以查看这篇文章。同时&#xff0c;希望我的文章能帮助到你&#xff0c;如果觉得我的文章写的不错&#xff0c;请留下你宝贵…

合合信息引领AI场景化革新,供应链金融智能化审核全面升级!

官.网地址&#xff1a;合合TextIn - 合合信息旗下OCR云服务产品 随着供给侧结构性改革的深入推进和产业结构的不断升级&#xff0c;金融机构在监管部门的指导下&#xff0c;积极拓展供应链金融业务&#xff0c;取得了显著成效。这一举措有效缓解了上下游中小企业的融资困难&a…

你的网站还在使用HTTP? 免费升级至HTTPS吧

如果您的网站还在使用老的http协议&#xff0c;可以申请一个免费的SSL证书升级至https&#xff01; 具体步骤如下&#xff1a; 1 申请免费SSL证书 根据你的需求选择合适的SSL证书类型&#xff0c;如单域名证书&#xff0c;多域名证书、通配符证书 登录免费供应商JoySSL官网&…

中电金信:深度解析|数字化营销运营体系搭建

如何更好更快地梳理好体系搭建思路&#xff0c;稳步实现落地&#xff1f;下文将为大家明确搭建的推进步骤、执行要点&#xff0c;帮助商业银行理顺数字化营销运营体系的“点”“线”“面”~ 与所有转型的曲折、阵痛等特征一样&#xff0c;商业银行构建数字化营销运营体系过程中…

数据结构与算法解题-20240426

这里写目录标题 面试题 08.04. 幂集367. 有效的完全平方数192. 统计词频747. 至少是其他数字两倍的最大数718. 最长重复子数组 面试题 08.04. 幂集 中等 幂集。编写一种方法&#xff0c;返回某集合的所有子集。集合中不包含重复的元素。 说明&#xff1a;解集不能包含重复的子…

DSP开发实战教程--EPWM模块的影子寄存器详细讲解原理和代码实例

EPWM模块影子寄存器的原理 在TI&#xff08;Texas Instruments&#xff09;的DSP28335中&#xff0c;EPWM&#xff08;Enhanced Pulse Width Modulator&#xff09;模块提供了高精度、高灵活性的PWM信号生成功能。为了能在不影响当前PWM波形输出的情况下预装载新的PWM参数&…

电源小白入门学习6——锂离子电池特性及充电电路

锂离子电池特性及充电电路 锂离子电池18650电池锂聚合物电池锂电池的放电曲线 锂离子电池充电方法常见的充电方案 锂离子电池 锂离子电池是一种常见的可充电电池类型&#xff0c;主要依靠锂离子在正极和负极之间的移动来工作。在充放电过程中&#xff0c;锂离子在两个电极之间…

表情识别 | LBP+SVM实现脸部动态特征的人脸表情识别程序(Matlab)

表情识别 | LBPSVM实现脸部动态特征的人脸表情识别程序&#xff08;Matlab&#xff09; 目录 表情识别 | LBPSVM实现脸部动态特征的人脸表情识别程序&#xff08;Matlab&#xff09;预测效果基本介绍程序设计参考资料 预测效果 基本介绍 1 运行环境 程序运行在Windows系统下&am…

不同技术实现鼠标滚动图片的放大缩小

摘要&#xff1a; 最近弄PC端的需求时&#xff0c;要求在layui技术下实现鼠标滚动图片的放大缩小的功能&#xff01;下面来总结一下不同框架剩下这功能&#xff01; layui: 看了一下layui文档&#xff0c;其实这有自带的组件的&#xff01;但是又版本要求的!并且layui的官方文档…

让ThreadPoolExecutor无所遁形:Java线程池运行原理详解

ThreadPoolExecutor的核心工作原理 当我们在Java中讨论并发和多线程时&#xff0c;ThreadPoolExecutor 是不可或缺的一个类。在 java.util.concurrent 包下&#xff0c;该类负责管理线程池内的线程&#xff0c;包括线程的创建、执行、管理以及线程池的监控等。理解 ThreadPool…

玩转手机在AidLux上安装宝塔面板

AidLux&#xff0c;手机不用刷机、不用root&#xff0c;直接在手机应用市场就能下载使用。 1.4G的应用包&#xff0c;看起来挺大的&#xff0c;那是因为内嵌了一套完整的AIoT应用开发和部署平台。 不仅Android手机可以玩&#xff0c;华为的Harmony系统也可以使用。 使用它最主…

websocket爬虫

人群看板需求分析 先找到策略中心具体的数据。对应数据库中的数据 看看接口是否需要被逆向 点开消费者细分&#xff0c;可以找到人群包&#xff08;人群名称&#xff09; 点击查看透视 label字段分类: 在这里插入图片描述 预测年龄&#xff1a;tagTitle 苹果id&#x…

【Unity基础】TextMeshPro组件学习过程记录

目录 1.TextMeshPro组件渲染创建文本RTL Editor字体Font Asset字体加粗&#xff0c;下划线等字体大小控制字体颜色控制字体渐变控制字符间隔、单词间隔、行间距、段落间距控制WrappingUV映射控制代码 2.TextMeshPro组件AssetFace InfoGeneration Setting 3.使用Dynamic SDF Sys…

从C语言到C++过渡篇(快速入门C++)

目录 引言 命名空间 C 的输入输出&#xff08;cout & cin&#xff09; 输出 cout 输入 cin 缺省参数 函数重载 知识要点讲解 函数重载底层 引用& 内联函数 auto & nullptr 结语 引言 很多同学从C语言到C的转变不知从何下手&#xff0c;今天这篇文章主…

【MRI重建】Cartesian采样中data consistency 常规数据一致性实现(pytorch)

关于 在MRI重建中,data consistency 可以帮助加快MRI图像重建和减少模型重建带来的重建误差。 工具 方法实现 x_rec: 重建图像, (batch_size,2,H,W) mask: 欠采样模版,(batch_size,2,H,W) k_un: 真实欠采样采集数据, (batch_size,2,H,W) torch.view_as_complex: 将实数数据…

【Linux】HTTP协议2

欢迎来到Cefler的博客&#x1f601; &#x1f54c;博客主页&#xff1a;折纸花满衣 &#x1f3e0;个人专栏&#xff1a;题目解析 &#x1f30e;推荐文章&#xff1a;承接上文【Linux】HTTP协议1 目录 &#x1f449;&#x1f3fb;HTTP方法&#x1f449;&#x1f3fb;HTTP状态码&…

Swift - 流程控制

文章目录 Swift - 流程控制if-else2. while3. for3.1 闭区间运算符3.2 半开区间运算符3.3 for - 区间运算符用在数组上3.3.1 单侧区间 3.4 区间类型3.5 带间隔的区间值 4. switch4.1 fallthrough4.2 switch注意点 5. 复合条件6. 区间匹配、元组匹配7. 值绑定8. where9. 标签语句…

webpack中mode、NODE_ENV、DefinePlugin、cross-env的使用

本文讲的全部知识点&#xff0c;都是和webpack相关的。如果你之前有疑问&#xff0c;那本文一定能帮你搞清楚。 问题来源一般是类似下面代码&#xff08;webpack.json中&#xff09;&#xff1a; "scripts": {"dev": "cross-env NODE_ENVdevelopmen…

BUUCTF_[BSidesCF 2020]Had a bad day

[BSidesCF 2020]Had a bad day 1.一看题目直接尝试文件包含 2.直接报错&#xff0c;确实是存在文件包含漏洞 http://307b4461-36d6-443f-879a-68803a57f721.node5.buuoj.cn:81/index.php?categoryphp://filter/convert.base64-encode/resourceindex strpos() 函数查找字符串…

StarRocks x Paimon 构建极速实时湖仓分析架构实践

Paimon 介绍 Apache Paimon 是新一代的湖格式&#xff0c;可以使用 Flink 和 Spark 构建实时 Lakehouse 架构&#xff0c;以进行流式处理和批处理操作。Paimon 创新性地使用 LSM&#xff08;日志结构合并树&#xff09;结构&#xff0c;将实时流式更新引入 Lakehouse 架构中。 …
最新文章