generated from liutk/owl-admin-base
135 lines
3.4 KiB
PHP
135 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace App\Admin\Controllers;
|
|
|
|
use App\Traits\CustomActionTrait;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Arr;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Validation\ValidationException;
|
|
use Slowlyo\OwlAdmin\Controllers\AdminController as Controller;
|
|
use Slowlyo\OwlAdmin\Exceptions\AdminException;
|
|
use Throwable;
|
|
|
|
/**
|
|
* @property \App\Admin\Services\BaseService $service
|
|
*/
|
|
abstract class AdminController extends Controller
|
|
{
|
|
use CustomActionTrait;
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function store(Request $request)
|
|
{
|
|
try {
|
|
DB::beginTransaction();
|
|
|
|
if ($this->actionOfQuickEdit()) {
|
|
$result = $this->service->quickEdit($request->all());
|
|
} elseif ($this->actionOfQuickEditItem()) {
|
|
$result = $this->service->quickEditItem($request->all());
|
|
} else {
|
|
$result = $this->service->store($request->all());
|
|
}
|
|
|
|
if (! $result) {
|
|
admin_abort($this->service->getError() ?: __('admin.save').__('admin.failed'));
|
|
}
|
|
|
|
DB::commit();
|
|
} catch (Throwable $th) {
|
|
DB::rollBack();
|
|
|
|
return $this->renderException($th);
|
|
}
|
|
|
|
return $this->response()->successMessage(__('admin.save').__('admin.successfully'));
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function update(Request $request)
|
|
{
|
|
$input = Arr::except($request->all(), $this->service->primaryKey());
|
|
|
|
if ($request->filled('_fields')) {
|
|
$input = Arr::only($input, explode(',', $request->input('_fields')));
|
|
}
|
|
|
|
try {
|
|
DB::beginTransaction();
|
|
|
|
$result = $this->service->update($this->getPrimaryValue($request), $input);
|
|
|
|
if (! $result) {
|
|
admin_abort($this->service->getError() ?: __('admin.save').__('admin.failed'));
|
|
}
|
|
|
|
DB::commit();
|
|
} catch (Throwable $th) {
|
|
DB::rollBack();
|
|
|
|
return $this->renderException($th);
|
|
}
|
|
|
|
return $this->response()->successMessage(__('admin.save').__('admin.successfully'));
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function destroy($ids)
|
|
{
|
|
try {
|
|
DB::beginTransaction();
|
|
|
|
$result = $this->service->delete($ids);
|
|
if ($result === false) {
|
|
admin_abort($this->service->getError());
|
|
}
|
|
|
|
DB::commit();
|
|
} catch (Throwable $th) {
|
|
DB::rollBack();
|
|
|
|
return $this->renderException($th);
|
|
}
|
|
|
|
return $this->response()->successMessage(__('admin.delete').__('admin.successfully'));
|
|
}
|
|
|
|
public function getQuickEditItemPath(array $fields = ['*'])
|
|
{
|
|
$path = $this->getUpdatePath();
|
|
|
|
if ($fields != ['*']) {
|
|
$path .= '?_fields='.implode(',', $fields);
|
|
}
|
|
|
|
return $path;
|
|
}
|
|
|
|
protected function renderException(Throwable $e)
|
|
{
|
|
report($e);
|
|
|
|
if ($e instanceof AdminException) {
|
|
return $e->render();
|
|
}
|
|
|
|
$message = $e->getMessage();
|
|
|
|
if ($e instanceof ValidationException) {
|
|
$message = Arr::first($e->errors());
|
|
if (is_array($message)) {
|
|
$message = Arr::first($message) ?: '参数错误';
|
|
}
|
|
}
|
|
|
|
return $this->response()->fail($message);
|
|
}
|
|
}
|