From d11e31d9d6732be183dfd5e4759304a7c0e82628 Mon Sep 17 00:00:00 2001 From: wanghongjun <1445693971@qq,com> Date: Thu, 8 Aug 2024 10:33:04 +0800 Subject: [PATCH] =?UTF-8?q?=E8=8E=B7=E5=8F=96=E5=BE=AE=E4=BF=A1JsSDK?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E6=8E=A5=E5=8F=A35?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controller/wechat/Wechat.php | 10 ++++++++-- app/service/wechat/WechatService.php | 14 ++++++++------ route/wechatRoute.php | 2 +- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/app/controller/wechat/Wechat.php b/app/controller/wechat/Wechat.php index 77a8c20..3bb8966 100644 --- a/app/controller/wechat/Wechat.php +++ b/app/controller/wechat/Wechat.php @@ -130,11 +130,17 @@ class Wechat extends Base } } - public function getJsSdkData() + public function getJsSdkData(Request $request) { try { + $param = $request->param(); + + validate()->rule(['url' => 'require'])->check($param); + + $url = urldecode($param['url']); + $WechatService = new WechatService(); - return $this->buildSuccess($WechatService->getJsSdkSignature()); + return $this->buildSuccess($WechatService->getJsSdkSignature($url)); } 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 abc51ef..6cd6ed9 100644 --- a/app/service/wechat/WechatService.php +++ b/app/service/wechat/WechatService.php @@ -71,26 +71,28 @@ class WechatService extends BaseService /** * + * @param $url * @return array * @throws FuncException */ - public function getJsSdkSignature(): array + public function getJsSdkSignature($url): array { - $url = 'https://'.$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]; + $timestamp = time(); - $nonceStr = generate_random_string(10); + $nonceStr = generate_random_string(16); $jsapi_ticket = $this->getJsApiTicket(); - $str = sprintf('jsapi_ticket=%s&noncestr=%s×tamp=%s&url=%s', $jsapi_ticket, $nonceStr, $timestamp, $url); + $str = "jsapi_ticket={$jsapi_ticket}&noncestr={$nonceStr}×tamp={$timestamp}&url={$url}"; $signature = sha1($str); return [ 'appId' => env('wechat.appid'), 'timestamp' => $timestamp, 'nonceStr' => $nonceStr, - 'signature' => $signature + 'signature' => $signature, + 'url ' => $url ]; } @@ -116,7 +118,7 @@ class WechatService extends BaseService throw new FuncException($res['msg']); } - $data = json_decode($res['data'],true); + $data = json_decode($res['data'], true); if (isset($data['errcode']) && $data['errcode'] != 0) { throw new FuncException($data['errmsg']); diff --git a/route/wechatRoute.php b/route/wechatRoute.php index a39d343..1317557 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', 'get')->middleware(WechatAuth::class); + Route::rule('Wechat/getJsSdkData', 'wechat.Wechat/getJsSdkData', 'post')->middleware(WechatAuth::class); });