RPCApi.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: liyunlong
  5. * Date: 2018-12-24
  6. * Time: 09:19
  7. */
  8. namespace common\libs\swoole;
  9. use common\helpers\Form;
  10. use common\helpers\Log;
  11. use common\models\forms\DeclarationLoopForm;
  12. use console\helpers\Logger;
  13. use yii\base\Exception;
  14. use yii\helpers\Json;
  15. class RPCApi
  16. {
  17. public $errorCode = 0;
  18. public $message = '';
  19. private static $allowClassList = [
  20. '\\'.DeclarationLoopForm::class,
  21. ];
  22. public function getError(){
  23. $result = [
  24. 'errorCode' => 0,
  25. 'message' => '',
  26. ];
  27. $result['errorCode'] = $this->errorCode;
  28. if($this->errorCode){
  29. switch($this->errorCode){
  30. case 9001:
  31. $result['message'] = '错误的包头';
  32. break;
  33. case 9002:
  34. $result['message'] = '请求包体长度超过允许的范围';
  35. break;
  36. case 9003:
  37. $result['message'] = '服务器繁忙,超过处理能力';
  38. break;
  39. case 9204:
  40. $result['message'] = '解包失败';
  41. break;
  42. case 9205:
  43. $result['message'] = '参数错误';
  44. break;
  45. case 9206:
  46. $result['message'] = '函数不存在';
  47. break;
  48. case 9207:
  49. $result['message'] = '执行错误';
  50. break;
  51. case 9208:
  52. $result['message'] = '访问被拒绝,客户端主机未被授权';
  53. break;
  54. case 9209:
  55. $result['message'] = '用户名密码错误';
  56. break;
  57. default:
  58. $result['message'] = $this->message;
  59. }
  60. }
  61. return $result;
  62. }
  63. /**
  64. * 接口
  65. * @param array $condition
  66. * @return mixed
  67. * @throws Exception
  68. */
  69. public static function api(array $condition)
  70. {
  71. // 记录debug日志
  72. Logger::trace($condition, 'rpc');
  73. $default = [
  74. //model名字
  75. 'class' => '',
  76. //fields
  77. 'fields' => [],
  78. //方法名
  79. 'action' => '',
  80. //参数
  81. 'param' => [],
  82. ];
  83. $condition = array_merge($default, $condition);
  84. if (!$condition['class']) {
  85. throw new Exception('Error Model Not Found ');
  86. }
  87. if (!$condition['action']) {
  88. throw new Exception('Error Action Not Found ');
  89. }
  90. if(!class_exists($condition['class'])){
  91. throw new Exception('require a class name what\'s exists ');
  92. }
  93. if(!in_array($condition['class'], self::$allowClassList)){
  94. throw new Exception('class is not allowed ');
  95. }
  96. $model = new $condition['class'];
  97. if(empty($fields))
  98. foreach($condition['fields'] as $key=>$field){
  99. if($key == 'data'){
  100. $model->$key = Json::encode($field);
  101. } else {
  102. $model->$key = $field;
  103. }
  104. }
  105. $action = $condition['action'];
  106. if ($condition['param']) {
  107. $result = $model->$action($condition['param']);
  108. } else {
  109. $result = $model->$action();
  110. }
  111. if(!$result){
  112. // 记录错误日志
  113. Logger::trace(Form::formatErrorsForApi($model->getErrors()), 'rpc');
  114. throw new Exception(Form::formatErrorsForApi($model->getErrors()));
  115. }
  116. // 记录结果日志
  117. Logger::trace($result, 'rpc');
  118. return $result;
  119. }
  120. /**
  121. * 请求
  122. * @param array $condition
  123. * @return array|null
  124. * @throws \yii\base\Exception
  125. */
  126. public function request(array $condition){
  127. $default = [
  128. 'namespace' => '\\'.RPCApi::class.'::api',
  129. 'param' => [],
  130. ];
  131. $condition = array_merge($default,$condition);
  132. if (!$condition['namespace'] || empty($condition['param'])) {
  133. return null;
  134. }
  135. $client = RPCClient::getInstance();
  136. // //$client->setEncodeType(false, true);
  137. // // $client->putEnv('app', 'test');
  138. $client->auth('test', 'ttt');//认证账号和密码
  139. $client->addServers(array('host' => '192.168.0.78', 'port' => 9515));
  140. $ret = $client->task($condition['namespace'], [$condition['param']]);
  141. $client->wait(10); //500ms超时
  142. $this->errorCode = $ret->code;
  143. $this->message = $ret->msg;
  144. return $ret->data;
  145. }
  146. }