Admin.php 2.1 KB

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