106 lines
2.8 KiB
PHP
106 lines
2.8 KiB
PHP
<?php
|
|||
|
|
declare(strict_types=1);
|
||
|
|
/**
|
||
|
|
*+------------------
|
||
|
|
* madong
|
||
|
|
*+------------------
|
||
|
|
* Copyright (c) https://gitee.com/motion-code All rights reserved.
|
||
|
|
*+------------------
|
||
|
|
* Author: Mr. April (405784684@qq.com)
|
||
|
|
*+------------------
|
||
|
|
* Official Website: http://www.madong.tech
|
||
|
|
*/
|
||
|
|
|
||
|
|
namespace app\service\admin\message;
|
||
|
|
|
||
|
|
use app\dao\message\MessageDao;
|
||
|
|
use app\enum\system\BusinessPlatform;
|
||
|
|
use app\enum\system\MessageEvent;
|
||
|
|
use app\enum\system\MessageStatus;
|
||
|
|
use core\base\BaseService;
|
||
|
|
use core\notify\enum\PushClientType;
|
||
|
|
use core\notify\Notification;
|
||
|
|
use madong\helper\Arr;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @method save(array $data)
|
||
|
|
*/
|
||
|
|
class MessageService extends BaseService
|
||
|
|
{
|
||
|
|
|
||
|
|
public function __construct(MessageDao $dao)
|
||
|
|
{
|
||
|
|
$this->dao = $dao;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 系统登录-推送消息
|
||
|
|
*
|
||
|
|
* @param string|int|array $id
|
||
|
|
*
|
||
|
|
* @return void
|
||
|
|
* @throws \Exception
|
||
|
|
*/
|
||
|
|
public function notifyOnFirstLoginToAll(string|int|array $id): void
|
||
|
|
{
|
||
|
|
try {
|
||
|
|
// 获取历史消息记录
|
||
|
|
$models = $this->selectList(['status' => MessageStatus::UNREAD->value], '*', 0, 0, '', [])->toArray();
|
||
|
|
if (!empty($models)) {
|
||
|
|
foreach ($models as $value) {
|
||
|
|
Notification::pushOnly(PushClientType::BACKEND, BusinessPlatform::ADMIN->value, '*', $value['receiver_id'], MessageEvent::DEFAULT->value, [], $value,null);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
} catch (\Throwable $e) {
|
||
|
|
throw new \Exception($e->getMessage());
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 标记已读
|
||
|
|
*
|
||
|
|
* @param string|array $param
|
||
|
|
*
|
||
|
|
* @throws \Exception
|
||
|
|
*/
|
||
|
|
public function isRead(string|array $param): void
|
||
|
|
{
|
||
|
|
try {
|
||
|
|
$data = Arr::normalize($param);
|
||
|
|
$messageModel = $this->dao->getModel();
|
||
|
|
$models = $messageModel->find($data);
|
||
|
|
foreach ($models as $model) {
|
||
|
|
$model->status = MessageStatus::READ;
|
||
|
|
$model->read_at = time();
|
||
|
|
$model->save();
|
||
|
|
}
|
||
|
|
} catch (\Exception $e) {
|
||
|
|
throw new \Exception($e->getMessage());
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 标记未读
|
||
|
|
*
|
||
|
|
* @param string|array $param
|
||
|
|
*
|
||
|
|
* @throws \Exception
|
||
|
|
*/
|
||
|
|
public function isUnread(string|array $param): void
|
||
|
|
{
|
||
|
|
try {
|
||
|
|
$data = Arr::normalize($param);
|
||
|
|
$messageModel = $this->dao->getModel();
|
||
|
|
$models = $messageModel->find($data);
|
||
|
|
foreach ($models as $model) {
|
||
|
|
$model->status = MessageStatus::UNREAD;
|
||
|
|
$model->updated_at = time();
|
||
|
|
$model->save();
|
||
|
|
}
|
||
|
|
} catch (\Exception $e) {
|
||
|
|
throw new \Exception($e->getMessage());
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|