You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

209 lines
7.0 KiB

  1. <?php
  2. /**
  3. *
  4. * ============================================================================
  5. * * 版权所有 蜘蛛出行 * *
  6. * 网站地址: http://www.zhizhuchuxing.com
  7. * ----------------------------------------------------------------------------
  8. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和
  9. * 使用;不允许对程序代码以任何形式任何目的的再发布。
  10. * ============================================================================
  11. * Author By: 倪宗锋
  12. * PhpStorm SoapService.php
  13. * Create By 2017/4/18 11:00 $
  14. */
  15. namespace backend\modules\hotel\models;
  16. use Yii;
  17. use backend\common\Utils;
  18. use common\components\zOfficeWechat;
  19. class SoapService
  20. {
  21. private $soap = null;
  22. private $send = '';
  23. private $getRe = '';
  24. private $version = '';
  25. private $bodyName = '';
  26. private $scTime = 0;
  27. private $data = '';
  28. private $RequestTypeName = '';
  29. private $user_id = 1;
  30. public function __construct($data, $RequestTypeName, $bodyName, $version = '')
  31. {
  32. libxml_disable_entity_loader(false);
  33. if (empty($version)) {
  34. $params = \Yii::$app->params['ctrip']['soap'];
  35. $version = $params['Version'];
  36. }
  37. $this->bodyName = $bodyName;
  38. $this->data = $data;
  39. $this->RequestTypeName = $RequestTypeName;
  40. $this->version = $version;
  41. }
  42. /**
  43. * Des:执行并返回结果
  44. * Name: exec
  45. * @param int $type 1:标准模式如mapping、订单推送 2:UpdateRoomQuantity(更新保留房数量) 3. 房价房态推送
  46. * @return array
  47. * @author 倪宗锋
  48. */
  49. public function exec($type = 1, $user_id = 1)
  50. {
  51. $this->user_id = $user_id;
  52. $startTime = microtime(TRUE);
  53. if ($type == 1) {
  54. $return = $this->doExecForCommon();
  55. } elseif ($type == 2) {
  56. $return = $this->doExecForRoomQuantity();
  57. } elseif ($type == 3) {
  58. $return = $this->doExecForPrice();
  59. } else {
  60. $return = $this->reTu(false, 'type is error');
  61. }
  62. $endTime = microtime(TRUE);
  63. $this->scTime = round(floatval($endTime - $startTime), 3);//时间差
  64. return $return;
  65. }
  66. /**
  67. * Des:标准模式调用并处理
  68. * Name: doExecForCommon
  69. * @return array
  70. * @author 倪宗锋
  71. */
  72. public function doExecForCommon()
  73. {
  74. $this->soap = \Yii::$app->soapClient;
  75. $this->setXml();
  76. if (Yii::$app->params['ctrip']['flag']) {
  77. $re = $this->soap->AdapterRequest(['requestXml' => $this->send]);
  78. $this->getRe = $re->AdapterRequestResult;
  79. $result = Utils::xml_to_array($this->getRe);
  80. } else {
  81. $result = '<>';
  82. }
  83. return $this->reTu(true, 'OK', $result);
  84. }
  85. /**
  86. * Des:房价房态推送
  87. * Name: doExecForCommon
  88. * @return array
  89. * @author 王雷刚
  90. * 2018-12-7 14:10:35
  91. */
  92. public function doExecForPrice()
  93. {
  94. $this->soap = \Yii::$app->soapClientPrice;
  95. $this->setXml();
  96. if (Yii::$app->params['ctrip']['flag']) {
  97. $re = $this->soap->AdapterRequest(['requestXml' => $this->send]);
  98. $this->getRe = $re->AdapterRequestResult;
  99. $result = Utils::xml_to_array($this->getRe);
  100. } else {
  101. $result = '<>';
  102. }
  103. return $this->reTu(true, 'OK', $result);
  104. }
  105. /**
  106. * User:Steven
  107. * Desc:更新保留房
  108. * @return array
  109. */
  110. public function doExecForRoomQuantity()
  111. {
  112. $this->soap = \Yii::$app->soapClientRemain;
  113. $this->setXmlNs();
  114. if (Yii::$app->params['ctrip']['flag']) {
  115. $re = $this->soap->RoomQuantityReceive(['receiveRoomQuantityXml' => $this->send]);
  116. $this->getRe = $re->RoomQuantityReceiveResult;
  117. $result = Utils::xml_to_array($this->getRe);
  118. } else {
  119. $result = '<>';
  120. }
  121. return $this->reTu(true, 'OK', $result);
  122. }
  123. /**
  124. * Des:设置xml报文
  125. * Name: setXml
  126. * @author 倪宗锋
  127. */
  128. private function setXml()
  129. {
  130. $params = \Yii::$app->params['ctrip']['soap'];
  131. $timeStamp = date("Y-m-d H:i:s", time());
  132. $xml = "<Request>";
  133. $xml .= "<HeaderInfo UserID=\"" . $params['UserID'] . "\" RequestorId=\"" . $params['RequestorId'] .
  134. "\" AsyncRequest=\"" . $params['AsyncRequest'] . "\" TimeStamp=\"$timeStamp\"><Authentication UserName=\"" .
  135. $params['UserName'] . "\" Password=\"" . $params['Password'] . "\" /><RequestType Name=\"$this->RequestTypeName\" Version=\"" . $this->version . "\" /></HeaderInfo><{$this->bodyName}>";
  136. $xml .= Utils::data_to_xml($this->data);
  137. $xml .= "</{$this->bodyName}></Request>";
  138. $this->send = $xml;
  139. }
  140. /**
  141. * Des:设置带有命名空间的xml报文
  142. * Name: setXml
  143. * @author 倪宗锋
  144. */
  145. private function setXmlNs()
  146. {
  147. $params = \Yii::$app->params['ctrip']['soap'];
  148. $timeStamp = date("Y-m-d\TH:i:s+08:00", time());
  149. $xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
  150. $xml .= "<q1:Request xmlns:q1=\"http://soa.ctrip.com/hotel/vendor/RoomQuantity/v1\">";
  151. $xml .= "<q1:HeaderInfo UserID=\"" . $params['UserID'] . "\" RequestorId=\"" . $params['RequestorId'] .
  152. "\" AsyncRequest=\"" . $params['AsyncRequest'] . "\" TimeStamp=\"$timeStamp\"><q1:Authentication UserName=\"" .
  153. $params['UserNameRemain'] . "\" Password=\"" . $params['PasswordRemain'] . "\" /><q1:RequestType Name=\"$this->RequestTypeName\" Version=\"" . $params['VersionRemain'] . "\" /></q1:HeaderInfo><q1:{$this->bodyName}>";
  154. $xml .= Utils::data_to_xml_ns($this->data, 'q1:');
  155. $xml .= "</q1:{$this->bodyName}></q1:Request>";
  156. $this->send = $xml;
  157. }
  158. /**
  159. * Des:统一返回格式
  160. * Name: reTu
  161. * @param $flag
  162. * @param string $msg
  163. * @param string $data
  164. * @return array
  165. * @author 倪宗锋
  166. */
  167. private function reTu($flag, $msg = '', $data = '')
  168. {
  169. return [
  170. 'flag' => $flag,
  171. 'msg' => $msg,
  172. 'data' => $data
  173. ];
  174. }
  175. public function __destruct()
  176. {
  177. $string = date('Y-m-d H:i:s', time()) . ' ' . $this->version . PHP_EOL;
  178. $string .= "send:【{$this->user_id}】" . $this->send . PHP_EOL;
  179. $string .= "get:【{$this->user_id}】" . $this->getRe . PHP_EOL;
  180. $string .= 'time:' . $this->scTime . 's' . PHP_EOL . PHP_EOL;//时间差
  181. $result = Utils::xml_to_array($this->getRe);
  182. if (empty($result) || $result['RequestResult']['ResultCode'] != 0) {
  183. $arr = array(
  184. "agentid" => 1000002,
  185. "title" => '推送失败:',
  186. "msg" => $this->scTime . ':' . $this->send,
  187. "touser" => 'wangxj|shifp|yuw',
  188. );
  189. zOfficeWechat::sendMsg($arr);
  190. }
  191. file_put_contents(__DIR__ . '/../../../runtime/logs/soap/' . date('Y-m-d') . '.log', $string, FILE_APPEND);
  192. }
  193. }