User.php 1.6 KB

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