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.

CtripSwitchService.php 6.3 KiB

3 vuotta sitten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. /**
  3. *
  4. * ============================================================================
  5. * * 版权所有 蜘蛛出行 * *
  6. * 网站地址: http://www.zhizhuchuxing.com
  7. * ----------------------------------------------------------------------------
  8. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和
  9. * 使用;不允许对程序代码以任何形式任何目的的再发布。
  10. * ============================================================================
  11. * Author By: 倪宗锋
  12. * PhpStorm CtripSwitchService.php
  13. * Create By 2018/5/2 19:08 $
  14. */
  15. namespace backend\modules\hotel\service;
  16. use yii;
  17. use yii\httpclient\Client;
  18. class CtripSwitchService
  19. {
  20. const SUCCESS_CODE = 0;
  21. const SYS_ERR0R = 100; //系统错误
  22. const CTRIP_HOTEL_CODE = 101; //携程创建子酒店失败
  23. const MASTER_HOTEL_EMPTY = 102; //母酒店ID不能为空
  24. const REPEAT_CREATE = 103; //该酒店已添加
  25. const MAPPING_HOTEL_ERROR = 104; //酒店关联失败
  26. const UNMAPPING_CODE = 105; //酒店未关联
  27. const PARAM_ERR0R = 106; //请求参数缺失
  28. const ADD_HOTEL_ERR = 107; //添加酒店信息失败
  29. const GET_MASTER_HOTEL_ERR = 108; //添加酒店信息失败
  30. const RETURN_MSG = [
  31. self::SUCCESS_CODE => 'success',
  32. self::CTRIP_HOTEL_CODE => '携程创建子酒店失败',
  33. self::SYS_ERR0R => '系统错误',
  34. self::MASTER_HOTEL_EMPTY => '母酒店ID不能为空',
  35. self::REPEAT_CREATE => '携程子酒店已创建,但cs系统不存在子酒店id',
  36. self::MAPPING_HOTEL_ERROR => '酒店关联失败',
  37. self::UNMAPPING_CODE => '请检查 Mapping 关系!',
  38. self::PARAM_ERR0R => '请求参数缺失',
  39. self::ADD_HOTEL_ERR => '添加酒店信息失败',
  40. self::GET_MASTER_HOTEL_ERR => '获取代理通酒店信息失败',
  41. ];
  42. const CTRIP_ID = 669; //携程
  43. const QUNAR_ID = 1667; //去哪
  44. const ELONG_ID = 1668; //艺龙
  45. const CHANNELA_ID = 1669; //分销A
  46. const B2B_ID = 1670; //b2b
  47. public $enableCsrfValidation = false;
  48. public $ctripConf;
  49. public $request_params;
  50. /**
  51. * CtripSwitchController constructor.
  52. */
  53. public function __construct()
  54. {
  55. $this->ctripConf = Yii::$app->params['ctrip_switch'];
  56. }
  57. /**
  58. * Notes:request请求
  59. * User: Steven
  60. * Date: 2018/4/16
  61. * Time: 15:28
  62. * @param $target_url
  63. * @param $data
  64. * @return mixed
  65. */
  66. public function request($target_url, $data)
  67. {
  68. $data['requestor'] = $this->getCommonParams();
  69. $timestamp = $this->getTimestamp();
  70. $signature = $this->setSignature($this->ctripConf['switch_supplier_id'], $timestamp, $this->ctripConf['interfacekey']);
  71. $client = new Client(['baseUrl' => $this->ctripConf['base_url']]);
  72. $request = $client->createRequest()
  73. ->setHeaders(['content-type' => 'application/json;charset=UTF-8'])
  74. ->addHeaders(["timestamp" => $timestamp])
  75. ->addHeaders(['signature' => $signature]);
  76. $response = $request->setFormat(Client::FORMAT_JSON)
  77. ->setMethod('post')
  78. ->setUrl($target_url)
  79. ->setData($data)
  80. ->send();
  81. return json_decode($response->content);
  82. }
  83. /**
  84. * Notes:生成公共节点
  85. * User: Steven
  86. * Date: 2018/4/19
  87. * Time: 14:38
  88. * @return array
  89. */
  90. private function getCommonParams()
  91. {
  92. return [
  93. 'invoker' => $this->ctripConf['requestor']['invoker'],
  94. 'operatorName' => $this->ctripConf['requestor']['operatorName'],
  95. 'opClientIP' => $this->ctripConf['requestor']['opClientIP'],
  96. 'userId' => $this->ctripConf['requestor']['userId'],
  97. 'languageType' => $this->ctripConf['requestor']['languageType'],
  98. ];
  99. }
  100. /**
  101. * Notes:加密验证逻辑
  102. * User: Steven
  103. * Date: 2018/4/16
  104. * Time: 15:42
  105. * @param $supplierID //供应商ID,int类型
  106. * @param $timestamp
  107. * @param $interfacekey //密钥Key
  108. * @param $signature
  109. * @return bool
  110. */
  111. private function getSignature($supplierID, $timestamp, $interfacekey, $signature)
  112. {
  113. //加密算法是md5(base64)
  114. $signature_str = strtoupper(base64_encode(md5($supplierID . $timestamp . $interfacekey, true)));
  115. return $signature == $signature_str;
  116. }
  117. /**
  118. * Notes:生成加密串
  119. * User: Steven
  120. * Date: 2018/4/16
  121. * Time: 17:58
  122. * @param $supplierID
  123. * @param $timestamp
  124. * @param $interfacekey
  125. * @return string
  126. */
  127. private function setSignature($supplierID, $timestamp, $interfacekey)
  128. {
  129. //加密算法是md5(base64)
  130. $signature = strtoupper(base64_encode(md5($supplierID . $timestamp . $interfacekey, true)));
  131. return $signature;
  132. }
  133. /**
  134. * Notes:获取当前时间的毫秒数
  135. * User: Steven
  136. * Date: 2018/4/16
  137. * Time: 18:01
  138. * @return float
  139. */
  140. private function getTimestamp()
  141. {
  142. list($msec, $sec) = explode(' ', microtime());
  143. $msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
  144. return $msectime;
  145. }
  146. /**
  147. * Created by PhpStorm.
  148. * NOTES:获取接口所需要的渠道ID
  149. * User: Steven
  150. * Date: 2018/4/27
  151. * Time: 16:25
  152. * Class getChannelID
  153. * @param $channel_str
  154. * @return string
  155. */
  156. public function getChannelID($channel_str)
  157. {
  158. $channel_arr = explode(',', $channel_str);
  159. $channel_id_str = '';
  160. foreach ($channel_arr as $value) {
  161. $channel_id_str .= $channel_id_str == '' ? '' : ',';
  162. switch ($value) {
  163. case self::CTRIP_ID:
  164. $channel_id_str .= 'Ctrip';
  165. break;
  166. case self::QUNAR_ID:
  167. $channel_id_str .= 'Qunar';
  168. break;
  169. case self::ELONG_ID:
  170. $channel_id_str .= 'Elong';
  171. break;
  172. case self::CHANNELA_ID:
  173. $channel_id_str .= 'ChannelA';
  174. break;
  175. case self::B2B_ID:
  176. $channel_id_str .= 'B2B';
  177. break;
  178. default:
  179. break;
  180. }
  181. }
  182. return $channel_id_str;
  183. }
  184. }