SystemRepositoryEloquent.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. namespace App\Repositories;
  3. use Prettus\Repository\Eloquent\BaseRepository;
  4. use Prettus\Repository\Criteria\RequestCriteria;
  5. use App\Repositories\SystemRepository;
  6. use App\Models\System;
  7. use Illuminate\Container\Container as Application;
  8. /**
  9. * Class SystemRepositoryEloquent
  10. * @package namespace App\Repositories;
  11. */
  12. class SystemRepositoryEloquent extends BaseRepository implements SystemRepository
  13. {
  14. private $config;
  15. public function __construct(Application $app)
  16. {
  17. parent::__construct($app);
  18. $this->config = config('blog.system_key');
  19. }
  20. /**
  21. * Specify Model class name
  22. *
  23. * @return string
  24. */
  25. public function model()
  26. {
  27. return System::class;
  28. }
  29. /**
  30. * Boot up the repository, pushing criteria
  31. */
  32. public function boot()
  33. {
  34. $this->pushCriteria(app(RequestCriteria::class));
  35. }
  36. /**
  37. * 保存博客设置
  38. *
  39. * @param array $data
  40. * @return bool
  41. */
  42. public function store(array $data)
  43. {
  44. if (!$data) {
  45. return false;
  46. }
  47. unset($data['_token']);
  48. foreach ($data as $key => $value) {
  49. if (in_array($key, $this->config)) {
  50. $this->updateOrCreate(['key' => $key], ['value' => $value]);
  51. }
  52. }
  53. return true;
  54. }
  55. /**
  56. * 获取选项列表
  57. *
  58. * @return array
  59. */
  60. public function optionList()
  61. {
  62. $all = $this->all(['key', 'value']);
  63. $system = $this->initSystemKey();
  64. foreach ($all as $a) {
  65. $system[$a['key']] = $a['value'];
  66. }
  67. return $system;
  68. }
  69. /**
  70. * @return array
  71. */
  72. private function initSystemKey()
  73. {
  74. $init = [];
  75. $config = array_flip($this->config);
  76. foreach ($config as $key => $value) {
  77. $init[$key] = '';
  78. }
  79. return $init;
  80. }
  81. /**
  82. * @param $key
  83. * @return string
  84. */
  85. public function getKeyValue($key)
  86. {
  87. $data = $this->findByField('key', $key);
  88. if (isset($data[0])){
  89. return $data[0]->value;
  90. }
  91. return '';
  92. }
  93. }