Admin.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. namespace app\admin\model;
  3. use think\facade\Config;
  4. use think\facade\Db;
  5. use think\Model;
  6. use ba\Random;
  7. use app\admin\model\department\Department;
  8. use app\admin\model\department\Jobs;
  9. /**
  10. * Admin模型
  11. * @controllerUrl 'authAdmin'
  12. */
  13. class Admin extends Model
  14. {
  15. /**
  16. * @var string 自动写入时间戳
  17. */
  18. protected $autoWriteTimestamp = 'int';
  19. /**
  20. * @var string 自动写入创建时间
  21. */
  22. protected $createTime = 'createtime';
  23. /**
  24. * @var string 自动写入更新时间
  25. */
  26. protected $updateTime = 'updatetime';
  27. /**
  28. * 追加属性
  29. */
  30. protected $append = [
  31. 'group_arr',
  32. 'group_name_arr',
  33. ];
  34. public function getGroupArrAttr($value, $row)
  35. {
  36. return Db::name('admin_group_access')
  37. ->where('uid', $row['id'])
  38. ->column('group_id');
  39. }
  40. public function getGroupNameArrAttr($value, $row)
  41. {
  42. $groupAccess = Db::name('admin_group_access')
  43. ->where('uid', $row['id'])
  44. ->column('group_id');
  45. return AdminGroup::whereIn('id', $groupAccess)->column('name');
  46. }
  47. public function getAvatarAttr($value)
  48. {
  49. return full_url($value, true, Config::get('buildadmin.default_avatar'));
  50. }
  51. public function getLastlogintimeAttr($value)
  52. {
  53. return $value ? date('Y-m-d H:i:s', $value) : 'none';
  54. }
  55. /**
  56. * 重置用户密码
  57. * @param int $uid 管理员ID
  58. * @param string $newPassword 新密码
  59. */
  60. public function resetPassword($uid, $newPassword)
  61. {
  62. $salt = Random::build('alnum', 16);
  63. $passwd = encrypt_password($newPassword, $salt);
  64. return $this->where(['id' => $uid])->update(['password' => $passwd, 'salt' => $salt]);
  65. }
  66. public function department()
  67. {
  68. return $this->belongsTo(Department::class);
  69. }
  70. public function jobs()
  71. {
  72. return $this->belongsTo(Jobs::class);
  73. }
  74. }