User.php 3.2 KB

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