<?php
/*
 +----------------------------------------------------------------------
 | TEMMOKUMVC [ NO BEST , ONLY BETTER ]
 +----------------------------------------------------------------------
 | Copyright (c) 2018~2019 https://www.temmoku.cn All rights reserved.
 +----------------------------------------------------------------------
 | Author: 张宗强 Email:webmaster@temmoku.cn QQ:158726877 516669373 TEL:17895221001 微信:temmokumvc
 +---------------------------------------------------------------------- 
*/
namespace plugin\wechat\home\controller;
use \temmoku\db;
use \temmoku\controller;
use \temmoku\lib\cookie;
use \plugin\wechat\public_class\wechat;
class login extends controller{
	

	public  function index(){
		if(C('wechat|WECHAT.is_login')==0){
			$this->err('暂停微信登陆');
		}
		$CODE=NOWTIME.rand(100000,999999);
		$_GET['codelogin']=$CODE;
		if(DEVICE != 'pc'){
			$this->assign('webname','微信登陆');
		}
		$this->display(tpl('login'));
	}
	public function login(){
		if(C('wechat|WECHAT.is_login')==0){
			$this->err('暂停微信登陆');
		}
		if($_GET['state']==1){
			if(!$_GET['code']){
				$this->err('没有获取到必须的code值');
			}
			$url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=".C('wechat|wechat.appid')."&secret=".C('wechat|wechat.appsecret')."&code=".$_GET['code']."&grant_type=authorization_code";
			$data = json_decode(http_request($url),true);
			if(!$data['openid']){
				$this->err(C('wechat|errcode.'.$data['errcode']));
			}
			
			$fromurl=Cookie::set('fromurl',$_GET['fromurl']);
			if(!$fromurl){
				$fromurl=WEBURL;
			}
			$codelogin=cookie::get('codelogin');
			//查询是否已经绑定,绑定直接登陆。
			$user=db::select('*')->from(jab.'user')->where("wechat_openid= '$data[openid]'")->row();
			if($user){
				if($codelogin){
					db::update(P_jab.'yzm')->cols(['uid'=>$user['uid']])->where("code='$codelogin'")->query();
				}
				$ORDINARY_MEMBER=encode($user['uid'].' '.$user['password'].' '.C('ONLINEIP'));
				Cookie::set('ORDINARY_MEMBER',$ORDINARY_MEMBER);
				$this->success('登陆成功,正在为你跳转',$fromurl);
			}
			//没有绑定说明是新账户。直接注册
			$uid=(new wechat)->reg($data['openid']);
			if(is_array($uid)){
				$this->err(C('wechat|errcode.'.$res['errcode']));
			}
			$user=db::select('*')->from(jab.'user')->where("uid= '$uid'")->row();
			if($codelogin){
				db::update(P_jab.'yzm')->cols(['uid'=>$uid])->where("code='$codelogin'")->query();
			}
			$ORDINARY_MEMBER=encode($user['uid'].' '.$user['password'].' '.C('ONLINEIP'));
			Cookie::set('ORDINARY_MEMBER',$ORDINARY_MEMBER);
			hook_listen('wechat_login',$user);
			$this->success('登陆成功,正在为你跳转',$fromurl);
		}
		
		if($_GET['fromurl']){
			Cookie::set('fromurl',$_GET['fromurl']);
		}
		
		if($_GET['codelogin']){
			Cookie::set('codelogin',$_GET['codelogin']);
		}
		
		//跳转到微信服务器再返回来，将会得到一个有效的code值。
		$url = urlencode(WEBURL.'/wechat/login/login');
		$appid=C('wechat|wechat.appid');
		header("location:https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appid&redirect_uri=$url&response_type=code&scope=snsapi_base&state=1#wechat_redirect");
		exit;
	}
	
	//检测是否登陆了PC，专用
	public function testlogin(){
		$user=db::select('uid')->from(P_jab.'yzm')->where("code= '$_GET[codelogin]'")->row();
		if($user['uid']){
			$data=(new \temmoku\lib\user)->get_user_uid($user['uid']);
			if($data['code']=='0'){
				$ORDINARY_MEMBER=encode($data['data']['uid'].' '.$data['data']['password'],C('ONLINEIP'));
				Cookie::set('ORDINARY_MEMBER',$ORDINARY_MEMBER);
				echo '1';
			}else{
				echo '0';
			}
		}
		echo '0';
	}
	
	//生成二维码，供PC使用
	public function qrcode_login(){
		if(C('wechat|WECHAT.is_login')==0){
			$this->err('暂停微信登陆');
		}
		$array=['code'=>$_GET['codelogin'],'posttime'=>NOWTIME];
		db::insert(P_jab.'yzm')->cols($array)->query();
		vendor('phpqrcode.phpqrcode');
		error_reporting(E_ERROR);
		$url = WEBURL.'/wechat/login/login/codelogin/'.$_GET['codelogin'].'/fromurl/'.urlencode(urlencode(FromUrl));
		\QRcode::png($url,false,'L','6','2');
	}
}
?>