代码拉取完成,页面将自动刷新
一个简单易用的接口开放平台,平台封装了常用的参数校验、结果返回等功能,开发者只需实现业务代码即可。
easyopen的功能类似于淘宝开放平台,它的所有接口只提供一个url,通过参数来区分不同业务。这样做的好处是接口url管理方便了,平台管理者只需维护好接口参数即可。由于参数的数量是可知的,这样可以在很大程度上进行封装。封装完后平台开发者只需要写业务代码,其它功能可以通过配置来完成。
<?php
namespace App\Controller;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\HttpServer\Contract\RequestInterface;
use Lmh\EasyOpen\Dispatcher\OpenRequestDispatcher;
/**
* @Controller
*/
class GateWayController
{
/**
* @RequestMapping(path="/gateway",methods={"GET","POST"})
* @param RequestInterface $request
* @param OpenRequestDispatcher $openRequestDispatcher
* @return \Psr\Http\Message\ResponseInterface
*/
public function index(RequestInterface $request, OpenRequestDispatcher $openRequestDispatcher)
{
return $openRequestDispatcher->dispatch($request);
}
}
<?php
namespace App\Service\Merchant;
use Lmh\EasyOpen\Annotation\OpenService;
use Lmh\EasyOpen\Annotation\OpenMapping;
/**
* @OpenService()
*/
class MerchantService
{
/**
* @OpenMapping(path="merchant.create")
* @param array $params
* @return string
*/
public function create(array $params = [])
{
return json_encode($params);
}
}
{
"method": "merchant.create",
"app_id": "2016072300007148",
"sign_type":"MD5",
"nonce":"xsadasd823mxdjrewrew",
"sign": "2FF9EE4B7908DF976FD11E405529DD67",
"version": "1.0",
"biz_content": {
"merchant_name": "测试公司",
"city": "杭州"
}
}
<?php
class ApplicationDataFetchFactory implements ApplicationDataFetchInterface{
/**
* @var
*/
private $data;
/**
* @param string $appId
* @return ApplicationDataFetchInterface
* @throws ApplicationDataFetchException
*/
public function make(string $appId): ApplicationDataFetchInterface
{
//可以存入redis
$data = Application::query()->select(['appid', 'secret', 'public_key', 'status'])->where(['appid' => $appId])->first();
if (!$data) {
throw new ApplicationDataFetchException(ErrorSubCode::getMessage(ErrorSubCode::INVALID_APP_ID));
}
$this->data = $data;
return $this;
}
/**
* @inheritDoc
*/
public function getSecret(?string $appId = null): string
{
return $this->data['secret'] ?? '';
}
/**
* @inheritDoc
*/
public function getPublicKey(?string $appId = null): string
{
return $this->data['public_key'] ?? '';
}
}
<?php
return [
\Lmh\EasyOpen\ApplicationDataFetchInterface::class => \App\Factory\ApplicationDataFetchFactory::class,
];
php bin/hyperf.php vendor:publish lmh/easyopen
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。