User.php 3.9 KB

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