User.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. $origin = $row->getOriginData();
  43. if (isset($changedata['money']) && (function_exists('bccomp') ? bccomp($changedata['money'], $origin['money'], 2) !== 0 : (double)$changedata['money'] !== (double)$origin['money'])) {
  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']) && (int)$changedata['score'] !== (int)$origin['score']) {
  47. ScoreLog::create(['user_id' => $row['id'], 'score' => $changedata['score'] - $origin['score'], 'before' => $origin['score'], 'after' => $changedata['score'], 'memo' => '管理员变更积分']);
  48. }
  49. });
  50. }
  51. public function getGenderList()
  52. {
  53. return ['1' => __('Male'), '0' => __('Female')];
  54. }
  55. public function getStatusList()
  56. {
  57. return ['normal' => __('Normal'), 'hidden' => __('Hidden')];
  58. }
  59. public function getPrevtimeTextAttr($value, $data)
  60. {
  61. $value = $value ? $value : ($data['prevtime'] ?? "");
  62. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  63. }
  64. public function getLogintimeTextAttr($value, $data)
  65. {
  66. $value = $value ? $value : ($data['logintime'] ?? "");
  67. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  68. }
  69. public function getJointimeTextAttr($value, $data)
  70. {
  71. $value = $value ? $value : ($data['jointime'] ?? "");
  72. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  73. }
  74. protected function setPrevtimeAttr($value)
  75. {
  76. return $value && !is_numeric($value) ? strtotime($value) : $value;
  77. }
  78. protected function setLogintimeAttr($value)
  79. {
  80. return $value && !is_numeric($value) ? strtotime($value) : $value;
  81. }
  82. protected function setJointimeAttr($value)
  83. {
  84. return $value && !is_numeric($value) ? strtotime($value) : $value;
  85. }
  86. protected function setBirthdayAttr($value)
  87. {
  88. return $value ? $value : null;
  89. }
  90. public function group()
  91. {
  92. return $this->belongsTo('UserGroup', 'group_id', 'id', [], 'LEFT')->setEagerlyType(0);
  93. }
  94. }