12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- <?php
- namespace App\Modules\Admin\Services;
- use App\Base\BaseService;
- use App\Models\Setting;
- use Illuminate\Database\Eloquent\Builder;
- use Illuminate\Support\Arr;
- class SettingService extends BaseService
- {
- public function settingMap()
- {
- $r = Setting::all(['id', 'type', "key", "value"])->groupBy("type");
- return $r;
- }
- public function bannerPaginate($params)
- {
- $p = Setting::query()->where("type", Setting::TYPE_BANNER)->when($params['status'], function (Builder $query) use ($params) {
- return $query->where("status", $params['status']);
- })->when($params['name'], function (Builder $query) use ($params) {
- return $query->where("key", "like", "%{$params['name']}%");
- })->orderByDesc("id")->paginate($params['page_size']);
- return [
- "total" => $p->total(),
- "page_total" => $p->lastPage(),
- "list" => array_map(function (Setting $u) {
- return array_merge([
- "id" => $u->id,
- "name" => $u->key,
- "created_at" => $u->created_at->format("Y-m-d H:i:s"),
- ], $u->value ?? []);
- }, $p->items()),
- ];
- }
- public function store($params)
- {
- $id = $params['id'];
- if ($id) {
- $m = Setting::find($id);
- } else {
- $m = new Setting();
- }
- $m->type = Setting::TYPE_BANNER;
- $m->key = Arr::get($params, "name");
- $m->value = [
- "sort" => $params['sort'] ?? 0,
- "imageUrl" => $params['imageUrl'],
- "is_use" => $params['is_use'] ?? 1,
- ];
- $m->save();
- return true;
- }
- public function settingSave($data)
- {
- //{"hot_keywords":{"id":2,"values":["文本1","文本2","test","test2"]}}
- foreach ($data as $k => $v) {
- $id = Arr::get($v, "id");
- if ($id) {
- $setting = Setting::find($id);
- $setting->value = Arr::get($v, "values");
- $setting->save();
- }
- }
- return true;
- }
- }
|