From 9447061fa81f143820d43f21219b9ab28b3cf7a2 Mon Sep 17 00:00:00 2001 From: "ZLW-PC\\Administrator" <374861669@qq.com> Date: Thu, 2 Nov 2017 17:29:28 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BE=AE=E4=BF=A1=E5=88=86=E4=BA=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- resources/org/wxJsSdk/README.txt | 26 ++++ resources/org/wxJsSdk/access_token.php | 2 + resources/org/wxJsSdk/demo.php | 42 ++++++ resources/org/wxJsSdk/jsapi_ticket.php | 2 + resources/org/wxJsSdk/jssdk.php | 134 ++++++++++++++++++ .../weixin/address/userAddressAdd.blade.php | 2 +- .../views/weixin/cart/cartCheckout.blade.php | 41 +++++- .../views/weixin/common/wxshare.blade.php | 101 +++++++++++++ resources/views/weixin/user/login.blade.php | 2 +- .../views/weixin/user/register.blade.php | 2 +- 10 files changed, 348 insertions(+), 6 deletions(-) create mode 100644 resources/org/wxJsSdk/README.txt create mode 100644 resources/org/wxJsSdk/access_token.php create mode 100644 resources/org/wxJsSdk/demo.php create mode 100644 resources/org/wxJsSdk/jsapi_ticket.php create mode 100644 resources/org/wxJsSdk/jssdk.php create mode 100644 resources/views/weixin/common/wxshare.blade.php diff --git a/resources/org/wxJsSdk/README.txt b/resources/org/wxJsSdk/README.txt new file mode 100644 index 0000000..5d7c36b --- /dev/null +++ b/resources/org/wxJsSdk/README.txt @@ -0,0 +1,26 @@ + +΢JS-SDK DEMO + +DEMOҳ棺 +http://demo.open.weixin.qq.com/jssdk + + + + + + + + + + + + + + + + + + + + + diff --git a/resources/org/wxJsSdk/access_token.php b/resources/org/wxJsSdk/access_token.php new file mode 100644 index 0000000..c81b4d4 --- /dev/null +++ b/resources/org/wxJsSdk/access_token.php @@ -0,0 +1,2 @@ + +{"access_token":"","expire_time":0} diff --git a/resources/org/wxJsSdk/demo.php b/resources/org/wxJsSdk/demo.php new file mode 100644 index 0000000..c4c2767 --- /dev/null +++ b/resources/org/wxJsSdk/demo.php @@ -0,0 +1,42 @@ +GetSignPackage(); +?> + + + + + + + + + + + + diff --git a/resources/org/wxJsSdk/jsapi_ticket.php b/resources/org/wxJsSdk/jsapi_ticket.php new file mode 100644 index 0000000..d6da31b --- /dev/null +++ b/resources/org/wxJsSdk/jsapi_ticket.php @@ -0,0 +1,2 @@ + +{"jsapi_ticket":"","expire_time":0} diff --git a/resources/org/wxJsSdk/jssdk.php b/resources/org/wxJsSdk/jssdk.php new file mode 100644 index 0000000..473567b --- /dev/null +++ b/resources/org/wxJsSdk/jssdk.php @@ -0,0 +1,134 @@ +appId = $appId; + $this->appSecret = $appSecret; + } + + public function getSignPackage() + { + $jsapiTicket = $this->getJsApiTicket(); + + // 注意 URL 一定要动态获取,不能 hardcode. + $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->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; + } + + private function getJsApiTicket() + { + // jsapi_ticket 应该全局存储与更新,以下代码以写入到文件中做示例 + $data = json_decode($this->get_php_file("jsapi_ticket.php")); + if ($data->expire_time < time()) + { + $accessToken = $this->getAccessToken(); + // 如果是企业号用以下 URL 获取 ticket + // $url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=$accessToken"; + $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->set_php_file("jsapi_ticket.php", json_encode($data)); + } + } + else + { + $ticket = $data->jsapi_ticket; + } + + return $ticket; + } + + private function getAccessToken() + { + // access_token 应该全局存储与更新,以下代码以写入到文件中做示例 + $data = json_decode($this->get_php_file("access_token.php")); + if ($data->expire_time < time()) + { + // 如果是企业号用以下URL获取access_token + // $url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$this->appId&corpsecret=$this->appSecret"; + $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->set_php_file("access_token.php", json_encode($data)); + } + } + 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, true); + curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, true); + curl_setopt($curl, CURLOPT_URL, $url); + + $res = curl_exec($curl); + curl_close($curl); + + return $res; + } + + private function get_php_file($filename) + { + return trim(substr(file_get_contents($filename), 15)); + } + + private function set_php_file($filename, $content) + { + $fp = fopen($filename, "w"); + fwrite($fp, "" . $content); + fclose($fp); + } +} \ No newline at end of file diff --git a/resources/views/weixin/address/userAddressAdd.blade.php b/resources/views/weixin/address/userAddressAdd.blade.php index fcbc718..47794f3 100644 --- a/resources/views/weixin/address/userAddressAdd.blade.php +++ b/resources/views/weixin/address/userAddressAdd.blade.php @@ -24,7 +24,7 @@ .adr-form-group{margin-top:10px;} .adr-form-group input[type=text],.adr-form-group textarea{display: block;width: 100%;font-size:16px;padding:10px;color: #777;vertical-align: middle;background-color: #fff;background-image: none;border: 1px solid #ddd;border-radius: 0;box-sizing:border-box;} .adr-form-group select{padding:5px;margin-right:10px;} -.bottoma{display:block;font-size:18px;padding:10px;color:white;background-color: #f23030;text-align:center;} +.bottoma{display:block;font-size:18px;padding:10px;color:white;background-color:#f23030;text-align:center;}
diff --git a/resources/views/weixin/cart/cartCheckout.blade.php b/resources/views/weixin/cart/cartCheckout.blade.php index cf3d5e7..b62b185 100644 --- a/resources/views/weixin/cart/cartCheckout.blade.php +++ b/resources/views/weixin/cart/cartCheckout.blade.php @@ -55,13 +55,48 @@