SettingService.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. namespace App\Modules\Admin\Services;
  3. use App\Base\BaseService;
  4. use App\Models\Setting;
  5. use Illuminate\Database\Eloquent\Builder;
  6. use Illuminate\Support\Arr;
  7. class SettingService extends BaseService
  8. {
  9. public function settingMap()
  10. {
  11. $r = Setting::all(['id', 'type', "key", "value"])->groupBy("type");
  12. return $r;
  13. }
  14. public function bannerPaginate($params)
  15. {
  16. $p = Setting::query()->where("type", Setting::TYPE_BANNER)->when($params['status'], function (Builder $query) use ($params) {
  17. return $query->where("status", $params['status']);
  18. })->when($params['name'], function (Builder $query) use ($params) {
  19. return $query->where("key", "like", "%{$params['name']}%");
  20. })->orderByDesc("id")->paginate($params['page_size']);
  21. return [
  22. "total" => $p->total(),
  23. "page_total" => $p->lastPage(),
  24. "list" => array_map(function (Setting $u) {
  25. return array_merge([
  26. "id" => $u->id,
  27. "name" => $u->key,
  28. "created_at" => $u->created_at->format("Y-m-d H:i:s"),
  29. ], $u->value ?? []);
  30. }, $p->items()),
  31. ];
  32. }
  33. public function store($params)
  34. {
  35. $id = $params['id'];
  36. if ($id) {
  37. $m = Setting::find($id);
  38. } else {
  39. $m = new Setting();
  40. }
  41. $m->type = Setting::TYPE_BANNER;
  42. $m->key = Arr::get($params, "name");
  43. $m->value = [
  44. "sort" => $params['sort'] ?? 0,
  45. "imageUrl" => $params['imageUrl'],
  46. "is_use" => $params['is_use'] ?? 1,
  47. ];
  48. $m->save();
  49. return true;
  50. }
  51. public function settingSave($data)
  52. {
  53. //{"hot_keywords":{"id":2,"values":["文本1","文本2","test","test2"]}}
  54. foreach ($data as $k => $v) {
  55. $id = Arr::get($v, "id");
  56. if ($id) {
  57. $setting = Setting::find($id);
  58. $setting->value = Arr::get($v, "values");
  59. $setting->save();
  60. }
  61. }
  62. return true;
  63. }
  64. }