response->format = \yii\web\Response::FORMAT_JSON; return [ 'message' => 'API test Ok!', 'code' => 100, ]; // return $this->render('index'); } /** * Displays homepage. * * @return mixed */ public function actionPage() { \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; $params=Yii::$app->request->get();//获取参数 $rabbitdata=$this->validate($params); if($rabbitdata){ PhpClient::CallMq($rabbitdata); return [ 'message' => 'rabbit insert Ok!', 'action' => 'insert', 'rabbitdata' => $params, 'code' => 100, ]; } } /** * Displays homepage. * * @return mixed */ public function actionMqinsert() { \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; $params=Yii::$app->request->get();//获取参数 $rabbitdata=$this->validate($params); if($rabbitdata){ PhpClient::CallMq($rabbitdata); return [ 'message' => 'rabbit insert Ok!', 'action' => 'insert', 'rabbitdata' => $params, 'code' => 100, ]; } } /** * Displays homepage. * * @return mixed */ private function validate($params) { \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; if(isset($params["signature"]) && isset($params["timestamp"]) && isset($params["rabbitdata"])){ $signature = $params["signature"];//本地签名 $timestamp = $params["timestamp"];//时间戳 $rabbitdata = $params["rabbitdata"];//rabbitdata 存入mq中的数据 unset($params['r'],$params['signature'],$params['rabbitdata']); //valid signature , option if($this->checkSignature($params,$timestamp,$signature)){ return $rabbitdata;//返回rabbitdata }else if(!$this->checkRabbitdata($rabbitdata)){ return false; }else{ exit(json_encode([ 'message' => 'signature test fail!', 'code' => 201, ])); } }else{ exit(json_encode([ 'message' => 'params key canot be null!', 'code' => 203, ])); } } private static function getSign($params, $appkey, $appSecret, $time) { $sign = ''; if (!empty($params)) { ksort($params); $string = http_build_query($params); $result = md5($appkey . $string . $appSecret . $time); $sign = strtoupper($result); } return $sign; } private function checkSignature($params,$timestamp,$signature) { defined('APP_ID') or define("APP_ID", "disanbo"); defined('APP_SECRET') or define("APP_SECRET", "di~sanbo1"); $appkey = APP_ID; $appSecret = APP_SECRET; $sign= $this->getSign($params, $appkey, $appSecret, $timestamp); // var_dump($sign);die; if( $sign == $signature ){ //do something return true; }else{ return false; } } /* rabbitdata * * * */ private function checkRabbitdata($rabbitdata) { //其他验证 dosomething if(!empty($rabbitdata)){ return true; }else{ exit(json_encode([ 'message' => 'rabbitdata cannot be null!', 'code' => 202, ])); } } }