User.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. 'birthday_text',
  21. 'experiencetime_text'
  22. ];
  23. public function getOriginData()
  24. {
  25. return $this->origin;
  26. }
  27. protected static function init()
  28. {
  29. self::beforeUpdate(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(['user_id' => $row['id'], 'type' => 3, 'money' => $changedata['money'] - $origin['money'], 'before' => $origin['money'], 'after' => $changedata['money'], 'memo' => '管理员变更金额']);
  47. }
  48. // if (isset($changedata['score']) && (int)$changedata['score'] !== (int)$origin['score']) {
  49. // ScoreLog::create(['user_id' => $row['id'], 'score' => $changedata['score'] - $origin['score'], 'before' => $origin['score'], 'after' => $changedata['score'], 'memo' => '管理员变更积分']);
  50. // }
  51. });
  52. }
  53. public function getGenderList()
  54. {
  55. return ['1' => __('Male'), '2' => __('Female')];
  56. }
  57. public function getStatusList()
  58. {
  59. return ['normal' => __('Normal'), 'hidden' => __('Hidden')];
  60. }
  61. public function getPrevtimeTextAttr($value, $data)
  62. {
  63. $value = $value ? $value : ($data['prevtime'] ?? "");
  64. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  65. }
  66. public function getLogintimeTextAttr($value, $data)
  67. {
  68. $value = $value ? $value : ($data['logintime'] ?? "");
  69. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  70. }
  71. public function getJointimeTextAttr($value, $data)
  72. {
  73. $value = $value ? $value : ($data['jointime'] ?? "");
  74. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  75. }
  76. public function getBirthdayTextAttr($value, $data)
  77. {
  78. $value = $value ? $value : ($data['birthday'] ?? "");
  79. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  80. }
  81. public function getExperiencetimeTextAttr($value, $data)
  82. {
  83. $value = $value ? $value : ($data['experiencetime'] ?? "");
  84. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  85. }
  86. protected function setPrevtimeAttr($value)
  87. {
  88. return $value && !is_numeric($value) ? strtotime($value) : $value;
  89. }
  90. protected function setLogintimeAttr($value)
  91. {
  92. return $value && !is_numeric($value) ? strtotime($value) : $value;
  93. }
  94. protected function setJointimeAttr($value)
  95. {
  96. return $value && !is_numeric($value) ? strtotime($value) : $value;
  97. }
  98. protected function setBirthdayAttr($value)
  99. {
  100. return $value ? $value : null;
  101. }
  102. public function group()
  103. {
  104. return $this->belongsTo('UserGroup', 'group_id', 'id', [], 'LEFT')->setEagerlyType(0);
  105. }
  106. public function provincearea()
  107. {
  108. return $this->belongsTo('Area', 'province_id', 'id', [], 'LEFT')->setEagerlyType(0);
  109. }
  110. public function cityarea()
  111. {
  112. return $this->belongsTo('Area', 'city_id', 'id', [], 'LEFT')->setEagerlyType(0);
  113. }
  114. public function area()
  115. {
  116. return $this->belongsTo('Area', 'area_id', 'id', [], 'LEFT')->setEagerlyType(0);
  117. }
  118. public function rank()
  119. {
  120. return $this->belongsTo('Rank', 'rank_id', 'id', [], 'LEFT')->setEagerlyType(0);
  121. }
  122. }