调整openapi返回格式

master
李春波 2 years ago
parent 33cc2a5125
commit b656b4db52
  1. 29
      app/wechat/controller/OpenApiController.php

@ -4,7 +4,14 @@ namespace app\wechat\controller;
use app\admin\model\Platform;
use app\common\service\wechat\MiniProgram;
use EasyWeChat\Kernel\Exceptions\HttpException;
use EasyWeChat\Kernel\Exceptions\InvalidArgumentException;
use EasyWeChat\Kernel\Exceptions\InvalidConfigException;
use EasyWeChat\Kernel\Exceptions\RuntimeException;
use support\Request;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use Tinywan\ExceptionHandler\Exception\BadRequestHttpException;
class OpenApiController
@ -12,22 +19,34 @@ class OpenApiController
/**
* 获取token给第三方平台使用
* @param Request $request
* @return int
* @return array
* @throws BadRequestHttpException
* @throws DataNotFoundException
* @throws DbException
* @throws HttpException
* @throws InvalidArgumentException
* @throws InvalidConfigException
* @throws ModelNotFoundException
* @throws RuntimeException
* @throws \Psr\SimpleCache\InvalidArgumentException
*/
public function getToken(Request $request)
public function getToken(Request $request): array
{
// 开放平台应用ID
$platformAppId = $request->input('platform_appid');
// 被授权的应用ID
$appid = $request->input('appid');
// 校验参数
if (empty($platformAppId) || empty($appid)) return '参数错误';
if (empty($platformAppId) || empty($appid)) {
return error('参数错误');
};
$platformSetting = Platform::where('app_id', $platformAppId)->find();
$app = new MiniProgram($platformSetting->id);
if (empty($platformSetting->third_secret)) return '请先在wechat-mp开放平台配置外部平台解密secret';
if (empty($platformSetting->third_secret)) {
return error('请先在wechat-mp开放平台配置外部平台解密secret');
}
// 获取 component_access_token
$component_access_token = $app->app->access_token->getToken()['component_access_token'];
@ -41,6 +60,6 @@ class OpenApiController
'authorizer_appid' => $appid,
'authorizer_access_token' => $authorizer_access_token
];
return encrypt(json_encode($result), $platformSetting->third_secret);
return success($result);
}
}
Loading…
Cancel
Save