ApiController.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. namespace api\controllers;
  3. use common\models\RabbitLog;
  4. use components\PhpClient;
  5. use Yii;
  6. use yii\base\InvalidParamException;
  7. use yii\web\BadRequestHttpException;
  8. use yii\web\Controller;
  9. use yii\filters\VerbFilter;
  10. use yii\filters\AccessControl;
  11. use common\models\LoginForm;
  12. use frontend\models\PasswordResetRequestForm;
  13. use frontend\models\ResetPasswordForm;
  14. use frontend\models\SignupForm;
  15. use frontend\models\ContactForm;
  16. /**
  17. * Site controller
  18. */
  19. class ApiController extends Controller
  20. {
  21. /**
  22. * @inheritdoc
  23. /**
  24. * Displays homepage.
  25. *
  26. * @return mixed
  27. */
  28. public function actionIndex()
  29. {
  30. \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
  31. return [
  32. 'message' => 'API test Ok!',
  33. 'code' => 100,
  34. ];
  35. // return $this->render('index');
  36. }
  37. /**
  38. * Displays homepage.
  39. *
  40. * @return mixed
  41. */
  42. public function actionPage()
  43. {
  44. \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
  45. $params=Yii::$app->request->get();//获取参数
  46. $rabbitdata=$this->validate($params);
  47. if($rabbitdata){
  48. PhpClient::CallMq($rabbitdata);
  49. return [
  50. 'message' => 'rabbit insert Ok!',
  51. 'action' => 'insert',
  52. 'rabbitdata' => $params,
  53. 'code' => 100,
  54. ];
  55. }
  56. }
  57. /**
  58. * Displays homepage.
  59. *
  60. * @return mixed
  61. */
  62. public function actionMqinsert()
  63. {
  64. \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
  65. $params=Yii::$app->request->get();//获取参数
  66. $rabbitdata=$this->validate($params);
  67. if($rabbitdata){
  68. PhpClient::CallMq($rabbitdata);
  69. return [
  70. 'message' => 'rabbit insert Ok!',
  71. 'action' => 'insert',
  72. 'rabbitdata' => $params,
  73. 'code' => 100,
  74. ];
  75. }
  76. }
  77. /**
  78. * Displays homepage.
  79. *
  80. * @return mixed
  81. */
  82. private function validate($params)
  83. {
  84. \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
  85. if(isset($params["signature"]) && isset($params["timestamp"]) && isset($params["rabbitdata"])){
  86. $signature = $params["signature"];//本地签名
  87. $timestamp = $params["timestamp"];//时间戳
  88. $rabbitdata = $params["rabbitdata"];//rabbitdata 存入mq中的数据
  89. unset($params['r'],$params['signature'],$params['rabbitdata']);
  90. //valid signature , option
  91. if($this->checkSignature($params,$timestamp,$signature)){
  92. return $rabbitdata;//返回rabbitdata
  93. }else if(!$this->checkRabbitdata($rabbitdata)){
  94. return false;
  95. }else{
  96. exit(json_encode([
  97. 'message' => 'signature test fail!',
  98. 'code' => 201,
  99. ]));
  100. }
  101. }else{
  102. exit(json_encode([
  103. 'message' => 'params key canot be null!',
  104. 'code' => 203,
  105. ]));
  106. }
  107. }
  108. private static function getSign($params, $appkey, $appSecret, $time)
  109. {
  110. $sign = '';
  111. if (!empty($params)) {
  112. ksort($params);
  113. $string = http_build_query($params);
  114. $result = md5($appkey . $string . $appSecret . $time);
  115. $sign = strtoupper($result);
  116. }
  117. return $sign;
  118. }
  119. private function checkSignature($params,$timestamp,$signature)
  120. {
  121. defined('APP_ID') or define("APP_ID", "disanbo");
  122. defined('APP_SECRET') or define("APP_SECRET", "di~sanbo1");
  123. $appkey = APP_ID;
  124. $appSecret = APP_SECRET;
  125. $sign= $this->getSign($params, $appkey, $appSecret, $timestamp);
  126. // var_dump($sign);die;
  127. if( $sign == $signature ){
  128. //do something
  129. return true;
  130. }else{
  131. return false;
  132. }
  133. }
  134. /* rabbitdata
  135. *
  136. *
  137. * */
  138. private function checkRabbitdata($rabbitdata)
  139. {
  140. //其他验证 dosomething
  141. if(!empty($rabbitdata)){
  142. return true;
  143. }else{
  144. exit(json_encode([
  145. 'message' => 'rabbitdata cannot be null!',
  146. 'code' => 202,
  147. ]));
  148. }
  149. }
  150. }