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; } }