捕梦者基础框架API接口
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

248 lines
7.3 KiB

<?php
declare (strict_types=1);
namespace app\api\model;
use app\index\model\User;
use think\Db;
use think\Model;
/**
* @mixin think\Model
*/
class Message extends Common
{
protected $table = 'system_message'; //如cloud_开头不需要
/**
* 字典列表数据模型
* @return array|\think\Collection
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function index()
{
//自定义处理逻辑
$title = $this->request->param('title', '');
$_op = $this->where('is_init',1);
//通用逻辑
return parent::parentLists($_op);
}
public function getUserMessage($type){
$_op = $this->where('receve_id',UID);
if($type == 99){
//获取未读系统消息总数
$gg_count = $this->where('receve_id',UID)->where('type',1)->where('read_status',0)->count();
//获取未读私信消息总数
$sx_count = $this->where('receve_id',UID)->where('type',0)->where('read_status',0)->count();
//获取通知类型最新5条消息
$gg_list = $this->where('receve_id',UID)->where('type',1)->where('read_status',0)->order('create_time desc,id desc')->limit(5)->select()->toArray();
//获取
$sx_list = $this->where('receve_id',UID)->where('type',0)->where('read_status',0)->order('create_time desc,id desc')->limit(5)->select()->toArray();
$reData = [];
$reData['gg']['count'] = $gg_count;
$reData['gg']['list'] = $gg_list;
$reData['sx']['count'] = $sx_count;
$reData['sx']['list'] = $sx_list;
return $reData;
}else if($type != 98){
$_op->where('type',$type);
}
$_op->order('read_status asc,create_time desc');
return parent::parentLists($_op);
}
/**
* 读取后
* @param Model $model
* @return bool|void
*/
public static function onAfterRead(Model $model)
{
//读取数据前格式化数据
if (in_array(request()->action(), ['edit', 'delete', 'status'])) {
return true;
}
$content_id = $model->getAttr('content_id');
$receve_id = $model->getAttr('receve_id');
$send_id = $model->getAttr('send_id');
if($content_id){
$c_model = new MessageContent();
$contents = $c_model->find($content_id);
$model->setAttr('title',$contents['title']);
$model->setAttr('content',$contents['content']);
$model->setAttr('pub_time',$contents['create_time']);
}
$u_model = new Manager();
if($receve_id>0){
$receve_user = $u_model->find($receve_id)->toArray();
if($receve_user){
$model->setAttr('receve_user_str',$receve_user['truename']);
}else{
$model->setAttr('receve_user_str','');
}
}else{
$model->setAttr('receve_user_str','所有人');
}
if($send_id>0){
$receve_user = $u_model->find($send_id)->toArray();
if($receve_user){
$model->setAttr('send_user_str',$receve_user['truename']);
$model->setAttr('avatar_str',$receve_user['avatar']);
}else{
$model->setAttr('send_user_str','');
}
}else{
$model->setAttr('send_user_str','');
}
// $model->setAttr('create_time_str',date('Y-m-d H:i:s',$model->getAttr('create_time')));
$model->setAttr('read_status_str',$model->getAttr('create_time')==1?'已读':'未读');
//业务逻辑
}
/**
* 新增或修改前
* @param Model $model
* @return mixed|void
*/
public static function onBeforeWrite(Model $model)
{
parent::onBeforeWrite($model);
//写入数据前格式化数据
if (!in_array(request()->action(), ['add', 'edit'])) {
return true;
}
$title = $model->getAttr('title');
$content = $model->getAttr('content');
$message_content = new MessageContent();
$ms_content['title'] = $title;
$ms_content['content'] = $content;
if(request()->action() == 'add'){
if($title && $content){
$content_id = $message_content->parentAdd($ms_content);
$model->setAttr('content_id',$content_id);
}
}else{
$c_id = $model->getAttr('content_id');
if($title && $content){
$message_content->where('id',$c_id)->save($ms_content);
}
}
$receve_id = $model->getAttr('receve_id');
$type = $receve_id<0?1:0;
$model->setAttr('send_id',UID);
$model->setAttr('type',$type);
$model->hidden(['title','content']);
//业务逻辑
// code...
}
public static function onAfterDelete(Model $model)
{
parent::onAfterDelete($model); // TODO: Change the autogenerated stub
}
public static function onAfterWrite(Model $model)
{
parent::onAfterWrite($model); // TODO: Change the autogenerated stub
}
/*
* 查看用户是否有待插入的公告
* */
public function setUserMessage($uid=0,$site_id=0){
if(!$uid){
$uid = UID;
}
if(!$site_id){
$site_id = SITE_ID;
}
//获取用户最近一份公告的时间
$last_gg = $this->withTrashed()->where('receve_id',$uid)->where('type',1)->order('create_time desc')->find();
$start_time = 0;
if($last_gg){
$last_gg = $last_gg->toArray();
$start_time = strtotime($last_gg['create_time']);
}
//查询所有该用户没有的公告
$no_ggs = $this->where('site_id','in',[1,$site_id])->where('type',1)->where('create_time','>',$start_time)->order('create_time asc')->select()->toArray();
//var_dump($no_ggs);die;
foreach ($no_ggs as $item){
$model = new Message();
$gg_item = [];
$gg_item['send_id'] = $item['send_id'];
$gg_item['site_id'] = $site_id;
$gg_item['receve_id'] = $uid;
$gg_item['content_id'] = $item['content_id'];
$gg_item['is_init'] = 0;
$gg_item['type'] = 1;
$model->save($gg_item);
}
return true;
}
public function setMessageRead($ids,$type){
$_where = [
['site_id', '=', SITE_ID],
['receve_id','=',UID]
];
if($ids == -1){
if($type==0 || $type==1){
$_where[] = ['type','=',$type];
}
}else{
$ids = explode(',', $ids);
$_where[] = ['id', 'in', $ids];
}
$affected = self::update(['read_status' => 1], $_where);
return $affected;
}
public function setMessageDelete($ids){
$ids = explode(',', $ids);
if($ids){
$_op = $this->where('id', 'in', $ids)->where('receve_id',UID)->where('site_id', SITE_ID)->select();
if ($_op->isEmpty()) {
return send_http_status('', 101);
}
return self::destroy($ids); //TP软删除
}else{
return false;
}
}
}