Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions resources/views/partials/alerts.blade.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
@if($error = session()->get('error'))
@if($error = \Dcat\Admin\Support\SessionMessage::tryFrom(session()->get('error')))
<div class="alert alert-danger alert-dismissable">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<h4><i class="icon fa fa-ban"></i> &nbsp;{{ \Illuminate\Support\Arr::get($error->get('title'), 0) }}</h4>
<p>{!! \Illuminate\Support\Arr::get($error->get('message'), 0) !!}</p>
<h4><i class="icon fa fa-ban"></i> &nbsp;{{ $error->getTitle() }}</h4>
<p>{!! $error->getMessage() !!}</p>
</div>
@elseif ($errors = session()->get('errors'))
@if ($errors->hasBag('error'))
Expand All @@ -16,26 +16,26 @@
@endif
@endif

@if($success = session()->get('success'))
@if($success = \Dcat\Admin\Support\SessionMessage::tryFrom(session()->get('success')))
<div class="alert alert-success alert-dismissable">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<h4><i class="icon fa fa-check"></i> &nbsp;{{ \Illuminate\Support\Arr::get($success->get('title'), 0) }}</h4>
<p>{!! \Illuminate\Support\Arr::get($success->get('message'), 0) !!}</p>
<h4><i class="icon fa fa-check"></i> &nbsp;{{ $success->getTitle() }}</h4>
<p>{!! $success->getMessage() !!}</p>
</div>
@endif

@if($info = session()->get('info'))
@if($info = \Dcat\Admin\Support\SessionMessage::tryFrom(session()->get('info')))
<div class="alert alert-info alert-dismissable">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<h4><i class="icon fa fa-info"></i> &nbsp;{{ \Illuminate\Support\Arr::get($info->get('title'), 0) }}</h4>
<p>{!! \Illuminate\Support\Arr::get($info->get('message'), 0) !!}</p>
<h4><i class="icon fa fa-info"></i> &nbsp;{{ $info->getTitle() }}</h4>
<p>{!! $info->getMessage() !!}</p>
</div>
@endif

@if($warning = session()->get('warning'))
@if($warning = \Dcat\Admin\Support\SessionMessage::tryFrom(session()->get('warning')))
<div class="alert alert-warning alert-dismissable">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<h4><i class="icon fa fa-warning"></i> &nbsp;{{ \Illuminate\Support\Arr::get($warning->get('title'), 0) }}</h4>
<p>{!! \Illuminate\Support\Arr::get($warning->get('message'), 0) !!}</p>
<h4><i class="icon fa fa-warning"></i> &nbsp;{{ $warning->getTitle() }}</h4>
<p>{!! $warning->getMessage() !!}</p>
</div>
@endif
@endif
11 changes: 8 additions & 3 deletions resources/views/partials/exception.blade.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
@if(isset($errors) && $errors->hasBag('exception'))
<?php $error = $errors->getBag('exception'); ?>
@php
$error = $errors->getBag('exception');
$errorType = $error->first('type');
$errorFile = $error->first('file');
$errorLine = $error->first('line');
@endphp
<div class="alert alert-warning alert-dismissable">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<h4>
<i class="icon fa fa-warning"></i>
<i style="border-bottom: 1px dotted #fff;cursor: pointer;" title="{{ $error->get('type')[0] }}" ondblclick="var f=this.innerHTML;this.innerHTML=this.title;this.title=f;">{{ class_basename($error->get('type')[0]) }}</i>
In <i title="{{ $error->get('file')[0] }} line {{ $error->get('line')[0] }}" style="border-bottom: 1px dotted #fff;cursor: pointer;" ondblclick="var f=this.innerHTML;this.innerHTML=this.title;this.title=f;">{{ basename($error->get('file')[0]) }} line {{ $error->get('line')[0] }}</i> :
<i style="border-bottom: 1px dotted #fff;cursor: pointer;" title="{{ $errorType }}" ondblclick="var f=this.innerHTML;this.innerHTML=this.title;this.title=f;">{{ class_basename($errorType) }}</i>
In <i title="{{ $errorFile }} line {{ $errorLine }}" style="border-bottom: 1px dotted #fff;cursor: pointer;" ondblclick="var f=this.innerHTML;this.innerHTML=this.title;this.title=f;">{{ basename($errorFile) }} line {{ $errorLine }}</i> :
</h4>
<p><a style="cursor: pointer;" onclick="$('#dcat-admin-exception-trace').toggleClass('hidden');$('i', this).toggleClass('fa-angle-double-down fa-angle-double-up');"><i class="fa fa-angle-double-down"></i>&nbsp;&nbsp;{!! $error->first('message') !!}</a></p>

