<?php
namespace Wechat\Controller;

use Think\Controller;
use Think\Cache;
use Think\Log;

/**
* wechat
*/
header("Content-Type:text/html;charset=utf-8");
class Wechat extends Controller
{
	protected static $openid = '';
	protected static $unionid = '';
	protected static $session_key = '';

	public static function wechat(){
		$options = array(
			'type'  =>  'memcache', 
		    'expire'=>  7200, 
		    'prefix'=>  'finbrain_openid_wechat_',
		    'path'  =>  APP_PATH.'runtime/cache/',
			);
		Cache::connect($options);
	}
	/**
	 * 获取微信返回的用户信息
	 * @param  [type] $code [description]
	 * @return [type]       [description]
	 */
	public static function get_wechat_info($code)
	{
		//获取openID、unionid、session_key
		$url = 'https://api.weixin.qq.com/sns/jscode2session';
		$config = array(
			"appid"=>WechatConfig::APPID,
			"secret"=>WechatConfig::SECRET,
			"js_code"=>$code,
			"grant_type"=>"authorization_code"
			);
		$json = json($config);
		$info = Curl::get_curl($json,$url);
		$data = json_decode($info);

		self::$openid = $data['openid'];
		self::$unionid = $data['unionid'];
		self::$session_key = $data['session_key'];

		//获取用户信息
		$url = 'https://api.weixin.qq.com/cgi-bin/user/info';
		$config = array(
			"access_token"=>self::get_access_token(),
			"openid"=>self::$openid,
			"lang"=>"zh_CN"
			);
		$json = json($config);
		$info = Curl::get_curl($json,$url);
		return $info;
	}

	/**
	 * 发送模板通知消息
	 * @param  [type] $json [description]
	 * @return [type]       [description]
	 */
	public static function send_template($json)
	{
		$url ='https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token='.self::get_access_token();
		$return = Curl::post_curl($json,$url);

		$return = json_decode($return);

		if ($return['errcode']!=0) {
			Log::record('微信模板消息发送失败'.$return['errcode'].':'.$return['errmsg'],'error');
			return FALSE;
		}

		return TRUE;
	}

	/**
	 * 获取小程序码
	 * @param  [type] $json [description]
	 * @return [type]       [description]
	 */
	public static function getwxacodeunlimit($json)
	{
		$url ='https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token='.self::get_access_token();
		$return = Curl::post_curl($json,$url);

		$return = json_decode($return);

		if ($return['errcode']!=0) {
			Log::record('微信模板消息发送失败'.$return['errcode'].':'.$return['errmsg'],'error');
			return FALSE;
		}

		return TRUE;
	}

	/**
	 * 获取token
	 * @return [type] [description]
	 */
	public static function get_access_token()
	{	
		if (Cache::get('access_token')) {
			$access_token = Cache::get('access_token');
		}else{
			$url = "https://api.weixin.qq.com/cgi-bin/token";
			$config = array(
				"grant_type"=>"client_credential",
				"appid"=>WechatConfig::APPID,
				"secret"=>WechatConfig::SECRET
				);
			$json = json_encode($config);
			$token = Curl::get_curl($json,$url);
			$token = json_decode(stripslashes($token));
	        $arr = json_decode(json_encode($token), true);
	        $access_token = $arr['access_token'];

	        Cache::set('access_token',$access_token,7200);
		}

		return $access_token;
	}
}
