• uniapp手机一键登录,微信授权登陆


    之前前端可以通过微信的接口获取openid,现在不允许了,所以把code传给后端后端反数据给我们

    <template>
    	<view class="content yd-center">
    		<!-- 手机登录 -->
    		<view class="phonebox">
    			<view class="u-demo-block__content" style=" width: 91vw;">
    				<u-input maxlength="11" placeholder="请输入手机号码" type="number" clearable color="#000" v-model="phone"
    					@blur="phoneblur" border="bottom">
    					<u--text text="手机号:" color="#6b6b6b" slot="prefix" size="18" margin="0 3px 0 0"></u--text>
    					<template slot="suffix">
    						<button class="Localnumber" type="default" open-type="getPhoneNumber"
    							@getphonenumber.stop="GetPhoneNumber">获取手机号</button>
    					</template>
    				</u-input>
    			</view>
    			<view class="u-demo-block__content" style="margin-top: 15px; width: 91vw;">
    				<u-input maxlength="6" type="number" placeholder="请输入验证码" color="#000" v-model="Scode" border="bottom">
    					<u--text text="验证码:" color="#6b6b6b" slot="prefix" size="18" margin="0 3px 0 0"></u--text>
    					<template slot="suffix">
    						<u-code ref="uCode" @change="codeChange" seconds="60" changeText="x秒重新获取"></u-code>
    						<view class="Codebox" @click="getCode">
    							{{tips}}
    						</view>
    						<!-- <u-button @tap="getCode" :text="tips" type="success" size="mini"></u-button> -->
    					</template>
    				</u-input>
    			</view>
    		</view>
    		<!-- 微信登录 -->
    		<view class="loginbtn yd-center" @click="phonelogin">登录</view>
    		<view class="weichatloginbtn yd-center" @click="weichatlogin">一键授权登录</view>
    		<!-- <view class="sysbox">切换登录方式</view>
    		<view class="iconfont icon-ydkjweixin" @click="weichatlogin"></view> -->
    	</view>
    </template>
    
    <script>
    	import {
    		login,
    		loginPhone,
    		sendCode,
    		encrypPhone,
    		getOpenId
    	} from '@/api/login.js'
    
    	import {
    		HTTP_REQUEST_URL
    	} from '@/common/config.js';
    	export default {
    		data() {
    			return {
    				tips: '', //验证码提示文字
    				phone: '', //手机号码
    				Scode: '', //验证码
    				code: '', //验证码
    				sessionKey: '', //秘钥
    				iv: '', //本机号码秘钥
    				encryptedData: '', //本机号码秘钥
    			};
    		},
    		onLoad() {
    			// 获取解密本机手机号码的信息
    			this.updateSessionKey()
    		},
    		methods: {
    			// 获取本机手机号码
    			GetPhoneNumber(e) {
    				console.log(e)
    				//需要检查sessionkey状态
    				uni.checkSession({
    					success: (_res) => {
    						if (e.detail.errMsg == 'getPhoneNumber:user deny') { //用户点击拒绝
    							uni.showToast({
    								title: "您已拒绝!授权失败",
    								icon: "none"
    							})
    						} else { //允许授权执行跳转 
    							this.iv = e.detail.iv;
    							this.encryptedData = e.detail.encryptedData;
    							this.getencrypPhone()
    						}
    					},
    					fail: (_res) => {
    						console.log(_res)
    						uni.hideLoading()
    						this.updateSessionKey()
    						uni.showModal({
    							title: '提示',
    							content: '您的登录失效,请重新授权',
    							showCancel: false
    						})
    					}
    				})
    			},
    			getencrypPhone() {
    				let data = {
    					iv: this.iv,
    					encrypted: this.encryptedData,
    					sessionKey: this.sessionKey,
    				}
    				//传给后端解密手机号
    				encrypPhone(data).then(res => {
    						let data = JSON.parse(res.data)
    						this.phone = data.phoneNumber
    					})
    					.catch(res => {
    						uni.hideLoading();
    						uni.$u.toast(res.msg);
    					})
    			},
    			// 获取SessionKey
    			updateSessionKey() {
    				this.getLoginCode() //获取微信登录js_code
    				// console.log('code=',jsCode)
    
    			},
    			// 获取code
    			getLoginCode() {
    				uni.login({
    					provider: 'weixin',
    					success: async (res) => {
    						this.code = res.code;
    						// uni.setStorageSync('code', res.code)
    						this.jsCode2OpenId() //openid和session数据
    					}
    				})
    			},
    			jsCode2OpenId() {
    				getOpenId({
    					code: this.code
    				}).then(res => {
    					let info = JSON.parse(res.data)
    					this.openid = info.openid;
    					this.sessionKey = info.session_key
    				})
    			},
    			// 电话号码验证登陆
    			phonelogin() {
    				if (this.phone == '') {
    					uni.showToast({
    						title: '请填写电话号码',
    						icon: 'error',
    						duration: 2000
    					});
    					return
    				} else if (this.Scode == '') {
    					uni.showToast({
    						title: '请输入验证码',
    						icon: 'error',
    						duration: 2000
    					});
    					return
    				}
    				loginPhone({
    					phone: this.phone,
    					code: this.Scode
    				}).then(res => {
    					if (res.code == 0) {
    						uni.setStorageSync('token', res.data.token)
    						uni.setStorageSync('phone', res.data.loginName)
    						uni.reLaunch({
    							url: '/pages/index/index'
    						})
    					}
    				})
    			},
    			// 微信登陆
    			weichatlogin() {
    				let that = this;
    				let token = uni.getStorageSync('token');
    				if (token) {
    					uni.redirectTo({
    						url: '/pages/index/index'
    					})
    				} else {
    					uni.getUserProfile({
    						lang: 'zh_CN',
    						desc: '获取你的昵称、头像、地区及性别',
    						success: (res) => {
    							uni.setStorageSync('userInfo', res.userInfo)
    							//先获取code,再授权登陆
    							uni.getProvider({
    								service: 'oauth',
    								success: function(res) {
    									console.log(res.provider);
    									//支持微信、qq和微博等
    									if (~res.provider.indexOf('weixin')) {
    										login({
    											openId: that
    												.openid
    										}).then(
    											res => {
    												if (res.code == 0) {
    													uni.setStorageSync('token', res.data.token)
    													uni.reLaunch({
    														url: '/pages/index/index'
    													})
    												}
    											});
    									}
    								}
    							});
    						}
    					});
    				}
    			},
    			// 电话号码输入失去焦点事件
    			phoneblur(e) {
    				let reg = /^[1][3,4,5,7,8][0-9]{9}$/
    				if (reg.test(Number(e)) == false) {
    					uni.showToast({
    						title: '请填写正确的电话号码',
    						icon: 'error',
    						duration: 2000
    					});
    					this.phone = ''
    				}
    			},
    			// 验证码倒数事件文字切换
    			codeChange(text) {
    				this.tips = text;
    			},
    			//监听验证码按钮状态事件
    			getCode() {
    				if (this.phone == '') {
    					uni.showToast({
    						title: '请填写电话号码',
    						icon: 'error',
    						duration: 2000
    					});
    					return
    				}
    				if (this.$refs.uCode.canGetCode) {
    					// 模拟向后端请求验证码
    					uni.showLoading({
    						title: '正在获取验证码'
    					})
    					// uni.showToast({
    					// 	title: '正在获取验证码',
    					// 	icon: 'loading',
    					// 	duration: 30000 // 当然这个时间可根据实用场景进行相关设置
    					// })
    					setTimeout(() => {
    						sendCode({
    								phone: this.phone
    							}).then(res => {
    								uni.hideLoading();
    								// uni.hideToast()
    								console.log(1, res)
    								// 这里此提示会被this.start()方法中的提示覆盖
    								uni.$u.toast('验证码已发送');
    								// 通知验证码组件内部开始倒计时
    								this.$refs.uCode.start();
    							})
    							.catch(res => {
    								uni.hideLoading();
    								uni.$u.toast(res.msg);
    							})
    							.finally(res => {
    								uni.hideLoading();
    								uni.$u.toast(res.msg);
    							})
    					}, 2000);
    				} else {
    					uni.$u.toast('倒计时结束后再发送');
    				}
    			},
    
    			change(e) {
    				console.log('change', e);
    			}
    		}
    	};
    </script>
    
    <style lang="scss" socped>
    	button {
    		margin: 0;
    		background-color: #ffffff;
    		border: none;
    	}
    
    	.content {
    		margin-bottom: 140rpx;
    		width: 100vw;
    		height: 100vh;
    		background-color: #ffffff;
    	}
    
    	.phonebox {
    		margin-bottom: 124rpx;
    		margin-top: -606rpx;
    	}
    
    	.loginbtn {
    		width: 92vw;
    		height: 86rpx;
    		background-color: #0092ff;
    		color: white;
    		border: 2rpx solid #0092ff;
    		border-radius: 10rpx;
    		margin: 20rpx auto;
    		font-size: 30rpx;
    		letter-spacing: 10rpx;
    		box-shadow: 0 0 10rpx #919191;
    	}
    
    	.weichatloginbtn {
    		width: 92vw;
    		height: 86rpx;
    		background-color: #ffffff;
    		color: #0092ff;
    		border: 2rpx solid #0092ff;
    		border-radius: 10rpx;
    		margin: 20rpx auto;
    		font-size: 30rpx;
    		letter-spacing: 10rpx;
    		box-shadow: 0 0 10rpx #919191;
    	}
    
    	.sysbox {
    		margin: 500rpx 0 30rpx;
    		color: #b5b5b5;
    	}
    
    	.icon-ydkjweixin {
    		color: #5ac725;
    		font-size: 83rpx;
    	}
    </style>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
    • 294
    • 295
    • 296
    • 297
    • 298
    • 299
    • 300
    • 301
    • 302
    • 303
    • 304
    • 305
    • 306
    • 307
    • 308
    • 309
    • 310
    • 311
    • 312
    • 313
    • 314
    • 315
    • 316
    • 317
    • 318
    • 319
    • 320
    • 321
    • 322
    • 323
    • 324
    • 325
    • 326
    • 327
    • 328
    • 329
    • 330
  • 相关阅读:
    Linux驱动开发(十一)---树莓派SPI驱动学习(OLED)
    2023/10/02 网络编程知识点脑图整合
    查询文件和目录的磁盘使用
    Task Flow使用指南之五:捕获异常
    基于数组的SCL算法实现S7-1200_1500多通道模拟量批量处理的方法
    一键部署mysql+redis
    MySQL进阶实战8,分区表详解
    智慧路灯| 云计算点亮智慧城市的“星星之火”
    RDP协议流程详解(一)Connection Initiation阶段
    0027【Edabit ★☆☆☆☆☆】Fix the Expression
  • 原文地址:https://blog.csdn.net/m0_47843571/article/details/125526203