Admin.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace app\admin\model;
  3. use ba\Random;
  4. use think\Model;
  5. use think\facade\Db;
  6. /**
  7. * Admin模型
  8. * @property int $id 管理员ID
  9. * @property string $username 管理员用户名
  10. * @property string $nickname 管理员昵称
  11. * @property string $email 管理员邮箱
  12. * @property string $mobile 管理员手机号
  13. * @property string $last_login_ip 上次登录IP
  14. * @property string $last_login_time 上次登录时间
  15. * @property int $login_failure 登录失败次数
  16. * @property string $update_pass_time 登录失败次数
  17. */
  18. class Admin extends Model
  19. {
  20. /**
  21. * @var string 自动写入时间戳
  22. */
  23. protected $autoWriteTimestamp = true;
  24. /**
  25. * 追加属性
  26. */
  27. protected $append = [
  28. 'group_arr',
  29. 'group_name_arr',
  30. ];
  31. public function getGroupArrAttr($value, $row): array
  32. {
  33. return Db::name('admin_group_access')
  34. ->where('uid', $row['id'])
  35. ->column('group_id');
  36. }
  37. public function getGroupNameArrAttr($value, $row): array
  38. {
  39. $groupAccess = Db::name('admin_group_access')
  40. ->where('uid', $row['id'])
  41. ->column('group_id');
  42. return AdminGroup::whereIn('id', $groupAccess)->column('name');
  43. }
  44. public function getAvatarAttr($value): string
  45. {
  46. return full_url($value, false, config('buildadmin.default_avatar'));
  47. }
  48. public function setAvatarAttr($value): string
  49. {
  50. return $value == full_url('', false, config('buildadmin.default_avatar')) ? '' : $value;
  51. }
  52. public function getLastLoginTimeAttr($value): string
  53. {
  54. return $value ? date('Y-m-d H:i:s', $value) : '';
  55. }
  56. /**
  57. * 重置用户密码
  58. * @param int|string $uid 管理员ID
  59. * @param string $newPassword 新密码
  60. * @return int|Admin
  61. */
  62. public function resetPassword(int|string $uid, string $newPassword): int|Admin
  63. {
  64. $salt = Random::build('alnum', 16);
  65. $passwd = encrypt_password($newPassword, $salt);
  66. return $this->where(['id' => $uid])->update(['password' => $passwd, 'salt' => $salt]);
  67. }
  68. }