api
parent
7baf63523d
commit
f8389d8d99
|
|
@ -25,13 +25,9 @@ class AdminNoticeController extends AdminController
|
|||
->columns([
|
||||
TableColumn::make()->name('id')->label(__('admin-notice.id'))->sortable(true),
|
||||
TableColumn::make()->name('title')->label(__('admin-notice.title')),
|
||||
TableColumn::make()
|
||||
->name('article.title')
|
||||
->label(__('admin-notice.article_id')),
|
||||
// ->type('button')
|
||||
// ->actionType('link')
|
||||
// ->link('articles/${article_id}'),
|
||||
TableColumn::make()->name('article.title')->label(__('admin-notice.article_id')),
|
||||
TableColumn::make()->name('is_enable')->type('switch')->label(__('admin-notice.is_enable'))->quickEdit(SwitchControl::make()->saveImmediately(true)->mode('inline')),
|
||||
TableColumn::make()->name('sort')->label(__('admin-notice.sort'))->align('center')->quickEdit(Components::make()->sortControl('sort', __('admin-notice.sort'))->saveImmediately(true)),
|
||||
TableColumn::make()->name('published_at')->label(__('admin-notice.published_at')),
|
||||
$this->rowActions(),
|
||||
]);
|
||||
|
|
|
|||
|
|
@ -79,7 +79,7 @@ class ArticleController extends AdminController
|
|||
|
||||
public function options()
|
||||
{
|
||||
$list = $this->service->listQuery()->select(['id as value', 'title as label'])->without('category')->show()->get();
|
||||
$list = $this->service->listQuery()->select(['id as value', 'title as label'])->without('category')->get();
|
||||
|
||||
return $this->response()->success($list);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@
|
|||
namespace App\Admin\Controllers;
|
||||
|
||||
use Slowlyo\OwlAdmin\Renderers\{Page, Form};
|
||||
use Slowlyo\OwlAdmin\Renderers\{TableColumn, TextControl, Image, ImageControl, SelectControl, SwitchControl, DateTimeControl, InputKV, Status, Json};
|
||||
use Slowlyo\OwlAdmin\Renderers\{Component, TableColumn, TextControl, Image, Button, ImageControl, SelectControl, SwitchControl, DateTimeControl, InputKV, Status, Json};
|
||||
use Slowlyo\OwlAdmin\Controllers\AdminController;
|
||||
use App\Services\Admin\BannerService;
|
||||
use App\Admin\Components;
|
||||
|
|
@ -16,11 +16,13 @@ class BannerController extends AdminController
|
|||
{
|
||||
$crud = $this->baseCRUD()
|
||||
->filterTogglable(false)
|
||||
// ->headerToolbar([
|
||||
// $this->createButton(true),
|
||||
// ...$this->baseHeaderToolBar(),
|
||||
// ])
|
||||
->quickSaveItemApi(admin_url('quick-edit/banners/$id'))
|
||||
->filter($this->baseFilter()->actions([])->body([
|
||||
SelectControl::make()->name('place_id')->label(__('banner.place_id'))->source(admin_url('api/banner-places/options'))->size('md'),
|
||||
TextControl::make()->name('name')->label(__('banner.name'))->size('md'),
|
||||
Button::make()->label(__('admin.reset'))->actionType('clear-and-submit'),
|
||||
Component::make()->setType('submit')->label(__('admin.search'))->level('primary'),
|
||||
]))
|
||||
->columns([
|
||||
TableColumn::make()->name('place.name')->label(__('banner.place_id')),
|
||||
TableColumn::make()->name('name')->label(__('banner.name')),
|
||||
|
|
|
|||
|
|
@ -6,6 +6,8 @@ use EloquentFilter\ModelFilter;
|
|||
|
||||
class ArticleCategoryFilter extends ModelFilter
|
||||
{
|
||||
protected $drop_id = false;
|
||||
|
||||
/**
|
||||
* Related Models that have ModelFilters as well as the method on the ModelFilter
|
||||
* As [relationMethod => [input_key1, input_key2]].
|
||||
|
|
@ -28,4 +30,9 @@ class ArticleCategoryFilter extends ModelFilter
|
|||
{
|
||||
$this->where('path', 'like', '%-'.$v.'-%');
|
||||
}
|
||||
|
||||
public function level($v)
|
||||
{
|
||||
$this->where('level', $v);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,6 +6,8 @@ use EloquentFilter\ModelFilter;
|
|||
|
||||
class ArticleFilter extends ModelFilter
|
||||
{
|
||||
protected $drop_id = false;
|
||||
|
||||
/**
|
||||
* Related Models that have ModelFilters as well as the method on the ModelFilter
|
||||
* As [relationMethod => [input_key1, input_key2]].
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@ class BannerFilter extends ModelFilter
|
|||
|
||||
public function key($v)
|
||||
{
|
||||
$this->whereHas('place', fn ($q) => $q->whereIn('key', explode(',', $v)));
|
||||
$this->whereHas('place', fn ($q) => $q->enable()->whereIn('key', explode(',', $v)));
|
||||
}
|
||||
|
||||
public function name($v)
|
||||
|
|
|
|||
|
|
@ -0,0 +1,31 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Models\AdminNotice;
|
||||
use App\Http\Resources\AdminNoticeResource;
|
||||
|
||||
class AdminNoticeController extends Controller
|
||||
{
|
||||
public function index(Request $request)
|
||||
{
|
||||
$query = AdminNotice::filter($request->all())->sort()->enable();
|
||||
|
||||
$list = $query->get();
|
||||
|
||||
return $this->json(AdminNoticeResource::collection($list));
|
||||
}
|
||||
|
||||
public function show($id)
|
||||
{
|
||||
$info = AdminNotice::enable()->find($id);
|
||||
|
||||
if (!$info) {
|
||||
return $this->error('记录不存在');
|
||||
}
|
||||
|
||||
return $this->json(AdminNoticeResource::make($info));
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,48 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Models\{Article, ArticleCategory};
|
||||
use App\Http\Resources\{ArticleResource, ArticleCategoryResource};
|
||||
|
||||
class ArticleController extends Controller
|
||||
{
|
||||
public function category(Request $request)
|
||||
{
|
||||
$query = ArticleCategory::filter($request->all())->enable()->sort();
|
||||
|
||||
$list = $query->get();
|
||||
|
||||
return $this->json(ArticleCategoryResource::collection($list));
|
||||
}
|
||||
|
||||
public function tree(Request $request)
|
||||
{
|
||||
$pid = $request->input('parent_id');
|
||||
$list = ArticleCategory::filter(['parent_path' => $pid])->enable()->sort()->select(['id', 'name', 'icon', 'parent_id'])->get()->toArray();
|
||||
|
||||
return $this->json(array2tree($list, $pid ?? 0));
|
||||
}
|
||||
|
||||
public function index(Request $request)
|
||||
{
|
||||
$query = Article::filter($request->all())->enable()->sort();
|
||||
|
||||
$list = $query->paginate($request->input('per_page'));
|
||||
|
||||
return $this->json(ArticleResource::collection($list));
|
||||
}
|
||||
|
||||
public function show($id)
|
||||
{
|
||||
$info = Article::enable()->find($id);
|
||||
|
||||
if (!$info) {
|
||||
return $this->error('记录不存在');
|
||||
}
|
||||
|
||||
return $this->json(ArticleResource::make($info));
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Models\Banner;
|
||||
use App\Http\Resources\BannerResource;
|
||||
|
||||
class BannerController extends Controller
|
||||
{
|
||||
public function index(Request $request)
|
||||
{
|
||||
$query = Banner::with(['place'])->filter($request->all())->sort()->enable();
|
||||
|
||||
$list = $query->get();
|
||||
|
||||
return $this->json(BannerResource::collection($list));
|
||||
}
|
||||
}
|
||||
|
|
@ -6,8 +6,30 @@ use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
|
|||
use Illuminate\Foundation\Bus\DispatchesJobs;
|
||||
use Illuminate\Foundation\Validation\ValidatesRequests;
|
||||
use Illuminate\Routing\Controller as BaseController;
|
||||
use Illuminate\Pagination\LengthAwarePaginator;
|
||||
use Illuminate\Http\Resources\Json\ResourceCollection ;
|
||||
use Illuminate\Support\Arr;
|
||||
|
||||
class Controller extends BaseController
|
||||
{
|
||||
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
|
||||
|
||||
public function json($data, $code = 0, $message = '')
|
||||
{
|
||||
if ($data instanceof ResourceCollection ) {
|
||||
$data = $data->resource;
|
||||
}
|
||||
$result = ['data' => $data ?: '', 'status' => $code, 'msg' => $message];
|
||||
return response()->json($result);
|
||||
}
|
||||
|
||||
public function success($message = '', $data = null)
|
||||
{
|
||||
return $this->json($data, 0, $message);
|
||||
}
|
||||
|
||||
public function error($message = '', $data = null)
|
||||
{
|
||||
return $this->json($data, 1, $message);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,27 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class AdminNoticeResource extends JsonResource
|
||||
{
|
||||
/**
|
||||
* Transform the resource into an array.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
|
||||
*/
|
||||
public function toArray($request)
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'title' => $this->title,
|
||||
'content' => $this->content,
|
||||
'article_id' => $this->article_id,
|
||||
'sort' => $this->sort,
|
||||
'published_at' => $this->published_at ? $this->published_at->timestamp : '',
|
||||
'created_at' => $this->created_at->timestamp
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class ArticleCategoryResource extends JsonResource
|
||||
{
|
||||
/**
|
||||
* Transform the resource into an array.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
|
||||
*/
|
||||
public function toArray($request)
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'icon' => $this->icon,
|
||||
'name' => $this->name,
|
||||
'parent_id' => $this->parent_id,
|
||||
'level' => $this->level,
|
||||
'path' => $this->path,
|
||||
'sort' => $this->sort,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class ArticleResource extends JsonResource
|
||||
{
|
||||
/**
|
||||
* Transform the resource into an array.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
|
||||
*/
|
||||
public function toArray($request)
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'title' => $this->title,
|
||||
'sub_title' => $this->sub_title,
|
||||
'category_id' => $this->category_id,
|
||||
'category' => ArticleCategoryResource::make($this->whenLoaded('category')),
|
||||
'content' => $this->content,
|
||||
'cover' => $this->cover,
|
||||
'published_at' => $this->published_at ? $this->published_at->timestamp : '',
|
||||
'created_at' => $this->created_at->timestamp
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class BannerResource extends JsonResource
|
||||
{
|
||||
/**
|
||||
* Transform the resource into an array.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
|
||||
*/
|
||||
public function toArray($request)
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'place_id' => $this->place_id,
|
||||
'key' => data_get($this->whenLoaded('place'), 'key'),
|
||||
'name' => $this->name,
|
||||
'picture' => $this->picture,
|
||||
'link_config' => $this->link_config,
|
||||
'sort' => $this->sort,
|
||||
'published_at' => $this->published_at ? $this->published_at->timestamp : '',
|
||||
'created_at' => $this->created_at->timestamp,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
@ -5,6 +5,9 @@ namespace App\Models;
|
|||
use Illuminate\Database\Eloquent\Model;
|
||||
use EloquentFilter\Filterable;
|
||||
|
||||
/**
|
||||
* 公告
|
||||
*/
|
||||
class AdminNotice extends Model
|
||||
{
|
||||
use Filterable;
|
||||
|
|
@ -27,8 +30,13 @@ class AdminNotice extends Model
|
|||
return $q->orderBy('sort', 'desc')->orderBy('published_at', 'desc');
|
||||
}
|
||||
|
||||
public function scopeShow($q)
|
||||
public function scopeEnable($q)
|
||||
{
|
||||
return $q->where('is_enable', 1)->where('published_at', '<=', now());
|
||||
}
|
||||
|
||||
public function modelFilter()
|
||||
{
|
||||
return \App\Filters\AdminNoticeFilter::class;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -42,8 +42,13 @@ class Article extends Model
|
|||
return $q->orderBy('sort', 'desc')->orderBy('published_at', 'desc');
|
||||
}
|
||||
|
||||
public function scopeShow($q)
|
||||
public function scopeEnable($q)
|
||||
{
|
||||
return $q->where('is_enable', 1)->where('published_at', '<=', now());
|
||||
}
|
||||
|
||||
public function modelFilter()
|
||||
{
|
||||
return \App\Filters\ArticleFilter::class;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -50,8 +50,13 @@ class ArticleCategory extends Model
|
|||
return $q->orderBy('sort', 'desc');
|
||||
}
|
||||
|
||||
public function scopeShow($q)
|
||||
public function scopeEnable($q)
|
||||
{
|
||||
return $q->where('is_enable', 1);
|
||||
}
|
||||
|
||||
public function modelFilter()
|
||||
{
|
||||
return \App\Filters\ArticleCategoryFilter::class;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@
|
|||
namespace App\Models;
|
||||
|
||||
use Illuminate\Support\Str;
|
||||
use App\Filters\BannerFilter;
|
||||
use EloquentFilter\Filterable;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
|
@ -44,4 +45,9 @@ class Banner extends Model
|
|||
{
|
||||
return $q->where('is_enable', 1)->where('published_at', '<=', now());
|
||||
}
|
||||
|
||||
public function modelFilter()
|
||||
{
|
||||
return BannerFilter::class;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -26,4 +26,9 @@ class BannerPlace extends Model
|
|||
{
|
||||
return $q->where('is_enable', 1);
|
||||
}
|
||||
|
||||
public function scopeSort($q)
|
||||
{
|
||||
return $q->orderBy('id', 'desc');
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,6 +15,23 @@ class BannerPlaceService extends BaseService
|
|||
|
||||
protected string $modelFilterName = BannerPlaceFilter::class;
|
||||
|
||||
public function listQuery()
|
||||
{
|
||||
$model = $this->getModel();
|
||||
$filter = $this->getModelFilter();
|
||||
|
||||
$query = $this->query();
|
||||
if($this->withRelationships){
|
||||
$query->with($this->withRelationships);
|
||||
}
|
||||
|
||||
if ($filter) {
|
||||
$query->filter(request()->input(), $filter);
|
||||
}
|
||||
|
||||
return $query->sort();
|
||||
}
|
||||
|
||||
public function delete(string $ids): mixed
|
||||
{
|
||||
$id = explode(',', $ids);
|
||||
|
|
|
|||
|
|
@ -14,6 +14,12 @@ use Illuminate\Support\Facades\Route;
|
|||
|
|
||||
*/
|
||||
|
||||
Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
|
||||
return $request->user();
|
||||
});
|
||||
Route::get('banner', [\App\Http\Controllers\Api\BannerController::class, 'index']);
|
||||
|
||||
Route::get('notice', [\App\Http\Controllers\Api\AdminNoticeController::class, 'index']);
|
||||
Route::get('notice/{id}', [\App\Http\Controllers\Api\AdminNoticeController::class, 'show']);
|
||||
|
||||
Route::get('article/category', [\App\Http\Controllers\Api\ArticleController::class, 'category']);
|
||||
Route::get('article/tree', [\App\Http\Controllers\Api\ArticleController::class, 'tree']);
|
||||
Route::get('article', [\App\Http\Controllers\Api\ArticleController::class, 'index']);
|
||||
Route::get('article/{id}', [\App\Http\Controllers\Api\ArticleController::class, 'show']);
|
||||
|
|
|
|||
Loading…
Reference in New Issue