160 lines
6.5 KiB
PHP
160 lines
6.5 KiB
PHP
<?php
|
|
|
|
namespace App\Admin\Controllers;
|
|
|
|
use App\Admin\Services\UserService;
|
|
use App\Enums\Gender;
|
|
use Illuminate\Http\Request;
|
|
use Slowlyo\OwlAdmin\Controllers\AdminController;
|
|
use Slowlyo\OwlAdmin\Renderers\Form;
|
|
use Slowlyo\OwlAdmin\Renderers\Page;
|
|
use Slowlyo\OwlAdmin\Support\Excel\AdminExport;
|
|
|
|
class UserController extends AdminController
|
|
{
|
|
protected string $serviceName = UserService::class;
|
|
|
|
public function list(): Page
|
|
{
|
|
$crud = $this->baseCRUD()
|
|
->filterTogglable(false)
|
|
->columnsTogglable(false)
|
|
->alwaysShowPagination()
|
|
->headerToolbar([
|
|
$this->createButton(true),
|
|
$this->exportAction(),
|
|
])
|
|
->filter($this->baseFilter()->actions()->body([
|
|
amisMake()->TextControl()->name('keyword')->label(__('user.keyword'))->size('md')->placeholder(__('user.phone') . '或' . __('user.name'))->clearable(),
|
|
// amisMake()->Button()->label(__('admin.reset'))->actionType('clear-and-submit'),
|
|
amisMake()->Component()->setType('submit')->label(__('admin.search'))->level('primary'),
|
|
]))
|
|
->columns([
|
|
amisMake()->TableColumn()->name('id')->label(__('user.id')),
|
|
amisMake()->TableColumn()->type('avatar')->src('${avatar}')->name('avatar')->label(__('user.avatar')),
|
|
amisMake()->TableColumn()->name('phone')->label(__('user.phone')),
|
|
amisMake()->TableColumn()->name('name')->label(__('user.name')),
|
|
amisMake()->TableColumn()->name('sex_text')->label(__('user.sex')),
|
|
amisMake()->TableColumn()->name('age')->label(__('user.age')),
|
|
amisMake()->TableColumn()->name('address')->label(__('user.address')),
|
|
amisMake()->TableColumn()->name('created_at')->label(__('user.created_at')),
|
|
$this->rowActions(true),
|
|
]);
|
|
|
|
return $this->baseList($crud);
|
|
}
|
|
|
|
public function form($isEdit): Form
|
|
{
|
|
return $this->baseForm()->body([
|
|
amisMake()->TextControl()->name('phone')->label(__('user.phone'))->required(),
|
|
amisMake()->TextControl()->set('type', 'input-password')->name('password')->label(__('user.password'))->required(!$isEdit),
|
|
amisMake()->TextControl()->name('name')->label(__('user.name')),
|
|
amisMake()->ImageControl()->name('avatar')->label(__('user.avatar')),
|
|
amisMake()->SelectControl()->options(Gender::options())->name('sex')->label(__('user.sex')),
|
|
amisMake()->TextControl()->name('address')->label(__('user.address')),
|
|
amisMake()->DateControl()->name('birthday')->label(__('user.birthday')),
|
|
]);
|
|
}
|
|
|
|
public function detail()
|
|
{
|
|
return $this->baseDetail()->body([
|
|
amisMake()->TextControl()->name('id')->label(__('user.id'))->static(),
|
|
amisMake()->TextControl()->name('phone')->label(__('user.phone'))->static(),
|
|
amisMake()->TextControl()->name('name')->label(__('user.name'))->static(),
|
|
amisMake()->TextControl()->name('sex_text')->label(__('user.sex'))->static(),
|
|
amisMake()->TextControl()->name('age')->label(__('user.age'))->static(),
|
|
amisMake()->DateControl()->name('birthday')->label(__('user.birthday'))->static(),
|
|
amisMake()->TextControl()->name('address')->label(__('user.address'))->static(),
|
|
amisMake()->DateTimeControl()->name('created_at')->label(__('user.created_at'))->static(),
|
|
]);
|
|
}
|
|
|
|
public function getList(Request $request)
|
|
{
|
|
$list = $this->service->listQuery()->get();
|
|
|
|
return $this->response()->success($list);
|
|
}
|
|
|
|
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;
|
|
$buttons = [
|
|
amisMake()->VanillaAction()->label(__('admin.export.all'))->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
|
|
|
|
)
|
|
),
|
|
];
|
|
return amisMake()
|
|
->DropdownButton()
|
|
->label(__('admin.export.title'))
|
|
->set('icon', 'fa-solid fa-download')
|
|
->buttons($buttons)
|
|
->align('right')
|
|
->closeOnClick();
|
|
}
|
|
|
|
protected function export()
|
|
{
|
|
// 默认在 storage/app/ 下
|
|
$path = sprintf('%s-%s.xlsx', $this->exportFileName(), date('YmdHis'));
|
|
|
|
// 导出本页和导出选中项都是通过 _ids 查询
|
|
$ids = request()->input('_ids');
|
|
|
|
// listQuery() 为列表查询条件,与获取列表数据一致
|
|
$query = $this->service->listQuery()
|
|
->when($ids, fn($query) => $query->whereIn($this->service->primaryKey(), explode(',', $ids)));
|
|
|
|
// 此处使用 laravel-excel 导出,可自行修改
|
|
AdminExport::make($query)
|
|
->setHeadings([
|
|
__('user.id'),
|
|
__('user.phone'),
|
|
__('user.name'),
|
|
__('user.sex'),
|
|
__('user.age'),
|
|
__('user.birthday'),
|
|
__('user.address'),
|
|
__('user.created_at'),
|
|
])
|
|
->setMap(function ($item) {
|
|
return [
|
|
$item->id,
|
|
$item->phone,
|
|
$item->name,
|
|
$item->sex_text,
|
|
$item->age,
|
|
$item->birthday_format,
|
|
$item->address,
|
|
$item->created_at,
|
|
];
|
|
})
|
|
->store($path);
|
|
|
|
return $this->response()->success(compact('path'));
|
|
}
|
|
}
|