User.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. namespace app\admin\model;
  3. use app\common\model\ScoreLog;
  4. use think\Model;
  5. class User extends Model
  6. {
  7. // 表名
  8. protected $name = 'user';
  9. // 自动写入时间戳字段
  10. protected $autoWriteTimestamp = 'int';
  11. // 定义时间戳字段名
  12. protected $createTime = 'createtime';
  13. protected $updateTime = 'updatetime';
  14. // 追加属性
  15. protected $append = [
  16. 'logintime_text',
  17. 'is_cool_text',
  18. 'is_manager_text',
  19. ];
  20. public function getOriginData()
  21. {
  22. return $this->origin;
  23. }
  24. protected static function init()
  25. {
  26. self::beforeUpdate(function ($row) {
  27. $changed = $row->getChangedData();
  28. //如果有修改密码
  29. if (isset($changed['password'])) {
  30. if ($changed['password']) {
  31. $salt = \fast\Random::alnum();
  32. $row->password = \app\common\library\Auth::instance()->getEncryptPassword($changed['password'], $salt);
  33. $row->salt = $salt;
  34. } else {
  35. unset($row->password);
  36. }
  37. }
  38. });
  39. self::beforeUpdate(function ($row) {
  40. $changedata = $row->getChangedData();
  41. if (isset($changedata['score'])) {
  42. $origin = $row->getOriginData();
  43. ScoreLog::create(['user_id' => $row['id'], 'score' => $changedata['score'] - $origin['score'], 'before' => $origin['score'], 'after' => $changedata['score'], 'memo' => '管理员变更积分']);
  44. }
  45. });
  46. }
  47. public function getGenderList()
  48. {
  49. return ['1' => __('Male'), '0' => __('Female')];
  50. }
  51. public function getStatusList()
  52. {
  53. return ['normal' => __('Normal'), 'hidden' => __('Hidden')];
  54. }
  55. public function getPrevtimeTextAttr($value, $data)
  56. {
  57. $value = $value ? $value : $data['prevtime'];
  58. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  59. }
  60. public function getLogintimeTextAttr($value, $data)
  61. {
  62. $value = $value ? $value : $data['logintime'];
  63. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  64. }
  65. public function getJointimeTextAttr($value, $data)
  66. {
  67. $value = $value ? $value : $data['jointime'];
  68. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  69. }
  70. protected function setPrevtimeAttr($value)
  71. {
  72. return $value && !is_numeric($value) ? strtotime($value) : $value;
  73. }
  74. protected function setLogintimeAttr($value)
  75. {
  76. return $value && !is_numeric($value) ? strtotime($value) : $value;
  77. }
  78. protected function setJointimeAttr($value)
  79. {
  80. return $value && !is_numeric($value) ? strtotime($value) : $value;
  81. }
  82. protected function setBirthdayAttr($value)
  83. {
  84. return $value ? $value : null;
  85. }
  86. public function getIsCoolList()
  87. {
  88. return ['0' =>__('Is_cool 0'), '1' =>__('Is_cool 1')];
  89. }
  90. public function getIsCoolTextAttr($value, $data)
  91. {
  92. $value = $value ? $value : (isset($data['is_cool']) ? $data['is_cool'] : '');
  93. $list = $this->getIsCoolList();
  94. return isset($list[$value]) ? $list[$value] : '';
  95. }
  96. public function getIsManagerList()
  97. {
  98. return ['0' =>__('Is_manager 0'), '1' =>__('Is_manager 1')];
  99. }
  100. public function getIsManagerTextAttr($value, $data)
  101. {
  102. $value = $value ? $value : (isset($data['is_manager']) ? $data['is_manager'] : '');
  103. $list = $this->getIsManagerList();
  104. return isset($list[$value]) ? $list[$value] : '';
  105. }
  106. public function noble()
  107. {
  108. return $this->belongsTo('app\admin\model\noble\Level', 'noble', 'id', [], 'LEFT')->setEagerlyType(0);
  109. }
  110. public function preuser()
  111. {
  112. return $this->belongsTo('app\admin\model\User', 'pre_userid', 'id', [], 'LEFT')->setEagerlyType(0);
  113. }
  114. public function auth()
  115. {
  116. return $this->belongsTo('app\admin\model\user\Auth', 'id', 'user_id', [], 'LEFT')->setEagerlyType(0);
  117. }
  118. public function age()
  119. {
  120. return $this->belongsTo('app\admin\model\Age', 'age_id', 'id', [], 'LEFT')->setEagerlyType(0);
  121. }
  122. public function constellation()
  123. {
  124. return $this->belongsTo('app\admin\model\Constellation', 'constellation_id', 'id', [], 'LEFT')->setEagerlyType(0);
  125. }
  126. }