123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- <?php
- /**
- * Created by PhpStorm.
- * User: stanley-king
- * Date: 2017/6/20
- * Time: 上午10:25
- */
- namespace thrid_author;
- require_once(BASE_CORE_PATH . "/framework/function/http.php");
- use bonus\parameters;
- use Log;
- class signaturer
- {
- const appid = PUB_APPID;
- const appsecret = PUB_APPSECRET;
- 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 createwxaqrcode_url = "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode";
- const getwxacode_url = "https://api.weixin.qq.com/wxa/getwxacode";
- const send_submsg_url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send";
- const prefix = 'signaturer';
- const try_sleep = 5;
- 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($sign_url)
- {
- $ticket = $this->rjsapi_ticket();
- if($ticket == false) {
- Log::record(__METHOD__ . " sign error",Log::ERR);
- return false;
- }
- $timestamp = time();
- $nonceStr = $this->noncestr();
- $string = "jsapi_ticket={$ticket}&noncestr={$nonceStr}×tamp={$timestamp}&url={$sign_url}";
- $signature = sha1($string);
- Log::record(__METHOD__ . " signurl={$sign_url} noncestr={$nonceStr} timestamp={$timestamp} ticket={$ticket} signature={$signature}",Log::DEBUG);
- return ["appid" => signaturer::appid,"noncestr" => $nonceStr,"timestamp" => $timestamp,"signature" => $signature];
- }
- public function minicode($path,$width)
- {
- $token = $this->access_token();
- if($token == false) return false;
- $access_token = $token['token'];
- $param = ['path' => $path,'width' => $width];
- $url = self::getwxacode_url . "?access_token={$access_token}";
- $resp = http_post_data($url,json_encode($param));
- return $resp;
- }
- public function send_submsg($openid,$tmpid,$page,$data)
- {
- $token = $this->access_token();
- if($token == false) return false;
- $access_token = $token['token'];
- $url = self::send_submsg_url . "?access_token={$access_token}";
- $param = ['touser' => $openid,'template_id' => $tmpid,'page' => $page,'data' => $data];
- $resp = http_post_data($url,json_encode($param));
- $resp = json_decode($resp,true);
- if($resp['errcode'] == 0 || $resp['errmsg'] == 'ok') {
- return true;
- }
- else {
- Log::record("send_submsg errmsg:{$resp['errmsg']}",Log::ERR);
- return false;
- }
- }
- 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 rjsapi_ticket()
- {
- if(empty($this->mData) || empty($this->mData['jsapi_ticket'])) {
- $fReadCache = true;
- }
- else
- {
- $expires = $this->mData['jsapi_ticket']['expires'];
- if(time() >= $expires) {
- $fReadCache = true;
- } else {
- $fReadCache = false;
- }
- }
- if($fReadCache)
- {
- $items = rcache('jsapi_ticket',self::prefix);
- if(empty($items)) {
- $this->mData['jsapi_ticket'] = $this->request();
- }
- else
- {
- $expires = intval($items['expires']);
- if($expires <= time()) {
- $this->mData['jsapi_ticket'] = $this->request();
- } else {
- $this->mData['jsapi_ticket'] = $items;
- }
- }
- }
- return $this->mData['jsapi_ticket']['ticket'];
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- public function request()
- {
- while (true)
- {
- $token = $this->access_token();
- if($token != false) {
- break;
- } else {
- sleep(signaturer::try_sleep);
- }
- }
- $accessToken = $token['token'];
- while (true)
- {
- $ticket = $this->jsapi_ticket($accessToken);
- if($ticket != false) {
- break;
- }
- else {
- sleep(signaturer::try_sleep);
- }
- }
- $ticket['expires'] = intval($ticket['expires'] / 2 + time());
- wcache('jsapi_ticket',$ticket,self::prefix);
- Log::record(__METHOD__ . " ticket={$ticket['ticket']}",Log::DEBUG);
- return $ticket;
- }
- private function jsapi_ticket($accessToken)
- {
- $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'] = intval($res['expires_in']);
- $ticket['ticket'] = $res['ticket'];
- return $ticket;
- }
- private function access_token()
- {
- $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);
- if(array_key_exists('errcode',$res)) {
- $code = $res['errcode'];
- $msg = $res['errmsg'];
- Log::record("signurl access_token error code={$code} msg={$msg}",Log::DEBUG);
- return false;
- }
- else {
- $token['expires'] = intval($res['expires_in']);
- $token['token'] = $res['access_token'];
- return $token;
- }
- }
- }
|