SignSecretKeyController.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. namespace api\controllers;
  3. use Yii;
  4. use common\helpers\EncryptionHelper;
  5. use common\helpers\StringHelper;
  6. /**
  7. *
  8. * 签名加密控制器 - Test
  9. *
  10. * Class SignSecretKeyController
  11. * @package api\controllers
  12. * @author qimall
  13. */
  14. class SignSecretKeyController extends OnAuthController
  15. {
  16. public $modelClass = '';
  17. /**
  18. * 公钥
  19. *
  20. * @var string
  21. */
  22. protected $appId = 'doormen';
  23. /**
  24. * 密钥
  25. *
  26. * @var string
  27. */
  28. protected $appSecret = 'e3de3825cfbf';
  29. /**
  30. * 不用进行登录验证的方法
  31. * 例如: ['index', 'update', 'create', 'view', 'delete']
  32. * 默认全部需要验证
  33. *
  34. * @var array
  35. */
  36. protected $optional = ['index', 'create'];
  37. /**
  38. * 生成测试带签名秘钥的url
  39. *
  40. * 关于创建appId和appSecret可自行生成
  41. *
  42. * @return array|\yii\data\ActiveDataProvider
  43. */
  44. public function actionIndex()
  45. {
  46. $paraStr = EncryptionHelper::createUrlParam([
  47. 'appId' => $this->appId,
  48. 'time' => time(),
  49. 'nonceStr' => StringHelper::random(32),
  50. 'mobile' => '15888888888',
  51. ], $this->appSecret);
  52. return [
  53. 'url' => Yii::$app->request->hostInfo . '/api/sign-secret-key?' . $paraStr,
  54. 'method' => 'post',
  55. 'explain' => '请用post请求该链接进行测试带签名验证'
  56. ];
  57. }
  58. /**
  59. * 校验签名是否正确
  60. *
  61. * @return bool
  62. * @throws \yii\web\UnprocessableEntityHttpException
  63. */
  64. public function actionCreate()
  65. {
  66. return EncryptionHelper::decodeUrlParam(Yii::$app->request->get(), $this->appSecret);
  67. }
  68. }