TaoLer/extend/taoler/com/Message.php

80 lines
2.2 KiB
PHP
Raw Normal View History

2020-03-29 19:57:18 +08:00
<?php
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
{
/**
* 发送消息
* @param $sendId 发送者
* @param $receveId 接收者
* @param $data 发送数据
* @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为用户写入用户收件箱
if($data['type'] == 1){
$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
/**
* 接收消息
* @param $uid 接收消息用户ID
* @return \think\Collection
* @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)
{
$msg = Db::name('message_to')
->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'])
2020-03-29 19:57:18 +08:00
->select();
return $msg;
}
2020-10-19 17:23:55 +08:00
/**
* 登录用户把消息写入数据表
* @param $uid 登陆用户ID
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
2020-03-31 23:01:00 +08:00
public static function insertMsg($uid)
{
//得到所有系统消息
$sysmsg = MessageModel::where(['type'=>0,'delete_time'=>0])->select();
foreach($sysmsg as $smg){
2020-10-19 17:23:55 +08:00
//检验通知ID是否被写入个人收件箱
2020-03-31 23:01:00 +08:00
$msgId = Db::name('message_to')->where('message_id',$smg['id'])->find();
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
}