uniapp微信小程序登录

热度:3052020-09-01 08:16 收藏 0 点赞 0

uniapp端代码:

appLoginWx(){
	// #ifdef MP-WEIXIN
	var domain = this.domain;
		uni.getProvider({
		  service: 'oauth',
		  success: function (res) {
			if (~res.provider.indexOf('weixin')) {
				uni.login({
					provider: 'weixin',
					success: (res2) => {
						
						uni.getUserInfo({
							provider: 'weixin',
							success: (info) => {//这里请求接口
							console.log(res2);
							console.log(info);
							var pid = uni.getStorageSync('clienid') ? uni.getStorageSync('clienid') : 0;
								uni.request({
									url: 'https://'+domain+'/api/checklogin', //仅为示例,并非真实接口地址。
									data: {
										code: res2['code'],
										pid: pid,
										info: info.rawData
									},
									header: {
									   'content-type': 'application/json' // 默认值
									},
									success: (res3) => {
										console.log(res3.data);
										if(res3.data.status==1){
											var userinfo = JSON.stringify(res3.data['info']);
											uni.setStorageSync('userinfo', userinfo);
											uni.setStorageSync('session_key',res3.data.session_key);
											uni.setStorageSync('openid',res3.data.openid);
											uni.showToast({
												title: '登录成功!',
												duration: 2000,
												icon: 'success',
												complete: function(){
													setTimeout(function(){
														uni.reLaunch({
															url: '/pages/user/user'
														});
													},2000)
													
												}
											});
											
											
											
										}else{
											//登录失败
											wx.showModal({
											  title: '登录提示',
											  content: '登录失败!',
											  showCancel: false
											});
										}
										
									}
								});
							
								
								
							},
							fail: () => {
								uni.showToast({title:"微信登录授权失败",icon:"none"});
							}
						})
				
					},
					fail: () => {
						uni.showToast({title:"微信登录授权失败",icon:"none"});
					}
				})
				
			}else{
				uni.showToast({
					title: '请先安装微信或升级版本',
					icon:"none"
				});
			}
		  }
		});
		//#endif
}


PHP端代码:

	function checklogin(){
	    
	    //$postStr = file_get_contents('php://input');
	   // $params = json_decode($postStr,1);
	    
	    $params=$this->frparam();
    	$appid = $this->webconf['wxappid'];
    	$secret = $this->webconf['wxsecret'];
	    $url="https://api.weixin.qq.com/sns/jscode2session?appid=".$appid."&secret=".$secret."&js_code=".$params['code']."&grant_type=authorization_code";
	    $data=$this->doCurl($url);
	    $info = json_decode($params['info'],1);
		//注册用户信息
		if($data['openid']){
		    $member = M('member')->find(['openid'=>$data['openid']]);
			if(!$member){
				$w['openid'] = $data['openid'];
				$w['litpic'] = $info['avatarUrl'];
				$w['city'] = $info['city'];
				$w['username'] = $info['nickName'];
				$w['province'] = $info['province'];
				$w['pid'] = $info['pid'];
				$r = M('member')->add($w);
				$info['id'] = $r;
				$info['username'] = $params['nickName'];
				$info['sfz'] = '';
				$info['truename'] = '';
				$info['card'] = '';
				$info['bank'] = '';
			}else{
			    unset($member['pass']);
			   $info = $member;
			}
			
			
		}
	    
	 
	    JsonReturn(['status'=>1,'openid'=>$data['openid'],'info'=>$info,'session_key'=>$data['session_key']]);
	}
	
	public function doCurl($url)
	{
	    $curl = curl_init();
	    // 使用curl_setopt()设置要获取的URL地址
	    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);
	    // 使用 curl_close() 关闭CURL会话
	    curl_close($curl);
		
	    return json_decode($data,1);
	}


暂无评论
登录后才可以评论~立即登录

如何获取资源?

您可以关注底部公众号,回复文章提示的 “ 关键词 ” 获取对应资源。

每日分享

每日分享收集的网络资源,其中包含开源项目、小工具、常用软件,以及音频视频、电子书籍等。

外卖天天领红包,饿了么,美团红包天天都有!

外卖天天领红包,饿了么,美团红...

最低每顿可省2元

分享两个远程工具-ToDesk和向日葵

分享两个远程工具-ToDesk和向日葵...

在工作生活中经常用到远程,QQ远程很卡,第三方远程工具就比较方便了。

爱奇艺万能播放器2018年完整版

爱奇艺万能播放器2018年完整版

2018年的爱奇艺万能播放器,功能齐全,没有广告!

分享500套个人求职简历模板

分享500套个人求职简历模板

500套免费求职简历下载!

一款最近很火的自动跳过广告APP,无root直装版

一款最近很火的自动跳过广告APP,...

自动跳过广告,无需root

WPS免费去广告电脑软件

WPS免费去广告电脑软件

wps广告实在太多了,下载一个去广告版本