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.
		
		
		
		
		
			
		
			
				
					
					
						
							50 lines
						
					
					
						
							1.3 KiB
						
					
					
				
			
		
		
		
			
			
			
				
					
				
				
					
				
			
		
		
	
	
							50 lines
						
					
					
						
							1.3 KiB
						
					
					
				
								<?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::where('agent_id', $this->agent_id)
							 | 
						|
											->whereIn('user_id', [$this->user_id, 0]) //0是所有人都接收的系统消息
							 | 
						|
											->select('id', 'user_id', 'title', 'created_at')
							 | 
						|
											->orderBy('id', 'DESC')
							 | 
						|
											->simplePaginate(15);
							 | 
						|
										if (!empty($list->items())) {
							 | 
						|
											foreach ($list->items() as &$v) {
							 | 
						|
												$v->is_read = (int)MessageRead::where(['id' => $v->id, 'user_id' => $v->user_id])->exists();
							 | 
						|
											}
							 | 
						|
										}
							 | 
						|
										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);
							 | 
						|
									}
							 | 
						|
								}
							 |