|
@@ -0,0 +1,167 @@
|
|
|
+<?php
|
|
|
+/**
|
|
|
+ * Created by PhpStorm.
|
|
|
+ * User: stanley-king
|
|
|
+ * Date: 2017/6/20
|
|
|
+ * Time: 上午10:25
|
|
|
+ */
|
|
|
+
|
|
|
+namespace thrid_author;
|
|
|
+
|
|
|
+use Log;
|
|
|
+
|
|
|
+class signaturer
|
|
|
+{
|
|
|
+ const appid = 'wx6b42e00ecaade538';
|
|
|
+ const appsecret ='ee64233b3144d76217161666f8cb4c86';
|
|
|
+ const access_token_url = "https://api.weixin.qq.com/cgi-bin/token";
|
|
|
+ const ticket_url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket";
|
|
|
+
|
|
|
+ const prefix = 'signaturer';
|
|
|
+
|
|
|
+ private $mData;
|
|
|
+ private static $stInstance = null;
|
|
|
+
|
|
|
+ public static function instance()
|
|
|
+ {
|
|
|
+ if(self::$stInstance == null) {
|
|
|
+ self::$stInstance = new signaturer();
|
|
|
+ }
|
|
|
+ return self::$stInstance;
|
|
|
+ }
|
|
|
+
|
|
|
+ private function __construct()
|
|
|
+ {
|
|
|
+ $this->mData = [];
|
|
|
+ }
|
|
|
+
|
|
|
+ public function signurl()
|
|
|
+ {
|
|
|
+ $ticket = $this->jsapi_ticket();
|
|
|
+ if($ticket == false) {
|
|
|
+ Log::record(__METHOD__ . " sign error",Log::ERR);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
|
|
|
+ $url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
|
|
|
+
|
|
|
+ $timestamp = time();
|
|
|
+ $nonceStr = $this->noncestr();
|
|
|
+ $string = "jsapi_ticket={$ticket}&noncestr={$nonceStr}×tamp={$timestamp}&url=$url";
|
|
|
+ $signature = sha1($string);
|
|
|
+
|
|
|
+ $sign = array(
|
|
|
+ "appId" => signaturer::appid,
|
|
|
+ "nonceStr" => $nonceStr,
|
|
|
+ "timestamp" => $timestamp,
|
|
|
+ "url" => $url,
|
|
|
+ "signature" => $signature,
|
|
|
+ "rawString" => $string
|
|
|
+ );
|
|
|
+ return $sign;
|
|
|
+ }
|
|
|
+
|
|
|
+ private function noncestr($length = 16)
|
|
|
+ {
|
|
|
+ $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
|
|
+ $str = "";
|
|
|
+ for ($i = 0; $i < $length; $i++) {
|
|
|
+ $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
|
|
|
+ }
|
|
|
+ return $str;
|
|
|
+ }
|
|
|
+
|
|
|
+ private function jsapi_ticket()
|
|
|
+ {
|
|
|
+ $ticket = $this->rjsapi_ticket();
|
|
|
+ if($ticket == false)
|
|
|
+ {
|
|
|
+ $accessToken = $this->access_token();
|
|
|
+ if($accessToken == false) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ $params = ['type' => 'jsapi','access_token' => $accessToken];
|
|
|
+ $res = http_request(self::ticket_url,$params);
|
|
|
+ if($res == false) return false;
|
|
|
+
|
|
|
+ $res = json_decode($res,true);
|
|
|
+ if($res['errcode'] != 0) {
|
|
|
+ Log::record("jsapi_ticket error : code={$res['errcode']} msg={$res['errmsg']}",Log::ERR);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ $ticket['expires'] = time() + intval($res['expires_in']) - 5;
|
|
|
+ $ticket['ticket'] = $res['ticket'];
|
|
|
+
|
|
|
+ wcache('jsapi_ticket',$ticket,self::prefix);
|
|
|
+ $this->mData['jsapi_ticket'] = $ticket;
|
|
|
+ $ticket = $ticket['ticket'];
|
|
|
+ }
|
|
|
+
|
|
|
+ return $ticket;
|
|
|
+ }
|
|
|
+
|
|
|
+ private function rjsapi_ticket()
|
|
|
+ {
|
|
|
+ if(empty($this->mData) || empty($this->mData['jsapi_ticket']))
|
|
|
+ {
|
|
|
+ $items = rcache('jsapi_ticket',self::prefix);
|
|
|
+ if(empty($items)) {
|
|
|
+ return false;
|
|
|
+ } else {
|
|
|
+ $this->mData['jsapi_ticket'] = $items;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ $expires = $this->mData['jsapi_ticket']['expires'];
|
|
|
+ if(time() >= $expires) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ return $this->mData['jsapi_ticket']['ticket'];
|
|
|
+ }
|
|
|
+
|
|
|
+ private function access_token()
|
|
|
+ {
|
|
|
+ $acctoken = $this->raccess_token();
|
|
|
+ if($acctoken == false)
|
|
|
+ {
|
|
|
+ $params = ['grant_type' => 'client_credential','appid' => signaturer::appid,'secret' => signaturer::appsecret];
|
|
|
+ $res = http_request(self::access_token_url,$params);
|
|
|
+ if($res == false) return false;
|
|
|
+
|
|
|
+ $res = json_decode($res,true);
|
|
|
+
|
|
|
+ $token['expires'] = time() + intval($res['expires_in']) - 5;
|
|
|
+ $token['token'] = $res['access_token'];
|
|
|
+
|
|
|
+ wcache('access_token',$token,self::prefix);
|
|
|
+ $this->mData['access_token'] = $token;
|
|
|
+ $acctoken = $token['token'];
|
|
|
+ }
|
|
|
+
|
|
|
+ return $acctoken;
|
|
|
+ }
|
|
|
+
|
|
|
+ private function raccess_token()
|
|
|
+ {
|
|
|
+ if(empty($this->mData) || empty($this->mData['access_token']))
|
|
|
+ {
|
|
|
+ $token = rcache('access_token',self::prefix);
|
|
|
+ if(empty($token)) {
|
|
|
+ return false;
|
|
|
+ } else {
|
|
|
+ $this->mData['access_token'] = $token;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ $expires = $this->mData['access_token']['expires'];
|
|
|
+ if(time() >= $expires) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ return $this->mData['access_token']['token'];
|
|
|
+ }
|
|
|
+}
|