<?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 bind extends controller{
	public  function index(){
		if(!C('MYDB')){
			$this->err('登陆后绑定');
		}
		$CODE=NOWTIME.rand(100000,999999);
		$_GET['codelogin']=$CODE;
		$this->display(tpl('bind'));
	}
	public function login(){
		if(!C('MYDB')){
			$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']));
			}
			hook_listen('wechat_bind_begin',$data);
			$fromurl=Cookie::set('fromurl',$_GET['fromurl']);
			if(!$fromurl){
				$fromurl=WEBURL;
			}
			$codelogin=cookie::get('codelogin');
			//先删除此微信绑定的账户
			db::update(jab.'user')->cols(['wechat_openid'=>''])->where("wechat_openid= '$data[openid]'")->query();
			
			$yzm=db::select('uid')->from(P_jab.'yzm')->where("code= '$codelogin'")->row();
			
			db::update(jab.'user')->cols(['wechat_openid'=>$data['openid']])->where("uid= ".$yzm['uid'])->query();
			
			db::update(P_jab.'yzm')->cols(['uid'=>'0'])->where("code='$codelogin'")->query();
			hook_listen('wechat_bind_end',$data);
			$this->success('绑定完成',	WEBURL);
		}
		
		if($_GET['fromurl']){
			Cookie::set('fromurl',$_GET['fromurl']);
		}
		
		if($_GET['codelogin']){
			Cookie::set('codelogin',$_GET['codelogin']);
		}

		//跳转到微信服务器再返回来，将会得到一个有效的code值。
		$url = urlencode(WEBURL.'/wechat/bind/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 testbind(){
		if(!C('MYDB')){
			$this->err('登陆后绑定');
		}
		$user=db::select('*')->from(P_jab.'yzm')->where("code= '$_GET[codelogin]'")->row();
		if($user['uid']=='0'){
			echo '1';
		}else{
			echo '0';
		}
	}
	
	//生成二维码，供PC使用
	public function qrcode_login(){
		if(!C('MYDB')){
			$this->err('登陆后绑定');
		}
		$array=['code'=>$_GET['codelogin'],'posttime'=>NOWTIME,'uid'=>C('MYDB.uid')];
		db::insert(P_jab.'yzm')->cols($array)->query();
		vendor('phpqrcode.phpqrcode');
		error_reporting(E_ERROR);
		$url = WEBURL.'/wechat/bind/login/codelogin/'.$_GET['codelogin'];
		\QRcode::png($url,false,'L','6','2');
	}
}
?>