123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- <?php
- namespace api\controllers;
- use components\PhpClient;
- use Yii;
- use yii\base\InvalidParamException;
- use yii\web\BadRequestHttpException;
- use yii\web\Controller;
- use yii\filters\VerbFilter;
- use yii\filters\AccessControl;
- use common\models\LoginForm;
- use frontend\models\PasswordResetRequestForm;
- use frontend\models\ResetPasswordForm;
- use frontend\models\SignupForm;
- use frontend\models\ContactForm;
- /**
- * Site controller
- */
- class ApiController extends Controller
- {
- /**
- * @inheritdoc
- /**
- * Displays homepage.
- *
- * @return mixed
- */
- public function actionIndex()
- {
- \Yii::$app->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']);
- //valid signature , option
- if($this->checkSignature($params,$timestamp,$signature)){
- return true;//返回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);
- }
- // var_dump($sign);die;
- 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,
- ]));
- }
- }
- }
|