<?php
namespace model;
use \model;
class wx extends model
{
	/**
	  * @method 获取公众号TOKEN		  
	  * @return 
	  */
	public function getToken()
	{
		$LibCurl = new \lib\LibCurl();
		$cof = \mvc::$cfg['WX']['gzh'];
		$url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$cof['appid'].'&secret='.$cof['secret'];
		$tres = $LibCurl->fetch($url);
		$tres = json_decode($tres, true);
		if(isset($tres['access_token']))
		{
			return $tres['access_token'];
		}
		return ['code'=>0, 'msg'=>'获取微信token失败'];
	}

	/*
	<method>
		<name>获取微信js签名信息</name>
	</method>
	*/
	public function getSign()
	{
		$wxToken = $this->getToken();
		if(!empty($wxToken['msg'])) return $wxToken;
		$cof = \mvc::$cfg['WX']['gzh'];
		$jssdk = new \lib\JSSDK($cof['appid'], $cof['secret'], $wxToken);
		$signPackage = $jssdk->getSignPackage();
		return $signPackage;
	}

	/**
	  * @method 生成获取微信用户信息的链接[登录、绑定.....]
	  * @http	get
	  * @param  token              string [必填] 调用接口凭证 (post|get)
	  * @param  app                enum   [必填] 应用标识 gzh_app || ....
	  * @param  backUrl            string [必填] http://xxx/login/check 获取成功后需要跳转的URL
	  * @param  scope              string [可填] 微信公众平台（微信内使用）：snsapi_base、 snsapi_userinfo   微信开放平台：snsapi_login
												snsapi_base（不弹出授权页面，直接跳转，只能获取用户openid）
												snsapi_userinfo（弹出授权页面，可通过openid拿到昵称、性别、所在地。并且， 即使在未关注的情况下，只要用户授权，也能获取其信息 ）
												  
	  */
	public function getWxUserUrl($Params)
	{
		$cof = \mvc::$cfg['WX'][$Params['app']];
		$redirect_uri = urlencode($Params['backUrl']);
		if($cof['type'] == 'gzh')
		{
			if(empty($Params['scope'])) $Params['scope'] = 'snsapi_userinfo';
			$url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$cof['appid'].'&redirect_uri='.$redirect_uri.'&response_type=code&scope='.$Params['scope'].'&state=STATE#wechat_redirect';
		}
		else
		{
			if(empty($Params['scope'])) $Params['scope'] = 'snsapi_login';
			$url = 'https://open.weixin.qq.com/connect/qrconnect?appid='.$cof['appid'].'&redirect_uri='.$redirect_uri.'&response_type=code&scope='.$Params['scope'];
		}
		return ['url'=>$url];
	}



	/*
	<method>
		<name>获取用户信息</name>
		<remark></remark>
		<exp.>https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140839&token=&lang=zh_CN</exp.>
	</method>
	*/
	public function getAppUser($token, $openid)
	{
		$LibCurl = new \lib\LibCurl();
		$url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$token.'&openid='.$openid;
		$res = $LibCurl->fetch($url);
		$res = json_decode($res, true);
		if(!empty($res['errcode']))
			return ['code'=>0, 'msg'=>$res['errmsg']];
		return $res;
	}


	/**
	  * @method 获取用户信息
	  */
	public function getUserByCode($appKey, $userCode)
	{
		if(empty(\mvc::$cfg['WX'][$appKey])) return ['code'=>0, 'msg'=>'app key 不存在'];
		$LibCurl = new \lib\LibCurl();
		$cof = \mvc::$cfg['WX'][$appKey];
		if($cof['type'] == 'xcx')
		{
			$url = 'https://api.weixin.qq.com/sns/jscode2session?appid='.$cof['appid'].'&secret='.$cof['secret'].'&js_code='.$userCode.'&grant_type=authorization_code';
		}
		else
		{
			$url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$cof['appid'].'&secret='.$cof['secret'].'&code='.$userCode.'&grant_type=authorization_code';
		}
		$res = $LibCurl->fetch($url);
		$res = json_decode($res, true);
		if(!empty($res['msg']))
			return ['code'=>0, 'msg'=>$res['errmsg']];
		if( $res['scope'] == 'snsapi_base' || empty($res['access_token']))
			return $res;
		return $this->getAppUser($res['access_token'], $res['openid']);
		
	}

}