| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 | 
							- <?php
 
- namespace Cas\Controller;
 
- use KIF\Cache\Memcached;
 
- use KIF\Core\Controller;
 
- use KIF\Core\Request;
 
- use Cas\Module\RefreshAccessToken;
 
- use KIF\Core\Config;
 
- /**
 
-  * WeixinJsSDK 用于微信 js 调用 
 
-  * 吐出相应数据
 
-  * 可查看:http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html
 
-  *
 
-  * @author lihuanchun
 
-  *        
 
-  */
 
- class WeixinJsSDK extends Controller {
 
- 	private $objMemache;
 
- 	public function __construct() {
 
- 		$this->objMemache = new Memcached ();
 
- 	}
 
- 	
 
- 	/**
 
- 	 * ajax 返回微信所需js配置信息
 
- 	 * http://cas.lishuy.com/?c=WeixinJsSDK&a=AjaxConfigData
 
- 	 */
 
- 	public function doAjaxConfigData() {
 
- 		$thisPageUrl = Request::g ( 'thisPageUrl' );
 
- 		$returnData = $this->getSignPackage ( $thisPageUrl );
 
- 		$this->ajax_success_exit ( $returnData );
 
- 	}
 
- 	
 
- 	/**
 
- 	 * getSignPackage
 
- 	 */
 
- 	private function getSignPackage($url = null) {
 
- 		$wechat_cfg = Config::getInstance ()->get ( 'wechat_cfg' );
 
- 		$appid = $wechat_cfg ['appId'];
 
- 		
 
- 		$jsapiTicket = $this->getJsApiTicket ();
 
- 		if ($url == null) {
 
- 			$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
 
- 		} else {
 
- 			$url = htmlspecialchars_decode ( $url );
 
- 		}
 
- 		
 
- 		$timestamp = time ();
 
- 		$nonceStr = $this->createNonceStr ();
 
- 		$string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr×tamp=$timestamp&url=$url";
 
- 		$signature = sha1 ( $string );
 
- 		$signPackage = array (
 
- 				"appId" => $appid,
 
- 				"nonceStr" => $nonceStr,
 
- 				"timestamp" => $timestamp,
 
- 				"url" => $url,
 
- 				"signature" => $signature,
 
- 				"rawString" => $string 
 
- 		);
 
- 		return $signPackage;
 
- 	}
 
- 	
 
- 	/**
 
- 	 * createNonceStr
 
- 	 */
 
- 	private function createNonceStr($length = 16) {
 
- 		$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
 
- 		$str = "";
 
- 		for($i = 0; $i < $length; $i ++) {
 
- 			$str .= substr ( $chars, mt_rand ( 0, strlen ( $chars ) - 1 ), 1 );
 
- 		}
 
- 		return $str;
 
- 	}
 
- 	
 
- 	/**
 
- 	 * getJsApiTicket
 
- 	 */
 
- 	private function getJsApiTicket() {
 
- 		$ticket = $this->objMemache->get ( RefreshAccessToken::JSAPI_TICKET_KEY );
 
- 		return $ticket;
 
- 	}
 
- 	
 
- 	/*
 
- 	 * (non-PHPdoc)
 
- 	 * @see \KIF\Core\Controller::run()
 
- 	 */
 
- 	public function run() {
 
- 		$action = $this->action;
 
- 		$this->$action ();
 
- 	}
 
- }
 
 
  |