You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

196 lines
5.4 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. <?php
  2. namespace common\models;
  3. use Yii;
  4. use yii\behaviors\TimestampBehavior;
  5. use yii\web\IdentityInterface;
  6. use yii\web\NotFoundHttpException;
  7. use yii\web\UnauthorizedHttpException;
  8. /**
  9. * This is the model class for table "user".
  10. *
  11. * @property int $id
  12. * @property string $username 用户名
  13. * @property string $auth_key
  14. * @property string $password_hash
  15. * @property string $password_reset_token
  16. * @property string $name 姓名
  17. * @property int $sex 性别
  18. * @property string $phone 联系方式
  19. * @property string $email 邮箱
  20. * @property int $role 角色
  21. * @property int $status 状态
  22. * @property string $access_token 令牌
  23. * @property int $expire_at 令牌过期时间
  24. * @property string $nickname 昵称
  25. * @property string $avatar 头像
  26. * @property string $wx_openid 微信openid
  27. * @property string $mini_openid 小程序openid
  28. * @property string $unionid 微信联合id
  29. * @property string $session_key 小程序会话秘钥
  30. * @property string $member_code 会员编号
  31. * @property int $exp_point 经验值
  32. * @property int $consume_point 消费积分
  33. * @property int $created_at 创建时间
  34. * @property int $updated_at 更新时间
  35. */
  36. class User extends \yii\db\ActiveRecord implements IdentityInterface
  37. {
  38. const STATUS_DELETED = 0;
  39. const STATUS_ACTIVE = 1;
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public static function tableName()
  44. {
  45. return 'user';
  46. }
  47. public function fields()
  48. {
  49. $fields = parent::fields();
  50. unset($fields['auth_key']);
  51. unset($fields['password_hash']);
  52. unset($fields['password_reset_token']);
  53. unset($fields['role']);
  54. unset($fields['status']);
  55. unset($fields['access_token']);
  56. unset($fields['expire_at']);
  57. unset($fields['wx_openid']);
  58. unset($fields['mini_openid']);
  59. unset($fields['session_key']);
  60. unset($fields['created_at']);
  61. unset($fields['updated_at']);
  62. return $fields;
  63. }
  64. /**
  65. * {@inheritdoc}
  66. */
  67. public function rules()
  68. {
  69. return [
  70. [['auth_key', 'password_hash'], 'required'],
  71. [['sex', 'role', 'status', 'expire_at', 'exp_point', 'consume_point'], 'integer'],
  72. [['username', 'password_hash', 'password_reset_token', 'email', 'access_token', 'avatar'], 'string', 'max' => 255],
  73. [['auth_key', 'wx_openid', 'mini_openid'], 'string', 'max' => 32],
  74. [['name', 'nickname', 'session_key'], 'string', 'max' => 120],
  75. [['phone'], 'string', 'max' => 13],
  76. [['unionid'], 'string', 'max' => 60],
  77. [['member_code'], 'string', 'max' => 20],
  78. ];
  79. }
  80. /**
  81. * {@inheritdoc}
  82. */
  83. public function attributeLabels()
  84. {
  85. return [
  86. 'id' => 'id',
  87. 'username' => 'username',
  88. 'auth_key' => 'auth_key',
  89. 'password_hash' => 'password_hash',
  90. 'password_reset_token' => 'password_reset_token',
  91. 'name' => 'name',
  92. 'sex' => 'sex',
  93. 'phone' => '联系方式',
  94. 'email' => 'email',
  95. 'role' => 'role',
  96. 'status' => 'status',
  97. 'access_token' => 'access_token',
  98. 'expire_at' => 'expire_at',
  99. 'nickname' => 'nickname',
  100. 'avatar' => 'avatar',
  101. 'wx_openid' => '公众号openid',
  102. 'mini_openid' => '小程序openid',
  103. 'unionid' => 'unionid',
  104. 'session_key' => '小程序解密密钥',
  105. 'member_code' => '会员编号',
  106. 'exp_point' => '经验值',
  107. 'consume_point' => '消费积分',
  108. 'created_at' => '创建时间',
  109. 'updated_at' => '更新时间',
  110. ];
  111. }
  112. /**
  113. * @author linyao
  114. * @email 602604991@qq.com
  115. * @created Nov 8, 2019
  116. *
  117. * 行为存储创建时间和更新时间
  118. */
  119. public function behaviors()
  120. {
  121. return [
  122. [
  123. 'class' => TimestampBehavior::className(),
  124. 'createdAtAttribute' => 'created_at',
  125. 'updatedAtAttribute' => 'updated_at',
  126. 'value' => function () {
  127. return time();
  128. },
  129. ],
  130. ];
  131. }
  132. /**
  133. * @inheritDoc
  134. */
  135. public static function findIdentity($id)
  136. {
  137. return static::findOne(['id' => $id, 'status' => self::STATUS_ACTIVE]);
  138. }
  139. /**
  140. * @param mixed $token
  141. * @param null $type
  142. * @return array|\yii\db\ActiveRecord|IdentityInterface|null
  143. * @throws NotFoundHttpException
  144. * @throws UnauthorizedHttpException
  145. */
  146. public static function findIdentityByAccessToken($token, $type = null)
  147. {
  148. $user = static::find()
  149. ->where(['access_token' => $token, 'status' => self::STATUS_ACTIVE])
  150. ->one();
  151. if (!$user) {
  152. throw new NotFoundHttpException('user not found');
  153. }
  154. if ($user->expire_at < time()) {
  155. throw new UnauthorizedHttpException('access - token expired ', -1);
  156. } else {
  157. return $user;
  158. }
  159. }
  160. /**
  161. * @inheritDoc
  162. */
  163. public function getId()
  164. {
  165. return $this->getPrimaryKey();
  166. }
  167. /**
  168. * @inheritDoc
  169. */
  170. public function getAuthKey()
  171. {
  172. return $this->auth_key;
  173. }
  174. /**
  175. * @inheritDoc
  176. */
  177. public function validateAuthKey($authKey)
  178. {
  179. return $this->getAuthKey() === $authKey;
  180. }
  181. }