TaoLer/extend/taoler/com/Message.php

88 lines
2.4 KiB
PHP
Raw Normal View History

2020-03-29 19:57:18 +08:00
<?php
/*
* @Author: TaoLer <317927823@qq.com>
* @Date: 2021-12-06 16:04:50
* @LastEditTime: 2022-08-16 12:09:28
* @LastEditors: TaoLer
* @Description: 优化版
* @FilePath: \TaoLer\extend\taoler\com\Message.php
* Copyright (c) 2020~2022 https://www.aieok.com All rights reserved.
*/
2020-03-29 19:57:18 +08:00
namespace taoler\com;
use think\facade\Db;
use app\common\model\Message as MessageModel;
use app\common\model\MessageTo;
class Message
2020-10-19 17:23:55 +08:00
{
/**
* 发送消息
2020-11-30 17:13:55 +08:00
* @param $sendId
* @param $receveId
* @param $data
2020-10-19 17:23:55 +08:00
* @return bool
*/
2020-03-29 19:57:18 +08:00
public static function sendMsg($sendId,$receveId,$data)
{
2020-03-31 23:01:00 +08:00
//写入消息库
$msg = MessageModel::create($data); //写入消息库
2020-03-29 19:57:18 +08:00
$msgId = $msg->id;
2020-03-31 23:01:00 +08:00
//类型1为用户写入用户收件箱
2020-11-30 17:13:55 +08:00
if($data['type'] == 1 || $data['type'] == 2 ){
2020-03-31 23:01:00 +08:00
$result = MessageTo::create(['send_id'=>$sendId,'receve_id'=>$receveId,'message_id'=>$msgId,'message_type'=>$data['type']]);
if(!$result){
return false;
}
}
if($msg){
2020-03-29 19:57:18 +08:00
return true;
2020-03-31 23:01:00 +08:00
} else {
return false;
2020-03-29 19:57:18 +08:00
}
}
2020-10-19 17:23:55 +08:00
/**
* 接收消息
2023-03-16 22:33:28 +08:00
* @param $uid 接收消息用户ID
* @return array
2020-10-19 17:23:55 +08:00
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
2020-03-29 19:57:18 +08:00
public static function receveMsg($uid)
{
return Db::name('message_to')
2020-03-29 19:57:18 +08:00
->alias('t')
->join('message m','t.message_id = m.id' )
->join('user u','t.send_id = u.id')
2020-03-31 23:01:00 +08:00
->field('t.id as id,name,title,content,link,receve_id,t.create_time as create_time,message_type,is_read')
2020-03-29 19:57:18 +08:00
->where('t.receve_id',$uid)
->where(['t.delete_time'=>0])
2020-04-05 12:52:29 +08:00
->order(['t.is_read'=>'asc','t.create_time'=>'desc'])
->select()->toArray();
2020-03-29 19:57:18 +08:00
}
2020-10-19 17:23:55 +08:00
/**
2020-11-30 17:13:55 +08:00
* 登录用户把系统消息写入数据表
* @param int $uid
2020-10-19 17:23:55 +08:00
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
2020-11-30 17:13:55 +08:00
public static function insertMsg(int $uid)
2020-03-31 23:01:00 +08:00
{
//得到所有系统消息
$sysmsg = MessageModel::where(['type'=>0])->select();
2020-03-31 23:01:00 +08:00
foreach($sysmsg as $smg){
2020-10-19 17:23:55 +08:00
//检验通知ID是否被写入个人收件箱
$msgId = Db::name('message_to')->where(['message_id'=>$smg['id'],'delete_time'=>0])->find();
2020-03-31 23:01:00 +08:00
if(!$msgId){
$result = MessageTo::create(['send_id'=>$smg['user_id'],'receve_id'=>$uid,'message_id'=>$smg['id'],'message_type'=>$smg['type']]);
}
}
}
2020-03-29 19:57:18 +08:00
}