AuthGroupAccess.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php namespace App\Models\Manager;
  2. use Illuminate\Database\Eloquent\Factories\HasFactory;
  3. use Illuminate\Database\Eloquent\Model;
  4. /**
  5. * 用户权限组模型
  6. *
  7. */
  8. class AuthGroupAccess extends Model
  9. {
  10. use HasFactory;
  11. // 与模型关联的表名
  12. protected $table = 'auth_group_access';
  13. // 是否主动维护时间戳
  14. public $timestamps = false;
  15. protected $connection = 'mysql';
  16. // 定义时间戳字段名
  17. // const CREATED_AT = 'insert_time';
  18. // const UPDATED_AT = 'update_time';
  19. public static $allowShowPhone = null;
  20. /**
  21. * 添加数据
  22. *
  23. */
  24. public function add($data)
  25. {
  26. // 时间
  27. $data['insert_time'] = time();
  28. $data['update_time'] = time();
  29. // 写入数据表
  30. $id = $this->query()->insertGetId($data);
  31. // 如果操作失败
  32. if( !$id ) return 0;
  33. // 返回结果
  34. return $id;
  35. }
  36. /**
  37. * 添加数据
  38. *
  39. */
  40. public function edit($id,$data)
  41. {
  42. // 更新时间
  43. $data['update_time'] = time();
  44. // 写入数据表
  45. $result = $this->query()->where([['id','=',$id]])->update($data);
  46. // 如果操作失败
  47. if( !$result ) return 0;
  48. // 返回结果
  49. return $id;
  50. }
  51. /**
  52. * 获得权限列表
  53. * @param integer $uid 用户id
  54. *
  55. */
  56. public function getGroupByUid($uid)
  57. {
  58. // 获取分组名
  59. $group = $this->query()
  60. ->join('auth_group','auth_group_access.group_id','=','auth_group.id')
  61. ->where([['auth_group_access.admin_uid','=',$uid]])
  62. ->pluck('auth_group.name')->toArray();
  63. // 返回结果
  64. if ( is_super($uid,'manager') ) $group[] = '超管';
  65. // 返回结果
  66. return $group;
  67. }
  68. /**
  69. * 获得权限列表
  70. * @param integer $uid 用户id
  71. *
  72. */
  73. public function showPhoneByUid($uid)
  74. {
  75. // 如果有结果,返回结果过
  76. if( !is_null(self::$allowShowPhone) ) return self::$allowShowPhone;
  77. // 超管默认显示
  78. if ( is_super($uid,'manager') ) return self::$allowShowPhone = 1;
  79. // 非超管,查看用户是否有小组可以显示
  80. self::$allowShowPhone = (int) $this->query()->join('auth_group','auth_group_access.group_id','=','auth_group.id')->where([['auth_group_access.admin_uid','=',$uid],['auth_group.show_phone','=',1]])->value('auth_group.show_phone');
  81. // 返回结果
  82. return self::$allowShowPhone;
  83. }
  84. }