User.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. */
  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. $salt = Random::build('alnum', 16);
  30. $passwd = encrypt_password($newPassword, $salt);
  31. return $this->where(['id' => $uid])->update(['password' => $passwd, 'salt' => $salt]);
  32. }
  33. public function getMoneyAttr($value): string
  34. {
  35. return bcdiv($value, 100, 2);
  36. }
  37. /**
  38. * 用户的余额是不可以直接进行修改的,请通过 UserMoneyLog 模型插入记录来实现自动修改余额
  39. * 此处定义上 money 的修改器仅为防止直接对余额的修改造成数据错乱
  40. */
  41. public function setMoneyAttr($value): string
  42. {
  43. return bcmul($value, 100, 2);
  44. }
  45. }