CustomerAccessToken.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. /**
  3. * FecShop file.
  4. *
  5. * @link http://www.fecshop.com/
  6. * @copyright Copyright (c) 2016 FecShop Software LLC
  7. * @license http://www.fecshop.com/license/
  8. */
  9. namespace fecshop\models\mysqldb\customer;
  10. use Yii;
  11. use fecshop\models\mysqldb\Customer;
  12. use yii\base\NotSupportedException;
  13. use yii\behaviors\TimestampBehavior;
  14. use yii\web\IdentityInterface;
  15. use yii\filters\RateLimitInterface;
  16. /**
  17. * User model.
  18. *
  19. * @property int $id
  20. * @property string $username
  21. * @property string $password_hash
  22. * @property string $password_reset_token
  23. * @property string $email
  24. * @property string $auth_key
  25. * @property int $status
  26. * @property int $created_at
  27. * @property int $updated_at
  28. * @property string $password write-only password
  29. */
  30. /**
  31. * @author Terry Zhao <2358269014@qq.com>
  32. * @since 1.0
  33. */
  34. class CustomerAccessToken extends Customer implements IdentityInterface ,RateLimitInterface
  35. {
  36. # 速度控制 6秒内访问3次,注意,数组的第一个不要设置1,设置1会出问题,一定要
  37. #大于2,譬如下面 6秒内只能访问三次
  38. # 文档标注:返回允许的请求的最大数目及时间,例如,[100, 600] 表示在600秒内最多100次的API调用。
  39. public function getRateLimit($request, $action){
  40. $rateLimit = Yii::$app->params['rateLimit'];
  41. if(is_array($rateLimit['limit']) && !empty($rateLimit['limit'])){
  42. return $rateLimit['limit'];
  43. }else{
  44. return [120, 60];
  45. }
  46. }
  47. # 文档标注: 返回剩余的允许的请求和相应的UNIX时间戳数 当最后一次速率限制检查时。
  48. public function loadAllowance($request, $action){
  49. //return [1,strtotime(date("Y-m-d H:i:s"))];
  50. //echo $this->allowance;exit;
  51. return [$this->allowance, $this->allowance_updated_at];
  52. }
  53. # allowance 对应user 表的allowance字段 int类型
  54. # allowance_updated_at 对应user allowance_updated_at int类型
  55. # 文档标注:保存允许剩余的请求数和当前的UNIX时间戳。
  56. public function saveAllowance($request, $action, $allowance, $timestamp){
  57. $this->allowance = $allowance;
  58. $this->allowance_updated_at = $timestamp;
  59. $this->save();
  60. }
  61. /**
  62. * @inheritdoc
  63. */
  64. public function behaviors()
  65. {
  66. return [
  67. TimestampBehavior::className(),
  68. ];
  69. }
  70. }