|
- <?php
- /**
- *
- * ============================================================================
- * * 版权所有 蜘蛛出行 * *
- * 网站地址: http://www.zhizhuchuxing.com
- * ----------------------------------------------------------------------------
- * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和
- * 使用;不允许对程序代码以任何形式任何目的的再发布。
- * ============================================================================
- * Author By: 倪宗锋
- * PhpStorm CtripBus.php
- * Create By 2018/4/17 9:33 $
- */
-
- namespace common\util;
-
-
- use backend\modules\api\util\Util;
-
- class CtripBusUtil
- {
- private $accountId = '';//供应商系统分配给OTA的账户
- private $signkey = '';//秘钥
- private $supplier_id = '';
- private $head = [];//头部
- private $body = [];//报文体
- private $request = '';//请求报文
- private static $util = null;
-
- private function __construct()
- {
- $this->accountId = \Yii::$app->params['ctrip_bus_config']['accountId'];
- $this->signkey = \Yii::$app->params['ctrip_bus_config']['signkey'];
- $this->supplier_id = \Yii::$app->params['ctrip_bus_config']['supplier_id'];
-
- }
-
- /**
- * Des:通用方法 只是用一些通用方法
- * Name: util
- * @return CtripBusUtil
- * @author 倪宗锋
- */
- public static function util()
- {
- if (static::$util == null) {
- static::$util = new self();
- }
- return static::$util;
- }
-
- /**
- * Des:获取返回值
- * Name: getRequest
- * @return array head 头部 body 报文体
- * @author 倪宗锋
- */
- public function getRequest()
- {
- $data = file_get_contents("php://input");
- try {
- $this->request = json_decode($data, true);
- $this->head = empty($this->request['head']) ? ['head' => []] : ['head' => $this->request['head']];
- $this->body = empty($this->request['body']) ? ['body' => []] : ['body' => $this->request['body']];
- if (empty($this->head['head']['channel']) || $this->accountId != $this->head['head']['channel']) {
- return Util::returnArrEr('账户不存在', '', '300');
- }
- $sign = $this->setSign();
- if (!isset($this->head['head']['sign']) || $sign != $this->head['head']['sign']) {
- return Util::returnArrEr('签名校验失败', ['sign' => $sign], '300');
- }
- return Util::returnArrSu('', $this->request);
- } catch (\Exception $e) {
- return Util::returnArrEr('数据解析失败', '', '300');
- }
- }
-
- /**
- * Des:获取body参数
- * Name: getBody
- * @return array
- * @author 倪宗锋
- */
- public function getBody()
- {
- return $this->body['body'];
- }
-
- /**
- * Des:获取渠道ID
- * Name: getSupplierId
- * @return string
- * @author 倪宗锋
- */
- public function getSupplierId(){
- return $this->supplier_id;
- }
-
- /***
- * Des:获取签名
- * Name: setSign
- * @return string
- * @author 倪宗锋
- */
- public function setSign()
- {
- $string = $this->signkey . $this->arrayKeyVal($this->body) . $this->signkey;
- return md5(strtoupper($string));
- }
-
- /**
- * Des:数组所有的健值进行排序拼接
- * Name: arrayKeyVal
- * @param $array
- * @return string
- * @author 倪宗锋
- */
- public function arrayKeyVal($array)
- {
- if (!is_array($array) || count($array) == 0) {
- return '';
- }
- ksort($array);//数组更据key排序
- $keyVal = '';
- foreach ($array as $key => $val) {
- if (is_array($val)) {
- $keyVal .= $key . $this->arrayKeyVal($val);
- } elseif ($val === null) {
- continue;
- } else {
- $keyVal .= $key . $val;
- }
- }
- return $keyVal;
- }
-
- /**
- * Des:重置路由访问地址
- * Name: setAction
- * @param $action
- * @param $actionName
- * @author 倪宗锋
- */
- public function setAction($action, $actionName)
- {
- $action->controller->action->id = strtolower($actionName);
- $action->controller->action->actionMethod = 'action' . ucfirst($actionName);
- $action->id = strtolower($actionName);
- $action->actionMethod = 'action' . ucfirst($actionName);
- }
-
- /**
- * Des:返回信息
- * Name: returnErr
- * @param $msg
- * @param $body
- * @param $code
- * @return string
- * @author 倪宗锋
- */
- public function re($msg = '', $code = '1', $body = '')
- {
- if (empty($body)) {
- $body = (object) [];
- }
- $return = [
- 'head' => [
- 'code' => $code,
- 'message' => $msg
- ],
- 'body' => $body
- ];
- \Yii::$app->response->format = 'json';
- \Yii::$app->response->data = $return;
- return;
- }
- }
|