Admin.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. protected $name = 'admins';
  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. public function getLastLoginTimeAttr($value): string
  54. {
  55. return $value ? date('Y-m-d H:i:s', $value) : '';
  56. }
  57. /**
  58. * 重置用户密码
  59. * @param int|string $uid 管理员ID
  60. * @param string $newPassword 新密码
  61. * @return int|Admin
  62. */
  63. public function resetPassword(int|string $uid, string $newPassword): int|Admin
  64. {
  65. $salt = Random::build('alnum', 16);
  66. $passwd = encrypt_password($newPassword, $salt);
  67. return $this->where(['id' => $uid])->update(['password' => $passwd, 'salt' => $salt]);
  68. }
  69. }