增加JsonRpc支持

This commit is contained in:
Anyon 2019-12-17 15:46:15 +08:00
parent fce88ee803
commit c8dcc07525
8 changed files with 193 additions and 13 deletions

View File

@ -16,8 +16,10 @@
namespace app\service\controller\api;
use app\service\service\WechatService;
use library\service\JsonRpcServerService;
use think\Controller;
use think\Db;
use think\exception\HttpResponseException;
/**
* 获取微信SDK实例对象
@ -90,6 +92,23 @@ class Client extends Controller
}
}
/**
* JsonRpc 接口标准
* @param string $param
* @return string
*/
public function jsonrpc($param)
{
try {
$instance = $this->create($param);
JsonRpcServerService::instance()->create($instance);
} catch (HttpResponseException $exception) {
throw $exception;
} catch (\Exception $e) {
return $e->getMessage();
}
}
/**
* 创建接口服务
* @param string $token

View File

@ -15,6 +15,8 @@
namespace app\wechat\service;
use library\service\JsonRpcClientService;
/**
* 微信处理管理
* Class WechatService
@ -86,7 +88,6 @@ class WechatService extends \We
* @param string $name 静态类名
* @param array $arguments 参数集合
* @return mixed
* @throws \SoapFault
* @throws \think\Exception
* @throws \think\exception\PDOException
*/
@ -118,7 +119,6 @@ class WechatService extends \We
* @param string $type 接口类型
* @param array $config 微信配置
* @return mixed
* @throws \SoapFault
* @throws \think\Exception
* @throws \think\exception\PDOException
*/
@ -136,11 +136,9 @@ class WechatService extends \We
$token = strtolower("{$name}-{$appid}-{$appkey}-{$type}");
if (class_exists('Yar_Client')) {
return new \Yar_Client(config('wechat.service_url') . "/service/api.client/yar/{$token}");
} elseif (class_exists('SoapClient')) {
$location = config('wechat.service_url') . "/service/api.client/soap/{$token}";
return new \SoapClient(null, ['uri' => strtolower($name), 'location' => $location]);
} else {
throw new \think\Exception("Yar or Soap extensions are not installed.");
$location = config('wechat.service_url') . "/service/api.client/jsonrpc/{$token}";
return JsonRpcClientService::instance()->create($location);
}
}
}

View File

