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.
|
|
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;use App\Models\Message;use App\Models\MessageRead;use Illuminate\Http\Request;
/** * 短消息 * Class MessageController * @package App\Http\Controllers\Api */class MessageController extends Controller{ public function index() { $list = Message::query()->where('agent_id', $this->agent_id) ->whereIn('user_id', [$this->user_id, 0]) //0是所有人都接收的系统消息
->select('id', 'title', 'created_at') ->selectRaw('IF(( SELECT 1 FROM `message_reads` AS `mr` WHERE mr.id = messages.id AND mr.user_id = messages.user_id ), 1, 0) AS `is_read`') ->orderBy('id', 'DESC') ->simplePaginate(15);
return $this->success($list); }
public function show() { $id = (int)request()->input('id');
//增加where条件是为了防止偷看别人的消息
$message = Message::where(['agent_id' => $this->agent_id]) ->whereIn('user_id', [$this->user_id, 0]) //0是所有人都接收的系统消息
->find($id);
if (!$message) { return $this->error('消息不存在或已删除'); }
//标记短消息为已读
MessageRead::insertOrIgnore(['id' => $id, 'user_id' => $this->user_id]);
return $this->success($message); }}
|