Files
cunkebao_v3/Server/application/chukebao/controller/MessageController.php
2025-09-25 17:52:56 +08:00

150 lines
5.1 KiB
PHP

<?php
namespace app\chukebao\controller;
use library\ResponseHelper;
use think\Db;
class MessageController extends BaseController
{
public function getList(){
$page = $this->request->param('page', 1);
$limit = $this->request->param('limit', 10);
$accountId = $this->getUserInfo('s2_accountId');
if (empty($accountId)){
return ResponseHelper::error('请先登录');
}
$chatroomList = Db::table('s2_wechat_chatroom')->alias('wc')
->join(['s2_wechat_message' => 'm'], 'wc.id = m.wechatChatroomId', 'LEFT')
->where(['wc.accountId' => $accountId,'m.type' => 2,' wc.isDeleted' => 0])
->order('m.id desc')
->group('m.wechatChatroomId')
->page($page, $limit)
->select();
$friendIds = Db::table('s2_wechat_friend')
->where(['accountId' => $accountId,' isDeleted' => 0])
->group('id')
->column('id');
$friendList = Db::table('s2_wechat_message')
->where(['type' => 1])
->whereIn('wechatFriendId',$friendIds)
->order('id desc')
->group('wechatFriendId')
->page($page, $limit)
->select();
$list = array_merge($chatroomList,$friendList);
// 按createTime字段从大到小排序
usort($list, function($a, $b) {
return $b['createTime'] <=> $a['createTime'];
});
foreach ($list as $k=>&$v){
$v['createTime'] = !empty($v['createTime']) ? date('Y-m-d H:i:s',$v['createTime']) : '';
$v['wechatTime'] = !empty($v['wechatTime']) ? date('Y-m-d H:i:s',$v['wechatTime']) : '';
if (!empty($v['wechatFriendId'])){
$friend = Db::table('s2_wechat_friend')
->where(['id'=>$v['wechatFriendId']])
->field('id,nickname,avatar')
->find();
$v['msgInfo'] = $friend;
$v['unreadCount'] = Db::table('s2_wechat_message')
->where(['wechatFriendId' => $v['wechatFriendId'],'isRead' => 0])
->count();
}
if (!empty($v['wechatChatroomId'])){
$chatroom = Db::table('s2_wechat_chatroom')
->where(['id'=>$v['wechatChatroomId']])
->field('id,nickname,chatroomAvatar as avatar')
->find();
$v['msgInfo'] = $chatroom;
$v['unreadCount'] = Db::table('s2_wechat_message')
->where(['wechatChatroomId' => $v['wechatChatroomId'],'isRead' => 0])
->count();
}
}
unset($v);
return ResponseHelper::success($list);
}
public function readMessage(){
$wechatFriendId = $this->request->param('wechatFriendId', '');
$wechatChatroomId = $this->request->param('wechatChatroomId', '');
$accountId = $this->getUserInfo('s2_accountId');
if (empty($accountId)){
return ResponseHelper::error('请先登录');
}
if (empty($wechatChatroomId) && empty($wechatFriendId)){
return ResponseHelper::error('参数缺失');
}
$where = [];
if (!empty($wechatChatroomId)){
$where[] = ['wechatChatroomId','=',$wechatChatroomId];
}
if (!empty($wechatFriendId)){
$where[] = ['wechatFriendId','=',$wechatFriendId];
}
Db::table('s2_wechat_message')->where($where)->update(['isRead' => 1]);
return ResponseHelper::success([]);
}
public function details(){
$wechatFriendId = $this->request->param('wechatFriendId', '');
$wechatChatroomId = $this->request->param('wechatChatroomId', '');
$wechatAccountId = $this->request->param('wechatAccountId', '');
$page = $this->request->param('page', 1);
$limit = $this->request->param('limit', 10);
$from = $this->request->param('From', '');
$to = $this->request->param('To', '');
$olderData = $this->request->param('olderData', false);
$accountId = $this->getUserInfo('s2_accountId');
if (empty($accountId)){
return ResponseHelper::error('请先登录');
}
if (empty($wechatChatroomId) && empty($wechatFriendId)){
return ResponseHelper::error('参数缺失');
}
$where = [];
if (!empty($wechatChatroomId)){
$where[] = ['wechatChatroomId','=',$wechatChatroomId];
}
if (!empty($wechatFriendId)){
$where[] = ['wechatFriendId','=',$wechatFriendId];
}
if (!empty($From) && !empty($To)){
$where[] = ['wechatTime','between',[$from,$to]];
}
$total = Db::table('s2_wechat_message')->where($where)->count();
$list = Db::table('s2_wechat_message')->where($where)->page($page,$limit)->order('id DESC')->select();
foreach ($list as $k=>&$v){
$v['wechatTime'] = !empty($v['wechatTime']) ? date('Y-m-d H:i:s',$v['wechatTime']) : '';
}
return ResponseHelper::success(['total'=>$total,'list'=>$list]);
}
}