diff --git a/app/controller/wechat/Wechat.php b/app/controller/wechat/Wechat.php index f9ad7e9..77a8c20 100644 --- a/app/controller/wechat/Wechat.php +++ b/app/controller/wechat/Wechat.php @@ -130,16 +130,11 @@ class Wechat extends Base } } - public function getJsSdkData(Request $request) + public function getJsSdkData() { try { - $param = $request->param(); - - validate()->rule(['url' => 'require'])->check($param); - - $url = urldecode($param['url']); $WechatService = new WechatService(); - return $this->buildSuccess($WechatService->getJsSdkSignature($url)); + return $this->buildSuccess($WechatService->getJsSdkSignature()); } catch (\Exception $e) { return $this->buildFailed(ReturnCode::INVALID,$e->getMessage()); } diff --git a/app/service/wechat/WechatService.php b/app/service/wechat/WechatService.php index f7f21c7..abc51ef 100644 --- a/app/service/wechat/WechatService.php +++ b/app/service/wechat/WechatService.php @@ -71,25 +71,26 @@ class WechatService extends BaseService /** * - * @param $url * @return array * @throws FuncException */ - public function getJsSdkSignature($url): array + public function getJsSdkSignature(): array { + $url = 'https://'.$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]; $timestamp = time(); $nonceStr = generate_random_string(10); $jsapi_ticket = $this->getJsApiTicket(); - $signature = sha1(sprintf('jsapi_ticket=%s&noncestr=%s×tamp=%s&url=%s', $jsapi_ticket, $nonceStr, $timestamp, $url)); + $str = sprintf('jsapi_ticket=%s&noncestr=%s×tamp=%s&url=%s', $jsapi_ticket, $nonceStr, $timestamp, $url); + $signature = sha1($str); return [ 'appId' => env('wechat.appid'), 'timestamp' => $timestamp, 'nonceStr' => $nonceStr, - 'signature' => $signature, + 'signature' => $signature ]; } @@ -102,6 +103,11 @@ class WechatService extends BaseService $access_token = $this->getAccessToken(); + $ticket = $this->redis->get('wechat_js_api'); + if ($ticket) { + return $ticket; + } + $url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=' . $access_token . '&type=jsapi'; $res = Http::get($url); @@ -111,6 +117,13 @@ class WechatService extends BaseService } $data = json_decode($res['data'],true); + + if (isset($data['errcode']) && $data['errcode'] != 0) { + throw new FuncException($data['errmsg']); + } + + $this->redis->set('wechat_js_api', $data['ticket'], 7200); + return $data['ticket']; } } diff --git a/route/wechatRoute.php b/route/wechatRoute.php index 1317557..a39d343 100644 --- a/route/wechatRoute.php +++ b/route/wechatRoute.php @@ -15,6 +15,6 @@ Route::group('wechat', function() { Route::rule('Wechat/index', 'wechat.Wechat/index', 'get'); Route::rule('Wechat/savePuCode', 'wechat.Wechat/savePuCode', 'post')->middleware(WechatAuth::class); Route::rule('Wechat/sendSms', 'wechat.Wechat/sendSms', 'post')->middleware(WechatAuth::class); - Route::rule('Wechat/getJsSdkData', 'wechat.Wechat/getJsSdkData', 'post')->middleware(WechatAuth::class); + Route::rule('Wechat/getJsSdkData', 'wechat.Wechat/getJsSdkData', 'get')->middleware(WechatAuth::class); });