浏览代码

add jsapi signature

stanley-king 7 年之前
父节点
当前提交
be84532a48
共有 5 个文件被更改,包括 189 次插入3 次删除
  1. 167 0
      helper/third_author/signaturer.php
  2. 1 1
      mobile/control/bonusex.php
  3. 12 0
      mobile/control/mshop.php
  4. 2 2
      mobile/util/errcode.php
  5. 7 0
      test/TestAuthor.php

+ 167 - 0
helper/third_author/signaturer.php

@@ -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}&timestamp={$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'];
+    }
+}

+ 1 - 1
mobile/control/bonusex.php

@@ -1040,7 +1040,7 @@ function bonus_out_download()
 {
     if(!session_helper::isapp()) {
         echo '<div class="use_bonus">
-                <a href=" " class="btn_use_bonus" id="link">打开APP使用</a >
+                <a href=" " class="btn_use_bonus" id="link">打开APP,查看更多商品</a >
               </div>';
     }
 }

+ 12 - 0
mobile/control/mshop.php

@@ -16,6 +16,8 @@ require_once(BASE_ROOT_PATH . '/helper/third_author/wxauthor.php');
 require_once(BASE_ROOT_PATH . '/mobile/control/special.php');
 require_once (BASE_ROOT_PATH . '/helper/third_author/wxauthor.php');
 require_once(BASE_ROOT_PATH . '/helper/session_helper.php');
+require_once (BASE_ROOT_PATH . '/helper/third_author/signaturer.php');
+
 
 class mshopControl extends specialControl
 {
@@ -117,4 +119,14 @@ class mshopControl extends specialControl
         if(empty($special)) return false;
         return $special[0]['special_desc'];
     }
+
+    public function signurlOp()
+    {
+        $result = thrid_author\signaturer::instance()->signurl();
+        if($result == false) {
+            return self::outerr(errcode::ErrAuthor);
+        } else {
+            return self::outsuccess($result);
+        }
+    }
 }

+ 2 - 2
mobile/util/errcode.php

@@ -85,10 +85,10 @@ class errcode extends SplEnum
     const ErrMemberExist = 13004;
     const ErrWxNotExist = 13005;
     const ErrLoginType = 13006;
-
     const ErrGetConfig = 14000;
     const ErrUpfile = 15000;
-    
+    const ErrAuthor = 16000;
+
     static function msg($code)
     {
         switch ($code) {

+ 7 - 0
test/TestAuthor.php

@@ -11,6 +11,7 @@ define('BASE_ROOT_PATH',str_replace('/test','',dirname(__FILE__)));
 
 require_once (BASE_ROOT_PATH . '/fooder.php');
 require_once (BASE_ROOT_PATH . '/helper/third_author/wxauthor.php');
+require_once (BASE_ROOT_PATH . '/helper/third_author/signaturer.php');
 require_once (BASE_ROOT_PATH . '/helper/login_helper.php');
 
 class TestAuthor extends PHPUnit_Framework_TestCase
@@ -34,4 +35,10 @@ class TestAuthor extends PHPUnit_Framework_TestCase
         $val = 'Mozilla/5.0 (iPhone; CPU iPhone OS 10_2 like Mac OS X) AppleWebKit/602.3.12 (KHTML, like Gecko) Mobile/14C92 MicroMessenger/6.5.7 NetType/WIFI Language/zh_CN';
         $pos = strpos($val,"MicroMessenger");
     }
+
+    public function testSignature()
+    {
+        //thrid_author\signaturer::instance()->access_token();
+//        thrid_author\signaturer::instance()->jsapi_ticket();
+    }
 }