与老涂一起写代码

uniapp小程序登录功能实现

admin 187 ℃ 0 条
uniapp小程序登录功能实现

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);
	}


上一篇:没有了

下一篇:微信小程序开发相关常用代码

发表评论 (已有0条评论)

快来评论,快来抢沙发吧~