dggjimai/application/common/model/Notice.php

199 lines
6.7 KiB
PHP
Executable File

<?php
/**
* 消息模型
*/
namespace app\common\model;
use think\model\concern\SoftDelete;
use think\db;
use tools\Crypt;
class Notice extends Model
{
protected $name = 'notice';
protected $autoWriteTimestamp = true;
/**
* Notes: 根据各个场景发送通知
* @param $user_id
* @param $params
*/
public static function noticeByScene($user_id, $params)
{
$scene_info = NoticeSetting::where('scene', $params['scene'])->find();
if (empty($scene_info)) {
throw new Exception('信息错误');
}
$scene_info['system_notice']=json_decode($scene_info['system_notice'],true);
$scene_info['sms_notice']=json_decode($scene_info['sms_notice'],true);
$scene_info['oa_notice']=json_decode($scene_info['oa_notice'],true);
$scene_info['mnp_notice']=json_decode($scene_info['mnp_notice'],true);
$params = self::mergeParams($params);
//发送系统消息
if (isset($scene_info['system_notice']['status']) && $scene_info['system_notice']['status'] == 1) {
$content = self::contentFormat($scene_info['system_notice']['content'], $params);
self::addNoticeLog($params, $scene_info,1, $content);
}
//发送短信记录
if (isset($scene_info['sms_notice']['status']) && $scene_info['sms_notice']['status'] == 1) {
}
//发送公众号记录
if (isset($scene_info['oa_notice']['status']) && $scene_info['oa_notice']['status'] == 1) {
//(new WxMessageServer($user_id,Client_::oa))->send($params);
}
//发送小程序记录
if (isset($scene_info['mnp_notice']['status']) && $scene_info['mnp_notice']['status'] == 1) {
//echo 22;
//(new WxMessageServer($user_id, Client_::mnp))->send($params);
}
}
/**
* Notes: 拼装额外参数
* @param $params
*/
public static function mergeParams($params)
{
//订单相关信息
if (!empty($params['order_id'])) {
$order = Db::name('order')->where(['id' => $params['order_id']])->find();
$order_goods = Db::name('order_goods')->alias('og')
->field('g.name')
->join('goods g', 'og.goods_id = g.id')
->where('og.order_id', $params['order_id'])
->find();
$goods_name = $order_goods['name'] ?? '商品';
if (mb_strlen($goods_name) > 8 ) {
$goods_name = mb_substr($goods_name,0,8) . '..';
}
$params['goods_name'] = $goods_name;
$params['order_sn'] = $order['order_sn'];
$params['time'] = date('Y-m-d H:i', $order['create_time']);
$params['total_num'] = $order['total_num'];
$params['order_amount'] = $order['order_amount'];
$order_id=Crypt::encrypt($params['order_id']);
}
//寄卖订单相关信息
if (!empty($params['jm_order_id'])) {
$order = Db::name('consignment_order')->where(['id' => $params['jm_order_id']])->find();
$order_goods = Db::name('consignment_order_goods')
->field('goods_name')
->where('order_id', $params['jm_order_id'])
->find();
$goods_name = $order_goods['goods_name'] ?? '商品';
if (mb_strlen($goods_name) > 8 ) {
$goods_name = mb_substr($goods_name,0,8) . '..';
}
$params['goods_name'] = $goods_name;
$params['order_sn'] = $order['order_sn'];
$params['time'] = date('Y-m-d H:i', $order['create_time']);
$order_id=Crypt::encrypt($params['jm_order_id']);
}
//用户相关信息
if (!empty($params['user_id'])) {
$user = Db::name('user')->where('id', $params['user_id'])->find();
$params['nickname'] = $user['nickname'];
}
//跳转路径
$jump_path = NoticeSetting::getPathByScene($params['scene'], $order_id ?? 0);
$params['url'] = $jump_path['url'];
$params['page'] = $jump_path['page'];
return $params;
}
//格式化消息内容(替换文本)
public static function contentFormat($content, $params)
{
foreach ($params as $k => $v) {
$search_replace = '{'.$k.'}';
$content = str_replace($search_replace, $v, $content);
}
return $content;
}
//添加通知记录
public static function addNoticeLog($params, $scene_info, $send_type, $content, $extra = '')
{
return Db::name('notice')->insertGetId([
'user_id' => $params['user_id'] ?? 0,
'title' => self::getTitleByScene($send_type, $scene_info),
'content' => $content,
'scene' => $params['scene'],
'receive_type' => self::getReceiveTypeByScene($params['scene']),
'send_type' => $send_type,
'extra' => $extra,
'create_time' => time()
]);
}
//根据不同发送类型获取标题
public static function getTitleByScene($send_type, $scene_info)
{
$title = '';
switch ($send_type) {
case NoticeSetting::SYSTEM_NOTICE:
$title = $scene_info['system_notice']['title'] ?? '';
break;
case NoticeSetting::SMS_NOTICE:
$title = '';
break;
case NoticeSetting::OA_NOTICE:
$title = $scene_info['oa_notice']['name'] ?? '';
break;
case NoticeSetting::MNP_NOTICE:
$title = $scene_info['mnp_notice']['name'] ?? '';
break;
}
return $title;
}
//根据场景返回当前接收对象
public static function getReceiveTypeByScene($scene)
{
if (in_array($scene, NoticeSetting::NOTICE_PLATFORM_SCENE)) {
return NoticeSetting::NOTICE_PLATFORM;
}
if (in_array($scene, NoticeSetting::NOTICE_USER_SCENE)) {
return NoticeSetting::NOTICE_USER;
}
if (in_array($scene, NoticeSetting::NOTICE_OTHER_SCENE)) {
return NoticeSetting::NOTICE_OTHER;
}
}
//更新为已读
public static function setRead($where)
{
//进入列表后全部已读
Db::name('notice')
->where($where)
->where('read','<>', 1)
->update(['read' => 1]);
}
//是否有未读的消息
public static function unRead($user_id)
{
$un_read = Db::name('notice')
->where(['user_id' => $user_id, 'read' => 0])
->where(['send_type' =>1])
->find();
if ($un_read){
return true;
}
return false;
}
}