@ -217,6 +217,8 @@ return array(
'library\\queue\\WorkQueue' => $vendorDir . '/zoujingli/think-library/src/queue/WorkQueue.php',
'library\\service\\AdminService' => $vendorDir . '/zoujingli/think-library/src/service/AdminService.php',
'library\\service\\CaptchaService' => $vendorDir . '/zoujingli/think-library/src/service/CaptchaService.php',
'library\\service\\JsonRpcClientService' => $vendorDir . '/zoujingli/think-library/src/service/JsonRpcClientService.php',
'library\\service\\JsonRpcServerService' => $vendorDir . '/zoujingli/think-library/src/service/JsonRpcServerService.php',
'library\\service\\MenuService' => $vendorDir . '/zoujingli/think-library/src/service/MenuService.php',
'library\\service\\NodeService' => $vendorDir . '/zoujingli/think-library/src/service/NodeService.php',
'library\\service\\ProcessService' => $vendorDir . '/zoujingli/think-library/src/service/ProcessService.php',

View File

@ -310,6 +310,8 @@ class ComposerStaticInit0ac7cd5b2cda0031cee9c92b2dc93c50
'library\\queue\\WorkQueue' => __DIR__ . '/..' . '/zoujingli/think-library/src/queue/WorkQueue.php',
'library\\service\\AdminService' => __DIR__ . '/..' . '/zoujingli/think-library/src/service/AdminService.php',
'library\\service\\CaptchaService' => __DIR__ . '/..' . '/zoujingli/think-library/src/service/CaptchaService.php',
'library\\service\\JsonRpcClientService' => __DIR__ . '/..' . '/zoujingli/think-library/src/service/JsonRpcClientService.php',
'library\\service\\JsonRpcServerService' => __DIR__ . '/..' . '/zoujingli/think-library/src/service/JsonRpcServerService.php',
'library\\service\\MenuService' => __DIR__ . '/..' . '/zoujingli/think-library/src/service/MenuService.php',
'library\\service\\NodeService' => __DIR__ . '/..' . '/zoujingli/think-library/src/service/NodeService.php',
'library\\service\\ProcessService' => __DIR__ . '/..' . '/zoujingli/think-library/src/service/ProcessService.php',

View File

@ -401,12 +401,12 @@
"source": {
"type": "git",
"url": "https://github.com/zoujingli/ThinkLibrary.git",
"reference": "b1a3c4812b2a8f069744afd7423d278c0c2b0462"
"reference": "802a2cf0211c40702665f7d068fd8a830abc67f5"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/zoujingli/ThinkLibrary/zipball/b1a3c4812b2a8f069744afd7423d278c0c2b0462",
"reference": "b1a3c4812b2a8f069744afd7423d278c0c2b0462",
"url": "https://api.github.com/repos/zoujingli/ThinkLibrary/zipball/802a2cf0211c40702665f7d068fd8a830abc67f5",
"reference": "802a2cf0211c40702665f7d068fd8a830abc67f5",
"shasum": "",
"mirrors": [
{
@ -426,7 +426,7 @@
"qiniu/php-sdk": "^7.2",
"topthink/framework": "5.1.*"
},
"time": "2019-12-02T06:12:32+00:00",
"time": "2019-12-17T07:42:02+00:00",
"type": "library",
"installation-source": "dist",
"autoload": {

View File

@ -47,24 +47,25 @@ abstract class Service
{
$this->app = $app;
$this->request = $request;
$this->initialize();
}
/**
* 初始化服务
* @return $this
*/
public function initialize()
protected function initialize()
{
return $this;
}
/**
* 静态实例对象
* @return $this
* @return static
*/
public static function instance()
{
return Container::getInstance()->make(static::class)->initialize();
return Container::getInstance()->make(static::class);
}
}

View File

@ -0,0 +1,96 @@
<?php
// +----------------------------------------------------------------------
// | Library for ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2019 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 官方网站: http://demo.thinkadmin.top
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | gitee 仓库地址 https://gitee.com/zoujingli/ThinkLibrary
// | github 仓库地址 https://github.com/zoujingli/ThinkLibrary
// +----------------------------------------------------------------------
namespace library\service;
use library\Service;
use library\tools\Data;
/**
* JsonRpc 客户端服务
* Class JsonRpcClientService
* @package think\admin\service
*/
class JsonRpcClientService extends Service
{
/**
* 服务端地址
* @var string
*/
private $proxy;
/**
* 请求ID
* @var integer
*/
private $requestid;
/**
* 创建连接对象
* @param string $proxy
* @return $this
*/
public function create($proxy)
{
$this->proxy = $proxy;
$this->requestid = Data::randomCode(16, 3);
return $this;
}
/**
* 执行 JsonRCP 请求
* @param string $method
* @param array $params
* @return array|boolean
* @throws \think\Exception
*/
public function __call($method, $params)
{
// check
if (!is_scalar($method)) {
throw new \think\Exception('Method name has no scalar value');
}
// check
if (is_array($params)) {
$params = array_values($params);
} else {
throw new \think\Exception('Params must be given as array');
}
// performs the HTTP POST
$options = [
'http' => [
'method' => 'POST', 'header' => 'Content-type: application/json',
'content' => json_encode(['method' => $method, 'params' => $params, 'id' => $this->requestid], JSON_UNESCAPED_UNICODE),
],
];
if ($fp = fopen($this->proxy, 'r', false, stream_context_create($options))) {
$response = '';
while ($row = fgets($fp)) $response .= trim($row) . "\n";
fclose($fp);
$response = json_decode($response, true);
} else {
throw new \think\Exception("Unable to connect to {$this->proxy}");
}
// final checks and return
if ($response['id'] != $this->requestid) {
throw new \think\Exception("Incorrect response id (request id: {$this->requestid}, response id: {$response['id']}");
}
if (is_null($response['error'])) {
return $response['result'];
} else {
throw new \think\Exception("Request error: {$response['error']}");
}
}
}

View File

@ -0,0 +1,62 @@
<?php
// +----------------------------------------------------------------------
// | Library for ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2019 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 官方网站: http://demo.thinkadmin.top
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | gitee 仓库地址 https://gitee.com/zoujingli/ThinkLibrary
// | github 仓库地址 https://github.com/zoujingli/ThinkLibrary
// +----------------------------------------------------------------------
namespace library\service;
use library\Service;
use think\exception\HttpResponseException;
/**
* JsonRpc 服务端服务
* Class JsonRpcServerService
* @package think\admin\service
*/
class JsonRpcServerService extends Service
{
/**
* 设置监听对象
* @param mixed $object
* @throws \think\Exception
*/
public function handle($object)
{
// Checks if a JSON-RCP request has been received
if ($this->app->request->method() !== "POST" || $this->app->request->contentType() != 'application/json') {
echo "<h2>" . get_class($object) . "</h2>";
foreach (get_class_methods($object) as $method) {
if ($method[0] !== '_') echo "<p>method {$method}()</p>";
}
} else {
// Reads the input data
$request = json_decode(file_get_contents('php://input'), true);
if (empty($request['id'])) {
throw new \think\Exception('JsonRpc Request id cannot be empty');
}
// Executes the task on local object
try {
if ($result = @call_user_func_array([$object, $request['method']], $request['params'])) {
$response = ['id' => $request['id'], 'result' => $result, 'error' => null];
} else {
$response = ['id' => $request['id'], 'result' => null, 'error' => 'unknown method or incorrect parameters'];
}
} catch (\Exception $e) {
$response = ['id' => $request['id'], 'result' => null, 'error' => $e->getMessage()];
}
// Output the response
throw new HttpResponseException(json($response)->contentType('text/javascript'));
}
}
}