User.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. ];
  18. public function getOriginData()
  19. {
  20. return $this->origin;
  21. }
  22. protected static function init()
  23. {
  24. self::beforeUpdate(function ($row) {
  25. $changed = $row->getChangedData();
  26. //如果有修改密码
  27. if (isset($changed['password'])) {
  28. if ($changed['password']) {
  29. $salt = \fast\Random::alnum();
  30. $row->password = \app\common\library\Auth::instance()->getEncryptPassword($changed['password'], $salt);
  31. $row->salt = $salt;
  32. } else {
  33. unset($row->password);
  34. }
  35. }
  36. });
  37. self::beforeUpdate(function ($row) {
  38. $changedata = $row->getChangedData();
  39. if (isset($changedata['score'])) {
  40. $origin = $row->getOriginData();
  41. ScoreLog::create(['user_id' => $row['id'], 'score' => $changedata['score'] - $origin['score'], 'before' => $origin['score'], 'after' => $changedata['score'], 'memo' => '管理员变更积分']);
  42. }
  43. });
  44. }
  45. public function getGenderList()
  46. {
  47. return ['1' => __('Male'), '0' => __('Female')];
  48. }
  49. public function getStatusList()
  50. {
  51. return ['normal' => __('Normal'), 'hidden' => __('Hidden')];
  52. }
  53. public function getPrevtimeTextAttr($value, $data)
  54. {
  55. $value = $value ? $value : $data['prevtime'];
  56. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  57. }
  58. public function getLogintimeTextAttr($value, $data)
  59. {
  60. $value = $value ? $value : $data['logintime'];
  61. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  62. }
  63. public function getJointimeTextAttr($value, $data)
  64. {
  65. $value = $value ? $value : $data['jointime'];
  66. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  67. }
  68. protected function setPrevtimeAttr($value)
  69. {
  70. return $value && !is_numeric($value) ? strtotime($value) : $value;
  71. }
  72. protected function setLogintimeAttr($value)
  73. {
  74. return $value && !is_numeric($value) ? strtotime($value) : $value;
  75. }
  76. protected function setJointimeAttr($value)
  77. {
  78. return $value && !is_numeric($value) ? strtotime($value) : $value;
  79. }
  80. protected function setBirthdayAttr($value)
  81. {
  82. return $value ? $value : null;
  83. }
  84. public function noble()
  85. {
  86. return $this->belongsTo('app\admin\model\noble\Level', 'noble', 'id', [], 'LEFT')->setEagerlyType(0);
  87. }
  88. public function preuser()
  89. {
  90. return $this->belongsTo('app\admin\model\User', 'pre_userid', 'id', [], 'LEFT')->setEagerlyType(0);
  91. }
  92. public function auth()
  93. {
  94. return $this->belongsTo('app\admin\model\user\Auth', 'id', 'user_id', [], 'LEFT')->setEagerlyType(0);
  95. }
  96. public function age()
  97. {
  98. return $this->belongsTo('app\admin\model\Age', 'age_id', 'id', [], 'LEFT')->setEagerlyType(0);
  99. }
  100. public function constellation()
  101. {
  102. return $this->belongsTo('app\admin\model\Constellation', 'constellation_id', 'id', [], 'LEFT')->setEagerlyType(0);
  103. }
  104. }