<?php
/*
* Copyright (c)
* All rights reserved.
*
* 文件名稱: wx_jssdk
* 摘 要: 微信js_sdk config接口注入
*
* $Id: wx_jssdk.php 162850 2019-04-02 09:13:15Z niezichuan $
*/
require_once dirname(__FILE__) . "/../function/cache.php";
class JSSDK
{
private $appId;
private $appSecret;
private $mMemcache = null;
public $url;
public function __construct($appId, $appSecret)
{
$this->appId = $appId;
$this->appSecret = $appSecret;
$this->getMemcacheHost();
$this->url = -1;
}
//獲取注入信息用於調用基礎接口
public function getSignPackage()
{
$jsapiTicket = $this->getJsApiTicket();
// 注意 URL 必定要動態獲取,不能 hardcode.
if($this->url >= 0)
{
$url = $this->url;
}
else
{
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
// $url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$url = "https://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
}
$timestamp = time();
$nonceStr = $this->createNonceStr();
// 這裏參數的順序要按照 key 值 ASCII 碼升序排序
$string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr×tamp=$timestamp&url=$url";
$signature = sha1($string);
$signPackage = array(
"appId" => $this->appId,
"nonceStr" => $nonceStr,
"timestamp" => $timestamp,
"url" => $url,
"signature" => $signature,
"rawString" => $string
);
return $signPackage;
}
//生成簽名隨機串
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;
}
//獲取ticket
private function getJsApiTicket()
{
// jsapi_ticket 應該全局存儲與更新,如下代碼以寫入到文件中作示例
$data = json_decode($this->mMemcache->get('jsapi_ticket_' . $this->appId));
if ($data->expire_time < time()) {
$accessToken = $this->getAccessToken();
// 獲取 ticket
$url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken";
$res = json_decode($this->httpGet($url));
$ticket = $res->ticket;
if ($ticket) {
$data->expire_time = time() + 7000;
$data->jsapi_ticket = $ticket;
$this->mMemcache->set("jsapi_ticket_" . $this->appId, json_encode($data), 0,MEMCACHE_LONG);
}
} else {
$ticket = $data->jsapi_ticket;
}
return $ticket;
}
//獲取accesstoken
private function getAccessToken()
{
// access_token 應該全局存儲與更新,如下代碼以寫入到文件中作示例
$data = json_decode($this->mMemcache->get("jsapi_access_token_" . $this->appId));
if ($data->expire_time < time()) {
// 獲取access_token
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appId&secret=$this->appSecret";
$res = json_decode($this->httpGet($url));
$access_token = $res->access_token;
if ($access_token) {
$data->expire_time = time() + 7000;
$data->access_token = $access_token;
$this->mMemcache->set("jsapi_access_token_" . $this->appId, json_encode($data), 0,MEMCACHE_LONG);
}
} else {
$access_token = $data->access_token;
}
return $access_token;
}
//調用接口獲取數據
private function httpGet($url)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 500);
// 爲保證第三方服務器與微信服務器之間數據傳輸的安全性,全部微信接口採用https方式調用,必須使用下面2行代碼打開ssl安全校驗。
// 若是在部署過程當中代碼在此處驗證失敗,請到 http://curl.haxx.se/ca/cacert.pem 下載新的證書判別文件。
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 2);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($curl, CURLOPT_URL, $url);
$res = curl_exec($curl);
curl_close($curl);
return $res;
}
//引入memcacahe
public function getMemcacheHost()
{
global $mem;
$this->mMemcache = $mem;
}
}