User.php 1.2 KB

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