User.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. namespace app\common\model;
  3. use ba\Random;
  4. use think\Model;
  5. /**
  6. * 会员公共模型
  7. * @property int $id 会员ID
  8. * @property string $password 密码密文
  9. * @property string $salt 密码盐
  10. * @property int $login_failure 登录失败次数
  11. * @property string $last_login_time 上次登录时间
  12. * @property string $last_login_ip 上次登录IP
  13. * @property string $email 会员邮箱
  14. * @property string $mobile 会员手机号
  15. * @property string $update_pass_time 上次修改密码时间
  16. */
  17. class User extends Model
  18. {
  19. protected $autoWriteTimestamp = true;
  20. public function getAvatarAttr($value): string
  21. {
  22. return full_url($value, false, config('buildadmin.default_avatar'));
  23. }
  24. public function setAvatarAttr($value): string
  25. {
  26. return $value == full_url('', false, config('buildadmin.default_avatar')) ? '' : $value;
  27. }
  28. public function resetPassword($uid, $newPassword): int|User
  29. {
  30. $salt = Random::build('alnum', 16);
  31. $passwd = encrypt_password($newPassword, $salt);
  32. return $this->where(['id' => $uid])->update(['password' => $passwd, 'salt' => $salt]);
  33. }
  34. public function getMoneyAttr($value): string
  35. {
  36. return bcdiv($value, 100, 2);
  37. }
  38. /**
  39. * 用户的余额是不可以直接进行修改的,请通过 UserMoneyLog 模型插入记录来实现自动修改余额
  40. * 此处定义上 money 的修改器仅为防止直接对余额的修改造成数据错乱
  41. */
  42. public function setMoneyAttr($value): string
  43. {
  44. return bcmul($value, 100, 2);
  45. }
  46. }