• 【微信小程序授权】获取用户手机号,昵称


    <template>
        <view>
            <view v-if="show == true">
                <view>
                    <view class='header' style="padding-bottom: 80upx;">
                        <image src='@/static/images/index/logo.jpg'></image>
                    </view>
                    <view class='content'>
                        <view>申请获取以下权限</view>
                        <text>获得您的公开信息(昵称,头像、手机号等)</text>
                    </view>
                    <button class='bottom' 
    				v-if="phoneBtn == false"
    				@click="getUserProfile"
    				withCredentials="true" lang="zh_CN">
                        授权登录
                    </button>
    				<button v-else class="rightBtn" type="primary" open-type="getPhoneNumber"
    					@getphonenumber="getPhone">获取手机号</button>
                </view>
            </view>
        </view>
    </template>
    
    <script>
        export default {
            data() {
                return {
    				show:false,
    				code:'',
    				encryptedData:'',
    				iv:'',
    				avatar:'',
    				nickName:'',
    				phoneBtn:false
                };
            },
    		created() {
    			
    		},
    		onLoad(options) {
    			let that = this
    			if(uni.getStorageSync('userInfo')){
    				uni.reLaunch({
    					url:'/pages/index/index'
    				})
    			}else{
    				uni.hideLoading()
    				that.show = true
    			}
    			// #ifdef MP-WEIXIN
    			uni.login({
    				provider: 'weixin',
    				success(res) {
    					that.code = res.code
    				}
    			})
    			// #endif
    		},
            methods: {
                //授权获取用户信息
    			getUserProfile(e) {
    				let that = this
    				uni.getUserProfile({
    					desc: '用于完善会员资料', 
    					success: (res) => {
    						// console.log(res,'获取的信息')
    						that.nickName = res.userInfo.nickName						
    						that.avatar = res.userInfo.avatarUrl
    						that.phoneBtn = true
    					}
    				})
    			},
    			getPhone(e){
    				let that = this
    				that.encryptedData = e.detail.encryptedData
    				that.iv = e.detail.iv	
    				that.login()
    			},
    			login() {				
    				let that = this;								
    				// 将用户信息和登录code传递到后台
    				uni.request({
    					url: 'http://47.111.146.137:8080/gq/' +'api/wechatLogin',
    					method: 'POST',
    					header: {
    						'content-type': 'application/x-www-form-urlencoded'
    					},
    					data: {
    						code:that.code,
    						nickName:that.nickName,
    						encryptedData:that.encryptedData,
    						iv:that.iv,
    						avatar:that.avatar
    					},
    					success: res => {
    						if(res.data.code == 0){
    							// uni.showToast({
    							// 	title:'登录成功',
    							// 	icon:'success'
    							// })
    							uni.showLoading({
    								title:'登录中...'
    							})
    							uni.setStorageSync('isLogin',true)
    							uni.setStorage({
    								key:'userInfo',
    								data:res.data.data
    							})
    							setTimeout(()=>{
    								uni.reLaunch({
    									url:'./index'
    								})
    							},2000)
    							
    						}
    					}
    				});			
                }
            }       
        }
    </script>
    
    <style lang="scss">
        .header {
            margin: 220rpx 0 0 50rpx;
            text-align: center;
            width: 650rpx;
        }
    
        .header image {
            width: 200rpx;
            height: 200rpx;
    		border-radius: 10rpx;
        }
    
        .content {  
    		padding-top: 50upx;
    		border-top: 1px solid #f5f5f5;
            width: 90%;
    	    margin: 0 auto;
            margin-bottom: 90rpx;
        }
    
        .content text {
            display: block;
            color: #9d9d9d;
            margin-top: 20rpx;
    		font-size: 28rpx;
        }
    
        .bottom {
            border-radius: 80rpx;
            margin: 70rpx 50rpx;
            font-size: 35rpx;
    		background-color: $uni-color-main;
    		color: #fff;
        }
    	.rightBtn{
    		border-radius: 80rpx;
    		margin: 70rpx 50rpx;
    		font-size: 35rpx;
    		color: #fff;
    	}
    </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

    // 其他页面判断是否授权

    onShow(){
    		let token = uni.getStorageSync('userInfo')
    		if (!token) {
    			uni.showModal({				
    				title:'请登录',
    				content:"您还没有登录,将无法正常使用功能,是否登录?",				
    				success:function(res){				
    					if (res.confirm) {
    						uni.navigateTo({
    							url:"/pages/index/getCode"				
    						});	
    					} else if (res.cancel) {
    						uni.navigateTo({
    							url:"/pages/index/index"				
    						});
    					}	
    				},
    			})
    		}
    	},
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    第二种 微信授权 登录 和 苹果账号登录

    <!-- 登录按钮 -->
    	<view class="loginButton-box">
    		<!-- 微信授权登录 -->
    		<button class="login-weixin" hover-class="touchOpacity" @click="weixinlogin" open-type="getPhoneNumber"
    			bindgetphonenumber="_wxlogin">
    			微信授权登录>
    		</button>
    		<!-- 苹果账号登录 -->
    		<view class="login-apple" v-show="isShow" @click="toApple">苹果账号登录</view>
    	</view>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    weixinlogin() {
    	if (!this.isallow) {
    		uni.showToast({
    			title: '请先同意用户协议',
    			icon: 'none'
    		});
    		
    		return
    	}else{
    		
    		/* uni.login({
    		  provider: 'weixin',
    		  success: function (loginRes) {
    		    console.log(loginRes.code);
    		  }
    		}); */
    		
    		
    	}
    	var that = this
    	uni.getProvider({
    		service: 'oauth',
    		success: function(res) {
    			console.log(res.provider);
    			//支持微信、qq和微博等
    			if (~res.provider.indexOf('weixin')) {
    				uni.login({
    					provider: 'weixin',
    					success: function(loginRes) {
    						console.log("App微信获取用户信息成功", loginRes);
    						//调用后端接口做登陆操作
    						//that.wechatLoginFun(loginRes.authResult)
    						wxlogin({
    							code: loginRes.authResult.openid
    						}).then(res => {
    							console.log(res)
    							if(res.code==2000){
    								console.log(res, 'login')
    								var loginInfo = res.data.data.access
    								var Authorization = 'JWT ' + loginInfo
    								// console.log(Authorization,'Authorization')
    								uni.setStorageSync('Authorization',Authorization)
    								uni.switchTab({
    									url:"/pages/home/home"
    								})
    							}else if(res.code==301){
    								
    								console.log(11)
    								
    								uni.reLaunch({
    									url:"../login/userLogin",
    									fail(err) {
    										console.log(err)
    									}
    								})
    								
    							}
    							
    							
    						})
    						
    					},
    					fail: function(loginRes) {
    						console.log("App微信获取用户信息失败", res);
    						//调用后端接口做登陆操作
    						//that.wechatLoginFun(loginRes)
    					}
    				})
    			}
    		}
    	});
    
    },
    // 苹果账号登录
    toApple() {
    	if (!this.isallow) {
    		uni.showToast({
    			title: '请先同意用户协议',
    			icon: 'none'
    		});
    		return
    	}
    	console.log('点击了苹果账号登录')
    	uni.login({
    		provider: 'apple',
    		scopes: "auth_user",
    		success: function(loginRes) {
    			console.log(loginRes)
    			//登录成功
    
    			uni.getUserInfo({
    				provider: 'apple',
    				success(res) {
    					//获取用户信息成功
    					console.log("App苹果账号登录获取用户信息成功", res)
    				}
    			})
    		},
    		fail: function(res) {
    			console.log('苹果账号登录失败', res)
    		}
    	})
    },
    
    • 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
  • 相关阅读:
    模型轻量化、模型减面、模型压缩、模型优化合集
    使用Docker部署ElasticSearch与kibana
    C#扩展——自定义VS公/私有方法代码片段
    Linux下安装 SkyWalking 分布式追踪系统
    极智AI | 三谈昇腾 auto tune
    【JS】将字符串保存成文件到本地(.txt、.json、.md...)
    【C++练级之路】【Lv.26】类型转换
    vue使用wangEditor
    FreeRTOS中PendSV和SysTick的中断优先级和SVC系统调用
    ORA-01861: literal does not match format string
  • 原文地址:https://blog.csdn.net/AAAXiaoApple/article/details/125909543