wsd_bridge.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. use rbridge\wsd\config;
  3. define('APP_ID', 'bridge-wsd');
  4. define('BASE_ROOT_PATH', str_replace('\\', '/', dirname(__FILE__)));
  5. require_once(BASE_ROOT_PATH . '/global.php');
  6. require_once(BASE_ROOT_PATH . '/fooder.php');
  7. require_once(BASE_ROOT_PATH . '/helper/event_looper.php');
  8. require_once(BASE_CORE_PATH . '/framework/function/http.php');
  9. global $config;
  10. $port = $config['wsd_bradge_port'];
  11. class WSDBridge
  12. {
  13. private $mListenPort;
  14. const KEY = 'ac59f54faf1ffcc1';
  15. public function __construct($port)
  16. {
  17. $this->mListenPort = $port;
  18. }
  19. public function looper()
  20. {
  21. $fd = event\util::listen_block("0.0.0.0", $this->mListenPort);
  22. if ($fd === false) {
  23. Log::record("Cannot Listen on port = {$this->mListenPort}", Log::DEBUG);
  24. return false;
  25. }
  26. while (true)
  27. {
  28. Log::record("Waitting another connect....");
  29. if (($client = socket_accept($fd)) !== false)
  30. {
  31. Log::record("Client {$client} has connected", Log::DEBUG);
  32. socket_set_option($client, SOL_SOCKET, SO_RCVTIMEO, ['sec' => 8, 'usec' => 0]);
  33. $body = $this->read_order($client);
  34. if(!empty($body)) {
  35. Log::record("req:{$body}",Log::DEBUG);
  36. $response = $this->relay_request($body);
  37. socket_write($client,$response);
  38. Log::record("resp:{$response}",Log::DEBUG);
  39. }
  40. else {
  41. Log::record("不合法的包",Log::DEBUG);
  42. }
  43. socket_close($client);
  44. }
  45. }
  46. }
  47. private function relay_request($body)
  48. {
  49. $url = BASE_SITE_URL . "/mobile/bridge_shr.php";
  50. $headers = ['Content-Type: application/json'];
  51. $resp = http_post_data($url,$body,$headers);
  52. if($resp === false) {
  53. Log::record("Net error.",Log::ERR);
  54. return $this->err_body($body);
  55. }
  56. else {
  57. return $resp;
  58. }
  59. }
  60. private function err_body($body)
  61. {
  62. $params = json_decode($body);
  63. //交易结果 0:未处理 1:充值成功 2:充值结果不确定 3:充值失败
  64. $retCode = 3;
  65. $retDetail = "服务维护中.....";
  66. $result = [
  67. 'action' => 'CZ',
  68. 'chargeId' => $params['chargeId'],
  69. 'retCode' => $retCode,
  70. 'retDetail' => $retDetail,
  71. 'retRsn' => $params['retRsn'] ];
  72. $body = "{$params['chargeId']}{$retCode}{$params['retRsn']}" . WSDBridge::KEY;
  73. $sign = md5($body);
  74. $result['sign'] = $sign;
  75. return json_encode($result);
  76. }
  77. private function read_order($client)
  78. {
  79. $content = '';
  80. $buf = socket_read($client, 1024);
  81. if ($buf === false) {
  82. $error = socket_strerror(socket_last_error($client));
  83. Log::record("read_order err:{$error}", Log::ERR);
  84. return false;
  85. } else {
  86. $content .= $buf;
  87. }
  88. if ($this->isbody($content)) {
  89. return $content;
  90. }
  91. else {
  92. return false;
  93. }
  94. }
  95. private function isbody($content)
  96. {
  97. $ret = json_decode($content, true);
  98. return !empty($ret);
  99. }
  100. }
  101. $brider = new WSDBridge($port);
  102. $brider->looper();