1 Star 1 Fork 0

lmh / easyopen

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

easyopen

一个简单易用的接口开放平台,平台封装了常用的参数校验、结果返回等功能,开发者只需实现业务代码即可。

easyopen的功能类似于淘宝开放平台,它的所有接口只提供一个url,通过参数来区分不同业务。这样做的好处是接口url管理方便了,平台管理者只需维护好接口参数即可。由于参数的数量是可知的,这样可以在很大程度上进行封装。封装完后平台开发者只需要写业务代码,其它功能可以通过配置来完成。

示例

  • 1、定义自己的控制器和网关路由,注入OpenRequestDispatcher 即可
<?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);
    }
}
  • 2、定义开发服务类如下,引入OpenService 和 OpenMapping即可
<?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);
    }
}
  • 3、 请求数据:
{
    "method": "merchant.create",
    "app_id": "2016072300007148",
    "sign_type":"MD5",
    "nonce":"xsadasd823mxdjrewrew",
    "sign": "2FF9EE4B7908DF976FD11E405529DD67",
    "version": "1.0",
    "biz_content": {
        "merchant_name": "测试公司",
        "city": "杭州"
    }
}

签名密钥获取

  • 1、定义数据获取类实现ApplicationDataFetchInterface接口,例如:
<?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'] ?? '';
   }

}
  • 2、在 config/autoload/dependencies.php 内完成关系配置:
<?php
return [
  \Lmh\EasyOpen\ApplicationDataFetchInterface::class => \App\Factory\ApplicationDataFetchFactory::class,
];
  • 3、发布平台密钥配置文件
php bin/hyperf.php vendor:publish lmh/easyopen
The MIT License (MIT) Copyright (c) Hyperf Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

暂无描述 展开 收起
PHP 等 2 种语言
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/lmh/easyopen.git
git@gitee.com:lmh/easyopen.git
lmh
easyopen
easyopen
master

搜索帮助