User.php 3.9 KB

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