<?php
/**
  * @method 微信api
  * @author soul
  * @copyright 2016/3/31
  */
class MainWeiXin extends MainBase
{
	/**
	  * @method 获取公众号TOKEN		  
	  * @author soul
	  * @copyright 2018/3/5
	  * @return 
	  {"status":true,"data": url}
	  {"status":false,"data":'失败原因',"code":''}
	  */
	public function getGzhToken()
	{
		$YpwBase = new YpwBase();
		$res = $YpwBase->get('wx_apitoken', ['wa_token'], sprintf(" and wa_appid='%s' and wa_expires > %d order by wa_id desc limit 1", mvc::$cfg['WX']['gzh']['appid'], time()), true);
		if(!empty($res['data']['wa_token']))
		{
			return LibFc::ReturnData(true, $res['data']['wa_token']);
		}
		$LibCurl = new LibCurl();
		$url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.mvc::$cfg['WX']['gzh']['appid'].'&secret='.mvc::$cfg['WX']['gzh']['secret'];
		$tres = $LibCurl->fetch($url);
		$tres = json_decode($tres, true);
		if(isset($tres['access_token']))
		{
			$token = $tres['access_token'];
			$add = [
				'wa_appid'=>mvc::$cfg['WX']['gzh']['appid'],
				'wa_token'=>$token,
				'wa_expires'=>time()+600
			];
			$YpwBase->add('wx_apitoken', $add);
			return LibFc::ReturnData(true, $token);
		}
		else
		{
			return LibFc::ReturnData(false, '获取微信token失败'.$tres['errmsg']);
		}
	}


	/*
	<method>
		<name>获取用户信息</name>
		<remark></remark>
		<exp.>https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140839&token=&lang=zh_CN</exp.>
		<Parameter></Parameter>
		<return></return>
		<author>Soul 2017/1/9</author>
	</method>
	*/
	public function getAppUser($token, $openid)
	{
		$LibCurl = new 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 LibFc::ReturnData(false, $res['errmsg'], $res['errcode']);
		if($res['unionid'] && $res['nickname']){
			$res['endtime'] = time();
			$YpwBase = new YpwBase();
			$YpwBase->Set('yp_user', ['u_weixin_info'=>LibFc::jsonEncode($res)], sprintf(" AND u_weixin_unionid='%s'", $res['unionid']));
		}
		return LibFc::ReturnData(true, $res);
	}


	/**
	  * @method 获取用户信息
	  * @author soul
	  * @copyright 2017/8/16
	  * @return
			成功：['status'=>true, 'data'=>]
			失败：['status'=>false, 'data'=>'xxx', 'code'=>'xxx']
	  */
	public function getUserByCode($appKey, $userCode)
	{
		if(empty(mvc::$cfg['WX'][$appKey])) return LibFc::ReturnData(false, 'app key 不存在');
		$LibCurl = new 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['errcode']))
			return LibFc::ReturnData(false, $res['errmsg'], $res['errcode']);
		if( $res['scope'] == 'snsapi_base' || empty($res['access_token']))
			return LibFc::ReturnData(true, $res);
		return $this->getAppUser($res['access_token'], $res['openid']);
		
	}
}