SignAuthForm.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. namespace common\forms;
  3. use Yii;
  4. use yii\base\Model;
  5. /**
  6. * Class SignAuthForm
  7. * @package common\forms
  8. * @author jianyan74 <751393839@qq.com>
  9. */
  10. class SignAuthForm extends Model
  11. {
  12. public $time;
  13. public $sign;
  14. public $nonceStr;
  15. public $appId;
  16. public $appSecret;
  17. /**
  18. * 时间过期容差
  19. *
  20. * @var int
  21. */
  22. public $toleranceTime = 60;
  23. /**
  24. * @inheritdoc
  25. */
  26. public function rules()
  27. {
  28. return [
  29. [['time', 'sign', 'appId', 'nonceStr'], 'required'],
  30. [['nonceStr'], 'string', 'min' => 8],
  31. [['time'], 'isPastDue'],
  32. [['appId'], 'isAuth'],
  33. ];
  34. }
  35. public function attributeLabels()
  36. {
  37. return [
  38. 'time' => '时间戳',
  39. 'nonceStr' => '随机数',
  40. 'sign' => '签名',
  41. 'appId' => 'appId',
  42. ];
  43. }
  44. /**
  45. * @param $attribute
  46. */
  47. public function isPastDue($attribute)
  48. {
  49. if (time() - $this->toleranceTime > $this->time) {
  50. $this->addError($attribute, '时间已过期');
  51. }
  52. }
  53. /**
  54. * @param $attribute
  55. */
  56. public function isAuth($attribute)
  57. {
  58. if (!isset(Yii::$app->params['user.httpSignAccount'][$this->appId])) {
  59. $this->addError($attribute, 'appId 无效');
  60. }
  61. $this->appSecret = Yii::$app->params['user.httpSignAccount'][$this->appId];
  62. }
  63. }
粤ICP备19079148号