UserInfoModel.php 989 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. namespace addons\exam\model;
  3. use addons\exam\enum\UserType;
  4. class UserInfoModel extends \app\admin\model\exam\UserInfoModel
  5. {
  6. public function user()
  7. {
  8. return $this->belongsTo(UserModel::class, 'user_id');
  9. }
  10. /**
  11. * 初始化用户扩展信息
  12. * @param $user_id
  13. * @param $type
  14. * @return UserInfoModel
  15. */
  16. public static function initInfo($user_id, $type = UserType::NORMAL)
  17. {
  18. return self::create([
  19. 'user_id' => $user_id,
  20. 'type' => $type,
  21. 'score' => 0,
  22. 'expire_time' => 0,
  23. 'member_config_id' => 0,
  24. ]);
  25. }
  26. /**
  27. * 获取用户扩展信息
  28. * @param $user_id
  29. * @return UserInfoModel
  30. */
  31. public static function getUserInfo($user_id)
  32. {
  33. if ($info = self::where('user_id', $user_id)->find()) {
  34. return $info;
  35. }
  36. return self::initInfo($user_id);
  37. }
  38. }