AdminRule.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php namespace App\Models;
  2. use Illuminate\Database\Eloquent\Factories\HasFactory;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Support\Facades\DB;
  5. /**
  6. *用户模型
  7. *
  8. */
  9. class AdminRule extends Model
  10. {
  11. use HasFactory;
  12. // 与模型关联的表名
  13. protected $table = 'admin_rule';
  14. // 是否主动维护时间戳
  15. public $timestamps = false;
  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 $id;
  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 $result;
  49. // 更新缓存
  50. //$this->getList(true);
  51. // 返回结果
  52. return $result;
  53. }
  54. /**
  55. * 获取列表
  56. * @param Bool $force 是否强制更新
  57. *
  58. */
  59. public function getList($force = false)
  60. {
  61. // 结果数据
  62. $list = $force ? [] : cache('admin:rule:list');
  63. // 不存在数据
  64. if ( !$list ) {
  65. // 从数据库获取数据
  66. $data = $this->query()->where(['status'=>0])->get();
  67. // 是否有数据
  68. $data = $data ? $data->toArray() : [];
  69. // 循环处理数据
  70. $list = [];
  71. // 进行更新
  72. foreach ($data as $value) {
  73. // 重组数据
  74. $list[$value['uid']] = $value;
  75. }
  76. // 存起来
  77. cache(['admin:rule:list'=>$list]);
  78. }
  79. // 返回结果
  80. return $list;
  81. }
  82. /**
  83. * 获取配置平台对应的应用数据
  84. *
  85. * @param int 用户ID
  86. * @param string 指定字段
  87. *
  88. */
  89. public function getOne($id,$field='')
  90. {
  91. // 获取列表数据
  92. $list = $this->getList();
  93. // 获取数据
  94. $one = isset($list[$id]) ? $list[$id] : [];
  95. // 返回值
  96. return empty($field) ? $one : ( isset($one[$field]) ? $one[$field] : null);
  97. }
  98. }