User.php 1.0 KB

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