uniapp小程序实现登录授权获取用户信息

2021-12-30   阅读:3135   分类:前端    标签: uni-app

登录授权获取用户信息,本文使用的新版的登录授权接口,结合后端ThinkPHP6框架实现

1、放置授权登录按钮

<template>
	<view class="userinfo" v-if="userInfo !=''">
		<view class="userimg">
			<image :src="userInfo.avatar" mode=""></image>
		</view>
		<view class="nickName">
			{{userInfo.nickName}}
		</view>
	</view>
	<view class="userinfo" v-else>
		<view class="userimg">
			<image src="../../static/tx.png" mode=""></image>
		</view>
		<button @click="onGotUserInfo">授权</button>
	</view>
</template>

2、授权方法

export default {
	data() {
		return {
			userInfo: '',
			
		}
	},
	methods: {
 /**
 * 用户同意授权个人微信信息
 * @param {Object} e 用户的信息
 */
 async onGotUserInfo() {	
	 uni.getUserProfile({
		 desc:'正在获取',//不写不弹提示框
		 success: res=> {
			uni.login({
			 success: async res1=> {
				if (res1.code) {
				 const res2 = await this.$myRequest({
					url: '/index/Index/getUserLogin',
					data: {
						code: res1.code,
						nickName: res.userInfo.nickName,
						avatar: res.userInfo.avatarUrl
					},
				 })	
				 if(res2.data.code==200){
					 uni.setStorageSync('dy_userInfo', res2.data.data);
					 this.userInfo = uni.getStorageSync('dy_userInfo');
					 // this.userInfo = res2.data
					
				 }
				} else {
				 uni.showModal({
					 title: '提示',
					 content: '网络繁忙,请稍后再试'
					})
				}
			 }
			})
		  
		},
		fail: err=> {
		  uni.showToast({
			title: '请点击授权进行登录',
			icon: 'none'
		  });
		 }
	 })
 },
 }
 }

3、后端代码

//授权·登录
public function getUserLogin(){
	$appId = 'APPID';
	$secret = 'secret';
	$authorization_code= 'authorization_code';
	if(empty(input('code'))){
		return json(['code'=>500,'msg'=>'error','data'=>'is null']);
		
	}else{
		$js_code = input('code');
		$curl = curl_init();
		//使用curl_setopt() 设置要获得url地址
		 $url = 'https://api.weixin.qq.com/sns/jscode2session?appid='.$appId.'&secret='.$secret.'&js_code='.$js_code.'&grant_type=authorization_code';
		 
		 curl_setopt($curl, CURLOPT_URL, $url);
		 //设置是否输出header
		 curl_setopt($curl, CURLOPT_HEADER, false);
		 //设置是否输出结果
		 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
		 //设置是否检查服务器端的证书
		 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
		 //使用curl_exec()将curl返回的结果转换成正常数据并保存到一个变量中
		 $data = curl_exec($curl);
		 $data =json_decode($data,true);
		 //关闭会话
		 curl_close($curl);
		 if(isset($data['openid'])){
			$addArray = [
				'avatar' =>input('avatar'),
				'nickName' =>input('nickName'),
				'openid' =>$data['openid'],
				'created_at' =>time(),
				'updated_at' =>time(),
				'ip' =>$_SERVER['REMOTE_ADDR'],
			];
			$updateArray = [
				'avatar' =>input('avatar'),
				'nickName' =>input('nickName'),
				'openid' =>$data['openid'],
				'updated_at' =>time(),
				'ip' =>$_SERVER['REMOTE_ADDR'],
			];
			$find = Db::name('weixin_dy_user')->where(array('openid'=>$data['openid']))->find();
			if($find){
				$res = Db::name('weixin_dy_user')->where(array('openid'=>$data['openid']))->update($updateArray);
			}else{
				$res = Db::name('weixin_dy_user')->insert($addArray);
			}
			if($res){
				$findRes = Db::name('weixin_dy_user')->where(array('openid'=>$data['openid']))->find();
				return json(['code'=>200, 'msg'=>'成功', 'data'=>$findRes]);
			}
			
			
		 }else{
			return json(['code'=>500, 'msg'=>'失败']);
		 }
		 
		
	}
}

4、效果展示:

【腾讯云】 爆款2核2G3M云服务器首年 61元,2核2G4M云服务器新老同享 99元/年,续费同价

‘简忆博客’微信公众号 扫码关注‘简忆博客’微信公众号,获取最新文章动态
转载:请说明文章出处“来源简忆博客”。http://www.tpxhm.com/fdetail/867.html

×
觉得文章有用就打赏一下文章作者
微信扫一扫打赏 微信扫一扫打赏
支付宝扫一扫打赏 支付宝扫一扫打赏

文章评论(0)

登录
简忆博客壁纸一
简忆博客壁纸二
简忆博客壁纸三
简忆博客壁纸四
简忆博客壁纸五
简忆博客壁纸六
简忆博客壁纸七
简忆博客壁纸八
头像

简忆博客
勤于学习,乐于分享

置顶推荐

打赏本站

如果你觉得本站很棒,可以通过扫码支付打赏哦!
微信扫码:你说多少就多少~
微信扫码
支付宝扫码:你说多少就多少~
支付宝扫码
×