145 lines
4.3 KiB
PHP
145 lines
4.3 KiB
PHP
<?php
|
|
|
|
namespace app\common\service;
|
|
|
|
use app\common\model\ChatFriend;
|
|
use app\common\model\ChatRecords;
|
|
use app\common\model\Photographer;
|
|
use app\constant\ResponseCode;
|
|
use think\Exception;
|
|
|
|
class ChatRecordsService
|
|
{
|
|
|
|
|
|
/**
|
|
* @desc 发送消息
|
|
* @param $request
|
|
* @return array
|
|
*/
|
|
public function sendMsg($request)
|
|
{
|
|
try {
|
|
|
|
$data = $request->post();
|
|
//记录聊天数据
|
|
$res = ChatRecords::create([
|
|
'from_user_id' => $request->user->id,
|
|
'to_user_id' => $data['user_id'],
|
|
'type' => $data['type'],
|
|
'content' => $data['content'],
|
|
]);
|
|
|
|
//判断是否是好友
|
|
$friend = ChatFriend::where(['user_id' => $request->user->id, 'friend_id' => $data['user_id']])->findOrEmpty();
|
|
if ($friend->isEmpty()) {
|
|
$friend_photographer = Photographer::where(['user_id' => $data['user_id']])->findOrEmpty();
|
|
$friend_photographer_id = 0;
|
|
if (!$friend_photographer->isEmpty()) {
|
|
$friend_photographer_id = $friend_photographer->id;
|
|
}
|
|
ChatFriend::create([
|
|
'user_id' => $request->user->id,
|
|
'friend_id' => $data['user_id'],
|
|
'photographer_id' => $friend_photographer_id
|
|
]);
|
|
$photographer = Photographer::where(['user_id' => $data['user_id']])->findOrEmpty();
|
|
$photographer_id = 0;
|
|
if (!$photographer->isEmpty()) {
|
|
$photographer_id = $photographer->id;
|
|
}
|
|
ChatFriend::create([
|
|
'user_id' => $data['user_id'],
|
|
'friend_id' => $request->user->id,
|
|
'photographer_id' => $photographer_id
|
|
]);
|
|
}
|
|
|
|
return [
|
|
'code' => ResponseCode::SUCCESS,
|
|
'msg' => '发送成功'
|
|
];
|
|
} catch (Exception $e) {
|
|
return [
|
|
'code' => ResponseCode::FAIL,
|
|
'msg' => $e->getMessage()
|
|
];
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* @desc 获取用户发送的消息
|
|
* @param $request
|
|
* @return array
|
|
*/
|
|
public function getUserChatMsg($request)
|
|
{
|
|
try {
|
|
$data = $request->get();
|
|
$chat_msg = ChatRecords::whereRaw('(from_user_id = ? and to_user_id = ?) or (from_user_id = ? and to_user_id = ?)', [
|
|
$request->user->id,
|
|
$data['user_id'],
|
|
$data['user_id'],
|
|
$request->user->id
|
|
])
|
|
->field('from_user_id,to_user_id,type,content, created_at')
|
|
->order('created_at desc');
|
|
|
|
$page = isset($data['page']) ? $data['page'] : 1;
|
|
$limit = isset($data['limit']) ? $data['limit'] : 10;
|
|
|
|
$total = $chat_msg->count();
|
|
$list = $chat_msg->page($page, $limit)->select()->toArray();
|
|
|
|
foreach ($list as &$item) {
|
|
if($item['from_user_id'] == $request->user->id){
|
|
$item['is_i_send'] = 1;
|
|
}else{
|
|
$item['is_i_send'] = 0;
|
|
}
|
|
}
|
|
|
|
return [
|
|
'code' => ResponseCode::SUCCESS,
|
|
'data' => [
|
|
'list' => $list,
|
|
'total' => $total,
|
|
'page' => $page
|
|
]
|
|
];
|
|
} catch (Exception $e) {
|
|
return [
|
|
'code' => ResponseCode::FAIL,
|
|
'msg' => $e->getMessage()
|
|
];
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* @desc 标记消息已读
|
|
* @param $request
|
|
* @return array
|
|
*/
|
|
public function updateMsgRead($request)
|
|
{
|
|
try {
|
|
|
|
$data = $request->post();
|
|
ChatRecords::where(['from_user_id' => $data['from_user_id'], 'to_user_id' => $request->user->id, 'status' => 0])->update(['status' => 1]);
|
|
|
|
return [
|
|
'code' => ResponseCode::SUCCESS,
|
|
'msg' => '已读',
|
|
];
|
|
} catch (Exception $e) {
|
|
return [
|
|
'code' => ResponseCode::FAIL,
|
|
'msg' => $e->getMessage()
|
|
];
|
|
}
|
|
}
|
|
|
|
|
|
} |