| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517 | <?php/*|--------------------------------------------------------------------------| core.php 微信平台核心类|--------------------------------------------------------------------------*/namespace Token\Lib;use Dever;class Wechat{	/**	 * config	 *	 * @var array	 */	private $config;		/**	 * project	 *	 * @var string	 */	private $project;	/**	 * alert	 *	 * @var int	 */	private $alert;		/**	 * 构造函数 初始化	 * 	 * @return mixed	 */	public function __construct($project = false, $type = '')	{		$this->config = Dever::config('wechat', $type)->cAll;		$type = $this->config['type'];		if (!$project) {			$appid = Dever::input('appid');			if ($appid) {				$project = Dever::db('token/project')->one(array('option_type' => $type, 'option_appid' => $appid));			}			/*			if (!$project) {				$project = Dever::db('token/project')->one(array('option_type' => $type));			}			*/			if (!$project) {				$project = Dever::input('project', 1);			}		}		if (is_numeric($project)) {			$project = Dever::db('token/project')->one(array('option_type' => $type, 'option_id' => $project));		}		if (!$project) {			Dever::alert('project is not exits!');		}		$this->project = $project;	}		/**	 * 获取当前站点的配置	 * 	 * @return mixed	 */	public function project()	{		return $this->project;	}	/**	 * 获取当前基本的配置	 * 	 * @return mixed	 */	public function config()	{		return $this->config;	}	/**	 * 更新数据库	 * state true为强制更新数据库中的token数据	 * 	 * @return mixed	 */	private function save($type = 'ticket', $value, $expires = false, $interval = 4000, $data = false, $state = false)	{		$refresh = false;		if (strpos($type, '.')) {			$temp = explode('.', $type);			if ($temp[1] == 'refresh') {				$refresh = true;				$temp[1] = 'oauth';			}			$table = 'token/' . $temp[1];		} else {			$table = 'token/' . $type;		}		$db = Dever::db($table);		if ($data) {			if (isset($data['id'])) {				$where['option_id'] = $data['id'];				unset($data['id']);			}			if (isset($data['openid'])) {				$where['option_openid'] = $data['openid'];			}			if (isset($data['unionid'])) {				$where['option_unionid'] = $data['unionid'];			}		}				$where['option_project_id'] = $this->project['id'];		$info = $db->one($where);		$update = false;					if ($state == true) {			$update = true;		} elseif ($info && time() - $info['mdate'] >= $info['expires']) {			$update = true;		} elseif($info) {			return $info;		}		if (!$info) {			$update = false;		}		if (!$value) {			$value = $this->param($type, $info);		}		if (is_array($value) || (is_string($value) && strstr($value, 'http://'))) {			if ($refresh) {				$result = $this->curl(false, $value, false, $type);			} else {				$result = $this->curl(false, $value, true, $type);			}						$key = $type;			if ($result && isset($result[$key])) {				$data = $result;				$data['value'] = $result[$key];				$data['expires'] = $result['expires_in'];			}		} else {			$data['value'] = $value;			$data['expires'] = $expires;		}				if ($data && isset($data['value']) && $data['value']) {			$data['project_id'] = $this->project['id'];			$expires = $data['expires'] - $interval;			if ($expires <= 0) {				$data['expires'] = $data['expires'] - 300;			} else {				$data['expires'] = $expires;			}			if ($update == true) {				$data['where_id'] = $info['id'];				$id = $info['id'];				$db->update($data);			} else {				$id = $db->insert($data);			}			$data['id'] = $id;			if ($id > 0 && isset($result['callback'])) {				foreach ($result['callback'] as $v) {					Dever::load($v[0], $id, $v[1], $this->project['id']);				}			}		} elseif($info && $info['value']) {			$data = $info;			$data['value'] = $info['value'];		}		return $data;	}	/**	 * 获取最新的token	 * 	 * @return mixed	 */	public function token($value = false, $expires = false, $interval = 4000, $state = false)	{		$result = $this->save('token', $value, $expires, $interval, false, $state);		return $result['value'];	}	/**	 * 获取最新的ticket	 * 	 * @return mixed	 */	public function ticket($value = false, $expires = false, $interval = 200, $state = false)	{		$result = $this->save('ticket', $value, $expires, $interval, false, $state);		return $result['value'];	}	/**	 * 获取最新的oauth token	 * 	 * @return mixed	 */	public function oauth($param, $interval = 4000, $state = false)	{		if (is_array($param) && isset($param['auth_code'])) {			$result = $this->curl('oauth.oauth', $param);			if (isset($result['oauth.oauth'])) {				return $this->save('oauth.oauth', $result['oauth.oauth'], $result['expires_in'], $interval, $result);			} else {				Dever::alert('oauth error');			}		} else {			if (is_numeric($param)) {				$id = $param;				$param = array();				$param['id'] = $id;			}			return $this->save('oauth.refresh', false, false, $interval, $param, $state);		}	}	/**	 * 获取最新的code	 * 	 * @return mixed	 */	public function code($value = false, $expires = false, $interval = 200)	{		$result = $this->save('oauth.code', $value, $expires, $interval, false, true);		return $result['value'];	}	/**	 * 获取最新的openid	 * 	 * @return mixed	 */	public function openid($id, $key = 'openid')	{		$info = Dever::db('token/oauth')->one($id);		return $info[$key];	}	/**	 * 获取最新的oauth login地址	 * 	 * @return mixed	 */	public function login($callback = false, $code = false, $location = true)	{		if (!$code) {			$code = $this->code();		}		$callback = Dever::url($callback);		$config = $this->param('oauth.login', array('code' => $code, 'redirect' => $callback));		$url = $config['url'] . http_build_query($config['param']);		if ($location == true) {			Dever::location($url);		}		return $url;	}	/**	 * 从wechat获取数据	 * 	 * @return mixed	 */	public function curl($method, $param = array(), $alert = true, $type = false)	{		if (!$this->alert) {			if (!$alert) {				$this->alert = 1;			} else {				$this->alert = 2;			}		}				//return array();		if (is_string($param)) {			$result = json_decode(Dever::curl($param), true);		} else {			if ($method) {				$param = $this->param($method, $param);			}			$result = Dever::curl($param['url'], $param['param'], $param['method'], $param['json']);			if (isset($param['response']['img'])) {				return $result;			}			$result = json_decode($result, true);			$this->log($result, $param['name'], $param['url'], $param['param']);		}		if (isset($result['errcode']) && $result['errcode'] != 0) {			if ($result['errcode'] == 40001) {				$token = $this->token(false, false, 4000, true);				return $this->curl($type, array(), $alert);			}			$result = $param + $result;			Dever::log($result);			if ($alert && $this->alert == 2) {				Dever::alert(json_encode($result, JSON_UNESCAPED_UNICODE));			}		}		if (isset($param['response'])) {			foreach ($param['response'] as $k => $v) {				if (strpos($k, '.')) {					$temp = explode('.', $k);					if (isset($result[$temp[0]][$temp[1]])) {						$result[$v] = $result[$temp[0]][$temp[1]];					}				} elseif (isset($result[$k])) {					$result[$v] = $result[$k];				}				if (strpos($v, 'callback.') !== false) {					$temp = explode('callback.', $v);					$result['callback'][] = array($temp[1], $result[$v]);				}			}		}		return $result;	}	public function log($result, $name, $url, $param)	{		$insert['name'] = $name;		$insert['url'] = $url;		$insert['result'] = json_encode($result, JSON_UNESCAPED_UNICODE);		$insert['param'] = json_encode($param, JSON_UNESCAPED_UNICODE);		Dever::db('token/log')->insert($insert);	}	/**	 * 拼装wechat需要的参数	 * 	 * @return mixed	 */	public function param($method, $param = array())	{		if (strpos($method, '.')) {			$temp = explode('.', $method);			$config = $this->config[$temp[0]][$temp[1]];		} else {			if (!isset($this->config[$method])) {				return false;			}			$config = $this->config[$method];		}		if (!$param) {			$param = array();		}				$param = $this->project + $param;		//print_r($param);die;		foreach ($config['param'] as $k => $v) {			if ($v == 'token') {				$config['url'] .= $k . '=' . $this->token();				unset($config['param'][$k]);			} elseif ($v == 'ticket') {				$config['param'][$k] = $this->ticket();			} elseif ($v == 'code') {				$config['param'][$k] = $this->code();			} elseif ($v == 'oauth') {				if (!isset($param['oauth'])) {					Dever::alert('oauth erorr');				} elseif (is_numeric($param['oauth'])) {					$oauth = $this->oauth($param['oauth']);					$param['oauth'] = $oauth['value'];				}				$config['url'] .= $k . '=' . $param['oauth'];				unset($config['param'][$k]);			} elseif (!is_array($v) && isset($param[$v])) {				$config['param'][$k] = $param[$v];			} elseif($v) {				$config['param'][$k] = $this->replace($v, $param);			}		}		$config['json'] = isset($config['json']) && $config['json'] ? true : false;		return $config;	}	/**	 * 替换{}	 * 	 * @return mixed	 */	public function replace($value, $param)	{		if (isset($param['domain']) && is_string($value) && strpos($value, '{domain}') !== false) {			foreach ($param['domain'] as $k => $v) {				$param['domain'][$k] = str_replace('{domain}', $v, $value);			}			return $param['domain'];		}		if (is_array($value)) {			foreach ($value as $k => $v) {				if (isset($param[$v])) {					$value[$k] = $param[$v];				} else {					$value[$k] = $this->replace($v, $param);				}			}			$value = Dever::json_encode($value);		}		return $value;	}	/**	 * 消息解密	 * 	 * @return mixed	 */	public function decode($sign, $timestamp, $nonce, $encrypt)	{		include(DEVER_PROJECT_PATH . 'example/wxBizMsgCrypt.php');		$crypt = new \WXBizMsgCrypt($this->project['token'], $this->project['key'], $this->project['appid']);		$format = "<xml><ToUserName><![CDATA[toUser]]></ToUserName><Encrypt><![CDATA[%s]]></Encrypt></xml>";		$xml = sprintf($format, $encrypt);		$result = '';		$code = $crypt->decryptMsg($sign, $timestamp, $nonce, $xml, $result);		if ($code == 0) {			libxml_disable_entity_loader(true);		  	$result = (array) simplexml_load_string($result, 'SimpleXMLElement', LIBXML_NOCDATA);		}		return $result;	}		/**	 * 获取nonce	 * 	 * @return mixed	 */	public function nonce()	{		return substr(md5(microtime()), rand(10, 15));	}	/**	 * 获取signature	 * 	 * @return mixed	 */	public function signature($ticket, $url, $timestamp, $noncestr)	{		/*		$info = array();		$info['jsapi_ticket'] = $ticket;		$info['url'] = $url;		$info['timestamp'] = $timestamp;		$info['noncestr'] = $noncestr;		ksort($info);		*/		$signature_string = "jsapi_ticket=$ticket&noncestr=$noncestr×tamp=$timestamp&url=$url";		//$signature_string = substr(http_build_query($info), 0, -1);		return sha1($signature_string);	}	/**	 * 获取sign签名数据包	 * 	 * @return mixed	 */	public function sign($url)	{		$ticket = $this->ticket();		if (!$url) {			$url = Dever::url();		} else {			$url = urldecode($url);		}		$timestamp = time();		$noncestr = $this->nonce();		$signature = $this->signature($ticket, $url, $timestamp, $noncestr);		$sign = array();		$sign['appId'] = $this->project['appid'];		$sign['nonceStr'] = $noncestr;		$sign['timestamp'] = $timestamp;		$sign['url'] = $url;		$sign['signature'] = $signature;		return $sign;			}}
 |