SystemUser.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | saiadmin [ saiadmin快速开发框架 ]
  4. // +----------------------------------------------------------------------
  5. // | Author: sai <1430792918@qq.com>
  6. // +----------------------------------------------------------------------
  7. namespace plugin\saiadmin\app\model\system;
  8. use plugin\saiadmin\basic\BaseModel;
  9. /**
  10. * 用户信息模型
  11. */
  12. class SystemUser extends BaseModel
  13. {
  14. /**
  15. * 数据表主键
  16. * @var string
  17. */
  18. protected $pk = 'id';
  19. /**
  20. * 数据表完整名称
  21. * @var string
  22. */
  23. protected $table = 'sa_system_user';
  24. /**
  25. * 获取器
  26. */
  27. public function getBackendSettingAttr($value)
  28. {
  29. return json_decode($value ?? '', true);
  30. }
  31. /**
  32. * 修改器
  33. */
  34. public function setBackendSettingAttr($value)
  35. {
  36. return json_encode($value, JSON_UNESCAPED_UNICODE);
  37. }
  38. /**
  39. * 权限范围
  40. */
  41. public function scopeAuth($query, $value)
  42. {
  43. $id = $value['id'];
  44. $dept = $value['dept'];
  45. if ($id > 1) {
  46. $ids = SystemDept::whereRaw('FIND_IN_SET("'.$dept['id'].'", level) > 0')->column('id');
  47. $query->whereIn('dept_id', $ids);
  48. }
  49. }
  50. /**
  51. * 根据岗位id进行搜索
  52. */
  53. public function searchPostIdAttr($query, $value)
  54. {
  55. $query->join('sa_system_user_post post', 'sa_system_user.id = post.user_id')
  56. ->where('post.post_id', $value);
  57. }
  58. /**
  59. * 根据角色id进行搜索
  60. */
  61. public function searchRoleIdAttr($query, $value)
  62. {
  63. $query->whereRaw('id in (SELECT user_id FROM sa_system_user_role WHERE role_id =?)', [$value]);
  64. }
  65. /**
  66. * 通过中间表关联角色
  67. */
  68. public function roles()
  69. {
  70. return $this->belongsToMany(SystemRole::class, SystemUserRole::class, 'role_id', 'user_id');
  71. }
  72. /**
  73. * 通过中间表关联岗位
  74. */
  75. public function posts()
  76. {
  77. return $this->belongsToMany(SystemPost::class, SystemUserPost::class, 'post_id', 'user_id');
  78. }
  79. /**
  80. * 通过中间表关联部门
  81. */
  82. public function depts()
  83. {
  84. return $this->belongsTo(SystemDept::class, 'dept_id', 'id');
  85. }
  86. }