ConfigController.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. namespace App\Http\Controllers\API;
  3. use App\Http\Controllers\Controller;
  4. use App\Http\Requests\API\Config\AllowSettingConfig;
  5. use App\Models\Config;
  6. use Illuminate\Http\Request;
  7. class ConfigController extends Controller
  8. {
  9. public function index(Request $request)
  10. {
  11. $groups = $request->get("group", []);
  12. $data = [];
  13. if ($groups) {
  14. $data = Config::query()->whereIn("group", $groups)->get([
  15. 'group', 'key', 'value'
  16. ])->groupBy("group");
  17. }
  18. return $this->success([
  19. 'data' => $data
  20. ]);
  21. }
  22. public function setting(Request $request)
  23. {
  24. $allowSettingConfig = new AllowSettingConfig();
  25. foreach ($request->all() as $item) {
  26. try {
  27. $allowSettingConfig->check($item['group'], $item['key'], $item['value']);
  28. } catch (\Exception $exception) {
  29. return $this->forbidden($exception->getMessage());
  30. }
  31. }
  32. foreach ($request->all() as $item) {
  33. Config::query()->updateOrCreate([
  34. 'group' => $item['group'],
  35. 'key' => $item['key'],
  36. ], [
  37. 'value' => $item['value']
  38. ]);
  39. }
  40. return $this->noContent();
  41. }
  42. }