AccessToken.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. namespace common\models\api;
  3. use addons\QiJuhe\common\models\ExpressModel;
  4. use common\enums\LoginEnum;
  5. use common\models\mall\Mall;
  6. use common\models\user\User;
  7. use common\models\user\UserInfo;
  8. use Yii;
  9. use yii\db\ActiveRecord;
  10. use yii\behaviors\TimestampBehavior;
  11. use yii\web\UnauthorizedHttpException;
  12. use common\enums\StatusEnum;
  13. use common\models\common\RateLimit;
  14. use common\models\rbac\AuthAssignment;
  15. /**
  16. * 如果不想速率控制请直接继承 \common\models\base\BaseModel
  17. *
  18. * This is the model class for table "{{%api_access_token}}".
  19. *
  20. * @property string $id
  21. * @property int $mch_id 商户id
  22. * @property string $refresh_token 刷新令牌
  23. * @property string $access_token 授权令牌
  24. * @property int $user_id 用户id
  25. * @property string $openid
  26. * @property string $group 组别
  27. * @property int $status 状态[-1:删除;0:禁用;1启用]
  28. * @property int $created_at 创建时间
  29. * @property int $updated_at 修改时间
  30. */
  31. class AccessToken extends RateLimit
  32. {
  33. /**
  34. * {@inheritdoc}
  35. */
  36. public static function tableName()
  37. {
  38. return '{{%access_token}}';
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function rules()
  44. {
  45. return [
  46. [['mch_id', 'user_id', 'status', 'created_at', 'updated_at'], 'integer'],
  47. [['refresh_token', 'access_token'], 'string', 'max' => 60],
  48. [['openid'], 'string', 'max' => 50],
  49. [['group'], 'string', 'max' => 100],
  50. [['access_token'], 'unique'],
  51. [['refresh_token'], 'unique'],
  52. ];
  53. }
  54. /**
  55. * {@inheritdoc}
  56. */
  57. public function attributeLabels()
  58. {
  59. return [
  60. 'id' => 'ID',
  61. 'mch_id' => '商户',
  62. 'refresh_token' => '重置令牌',
  63. 'access_token' => '登录令牌',
  64. 'openid' => 'openid',
  65. 'user_id' => '会员ID',
  66. 'group' => '组别',
  67. 'status' => '状态',
  68. 'created_at' => '创建时间',
  69. 'updated_at' => '更新时间',
  70. ];
  71. }
  72. /**
  73. * @param mixed $token
  74. * @param null $type
  75. * @return array|mixed|ActiveRecord|\yii\web\IdentityInterface|null
  76. * @throws UnauthorizedHttpException
  77. */
  78. public static function findIdentityByAccessToken($token, $type = null)
  79. {
  80. // 判断验证token有效性是否开启
  81. if($token){
  82. if (Yii::$app->params['user.accessTokenValidity'] === true) {
  83. $timestamp = (int)substr($token, strrpos($token, '_') + 1);
  84. $expire = Yii::$app->params['user.accessTokenExpire'];
  85. // 验证有效期
  86. if ($timestamp + $expire <= time()) throw new UnauthorizedHttpException('用户访问令牌已过期,请重新登录获取');
  87. }
  88. }else{
  89. if(!empty(Yii::$app->request->headers->get('openid'))){
  90. $where=['openid'=>Yii::$app->request->headers->get('openid'),'status'=>StatusEnum::ENABLED];
  91. $headers = \Yii::$app->request->headers;
  92. if(!empty($headers['x-mall-sign'])) $mall_sign = $headers['x-mall-sign'];
  93. if(!empty(\Yii::$app->request->get('mall-sign'))) $mall_sign = \Yii::$app->request->get('mall-sign');
  94. if(!empty($mall_sign)){
  95. $mall = Mall::getMallInfo($mall_sign);
  96. $where['mall_id'] = $mall['id'];
  97. }
  98. $user_info = UserInfo::findOne($where);
  99. if(empty($user_info))throw new UnauthorizedHttpException('此openid未授权,请重新授权');
  100. if($user_info['user_id'] == 0) throw new UnauthorizedHttpException('此用户未授权手机号码');
  101. }
  102. }
  103. // 优化版本到缓存读取用户信息 注意需要开启服务层的cache
  104. return Yii::$app->services->apiAccessToken->getTokenToCache($token, $type);
  105. }
  106. /**
  107. * @param $token
  108. * @param null $group
  109. * @return AccessToken|\common\models\base\User|null
  110. */
  111. public static function findIdentityByRefreshToken($token, $group = null)
  112. {
  113. return static::findOne(['group' => $group, 'refresh_token' => $token, 'status' => StatusEnum::ENABLED]);
  114. }
  115. /**
  116. * 关联用户
  117. *
  118. * @return \yii\db\ActiveQuery
  119. */
  120. public function getUser()
  121. {
  122. return $this->hasOne(User::class, ['id' => 'user_id']);
  123. }
  124. /**
  125. * 关联授权角色
  126. *
  127. * @return \yii\db\ActiveQuery
  128. */
  129. public function getAssignment()
  130. {
  131. return $this->hasOne(AuthAssignment::class, ['user_id' => 'member_id'])
  132. ->where(['app_id' => Yii::$app->id]);
  133. }
  134. /**
  135. * @return array
  136. */
  137. public function behaviors()
  138. {
  139. return [
  140. [
  141. 'class' => TimestampBehavior::class,
  142. 'attributes' => [
  143. ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
  144. ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
  145. ],
  146. ],
  147. ];
  148. }
  149. }