Files
2025-12-03 13:49:15 +08:00

98 lines
3.6 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 PLAT_FORM_SYS_ADMIN = 4;//推送平台-管理后台
const PLAT_FORM_SYS_PINGAN = 5;//推送平台-平安
const PLAT_FORM_SYS_WXAPP = 6;//推送平台-微信小程序
const PLAT_FORM_SYS_H5 = 7;//推送平台-h5报名
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 => '微信小程序',
];
// 系统账号
const SEND_UID_SYS = 8888888;
public function __construct()
{
parent::__construct($this->table_name, 'default');
}
/**
* 添加系统通知
* @param Array $params platform,uid,content,type,url
* @param $sendPlatform
* @param $sendUid
* @param $showWssErr
* @return MyResponse
*/
public function addNotice($params, $sendPlatform, $sendUid = self::SEND_UID_SYS, $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(),
'send_platform' => $sendPlatform,
'send_uid' => $sendUid
];
$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());
}
}
}