first
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Notification\Http\Controllers\Admin;
|
||||
|
||||
use Encore\Admin\Controllers\AdminController;
|
||||
use Encore\Admin\Form;
|
||||
use Encore\Admin\Grid;
|
||||
use Modules\Linker\Traits\WithLinker;
|
||||
use Modules\Notification\Models\Template;
|
||||
|
||||
class IndexController extends AdminController
|
||||
{
|
||||
|
||||
use WithLinker;
|
||||
|
||||
protected $title = '消息模板';
|
||||
|
||||
public function grid(): Grid
|
||||
{
|
||||
$grid = new Grid(new Template());
|
||||
|
||||
$grid->column('title', '模板标题');
|
||||
$grid->column('slug', '调用标识');
|
||||
$grid->column('status', '状态')->bool();
|
||||
$grid->column('created_at', '创建时间');
|
||||
|
||||
return $grid;
|
||||
}
|
||||
|
||||
public function form(): Form
|
||||
{
|
||||
$form = new Form((new Template())->disableModelCaching());
|
||||
|
||||
$form->text('title', '模板标题')
|
||||
->required();
|
||||
$form->text('slug', '调用标识')
|
||||
->required();
|
||||
$form->textarea('content', '模板内容')
|
||||
->required();
|
||||
$form->switch('status', '状态');
|
||||
|
||||
$this->withUrl($form);
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Notification\Http\Controllers\Admin;
|
||||
|
||||
use Encore\Admin\Controllers\AdminController;
|
||||
|
||||
class SettingController extends AdminController
|
||||
{
|
||||
|
||||
protected $title = '消息设置';
|
||||
|
||||
public function grid()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
141
modules/Notification/Http/Controllers/Api/IndexController.php
Normal file
141
modules/Notification/Http/Controllers/Api/IndexController.php
Normal file
@@ -0,0 +1,141 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Notification\Http\Controllers\Api;
|
||||
|
||||
use App\Api\Controllers\Controller;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Notifications\DatabaseNotification;
|
||||
use Illuminate\Support\Str;
|
||||
use Jason\Api\Api;
|
||||
use Modules\Notification\Http\Resources\NotificationCollection;
|
||||
use Modules\Notification\Http\Resources\NotificationResource;
|
||||
use Modules\User\Models\User;
|
||||
|
||||
class IndexController extends Controller
|
||||
{
|
||||
|
||||
/**
|
||||
* Notes : 分类消息数量
|
||||
*
|
||||
* @Date : 2021/6/21 9:30 上午
|
||||
* @Author : <Jason.C>
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function index(): JsonResponse
|
||||
{
|
||||
$user = Api::user();
|
||||
|
||||
$result = [];
|
||||
foreach (config('notification.types') as $type => $name) {
|
||||
$result[] = [
|
||||
'type' => Str::remove('App\\Notifications\\', $type),
|
||||
'icon' => $name['icon'],
|
||||
'name' => $name['name'],
|
||||
'count' => $user->unreadNotifications()->where('type', $type)->count(),
|
||||
'title' => data_get($user->unreadNotifications()->where('type', $type)->latest()->value('data'),
|
||||
'title') ?? '',
|
||||
];
|
||||
}
|
||||
|
||||
return $this->success($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes : 未读消息数量
|
||||
*
|
||||
* @Date : 2021/5/20 12:10 下午
|
||||
* @Author : <Jason.C>
|
||||
*/
|
||||
public function counts(): JsonResponse
|
||||
{
|
||||
$user = Api::user();
|
||||
|
||||
if ($user) {
|
||||
$count = $user->unreadNotifications()->count();
|
||||
} else {
|
||||
$count = 0;
|
||||
}
|
||||
|
||||
return $this->success($count);
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes : 分类读取消息
|
||||
*
|
||||
* @Date : 2021/5/18 3:12 下午
|
||||
* @Author : <Jason.C>
|
||||
* @param string $type
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function type(string $type): JsonResponse
|
||||
{
|
||||
$list = DatabaseNotification::query()
|
||||
->whereHasMorph(
|
||||
'notifiable',
|
||||
User::class,
|
||||
function (Builder $query) {
|
||||
$query->where('id', Api::userId());
|
||||
}
|
||||
)
|
||||
->where('type', 'like', "%$type")
|
||||
->orderBy('read_at')
|
||||
->orderByDesc('created_at')->paginate();
|
||||
|
||||
return $this->success(new NotificationCollection($list));
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes : 消息详情,自动标记已读
|
||||
*
|
||||
* @Date : 2021/5/18 3:17 下午
|
||||
* @Author : <Jason.C>
|
||||
* @param string $id
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function show(string $id): JsonResponse
|
||||
{
|
||||
$notification = DatabaseNotification::findOrFail($id);
|
||||
|
||||
$notification->markAsRead();
|
||||
|
||||
return $this->success(new NotificationResource($notification));
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes : 全部标记已读
|
||||
*
|
||||
* @Date : 2021/5/18 3:22 下午
|
||||
* @Author : <Jason.C>
|
||||
*/
|
||||
public function markAsRead(string $type = null): JsonResponse
|
||||
{
|
||||
$user = Api::user();
|
||||
|
||||
$user->unreadNotifications()->when($type, function ($query) use ($type) {
|
||||
$query->where('type', 'like', "%$type");
|
||||
})->update(['read_at' => now()]);
|
||||
|
||||
return $this->success('');
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes : 清空消息
|
||||
*
|
||||
* @Date : 2021/5/18 5:49 下午
|
||||
* @Author : <Jason.C>
|
||||
* @param string|null $type
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function clean(string $type = null): JsonResponse
|
||||
{
|
||||
$user = Api::user();
|
||||
|
||||
$user->notifications()->when($type, function ($query) use ($type) {
|
||||
$query->where('type', 'like', "%$type");
|
||||
})->delete();
|
||||
|
||||
return $this->success('');
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Notification\Http\Resources;
|
||||
|
||||
use App\Api\Resources\BaseCollection;
|
||||
|
||||
class NotificationCollection extends BaseCollection
|
||||
{
|
||||
|
||||
public function toArray($request): array
|
||||
{
|
||||
return [
|
||||
'data' => $this->collection->map(function ($notification) {
|
||||
return new NotificationResource($notification);
|
||||
}),
|
||||
'page' => $this->page(),
|
||||
];
|
||||
}
|
||||
|
||||
}
|
||||
23
modules/Notification/Http/Resources/NotificationResource.php
Normal file
23
modules/Notification/Http/Resources/NotificationResource.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Notification\Http\Resources;
|
||||
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class NotificationResource extends JsonResource
|
||||
{
|
||||
|
||||
public function toArray($request): array
|
||||
{
|
||||
return [
|
||||
'notification_id' => $this->id,
|
||||
'type' => Str::remove('App\\Notifications\\', $this->type),
|
||||
'title' => $this->data['title'] ?? '',
|
||||
'content' => $this->data['content'] ?? '',
|
||||
'read_at' => (string) $this->read_at,
|
||||
'created_at' => (string) $this->created_at,
|
||||
];
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user