AccessToken.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace common\models\api;
  3. use Yii;
  4. use yii\db\ActiveRecord;
  5. use yii\web\UnauthorizedHttpException;
  6. use common\enums\StatusEnum;
  7. use common\models\member\Member;
  8. use common\models\rbac\AuthAssignment;
  9. use common\models\base\User;
  10. /**
  11. * This is the model class for table "{{%api_access_token}}".
  12. *
  13. * @property int $id
  14. * @property int|null $merchant_id 商户ID
  15. * @property int|null $store_id 店铺ID
  16. * @property string|null $refresh_token 刷新令牌
  17. * @property string|null $access_token 授权令牌
  18. * @property int|null $member_id 用户id
  19. * @property int|null $member_type 用户类型
  20. * @property string|null $group 组别
  21. * @property int|null $status 状态[-1:删除;0:禁用;1启用]
  22. * @property int|null $created_at 创建时间
  23. * @property int|null $updated_at 修改时间
  24. */
  25. class AccessToken extends User
  26. {
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public static function tableName()
  31. {
  32. return '{{%api_access_token}}';
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function rules()
  38. {
  39. return [
  40. [['merchant_id', 'member_id', 'store_id', 'member_type', 'status', 'created_at', 'updated_at'], 'integer'],
  41. [['refresh_token', 'access_token'], 'string', 'max' => 60],
  42. [['group'], 'string', 'max' => 100],
  43. [['access_token'], 'unique'],
  44. [['refresh_token'], 'unique'],
  45. ];
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function attributeLabels()
  51. {
  52. return [
  53. 'id' => 'ID',
  54. 'merchant_id' => '商户ID',
  55. 'store_id' => '店铺ID',
  56. 'refresh_token' => '刷新令牌',
  57. 'access_token' => '授权令牌',
  58. 'member_id' => '用户id',
  59. 'member_type' => '用户类型',
  60. 'group' => '组别',
  61. 'status' => '状态',
  62. 'created_at' => '创建时间',
  63. 'updated_at' => '修改时间',
  64. ];
  65. }
  66. /**
  67. * @param mixed $token
  68. * @param null $type
  69. * @return array|mixed|ActiveRecord|\yii\web\IdentityInterface|null
  70. * @throws UnauthorizedHttpException
  71. */
  72. public static function findIdentityByAccessToken($token, $type = null)
  73. {
  74. // 判断验证token有效性是否开启
  75. if (Yii::$app->params['user.accessTokenValidity'] === true) {
  76. $timestamp = (int)substr($token, strrpos($token, '_') + 1);
  77. $expire = Yii::$app->params['user.accessTokenExpire'];
  78. // 验证有效期
  79. if ($timestamp + $expire <= time()) {
  80. throw new UnauthorizedHttpException('您的登录验证已经过期,请重新登录');
  81. }
  82. }
  83. // 优化版本到缓存读取用户信息 注意需要开启服务层的cache
  84. return Yii::$app->services->apiAccessToken->getTokenToCache($token, $type);
  85. }
  86. /**
  87. * @param $token
  88. * @param null $group
  89. * @return AccessToken|\common\models\base\User|null
  90. */
  91. public static function findIdentityByRefreshToken($token, $group = null)
  92. {
  93. return static::findOne(['group' => $group, 'refresh_token' => $token, 'status' => StatusEnum::ENABLED]);
  94. }
  95. /**
  96. * 关联用户
  97. *
  98. * @return \yii\db\ActiveQuery
  99. */
  100. public function getMember()
  101. {
  102. return $this->hasOne(Member::class, ['id' => 'member_id']);
  103. }
  104. /**
  105. * 关联授权角色
  106. *
  107. * @return \yii\db\ActiveQuery
  108. */
  109. public function getAssignment()
  110. {
  111. return $this->hasOne(AuthAssignment::class, ['user_id' => 'member_id'])->where(['app_id' => Yii::$app->id]);
  112. }
  113. }
粤ICP备19079148号