AuthGroup.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 AuthGroup extends Model
  9. {
  10. use HasFactory;
  11. // 与模型关联的表名
  12. protected $table = 'auth_group';
  13. // 是否主动维护时间戳
  14. public $timestamps = false;
  15. protected $connection = 'mysql';
  16. // 定义时间戳字段名
  17. // const CREATED_AT = 'insert_time';
  18. // const UPDATED_AT = 'update_time';
  19. /**
  20. * 添加数据
  21. *
  22. */
  23. public function add($data)
  24. {
  25. // 时间
  26. $data['insert_time'] = time();
  27. $data['update_time'] = time();
  28. // 写入数据表
  29. $id = $this->query()->insertGetId($data);
  30. // 如果操作失败
  31. if( !$id ) return 0;
  32. // 更新缓存
  33. $this->getList(true);
  34. // 返回结果
  35. return $id;
  36. }
  37. /**
  38. * 添加数据
  39. *
  40. */
  41. public function edit($id,$data)
  42. {
  43. // 更新时间
  44. $data['update_time'] = time();
  45. // 写入数据表
  46. $result = $this->query()->where([['id','=',$id]])->update($data);
  47. // 如果操作失败
  48. if( !$result ) return 0;
  49. // 更新缓存
  50. $this->getList(true);
  51. // 返回结果
  52. return $id;
  53. }
  54. /**
  55. * 获取列表
  56. * @param Bool $force 是否强制更新
  57. *
  58. */
  59. public function getList($force = false)
  60. {
  61. // 结果数据
  62. $list = $force ? [] : cache('manager:auth:group:list');
  63. // 不存在数据
  64. if ( !$list ) {
  65. // 从数据库获取数据
  66. $data = $this->query()->get(['id','status','name','description','insert_time','update_time'])->toArray();
  67. // 循环处理数据
  68. $list = [];
  69. // 进行更新
  70. foreach ($data as $value) {
  71. // 重组数据
  72. $list[$value['id']] = $value;
  73. }
  74. // 存起来
  75. cache(['manager:auth:group:list'=>$list]);
  76. }
  77. // 返回结果
  78. return $list;
  79. }
  80. /**
  81. * 获取配置平台对应的应用数据
  82. *
  83. * @param int 用户ID
  84. * @param string 指定字段
  85. *
  86. */
  87. public function getOne($id,$field='')
  88. {
  89. // 获取列表数据
  90. $list = $this->getList();
  91. // 获取数据
  92. $one = isset($list[$id]) ? $list[$id] : [];
  93. // 返回值
  94. return empty($field) ? $one : ( isset($one[$field]) ? $one[$field] : null);
  95. }
  96. }