UserInfoModel.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace app\admin\model\exam;
  3. use addons\exam\enum\UserType;
  4. use addons\exam\model\BaseModel;
  5. use think\Db;
  6. class UserInfoModel extends BaseModel
  7. {
  8. // 表名
  9. protected $name = 'exam_user_info';
  10. // 自动写入时间戳字段
  11. protected $autoWriteTimestamp = 'int';
  12. // 定义时间戳字段名
  13. protected $createTime = 'createtime';
  14. protected $updateTime = 'updatetime';
  15. protected $deleteTime = false;
  16. // 追加属性
  17. protected $append
  18. = [
  19. 'type_text',
  20. 'expire_time_text',
  21. 'status',
  22. ];
  23. // protected $type = [
  24. // 'default_cate_ids' => 'array',
  25. // 'default_cate_names' => 'array',
  26. // ];
  27. public function getTypeList()
  28. {
  29. // return ['NORMAL' => __('Type normal'), 'VIP_MONTH' => __('Type vip_month'), 'VIP_YEAR' => __('Type vip_year'), 'VIP_LIFE' => __('Type vip_life')];
  30. return UserType::getKeyDescription();
  31. }
  32. public function getTypeTextAttr($value, $data)
  33. {
  34. // $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
  35. // $list = $this->getTypeList();
  36. // return isset($list[$value]) ? $list[$value] : '';
  37. $member_config_id = isset($data['member_config_id']) ? $data['member_config_id'] : 0;
  38. if ($member_config_id) {
  39. // 会员或曾经是会员
  40. if ($member_name = Db::name('exam_member_config')->where('id', $member_config_id)->value('name')) {
  41. return $member_name;
  42. }
  43. }
  44. return '普通用户';
  45. }
  46. public function getExpireTimeTextAttr($value, $data)
  47. {
  48. $value = $value ? $value : (isset($data['expire_time']) ? $data['expire_time'] : '');
  49. return is_numeric($value) && $value ? date("Y-m-d H:i:s", $value) : $value;
  50. }
  51. protected function setExpireTimeAttr($value)
  52. {
  53. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  54. }
  55. public function getStatusAttr($value, $data)
  56. {
  57. // 非会员
  58. $status = 0;
  59. // $member_config_id = isset($data['member_config_id']) ? $data['member_config_id'] : 0;
  60. $expire_time = isset($data['expire_time']) ? $data['expire_time'] : 0;
  61. if ($expire_time > 0) {//$member_config_id &&
  62. // 是会员
  63. $status = 1;
  64. }
  65. if ($expire_time < time()) {
  66. // 会员过期
  67. $status = 2;
  68. }
  69. return $status;
  70. }
  71. public function user()
  72. {
  73. return $this->belongsTo('app\admin\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
  74. }
  75. public function memberConfig()
  76. {
  77. return $this->belongsTo(MemberConfigModel::class, 'member_config_id', 'id', [], 'LEFT')->setEagerlyType(0);
  78. }
  79. }