Browse Source

获取微信JsSDK数据接口5

master
wanghongjun 1 year ago
parent
commit
d11e31d9d6
  1. 10
      app/controller/wechat/Wechat.php
  2. 14
      app/service/wechat/WechatService.php
  3. 2
      route/wechatRoute.php

10
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());
}

14
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&timestamp=%s&url=%s', $jsapi_ticket, $nonceStr, $timestamp, $url);
$str = "jsapi_ticket={$jsapi_ticket}&noncestr={$nonceStr}&timestamp={$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']);

2
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);
});

Loading…
Cancel
Save