TaoLer/app/index/controller/Message.php

97 lines
2.5 KiB
PHP
Raw Normal View History

2020-01-01 13:17:19 +08:00
<?php
2022-08-02 21:13:36 +08:00
/*
* @Author: TaoLer <317927823@qq.com>
* @Date: 2021-12-06 16:04:50
* @LastEditTime: 2022-07-27 21:29:43
* @LastEditors: TaoLer
* @Description: 优化版
* @FilePath: \github\TaoLer\app\index\controller\Message.php
* Copyright (c) 2020~2022 https://www.aieok.com All rights reserved.
*/
2020-01-01 13:17:19 +08:00
namespace app\index\controller;
use app\common\controller\BaseController;
use think\facade\Session;
use think\facade\Request;
2020-03-28 21:41:07 +08:00
use think\facade\Db;
2020-03-31 23:01:00 +08:00
use app\common\model\Message as MessageModel;
use app\common\model\MessageTo;
2020-03-29 19:57:18 +08:00
use taoler\com\Message as MessageApi;
2020-01-01 13:17:19 +08:00
class Message extends BaseController
{
2020-03-28 21:41:07 +08:00
//消息数目
public function nums()
{
2022-08-02 21:13:36 +08:00
$messgeto = new MessageTo();
$num = $messgeto->getMsgNum($this->uid);
if(!is_null($num)){
$res = ['status' =>0,'count' => $num, 'msg' => 'ok'];
2020-03-28 21:41:07 +08:00
} else {
2022-08-02 21:13:36 +08:00
$res = ['status' =>-1,'count' => 0, 'msg' => 'message error'];
2020-03-28 21:41:07 +08:00
}
return json($res);
}
//消息查询
public function find()
{
2022-08-02 21:13:36 +08:00
$msg = MessageApi::receveMsg($this->uid);
2020-03-28 21:41:07 +08:00
$count = $msg->count();
$res = [];
if($count){
$res = ['status'=>0,'msg'=>'','count'=>$count];
foreach ($msg as $k => $v){
2020-03-31 23:01:00 +08:00
$data = ['id'=>$v['id'],'name'=>$v['name'],'title'=>$v['title'],'content'=>$v['content'],'time'=>date("Y-m-d H:i",$v['create_time']),'link'=>$v['link'],'read'=>$v['is_read'] ? '已读':'未读','type'=>$v['message_type']];
2020-03-28 21:41:07 +08:00
$res['rows'][] = $data;
}
} else {
2022-08-02 21:13:36 +08:00
$res = ['status'=>-1,'msg'=>'message find error','rows'=>''];;
2020-03-28 21:41:07 +08:00
}
2020-03-31 23:01:00 +08:00
//var_dump($res);
2020-03-28 21:41:07 +08:00
return json($res);
}
//读消息
public function read()
{
$id =input('id');
2020-04-01 12:18:43 +08:00
if($id){
$msg = MessageTo::field('id,message_id')->with(['messages' => function($query){
2020-03-31 23:01:00 +08:00
$query->where('delete_time',0)->field('id,content');
}])->where('id',$id)->find();
2020-04-01 12:18:43 +08:00
//改变读状态
2020-04-05 12:52:29 +08:00
if($msg->is_read == 0){
$result = $msg->update(['id'=>$id,'is_read'=>1]);
if($result){
$res=['status' =>0,'content'=>$msg['messages']['content']];
return json($res);
}
2020-04-01 12:18:43 +08:00
}
} else {
2022-08-02 21:13:36 +08:00
return json(['status' =>0, 'url'=>(string) url('User/message')]);
2020-04-05 12:52:29 +08:00
}
2020-03-28 21:41:07 +08:00
}
//消息删除
public function remove()
{
2020-10-19 17:23:55 +08:00
$uid = $this->uid;
2020-04-05 12:52:29 +08:00
2020-03-28 21:41:07 +08:00
$id = Request::only(['id']);
if($id['id'] == 'true'){
2020-04-05 12:52:29 +08:00
//删除所有此用户消息
2020-10-19 17:23:55 +08:00
$msg = Db::name('message_to')->where(['receve_id'=>$uid,'delete_time'=>0])->useSoftDelete('delete_time',time())->delete();
2020-03-28 21:41:07 +08:00
} else {
2020-04-05 12:52:29 +08:00
//删除单条消息
2020-10-19 17:23:55 +08:00
$msg = Db::name('message_to')->where('id',$id['id'])->useSoftDelete('delete_time',time())->delete();
2020-03-28 21:41:07 +08:00
}
if($msg){
$res = ['status'=>0];
}
return $res;
2020-01-01 13:17:19 +08:00
}
}