generated from panliang/owl-admin-starter
109 lines
3.8 KiB
PHP
109 lines
3.8 KiB
PHP
<?php
|
|
|
|
namespace App\Admin\Controllers;
|
|
|
|
use App\Admin\Services\CateRankService;
|
|
use App\Models\CateRank;
|
|
use Slowlyo\OwlAdmin\Controllers\AdminController;
|
|
use Slowlyo\OwlAdmin\Renderers\Page;
|
|
|
|
class CateRankController extends AdminController
|
|
{
|
|
protected string $serviceName = CateRankService::class;
|
|
|
|
protected $snOptions;
|
|
|
|
public function index()
|
|
{
|
|
if ($this->actionOfGetData()) {
|
|
$options = $this->getSnOptions();
|
|
if (!request()->has('sn') && count($options) > 0) {
|
|
request()->offsetSet('sn', $options[0]);
|
|
}
|
|
return $this->response()->success($this->service->list());
|
|
}
|
|
|
|
if ($this->actionOfExport()) {
|
|
return $this->export();
|
|
}
|
|
|
|
return $this->response()->success($this->list());
|
|
}
|
|
|
|
public function list(): Page
|
|
{
|
|
$options = $this->getSnOptions();
|
|
$crud = $this->baseCRUD()
|
|
->filterTogglable(false)
|
|
->columnsTogglable(false)
|
|
->headerToolbar([
|
|
$this->exportAction()
|
|
])
|
|
->footerToolbar([])
|
|
->loadDataOnce()
|
|
->filter($this->baseFilter()->actions()->body([
|
|
amisMake()->SelectControl()->name('sn')->label(__('user_rank.sn'))->options($options)->value(count($options) > 0 ? $options[0] : '')->size('md'),
|
|
amisMake()->Component()->setType('submit')->label(__('admin.search'))->level('primary'),
|
|
]))
|
|
->columns([
|
|
amisMake()->TableColumn()->name('cate.name')->label(__('user_rank.cate_id')),
|
|
amisMake()->TableColumn()->name('sort')->label(__('user_rank.sort'))->set('type', 'tpl')->tpl('${(start - 1) * size + index + 1}'),
|
|
amisMake()->TableColumn()->name('score')->label(__('user_rank.score')),
|
|
]);
|
|
|
|
return $this->baseList($crud);
|
|
}
|
|
|
|
public function getSnOptions()
|
|
{
|
|
if (!$this->snOptions) {
|
|
$this->snOptions = CateRank::select('sn')->groupBy('sn')->orderBy('sn', 'desc')->pluck('sn');
|
|
}
|
|
|
|
return $this->snOptions;
|
|
}
|
|
|
|
protected function exportAction($disableSelectedItem = false)
|
|
{
|
|
$event = fn($script) => ['click' => ['actions' => [['actionType' => 'custom', 'script' => $script]]]];
|
|
$downloadPath = '/' . admin_url('_download_export', true);
|
|
$exportPath = $this->getExportPath();
|
|
$doAction = <<<JS
|
|
doAction([
|
|
{ actionType: "ajax", args: { api: { url: url.toString(), method: "get" } } },
|
|
{
|
|
actionType: "custom",
|
|
expression: "\${event.data.responseResult.responseStatus === 0}",
|
|
script: "window.open('{$downloadPath}?path='+event.data.responseResult.responseData.path)"
|
|
}
|
|
])
|
|
JS;
|
|
return amisMake()->VanillaAction()
|
|
->label(__('admin.export.title'))
|
|
->set('icon', 'fa-solid fa-download')
|
|
->align('right')
|
|
->onEvent($event(
|
|
<<<JS
|
|
let url = new URL("{$exportPath}", window.location.origin)
|
|
let param = window.location.href.split('?')[1]
|
|
if (param) {
|
|
url = url + '&' + param
|
|
}
|
|
{$doAction}
|
|
JS
|
|
));
|
|
}
|
|
|
|
protected function export()
|
|
{
|
|
$sn = request('sn', data_get($this->getSnOptions(), 0));
|
|
// 默认在 storage/app/ 下
|
|
$path = __('user_rank.cate_rank') . '-' . $sn . '.xlsx';
|
|
|
|
// 此处使用 laravel-excel 导出,可自行修改
|
|
(new \App\Exports\CateRank)->store($path, 'local');
|
|
|
|
return $this->response()->success(compact('path'));
|
|
}
|
|
}
|