0
0

ApiController.php 4.3 KB

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