103 lines
3.6 KiB
PHP
103 lines
3.6 KiB
PHP
<?php
|
||
|
||
namespace app\common\service;
|
||
|
||
use app\common\model\Messages;
|
||
use app\common\traits\ServiceTrait;
|
||
use think\Log;
|
||
|
||
class MessageService
|
||
{
|
||
use ServiceTrait;
|
||
|
||
/** @var Messages */
|
||
public static $Model = Messages::class;
|
||
|
||
// 消息类型
|
||
const TYPE_SYSTEM = '0'; //系统消息:账户、审核、安全类
|
||
const TYPE_NOTICE = '1'; //公告消息:平台运营公告
|
||
const TYPE_DEMAND = '2'; //需求消息:需求/工单/合同/订单等业务流程
|
||
|
||
|
||
/**
|
||
* @param $to_user_id
|
||
* @param $title
|
||
* @param $content
|
||
* @param $extra_json
|
||
* @param $from_user_id
|
||
* @return mixed
|
||
* @throws \Exception
|
||
*/
|
||
public static function createData($to_user_id, $title, $content, $extra_json = [], $from_user_id = 0, $message_type = self::TYPE_SYSTEM)
|
||
{
|
||
$data = [
|
||
'message_type' => $message_type,
|
||
'from_user_id' => $from_user_id,
|
||
'to_user_id' => $to_user_id,
|
||
'title' => $title,
|
||
'content' => $content,
|
||
'extra_json' => $extra_json,
|
||
'is_read' => CommonService::STATUS_OFF,
|
||
];
|
||
|
||
$res = self::create($data);
|
||
if (!$res) {
|
||
Log::info(__METHOD__ . '-写入数据失败失败:' . json_encode($data, JSON_UNESCAPED_UNICODE));
|
||
exception('写入数据失败失败');
|
||
}
|
||
|
||
return $res;
|
||
}
|
||
|
||
/**
|
||
* 发送订阅消息时同步写入站内消息
|
||
* 数据来源:MSG_MAP 中模板的 name / message_type / valueNames / remark / page
|
||
* @param int $to_user_id 收件用户
|
||
* @param string $template_id 订阅消息模板ID
|
||
* @param array $msgData sendMsg 的原始参数 ['values'=>[], 'param'=>[], 'page'=>'']
|
||
* @param string $page 打包后的跳转页面(含query),如 pages/demand/list/index?opType=my&demandId=1
|
||
* @return mixed
|
||
*/
|
||
public static function createBySubscribe($to_user_id, $template_id, array $msgData = [], $page = '')
|
||
{
|
||
$template = SubscribeMessageService::MSG_MAP[$template_id] ?? [];
|
||
if (!$template) {
|
||
//未定义的模板不写站内消息
|
||
return false;
|
||
}
|
||
|
||
//按模板字段名组装可读内容:工单号:xxx;服务项目:xxx;...;备注:xxx
|
||
$values = $msgData['values'] ?? [];
|
||
$valueNames = $template['valueNames'] ?? [];
|
||
$lines = [];
|
||
foreach ($valueNames as $key => $name) {
|
||
if (isset($values[$key]) && $values[$key] !== '') {
|
||
$lines[] = $name . ':' . $values[$key];
|
||
}
|
||
}
|
||
// 备注处理:避免正文出现两行「备注」或语义冲突
|
||
// 1) 模板字段里已含「备注」时,发送方自定义了备注文案,不再追加模板默认 remark
|
||
// 2) 否则仅在正文尚未包含 remark 时追加,避免重复
|
||
$remark = $template['remark'] ?? '';
|
||
$hasRemarkField = in_array('备注', $valueNames, true);
|
||
if ($remark !== '' && !$hasRemarkField) {
|
||
$content = implode("\n", $lines);
|
||
if (mb_strpos($content, $remark) === false) {
|
||
$lines[] = '备注:' . $remark;
|
||
}
|
||
}
|
||
$content = implode("\n", $lines);
|
||
|
||
return self::createData(
|
||
$to_user_id,
|
||
$template['name'] ?? '系统通知',
|
||
$content,
|
||
[
|
||
'page' => $page ?: ($template['page'] ?? ''),
|
||
'template_id' => $template_id,
|
||
],
|
||
0, //系统发出
|
||
$template['message_type'] ?? self::TYPE_SYSTEM
|
||
);
|
||
}
|
||
} |