Expand Down
12 changes: 3 additions & 9 deletions resources/views/partials/toastr.blade.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
@if(Session::has('dcat-admin-toastr'))
@php
$toastr = Session::get('dcat-admin-toastr');
$type = $toastr->get('type')[0] ?? 'success';
$message = $toastr->get('message')[0] ?? '';
$options = admin_javascript_json($toastr->get('options', []));
@endphp
<script>$(function () { toastr.{{$type}}('{!! $message !!}', null, {!! $options !!}); })</script>
@endif
@if($toastr = \Dcat\Admin\Support\SessionMessage::tryFrom(Session::get('dcat-admin-toastr')))
<script>$(function () { toastr.{!! $toastr->getToastrType() !!}({!! json_encode($toastr->getMessage()) !!}, null, {!! admin_javascript_json($toastr->getOptions()) !!}); })</script>
@endif
12 changes: 3 additions & 9 deletions src/Http/Controllers/ScaffoldController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
use Dcat\Admin\Scaffold\ModelCreator;
use Dcat\Admin\Scaffold\RepositoryCreator;
use Dcat\Admin\Support\Helper;
use Dcat\Admin\Support\SessionMessage;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\MessageBag;
use Illuminate\Support\Str;

class ScaffoldController extends Controller
Expand Down Expand Up @@ -275,10 +275,7 @@ protected function getDatabaseColumns($db = null, $tb = null)

protected function backWithException(\Exception $exception)
{
$error = new MessageBag([
'title' => 'Error',
'message' => $exception->getMessage(),
]);
$error = SessionMessage::make('Error', $exception->getMessage());

return redirect()->refresh()->withInput()->with(compact('error'));
}
Expand All @@ -293,10 +290,7 @@ protected function backWithSuccess($paths, $message)

$messages[] = "<br />$message";

$success = new MessageBag([
'title' => 'Success',
'message' => implode('<br />', $messages),
]);
$success = SessionMessage::make('Success', implode('<br />', $messages));

return redirect()->refresh()->with(compact('success'));
}
Expand Down
93 changes: 93 additions & 0 deletions src/Support/SessionMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

namespace Dcat\Admin\Support;

/**
* Session 消息对象(兼容 Laravel 10-13,兼容 PHP session.serialization = json/php).
*
* 解决两类问题:
* 1. Laravel 13 中 MessageBag 通过 redirect()->with() 序列化后变为数组的问题
* 2. session.serialization = json 时,protected 属性不会被 json_encode 包含的问题
*/
final class SessionMessage implements \JsonSerializable
{
private const JSON_CLASS_KEY = '__dcat_class';

private const JSON_CLASS_VALUE = self::class;

private const TOASTR_TYPES = ['success', 'error', 'warning', 'info'];

public function __construct(
protected string $title = '',
protected string $message = '',
protected array $options = [],
) {
}

public static function make(string $title, string $message = '', array $options = []): self
{
return new self($title, $message, $options);
}

public function getTitle(): string
{
return $this->title;
}

public function getToastrType(): string
{
return in_array($this->title, self::TOASTR_TYPES, true) ? $this->title : 'info';
}

public function getMessage(): string
{
return $this->message;
}

public function getOptions(): array
{
return $this->options;
}

/**
* 支持 JSON session 序列化(session.serialization = json).
*
* 将对象编码为包含类型标识符的数组,确保 protected 属性不丢失。
*/
public function jsonSerialize(): mixed
{
return [
self::JSON_CLASS_KEY => self::JSON_CLASS_VALUE,
'title' => $this->title,
'message' => $this->message,
'options' => $this->options,
];
}

/**
* 从 session 中读取的值尝试构造实例.
*
* 兼容两种情况:
* - PHP 序列化(session.serialization = php):值已经是 SessionMessage 对象
* - JSON 序列化(session.serialization = json):值是带类型标识的数组
*/
public static function tryFrom(mixed $value): ?self
{
if ($value instanceof self) {
return $value;
}

if (
is_array($value)
&& ($value[self::JSON_CLASS_KEY] ?? null) === self::JSON_CLASS_VALUE
) {
return new self(
is_string($value['title'] ?? null) ? $value['title'] : '',
is_string($value['message'] ?? null) ? $value['message'] : '',
is_array($value['options'] ?? null) ? $value['options'] : [],
);
}

return null;
}
}
10 changes: 5 additions & 5 deletions src/Support/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

use Dcat\Admin\Admin;
use Dcat\Admin\Support\Helper;
use Dcat\Admin\Support\SessionMessage;
use Illuminate\Contracts\Support\Htmlable;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Http\Request;
use Illuminate\Support\MessageBag;
use Symfony\Component\HttpFoundation\Response;

if (! function_exists('admin_setting')) {
Expand Down Expand Up @@ -298,15 +298,15 @@ function admin_base_path($path = '')

if (! function_exists('admin_toastr')) {
/**
* Flash a toastr message bag to session.
* Flash a toastr message to session.
*
* @param string $message
* @param string $type
* @param array $options
*/
function admin_toastr($message = '', $type = 'success', $options = [])
{
$toastr = new MessageBag(get_defined_vars());
$toastr = SessionMessage::make($type, $message, $options);

session()->flash('dcat-admin-toastr', $toastr);
}
Expand Down Expand Up @@ -353,15 +353,15 @@ function admin_warning($title, $message = '')

if (! function_exists('admin_info')) {
/**
* Flash a message bag to session.
* Flash a message to session.
*
* @param string $title
* @param string $message
* @param string $type
*/
function admin_info($title, $message = '', $type = 'info')
{
$message = new MessageBag(get_defined_vars());
$message = SessionMessage::make($title, $message);

session()->flash($type, $message);
}
Expand Down
Loading
Loading