User.php 4.5 KB

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