SiteController.php 3.1 KB

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