User.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace app\admin\model;
  3. use think\Model;
  4. class User extends Model
  5. {
  6. // 表名
  7. protected $table = 'user';
  8. // 自动写入时间戳字段
  9. protected $autoWriteTimestamp = 'integer';
  10. // 定义时间戳字段名
  11. protected $createTime = 'createtime';
  12. protected $updateTime = 'updatetime';
  13. protected $deleteTime = false;
  14. // 追加属性
  15. protected $append = [
  16. 'gender_text',
  17. 'prevtime_text',
  18. 'logintime_text',
  19. 'jointime_text',
  20. 'status_text',
  21. 'birthday_text',
  22. ];
  23. public function getGenderList()
  24. {
  25. return ['1' => __('Gender 1'), '0' => __('Gender 0')];
  26. }
  27. public function getStatusList()
  28. {
  29. return ['1' => __('Status 1'), '0' => __('Status 0'), '-1' => __('Status -1')];
  30. }
  31. public function getGenderTextAttr($value, $data)
  32. {
  33. $value = $value ? $value : (isset($data['gender']) ? $data['gender'] : '');
  34. $list = $this->getGenderList();
  35. return isset($list[$value]) ? $list[$value] : '';
  36. }
  37. public function getPrevtimeTextAttr($value, $data)
  38. {
  39. $value = $value ? $value : (isset($data['prevtime']) ? $data['prevtime'] : '');
  40. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  41. }
  42. public function getBirthdayTextAttr($value, $data)
  43. {
  44. $value = $value ? $value : (isset($data['birthday']) ? $data['birthday'] : '');
  45. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  46. }
  47. public function getLogintimeTextAttr($value, $data)
  48. {
  49. $value = $value ? $value : (isset($data['logintime']) ? $data['logintime'] : '');
  50. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  51. }
  52. public function getJointimeTextAttr($value, $data)
  53. {
  54. $value = $value ? $value : (isset($data['jointime']) ? $data['jointime'] : '');
  55. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  56. }
  57. public function getStatusTextAttr($value, $data)
  58. {
  59. $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
  60. $list = $this->getStatusList();
  61. return isset($list[$value]) ? $list[$value] : '';
  62. }
  63. protected function setPrevtimeAttr($value)
  64. {
  65. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  66. }
  67. protected function setLogintimeAttr($value)
  68. {
  69. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  70. }
  71. protected function setJointimeAttr($value)
  72. {
  73. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  74. }
  75. protected function setBirthdayAttr($value)
  76. {
  77. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  78. }
  79. public function group()
  80. {
  81. return $this->belongsTo('UserGroup', 'group_id', 'id', [], 'LEFT')->setEagerlyType(0);
  82. }
  83. }