Files
spacestation/common/models/sys/Sys_notice_model.php
T
2025-11-29 10:40:14 +08:00

88 lines
3.2 KiB
PHP

<?php
defined('BASEPATH') or exit('No direct script access allowed');
class Sys_notice_model extends HD_Model
{
private $table_name = 'lc_sys_notice';
const PLAT_FORM_ADMIN = 1; //前端-管理后台
const PLAT_FORM_PINGAN = 2; //前端-平安
const PLAT_FORM_WXAPP = 3; //前端-微信小程序
const TYPE_SYS = 0; //系统通知
const ICON_DEFAULT = 'el-icon-s-comment';//默认图标
const ICON_TYPE = [
self::TYPE_SYS => 'el-icon-s-comment',
];
const STATUS_UNREAD = 0; //未读
const STATUS_READ = 1; //已读
const READ_STATUS = [
self::STATUS_UNREAD => '未读',
self::STATUS_READ => '已读',
];
const PLATFORM_LIST = [
self::PLAT_FORM_ADMIN => '管理后台',
self::PLAT_FORM_PINGAN => '平安',
self::PLAT_FORM_WXAPP => '微信小程序',
];
public function __construct()
{
parent::__construct($this->table_name, 'default');
}
/**
* 添加系统通知
* @param $sendUid 推送消息账号
* @param $sendPlatform 发消息平台
* @param $params 其它参数
* @return MyResponse
*/
public function addNotice($params, $sendPlatform, $sendUid = 8888888, $showWssErr = false)
{
$logPath = 'websocket';
$logFile = 'sendError.txt';
try {
if (!$params['platform'] || !$params['uid'] || !$params['content'] || !$sendPlatform) {
throw new Exception('参数错误');
}
$addData = [
'platform' => $params['platform'],
'uid' => $params['uid'],
'content' => $params['content'],
'type' => $params['type'] ?: 0,
'url' => $params['url'] ?: '',
'c_time' => time()
];
$addData['icon'] = self::ICON_TYPE[$params['type']] ?: self::ICON_DEFAULT;
$req = $this->add($addData);
if (!is_numeric($req)) {
throw new Exception('添加失败');
}
$this->load->model('ws/ws_conn_model');
$this->load->library('websocket/wsClient', ['platform' => $sendPlatform, 'uid' => $sendUid]);
//websocket推送
$requestId = uniqid();
$data = [
'id' => $req,
'icon' => $addData['icon'],
'url' => $addData['url'],
'type' => $addData['type'],
'read' => 0,
'content' => $addData['content'],
'c_time' => date('Y-m-d H:i:s')
];
/** @var MyResponse $websocketRes */
$websocketRes = $this->wsclient->sendMessage($requestId, $addData['platform'], $addData['uid'], $data);
if (!$websocketRes->isSuccess()) {
if ($showWssErr) {
throw new Exception("发送socket消息失败:{$websocketRes->getMessage()}");
}
debug_log($websocketRes->getMessage(), $logFile, $logPath);
}
return new MyResponse(EXIT_SUCCESS, 'success');
} catch (Exception $e) {
return new MyResponse(EXIT_ERROR, $e->getMessage());
}
}
}