parent
443addb63c
commit
334386073a
@ -0,0 +1,46 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
namespace app\wechat\controller; |
||||||
|
|
||||||
|
use app\admin\model\Platform; |
||||||
|
use app\common\service\wechat\MiniProgram; |
||||||
|
use support\Request; |
||||||
|
use Tinywan\ExceptionHandler\Exception\BadRequestHttpException; |
||||||
|
|
||||||
|
class OpenApiController |
||||||
|
{ |
||||||
|
/** |
||||||
|
* 获取token给第三方平台使用 |
||||||
|
* @param Request $request |
||||||
|
* @return int |
||||||
|
* @throws BadRequestHttpException |
||||||
|
*/ |
||||||
|
public function getToken(Request $request) |
||||||
|
{ |
||||||
|
// 开放平台应用ID |
||||||
|
$platformAppId = $request->input('platform_appid'); |
||||||
|
// 被授权的应用ID |
||||||
|
$appid = $request->input('appid'); |
||||||
|
// 校验参数 |
||||||
|
if (empty($platformAppId) || empty($appid)) return '参数错误'; |
||||||
|
|
||||||
|
$platformSetting = Platform::where('app_id', $platformAppId)->find(); |
||||||
|
$app = new MiniProgram($platformSetting->id); |
||||||
|
|
||||||
|
if (empty($platformSetting->third_secret)) return '请先在wechat-mp开放平台配置外部平台解密secret'; |
||||||
|
|
||||||
|
// 获取 component_access_token |
||||||
|
$component_access_token = $app->app->access_token->getToken()['component_access_token']; |
||||||
|
|
||||||
|
// 获取 authorizer_access_token |
||||||
|
$authorizer_access_token = $app->getToken($appid)['authorizer_access_token']; |
||||||
|
|
||||||
|
$result = [ |
||||||
|
'platform_id' => $platformAppId, |
||||||
|
'app_id' => $appid, |
||||||
|
'component_access_token' => $component_access_token, |
||||||
|
'authorizer_access_token' => $authorizer_access_token |
||||||
|
]; |
||||||
|
return encrypt(json_encode($result), $platformSetting->third_secret); |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,42 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
namespace app\wechat\middleware; |
||||||
|
|
||||||
|
use app\admin\model\Platform; |
||||||
|
use Webman\MiddlewareInterface; |
||||||
|
|
||||||
|
class OpenApiMiddleware implements MiddlewareInterface |
||||||
|
{ |
||||||
|
public function process($request, $handler): \Webman\Http\Response |
||||||
|
{ |
||||||
|
// 开放平台应用ID |
||||||
|
$platformAppId = $request->get('platform_appid'); |
||||||
|
$sign = $request->get('sign'); |
||||||
|
$time = $request->get('time'); |
||||||
|
|
||||||
|
// 校验参数 |
||||||
|
if (empty($platformAppId) || empty($sign) || empty($time)) { |
||||||
|
return error('参数不完整,请检查 platform_appid, time, sign 参数是否齐全'); |
||||||
|
} |
||||||
|
|
||||||
|
// 验证签名有效期 |
||||||
|
if ($time < time() - 3000 || $time > time() + 3000) { |
||||||
|
return error('签名已过期'); |
||||||
|
} |
||||||
|
|
||||||
|
$platformSetting = Platform::where('app_id', $platformAppId)->find(); |
||||||
|
if (empty($platformSetting->third_secret)) { |
||||||
|
return error('请先在开放平台处配置外部平台解密secret'); |
||||||
|
} |
||||||
|
|
||||||
|
// 验证签名 |
||||||
|
$data = $request->get(); |
||||||
|
unset($data['sign']); |
||||||
|
$secret = $platformSetting->third_secret; |
||||||
|
if (!verifySign($data, $secret, $sign)) { |
||||||
|
return error('签名验证失败'); |
||||||
|
} |
||||||
|
|
||||||
|
return $handler($request); |
||||||
|
} |
||||||
|
} |
||||||
Loading…
Reference in new issue