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.
 
 
 
 
 
 

178 lines
5.0 KiB

  1. <?php
  2. /**
  3. *
  4. * ============================================================================
  5. * * 版权所有 蜘蛛出行 * *
  6. * 网站地址: http://www.zhizhuchuxing.com
  7. * ----------------------------------------------------------------------------
  8. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和
  9. * 使用;不允许对程序代码以任何形式任何目的的再发布。
  10. * ============================================================================
  11. * Author By: 倪宗锋
  12. * PhpStorm CtripBus.php
  13. * Create By 2018/4/17 9:33 $
  14. */
  15. namespace common\util;
  16. use backend\modules\api\util\Util;
  17. class CtripBusUtil
  18. {
  19. private $accountId = '';//供应商系统分配给OTA的账户
  20. private $signkey = '';//秘钥
  21. private $supplier_id = '';
  22. private $head = [];//头部
  23. private $body = [];//报文体
  24. private $request = '';//请求报文
  25. private static $util = null;
  26. private function __construct()
  27. {
  28. $this->accountId = \Yii::$app->params['ctrip_bus_config']['accountId'];
  29. $this->signkey = \Yii::$app->params['ctrip_bus_config']['signkey'];
  30. $this->supplier_id = \Yii::$app->params['ctrip_bus_config']['supplier_id'];
  31. }
  32. /**
  33. * Des:通用方法 只是用一些通用方法
  34. * Name: util
  35. * @return CtripBusUtil
  36. * @author 倪宗锋
  37. */
  38. public static function util()
  39. {
  40. if (static::$util == null) {
  41. static::$util = new self();
  42. }
  43. return static::$util;
  44. }
  45. /**
  46. * Des:获取返回值
  47. * Name: getRequest
  48. * @return array head 头部 body 报文体
  49. * @author 倪宗锋
  50. */
  51. public function getRequest()
  52. {
  53. $data = file_get_contents("php://input");
  54. try {
  55. $this->request = json_decode($data, true);
  56. $this->head = empty($this->request['head']) ? ['head' => []] : ['head' => $this->request['head']];
  57. $this->body = empty($this->request['body']) ? ['body' => []] : ['body' => $this->request['body']];
  58. if (empty($this->head['head']['channel']) || $this->accountId != $this->head['head']['channel']) {
  59. return Util::returnArrEr('账户不存在', '', '300');
  60. }
  61. $sign = $this->setSign();
  62. if (!isset($this->head['head']['sign']) || $sign != $this->head['head']['sign']) {
  63. return Util::returnArrEr('签名校验失败', ['sign' => $sign], '300');
  64. }
  65. return Util::returnArrSu('', $this->request);
  66. } catch (\Exception $e) {
  67. return Util::returnArrEr('数据解析失败', '', '300');
  68. }
  69. }
  70. /**
  71. * Des:获取body参数
  72. * Name: getBody
  73. * @return array
  74. * @author 倪宗锋
  75. */
  76. public function getBody()
  77. {
  78. return $this->body['body'];
  79. }
  80. /**
  81. * Des:获取渠道ID
  82. * Name: getSupplierId
  83. * @return string
  84. * @author 倪宗锋
  85. */
  86. public function getSupplierId(){
  87. return $this->supplier_id;
  88. }
  89. /***
  90. * Des:获取签名
  91. * Name: setSign
  92. * @return string
  93. * @author 倪宗锋
  94. */
  95. public function setSign()
  96. {
  97. $string = $this->signkey . $this->arrayKeyVal($this->body) . $this->signkey;
  98. return md5(strtoupper($string));
  99. }
  100. /**
  101. * Des:数组所有的健值进行排序拼接
  102. * Name: arrayKeyVal
  103. * @param $array
  104. * @return string
  105. * @author 倪宗锋
  106. */
  107. public function arrayKeyVal($array)
  108. {
  109. if (!is_array($array) || count($array) == 0) {
  110. return '';
  111. }
  112. ksort($array);//数组更据key排序
  113. $keyVal = '';
  114. foreach ($array as $key => $val) {
  115. if (is_array($val)) {
  116. $keyVal .= $key . $this->arrayKeyVal($val);
  117. } elseif ($val === null) {
  118. continue;
  119. } else {
  120. $keyVal .= $key . $val;
  121. }
  122. }
  123. return $keyVal;
  124. }
  125. /**
  126. * Des:重置路由访问地址
  127. * Name: setAction
  128. * @param $action
  129. * @param $actionName
  130. * @author 倪宗锋
  131. */
  132. public function setAction($action, $actionName)
  133. {
  134. $action->controller->action->id = strtolower($actionName);
  135. $action->controller->action->actionMethod = 'action' . ucfirst($actionName);
  136. $action->id = strtolower($actionName);
  137. $action->actionMethod = 'action' . ucfirst($actionName);
  138. }
  139. /**
  140. * Des:返回信息
  141. * Name: returnErr
  142. * @param $msg
  143. * @param $body
  144. * @param $code
  145. * @return string
  146. * @author 倪宗锋
  147. */
  148. public function re($msg = '', $code = '1', $body = '')
  149. {
  150. if (empty($body)) {
  151. $body = (object) [];
  152. }
  153. $return = [
  154. 'head' => [
  155. 'code' => $code,
  156. 'message' => $msg
  157. ],
  158. 'body' => $body
  159. ];
  160. \Yii::$app->response->format = 'json';
  161. \Yii::$app->response->data = $return;
  162. return;
  163. }
  164. }