User.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. namespace app\admin\model;
  3. use ba\Random;
  4. use think\Model;
  5. use think\model\relation\BelongsTo;
  6. /**
  7. * User 模型
  8. */
  9. class User extends Model
  10. {
  11. protected $autoWriteTimestamp = true;
  12. public function getAvatarAttr($value): string
  13. {
  14. return full_url($value, false, config('buildadmin.default_avatar'));
  15. }
  16. public function setAvatarAttr($value): string
  17. {
  18. return $value == full_url('', false, config('buildadmin.default_avatar')) ? '' : $value;
  19. }
  20. public function getMoneyAttr($value): string
  21. {
  22. return bcdiv($value, 100, 2);
  23. }
  24. public function setMoneyAttr($value): string
  25. {
  26. return bcmul($value, 100, 2);
  27. }
  28. /**
  29. * 重置用户密码
  30. * @param int|string $uid 用户ID
  31. * @param string $newPassword 新密码
  32. * @return int|User
  33. */
  34. public function resetPassword(int|string $uid, string $newPassword): int|User
  35. {
  36. $salt = Random::build('alnum', 16);
  37. $passwd = encrypt_password($newPassword, $salt);
  38. return $this->where(['id' => $uid])->update(['password' => $passwd, 'salt' => $salt]);
  39. }
  40. public function group(): BelongsTo
  41. {
  42. return $this->belongsTo(UserGroup::class, 'group_id');
  43. }
  44. }