User.php 3.8 KB

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