Im.php 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270
  1. <?php
  2. namespace app\enterprise\controller;
  3. use app\BaseController;
  4. use think\facade\Session;
  5. use think\facade\Db;
  6. use app\enterprise\model\{User, Message, GroupUser, Friend,Group,ChatDelog};
  7. use GatewayClient\Gateway;
  8. use Exception;
  9. use League\Flysystem\Util;
  10. use think\facade\Cache;
  11. use app\manage\model\{Config};
  12. use app\admin\model\GuessAskLanguages;
  13. use app\admin\model\QuestionLanguages;
  14. use app\admin\model\Complaint;
  15. use app\admin\model\ComplaintItem;
  16. use app\admin\model\Score;
  17. class Im extends BaseController
  18. {
  19. protected $fileType = ['file', 'image','video','voice','emoji'];
  20. /**
  21. * 给客服打分
  22. */
  23. public function Score()
  24. {
  25. $score = $this->request->param('score');
  26. $msg_id = $this->request->param('msg_id');
  27. if (empty($msg_id)) {
  28. return $this->error('参数有误');
  29. }
  30. if (empty($score)) {
  31. return $this->error('请打分');
  32. }
  33. $user_id = $this->userInfo['user_id'];
  34. $user = User::where('user_id', $user_id)->find();
  35. if (!$user) {
  36. return $this->error('用户不存在');
  37. }
  38. $exist = Message::where('msg_id', $msg_id)->where('to_user', $user_id)->find();
  39. if ($exist && $exist['is_click'] == 0) {
  40. Score::create([
  41. 'user_id' => $user_id,
  42. 'cs_uid' => $user->cs_uid,
  43. 'score' => (int)$score,
  44. ]);
  45. $exist->is_click = 1;
  46. $exist->save();
  47. }
  48. return $this->success('提交成功');
  49. }
  50. /**
  51. */
  52. public function complaintList()
  53. {
  54. try {
  55. $params = $this->request->param();
  56. $page = $params['page'] ?? 1;
  57. $limit = $params['limit'] ?? 15;
  58. $language_code = $params['language_code'] ?? $this->lang;
  59. $query = ComplaintItem::where('language_code', $language_code);
  60. $count = $query->count();
  61. $list = $query->order('weight','desc')
  62. ->limit($limit)
  63. ->page($page)
  64. ->select();
  65. } catch (Exception $e) {
  66. return $this->error($e->getMessage());
  67. }
  68. return $this->success(['count' => $count, 'list' => $list]);
  69. }
  70. /**
  71. * 投诉客服
  72. */
  73. public function complaint()
  74. {
  75. $params = $this->request->param();
  76. if (empty($params['complaint_item_id'])) {
  77. return $this->error('请选择投诉项');
  78. }
  79. $user_id = $this->userInfo['user_id'];
  80. $user = User::where('user_id', $user_id)->find();
  81. if (!$user) {
  82. return $this->error('用户不存在');
  83. }
  84. $exist = Complaint::where('user_id', $user_id)->where('cs_uid', $user->cs_uid)->find();
  85. if ($exist) {
  86. return $this->error('您已投诉过该客服,无需重复投诉');
  87. }
  88. Complaint::create([
  89. 'user_id' => $user_id,
  90. 'cs_uid' => $user->cs_uid,
  91. 'complaint_item_id' => $params['complaint_item_id'],
  92. 'remark' => $params['remark'] ?? '',
  93. 'language_code' => $this->lang,
  94. ]);
  95. $data = $this->request->param();
  96. $data['user_id'] = $user['id'];
  97. $data['status'] = 0;
  98. $data['type'] = 1;
  99. Db::name('complaint')->insert($data);
  100. return $this->success('提交成功');
  101. }
  102. /**
  103. * 猜你想问列表
  104. */
  105. public function guessask()
  106. {
  107. try {
  108. $params = $this->request->param();
  109. $page = $params['page'] ?? 1;
  110. $limit = $params['limit'] ?? 15;
  111. $language_code = $this->lang;
  112. $query = GuessAskLanguages::where('language_code', $language_code)->where('status', 1);
  113. if (!empty($params['keyword'])) {
  114. $query = $query->where('name', 'like', '%'.$params['keyword'].'%');
  115. }
  116. if (isset($params['type']) && $params['type'] != '') {
  117. $query = $query->where('type', $params['type']);
  118. }
  119. $count = $query->count();
  120. $list = $query->order('is_top','desc')
  121. ->order('is_rec','desc')
  122. ->order('click_num','desc')
  123. ->limit($limit)
  124. ->page($page)
  125. ->select();
  126. } catch (Exception $e) {
  127. return $this->error($e->getMessage());
  128. }
  129. return $this->success([ 'count' => $count, 'list' => $list]);
  130. }
  131. /**
  132. * 猜你想问点击使用
  133. */
  134. function guessaskClick()
  135. {
  136. try {
  137. $id = $this->request->param('id');
  138. $info = GuessAskLanguages::where('id', $id)->where('status', 1)->find();
  139. if ($info) {
  140. $info->click_num = $info->click_num + 1;
  141. $info->save();
  142. }
  143. } catch (Exception $e) {
  144. return $this->error($e->getMessage());
  145. }
  146. return $this->success([]);
  147. }
  148. /**
  149. * 问题列表
  150. */
  151. function question()
  152. {
  153. try {
  154. $params = $this->request->param();
  155. $page = $params['page'] ?? 1;
  156. $limit = $params['limit'] ?? 15;
  157. $language_code = $this->lang;
  158. $query = QuestionLanguages::where('language_code', $language_code)->where('status', 1);
  159. if (isset($params['question_type']) && $params['question_type'] != '') {
  160. $query = $query->where('question_type', $params['question_type']);
  161. }
  162. $count = $query->count();
  163. $list = $query->order('weight','desc')
  164. ->limit($limit)
  165. ->page($page)
  166. ->select();
  167. } catch (Exception $e) {
  168. return error($e->getMessage());
  169. }
  170. return $this->success([ 'count' => $count, 'list' => $list]);
  171. }
  172. /**
  173. * 问题点赞、点否
  174. */
  175. function questionClick()
  176. {
  177. try {
  178. $is_like = $this->request->param('is_like');
  179. $msg_id = $this->request->param('msg_id');
  180. if (empty($msg_id) || !in_array($is_like, [1, 0])) {
  181. return $this->error('参数有误');
  182. }
  183. $user_id = $this->userInfo['user_id'];
  184. $exist = Message::where('msg_id', $msg_id)->where('to_user', $user_id)->find();
  185. if ($exist && $exist['is_click'] == 0) {
  186. $extends = $exist['extends'];
  187. $id = $extends['question_id'] ?? 0;
  188. if ($id) {
  189. if ($is_like == 1) {
  190. QuestionLanguages::where('id', $id)->update(['like_count'=>Db::raw('like_count+1')]);
  191. } else {
  192. QuestionLanguages::where('id', $id)->update(['dislike_count'=>Db::raw('dislike_count+1')]);
  193. }
  194. }
  195. $exist->is_click = 1;
  196. $exist->save();
  197. }
  198. } catch (Exception $e) {
  199. return $this->error($e->getMessage());
  200. }
  201. return $this->success([]);
  202. }
  203. /**
  204. * 转人工客服
  205. */
  206. function transferHuman()
  207. {
  208. try {
  209. $user_id = $this->userInfo['user_id'];
  210. $user = User::where('user_id', $user_id)->find();
  211. if (!$user) {
  212. return $this->error('用户不存在');
  213. }
  214. if ($user->role > 0) {
  215. return $this->error('系统管理员无法转人工');
  216. }
  217. // ($user->service_status == -1) {
  218. $user->service_status = 0;
  219. $user->service_start = time();
  220. $user->cs_uid = 0;
  221. $user->save();
  222. //机器人客服
  223. $autoTask = Config::autoTask();
  224. $param = [
  225. 'id' => \utils\Str::getUuid(),
  226. 'type' => 'text',
  227. 'status' => 'going',
  228. 'sendTime' => time() * 1000,
  229. 'toContactId' => $user_id,
  230. 'content' => Config::getFieldValue('transfer_to_human'),
  231. 'file_id' => 0,
  232. 'is_group' => 0,
  233. 'user_id' => !empty($autoTask['user_id']) ? $autoTask['user_id'] : 1,
  234. ];
  235. Message::sendMsg($param, 0);
  236. //通知客服用户排队中
  237. wsSendMsg(0,'handleChat',['user_id'=>$user_id]);
  238. //
  239. } catch (Exception $e) {
  240. return $this->error($e->getMessage());
  241. }
  242. return $this->success([]);
  243. }
  244. // 获取联系人列表
  245. public function getContacts()
  246. {
  247. $this->userInfo['cs_uid'] = User::where('user_id', $this->userInfo['user_id'])->value('cs_uid');
  248. $data = User::getUserList($this->userInfo, [['status', '=', 1], ['user_id', '<>', $this->userInfo['user_id']]], $this->userInfo['user_id'] , '', $this->userInfo['role']);
  249. $count=Friend::where(['status'=>2,'friend_user_id'=>$this->uid])->count();
  250. $time=Friend::where(['friend_user_id'=>$this->uid,'is_invite'=>1])->order('create_time desc')->value('create_time');
  251. return success('', $data,$count,$time*1000);
  252. }
  253. // 获取聊天列表
  254. public function getChatList()
  255. {
  256. $data = User::getChatList($this->userInfo['user_id']);
  257. $count=Friend::where(['status'=>2,'friend_user_id'=>$this->uid])->count();
  258. $time=Friend::where(['friend_user_id'=>$this->uid,'is_invite'=>1])->order('create_time desc')->value('create_time');
  259. return success('', $data,$count,$time*1000);
  260. }
  261. // 获取好友列表
  262. public function getFriendList()
  263. {
  264. $data = User::getFriendList([['status', '=', 1], ['user_id', '<>', $this->userInfo['user_id']]],$this->userInfo['user_id']);
  265. return success('', $data);
  266. }
  267. // 获取群聊列表
  268. public function getGroupList()
  269. {
  270. $data = User::getGroupList($this->userInfo['user_id']);
  271. return success('', $data);
  272. }
  273. // 获取单个人员的信息
  274. public function getContactInfo(){
  275. $id = $this->request->param('id');
  276. $is_group = is_string($id) ? 1 : 0;
  277. $user=new User;
  278. $data=$user->setContact($id,$is_group);
  279. if(!$data){
  280. return warning($user->getError());
  281. }
  282. return success('',$data);
  283. }
  284. //发送消息
  285. public function sendMessage()
  286. {
  287. $param = $this->request->param();
  288. $param['user_id'] = $this->userInfo['user_id'];
  289. $message=new Message();
  290. $data = $message->sendMessage($param,$this->globalConfig);
  291. if ($data) {
  292. return success('', $data);
  293. } else {
  294. return warning($message->getError());
  295. }
  296. }
  297. //转发消息
  298. public function forwardMessage()
  299. {
  300. $param = $this->request->param();
  301. $userIds=$param['user_ids'] ?? [];
  302. if(!$userIds || count($userIds)>5){
  303. return warning(lang('im.forwardLimit',['count'=>5]));
  304. }
  305. $msg_id=$param['msg_id'] ?? 0;
  306. $message=Message::find($msg_id);
  307. if(!$message){
  308. return warning(lang('im.exist'));
  309. }
  310. $msg=$message->toArray();
  311. $userInfo=$this->userInfo;
  312. queuePush([
  313. 'action'=>'forwardMessage',
  314. 'message'=>$msg,
  315. 'user_ids'=>$userIds,
  316. 'config'=>$this->globalConfig,
  317. 'userInfo'=>$userInfo
  318. ]);
  319. return success(lang('im.forwardOk'));
  320. }
  321. // 获取用户信息
  322. public function getUserInfo()
  323. {
  324. $user_id = $this->request->param('user_id');
  325. $group_id = $this->request->param('group_id');
  326. $groupInfo=[];
  327. if($group_id){
  328. $group_id = explode('-', $group_id)[1];
  329. $groupInfo=Group::where(['group_id'=>$group_id])->find();
  330. if($groupInfo){
  331. // 查询操作对象的角色
  332. $groupInfo['userInfo']=GroupUser::where(['user_id'=>$user_id,'group_id'=>$group_id])->find() ?: [];
  333. // 查询我的角色
  334. $groupInfo['manageRole']=GroupUser::where(['user_id'=>$this->userInfo['user_id'],'group_id'=>$group_id])->value('role') ?: 3;
  335. }
  336. }
  337. $user=User::find($user_id);
  338. if(!$user){
  339. return error(lang('user.exist'));
  340. }
  341. $user->avatar=avatarUrl($user->avatar,$user->realname,$user->user_id,120);
  342. // 账号前面截取3位,后面截取两位,中间星号展示
  343. $user->account=substr($user->account, 0, 3).'******'.substr($user->account, -2, 2);
  344. // 查询好友关系
  345. $friend=Friend::where(['friend_user_id'=>$user_id,'create_user'=>$this->userInfo['user_id'],'status'=>1])->find();
  346. $user->friend=$friend ? : '';
  347. $location='';
  348. if($user->last_login_ip){
  349. $location=implode(" ", \Ip::find($user->last_login_ip));
  350. }
  351. $user->location=$location;
  352. $user->groupInfo=$groupInfo ? : [];
  353. $user->password='';
  354. $userModel=new User;
  355. $user->contactInfo=$userModel->setContact($user_id,0) ?? [];
  356. return success('', $user);
  357. }
  358. // 扫码登录,向客户端推送数据
  359. public function tokenLogin(){
  360. $param = $this->request->param();
  361. $token=$param['token'] ?? '';
  362. $client_id='';
  363. if($token){
  364. $client_id=authcode(urldecode($token),"DECODE", config('app.app_id'));
  365. if(!$client_id){
  366. return warning(lang('user.loginError'));
  367. }
  368. }else{
  369. return warning(lang('user.loginError'));
  370. }
  371. $userInfo=$this->userInfo;
  372. if(!$userInfo){
  373. return warning(lang('user.loginError'));
  374. }
  375. // 如果用户已经有设置
  376. $setting=$userInfo['setting'] ?: '';
  377. if($setting){
  378. $setting['hideMessageName']= $setting['hideMessageName']=='true' ? true : false;
  379. $setting['hideMessageTime']= $setting['hideMessageTime']=='true' ? true : false;
  380. $setting['avatarCricle']= $setting['avatarCricle']=='true' ? true : false;
  381. $setting['isVoice']= $setting['isVoice']=='true' ? true : false;
  382. $setting['sendKey']=(int)$setting['sendKey'];
  383. }
  384. $userInfo['setting']=$setting;
  385. Gateway::$registerAddress = config('gateway.registerAddress');
  386. //如果登录信息中含有client——id则自动进行绑定
  387. if($client_id){
  388. $user_id=$userInfo['user_id'];
  389. // 如果当前ID在线,将其他地方登陆挤兑下线
  390. if(Gateway::isUidOnline($user_id)){
  391. wsSendMsg($user_id,'offline',['id'=>$user_id,'client_id'=>$client_id,'isMobile'=>false]);
  392. }
  393. Gateway::bindUid($client_id, $user_id);
  394. // 查询团队,如果有团队则加入团队
  395. $group=Group::getMyGroup(['gu.user_id'=>$user_id,'gu.status'=>1]);
  396. if($group){
  397. $group=$group->toArray();
  398. $group_ids=arrayToString($group,'group_id',false);
  399. foreach($group_ids as $v){
  400. Gateway::joinGroup($client_id, $v);
  401. }
  402. }
  403. }
  404. $update=[
  405. 'last_login_time'=>time(),
  406. 'last_login_ip'=>$this->request->ip(),
  407. 'login_count'=>Db::raw('login_count+1')
  408. ];
  409. User::where('user_id',$userInfo['user_id'])->update($update);
  410. $authToken=User::refreshToken($userInfo,'web');
  411. $data=[
  412. 'sessionId'=>Session::getId(),
  413. 'authToken'=>$authToken,
  414. 'userInfo'=>$userInfo
  415. ];
  416. Gateway::sendToClient($client_id, json_encode(array(
  417. 'type' => 'tokenLogin',
  418. 'data' => $data,
  419. )));
  420. return success(lang('user.loginOk'),$data);
  421. }
  422. // 搜索用户
  423. public function searchUser(){
  424. $keywords=$this->request->param('keywords','');
  425. if(!$keywords){
  426. return success('',[]);
  427. }
  428. $map=['status'=>1,'account'=>$keywords];
  429. $list=User::where($map)->field(User::$defaultField)->where([['account','<>',$this->userInfo['account']]])->select()->toArray();
  430. if($list){
  431. $ids=array_column($list,'user_id');
  432. $friendList=Friend::getFriend([['create_user','=',$this->uid],['friend_user_id','in',$ids]]);
  433. foreach($list as $k=>$v){
  434. $list[$k]['avatar']=avatarUrl($v['avatar'],$v['realname'],$v['user_id'],120);
  435. $list[$k]['friend']=$friendList[$v['user_id']] ?? '';
  436. }
  437. }
  438. return success('', $list);
  439. }
  440. // 获取系统所有人员加搜索
  441. public function userList(){
  442. $keywords=$this->request->param('keywords','');
  443. $listRows=$this->request->param('limit',20);
  444. $page=$this->request->param('page',1);
  445. $map=['status'=>1];
  446. $field="user_id,realname,avatar";
  447. if(!$keywords){
  448. $list=User::where($map)->field($field)->order('user_id asc')->limit(20)->paginate(['list_rows'=>$listRows,'page'=>$page]);;
  449. if($list){
  450. $list=$list->toArray()['data'];
  451. }
  452. }else{
  453. $list=User::where($map)->field($field)->where([['account','<>',$this->userInfo['account']]])->whereLike('account|realname|name_py','%'.$keywords.'%')->select()->toArray();
  454. }
  455. if($list){
  456. foreach($list as $k=>$v){
  457. $list[$k]['avatar']=avatarUrl($v['avatar'],$v['realname'],$v['user_id'],120);
  458. $list[$k]['id']=$v['user_id'];
  459. }
  460. }
  461. return success('', $list);
  462. }
  463. // 获取聊天记录
  464. public function getMessageList()
  465. {
  466. $param = $this->request->param();
  467. $is_group = isset($param['is_group']) ? $param['is_group'] : 0;
  468. // 如果toContactId是数字,绝对是单聊
  469. $is_group = is_numeric($param['toContactId']) ? 0 : $is_group;
  470. // 设置当前聊天消息为已读
  471. $chat_identify = $this->setIsRead($is_group, $param['toContactId']);
  472. $type = isset($param['type']) ? $param['type'] : '';
  473. $is_at = isset($param['is_at']) ? $param['is_at'] : '';
  474. $map = ['chat_identify' => $chat_identify, 'status' => 1];
  475. $where = [];
  476. if ($type && $type != "all") {
  477. $map['type'] = $type;
  478. } else {
  479. if (isset($param['type'])) {
  480. $where[] = ['type', '<>', 'event'];
  481. }
  482. }
  483. $groupManage=[];
  484. // 群聊查询入群时间以后的消息
  485. if($is_group==1){
  486. $group_id = explode('-', $param['toContactId'])[1];
  487. $group=Group::where(['group_id'=> $group_id])->find();
  488. $groupManage=GroupUser::getGroupManage($group_id);
  489. if($group && $group['setting']){
  490. $groupSetting=json_decode($group['setting'],true);
  491. $history=$groupSetting['history'] ?? false;
  492. // 如果开启了历史记录才可以查看所有记录,否者根据进群时间查询记录
  493. if(!$history){
  494. $createTime=GroupUser::where(['group_id'=> $group_id,'user_id'=>$this->userInfo['user_id']])->value('create_time');
  495. $where[] = ['create_time', '>=', $createTime ? : 0];
  496. }
  497. }
  498. }
  499. $keywords = isset($param['keywords']) ? $param['keywords'] : '';
  500. if ($keywords && in_array($type, ['text', 'all'])) {
  501. $where[] = ['content', 'like', '%' . $keywords . '%'];
  502. }
  503. // 如果是查询@数据
  504. if($is_at){
  505. $atList=Db::name('message')->where($map)->where($where)->whereFindInSet('at',$this->userInfo['user_id'])->order('msg_id desc')->select()->toArray();
  506. if($atList){
  507. $data = $this->recombileMsg($atList,false);
  508. Message::setAtread($data,$this->userInfo['user_id']);
  509. return success('', $data, count($data));
  510. }else{
  511. return success('', [], 0);
  512. }
  513. }
  514. $listRows = $param['limit'] ?: 20;
  515. $pageSize = $param['page'] ?: 1;
  516. $last_id = $param['last_id'] ?? 0;
  517. if($last_id){
  518. $where[]=['msg_id','<',$last_id];
  519. $pageSize=1;
  520. }
  521. //判断是客服还是用户
  522. if ($this->userInfo['role'] == 0) {
  523. //用户可见历史记录的时长
  524. $hours = Config::where('field', 'user_show_message')->value('val');
  525. if ($hours > 0) {
  526. $start_time = time() - $hours * 60 * 60;
  527. $where[] = ['create_time', '>=', $start_time];
  528. }
  529. } elseif ($this->userInfo['role'] == 3) {
  530. //客服可见历史记录的时长
  531. $hours = Config::where('field', 'kefu_show_message')->value('val');
  532. if ($hours > 0) {
  533. $start_time = time() - $hours * 60 * 60;
  534. $where[] = ['create_time', '>=', $start_time];
  535. }
  536. $toUser = User::where('user_id', $param['toContactId'])->find();
  537. if ($toUser && $toUser->service_status == 1) {
  538. //将用户待处理的状态更新成服务中
  539. $toUser->service_status = 2;
  540. $toUser->service_time = time();
  541. $toUser->save();
  542. //通知客服-会话服务中
  543. wsSendMsg(0,'handleChat',['user_id'=>$param['toContactId']]);
  544. }
  545. }
  546. $list = Message::getList($map, $where, 'msg_id desc', $listRows, $pageSize);
  547. $data = $this->recombileMsg($list,true,$groupManage);
  548. // 如果是群聊并且是第一页消息,需要推送@数据给用户
  549. if($param['is_group']==1 && $param['page']==1){
  550. $isPush=Cache::get('atMsgPush'.$chat_identify) ?? '';
  551. $atList=Db::name('message')->where(['chat_identify'=>$chat_identify,'is_group'=>1])->whereFindInSet('at',$this->userInfo['user_id'])->order('msg_id desc')->select()->toArray();
  552. $msgIda=array_column($atList,'msg_id');
  553. // 如果两次推送at数据的列表不一样,则推送
  554. if($isPush!=json_encode($msgIda)){
  555. $atData=$this->recombileMsg($atList,false);
  556. wsSendMsg($this->userInfo['user_id'],'atMsgList',[
  557. 'list'=>$atData,
  558. 'count'=>count($atData),
  559. 'toContactId'=>$param['toContactId']
  560. ]);
  561. Cache::set('atMsgPush'.$chat_identify,json_encode($msgIda),60);
  562. }
  563. }
  564. // 如果是消息管理器则不用倒序
  565. if (!isset($param['type'])) {
  566. $data = array_reverse($data);
  567. }
  568. return success('', $data, $list->total());
  569. }
  570. // 获取单条消息详情
  571. public function getMessageInfo()
  572. {
  573. $param = $this->request->param();
  574. $id = $param['msg_id'] ?? 0;
  575. $message = Message::where(['msg_id' => $id])->find();
  576. if ($message) {
  577. $data = $this->recombileMsg([$message], false);
  578. return success('', $data);
  579. } else {
  580. return warning(lang('im.exist'));
  581. }
  582. }
  583. // 获取单条消息上下文
  584. public function getMessageContext()
  585. {
  586. $param = $this->request->param();
  587. $is_group = isset($param['is_group']) ? $param['is_group'] : 0;
  588. $id = $param['msg_id'] ?? 0;
  589. $direction = $param['direction'] ?? 0;
  590. $message = Message::where(['msg_id' => $id])->find();
  591. if (!$message) {
  592. return warning(lang('im.exist'));
  593. }
  594. $groupManage=[];
  595. $where = [];
  596. $map = ['chat_identify' => $message['chat_identify'], 'status' => 1];
  597. if($is_group==1 && $direction<2){
  598. $group_id = $message['to_user'];
  599. $group=Group::where(['group_id'=> $group_id])->find();
  600. $groupManage=GroupUser::getGroupManage($group_id);
  601. if($group && $group['setting']){
  602. $groupSetting=json_decode($group['setting'],true);
  603. $history=$groupSetting['history'] ?? false;
  604. // 如果开启了历史记录才可以查看所有记录,否者根据进群时间查询记录
  605. if(!$history){
  606. $createTime=GroupUser::where(['group_id'=> $group_id,'user_id'=>$this->userInfo['user_id']])->value('create_time');
  607. $where[] = ['create_time', '>=', $createTime ? : 0];
  608. }
  609. }
  610. }
  611. if($direction==0){
  612. $where[] = ['msg_id', '<', $id];
  613. $beforeList = Message::where($map)->where($where)->order('msg_id desc')->limit(5)->select()->toArray();
  614. $beforeList = array_reverse($beforeList);
  615. $where2 = [];
  616. $where2[] = ['msg_id', '>=', $id];
  617. $afterList = Message::where($map)->where($where2)->order('msg_id asc')->limit(5)->select()->toArray();
  618. $data = array_merge($beforeList, $afterList);
  619. }elseif($direction==1){
  620. $where[] = ['msg_id', '<', $id];
  621. $data = Message::where($map)->where($where)->order('msg_id desc')->limit(5)->select()->toArray();
  622. $data = array_reverse($data);
  623. }else{
  624. $where[] = ['msg_id', '>', $id];
  625. $data = Message::where($map)->where($where)->order('msg_id asc')->limit(5)->select()->toArray();
  626. }
  627. if($data){
  628. $data = $this->recombileMsg($data, false,$groupManage);
  629. }
  630. return success('', $data);
  631. }
  632. protected function recombileMsg($list,$isPagination=true,$manage=[])
  633. {
  634. $data = [];
  635. $userInfo = $this->userInfo;
  636. if ($list) {
  637. $listData = $isPagination ? $list->toArray()['data'] : $list;
  638. $userList = User::matchUser($listData, true, 'from_user', 120);
  639. foreach ($listData as $k => $v) {
  640. // 屏蔽已删除的消息
  641. if ($v['del_user']) {
  642. $delUser = explode(',', $v['del_user']);
  643. if (in_array($userInfo['user_id'], $delUser)) {
  644. unset($list[$k]);
  645. continue;
  646. // $v['type']="event";
  647. // $v['content']="删除了一条消息";
  648. }
  649. }
  650. $content = str_encipher($v['content'],false);
  651. $preview = '';
  652. $ext='';
  653. if (in_array($v['type'], $this->fileType)) {
  654. $content = getFileUrl($content);
  655. $preview = previewUrl($content);
  656. $ext=getExtUrl($content);
  657. }
  658. $fromUser = $userList[$v['from_user']];
  659. // 处理撤回的消息
  660. if ($v['type'] == "event" && $v['is_undo']==1) {
  661. if ($v['from_user'] == $userInfo['user_id']) {
  662. $content = lang('im.you'). $content;
  663. } elseif ($v['is_group'] == 1) {
  664. $content = $fromUser['realname'] . $content;
  665. } else {
  666. $content = lang('im.other') . $content;
  667. }
  668. }
  669. $toContactId=$v['is_group'] ==1 ? 'group-'.$v['to_user'] : $v['to_user'];
  670. $atList=($v['at'] ?? null) ? explode(',',$v['at']): [];
  671. $role=$manage[$v['from_user']] ?? 3;
  672. $data[] = [
  673. 'msg_id' => $v['msg_id'],
  674. 'id' => $v['id'],
  675. 'status' => "succeed",
  676. 'type' => $v['type'],
  677. 'sendTime' => (is_string($v['create_time']) ? strtotime($v['create_time']) : $v['create_time'])* 1000,
  678. 'content' => $content,
  679. 'preview' => $preview,
  680. 'download' => $v['file_id'] ? getMainHost().'/filedown/'.encryptIds($v['file_id']) : '',
  681. 'is_read' => $v['is_read'],
  682. 'is_group' => $v['is_group'],
  683. 'at' => $atList,
  684. 'toContactId' => $toContactId,
  685. 'from_user' => $v['from_user'],
  686. 'file_id' => $v['file_id'],
  687. 'file_cate' => $v['file_cate'],
  688. 'fileName' => $v['file_name'],
  689. 'fileSize' => $v['file_size'],
  690. 'is_click' => $v['is_click'],
  691. 'fromUser' => $fromUser,
  692. 'extUrl'=>$ext,
  693. 'role'=>$role,
  694. 'extends'=>is_string($v['extends'])?json_decode($v['extends'],true) : $v['extends']
  695. ];
  696. }
  697. }
  698. return $data;
  699. }
  700. // 设置当前窗口的消息默认为已读
  701. public function setMsgIsRead()
  702. {
  703. $param = $this->request->param();
  704. // 判断是否是一个二维数组
  705. if (is_array($param['messages'][0] ?? '')) {
  706. $messages=$param['messages'];
  707. } else {
  708. $messages=[$param['messages']];
  709. }
  710. $this->setIsRead($param['is_group'], $param['toContactId'],$messages);
  711. if (!$param['is_group']) {
  712. wsSendMsg($param['fromUser'], 'isRead', $messages, 0);
  713. }
  714. return success('');
  715. }
  716. // 设置全部为已读
  717. public function readAllMsg()
  718. {
  719. // 阅读所有单聊
  720. $map = ['to_user' => $this->userInfo['user_id'], 'is_read' => 0, 'is_group' => 0];
  721. Message::where($map)->update(['is_read' => 1]);
  722. // 阅读所有群聊
  723. GroupUser::where(['user_id' => $this->userInfo['user_id'], 'status' => 1])->update(['unread'=>0]);
  724. return success('');
  725. }
  726. // 设置消息已读
  727. protected function setIsRead($is_group, $to_user,$messages=[])
  728. {
  729. if ($is_group==1) {
  730. $chat_identify = $to_user;
  731. } else if($is_group==0) {
  732. $chat_identify = chat_identify($this->userInfo['user_id'], $to_user);
  733. } else if($is_group==2){
  734. $chat_identify = $to_user;
  735. }
  736. $data=[
  737. 'action'=>'setIsRead',
  738. 'is_group'=>$is_group,
  739. 'to_user'=>$to_user,
  740. 'messages'=>$messages,
  741. 'user_id'=>$this->userInfo['user_id']
  742. ];
  743. queuePush($data,3);
  744. return $chat_identify;
  745. }
  746. // 聊天设置
  747. public function setting()
  748. {
  749. $param = $this->request->param();
  750. if ($param) {
  751. User::where(['user_id' => $this->userInfo['user_id']])->update(['setting' => $param]);
  752. return success('');
  753. }
  754. return warning('');
  755. }
  756. // 撤回消息
  757. public function undoMessage()
  758. {
  759. $param = $this->request->param();
  760. $id = $param['id'];
  761. $message = Message::where(['id' => $id])->find();
  762. if ($message) {
  763. // 如果时间超过了2分钟也不能撤回
  764. $createTime=is_string($message['create_time']) ? strtotime($message['create_time']) : $message['create_time'];
  765. $redoTime=$this->globalConfig['chatInfo']['redoTime'] ?? 120;
  766. if ($this->userInfo['role'] == 0 && $this->globalConfig['user_cancel_message'] && $this->globalConfig['user_cancel_message']) {
  767. $redoTime=$this->globalConfig['user_cancel_message_time'] * 60;
  768. } elseif ($this->userInfo['role'] != 0 && $this->globalConfig['kefu_cancel_message']) {
  769. $redoTime=$this->globalConfig['kefu_cancel_message'] * 60;
  770. }
  771. if(time()-$createTime>$redoTime && $message['is_group']==0){
  772. return warning(lang('im.redoLimitTime',['time'=>floor($redoTime/60)]));
  773. }
  774. $text = lang('im.redo');
  775. $fromUserName = lang('im.other');
  776. $toContactId = $message['to_user'];
  777. if ($message['is_group'] == 1) {
  778. $fromUserName = $this->userInfo['realname'];
  779. $toContactId = explode('-', $message['chat_identify'])[1];
  780. // 如果是群聊消息撤回,需要判断是否是群主或者管理员,如果是则可以撤回
  781. if($message['from_user']!=$this->userInfo['user_id']){
  782. $groupUser=GroupUser::where(['user_id'=>$this->userInfo['user_id'],'group_id'=>$toContactId])->find();
  783. if(!$groupUser || !in_array($groupUser['role'],[1,2])){
  784. return warning(lang('system.notAuth'));
  785. }
  786. $text=lang('im.manageRedo');
  787. }
  788. }
  789. $message->content = str_encipher($text);
  790. $message->type = 'event';
  791. $message->is_undo = 1;
  792. //@的数据清空
  793. $message->at = '';
  794. $message->save();
  795. $info = $message->toArray();
  796. // $data = $info;
  797. $data['content'] = $fromUserName . $text;
  798. $data['sendTime'] = $createTime * 1000;
  799. $data['id'] = $info['id'];
  800. $data['from_user'] = $info['from_user'];
  801. $data['msg_id'] = $info['msg_id'];
  802. $data['status'] = $info['status'];
  803. $data['type'] = 'event';
  804. $data['is_last'] = $info['is_last'];
  805. $data['toContactId'] = $message['is_group'] == 1 ? $info['chat_identify'] : $toContactId;
  806. $data['isMobile'] = $this->request->isMobile() ? 1 : 0;
  807. wsSendMsg($toContactId, 'undoMessage', $data, $info['is_group']);
  808. if($info['is_group']==0){
  809. // 给自己也发一份推送,多端同步
  810. $data['content'] =lang('im.you'). $text;
  811. wsSendMsg($this->userInfo['user_id'], 'undoMessage', $data, $info['is_group']);
  812. }
  813. return success('');
  814. } else {
  815. return warning();
  816. }
  817. }
  818. // 删除消息
  819. public function removeMessage()
  820. {
  821. $param = $this->request->param();
  822. $id = $param['id'];
  823. $map = ['id' => $id];
  824. $message = Message::where($map)->find();
  825. if ($message) {
  826. $message->del_user = $this->userInfo['user_id'];
  827. if ($message['is_group'] == 1) {
  828. if ($message['del_user']) {
  829. $message->del_user .= ',' . $this->userInfo['user_id'];
  830. }
  831. } else {
  832. if ($message['del_user'] > 0) {
  833. $message->where($map)->delete();
  834. return success(lang('system.delOk'));
  835. }
  836. }
  837. $message->save();
  838. return success('');
  839. } else {
  840. return warning('');
  841. }
  842. }
  843. // 消息免打扰
  844. public function isNotice()
  845. {
  846. $param = $this->request->param();
  847. $user_id = $this->userInfo['user_id'];
  848. $id = $param['id'];
  849. if ($param['is_group'] == 1) {
  850. $group_id = explode('-', $param['id'])[1];
  851. GroupUser::update(['is_notice' => $param['is_notice']], ['user_id' => $user_id, 'group_id' => $group_id]);
  852. } else {
  853. $map = ['create_user' => $user_id, 'friend_user_id' => $id];
  854. $friend = Friend::where($map)->find();
  855. try {
  856. if ($friend) {
  857. $friend->is_notice = $param['is_notice'];
  858. $friend->save();
  859. } else {
  860. $info = [
  861. 'create_user' => $user_id,
  862. 'friend_user_id' => $id,
  863. 'is_notice' => $param['is_notice']
  864. ];
  865. Friend::create($info);
  866. }
  867. return success('');
  868. } catch (Exception $e) {
  869. return error($e->getMessage());
  870. }
  871. }
  872. wsSendMsg($user_id,"setIsNotice",['id'=>$id,'is_notice'=>$param['is_notice'],'is_group'=>$param['is_group']]);
  873. return success('');
  874. }
  875. // 设置聊天置顶
  876. public function setChatTop()
  877. {
  878. $param = $this->request->param();
  879. $user_id = $this->userInfo['user_id'];
  880. $is_group = $param['is_group'] ?: 0;
  881. $id = $param['id'];
  882. try {
  883. if ($is_group == 1) {
  884. $group_id = explode('-', $param['id'])[1];
  885. GroupUser::update(['is_top' => $param['is_top']], ['user_id' => $user_id, 'group_id' => $group_id]);
  886. } else {
  887. $map = ['create_user' => $user_id, 'friend_user_id' => $id];
  888. $friend = Friend::where($map)->find();
  889. if ($friend) {
  890. $friend->is_top = $param['is_top'];
  891. $friend->save();
  892. } else {
  893. $info = [
  894. 'create_user' => $user_id,
  895. 'friend_user_id' => $id,
  896. 'is_top' => $param['is_top']
  897. ];
  898. Friend::create($info);
  899. }
  900. }
  901. wsSendMsg($user_id,"setChatTop",['id'=>$id,'is_top'=>$param['is_top'],'is_group'=>$is_group]);
  902. return success('');
  903. } catch (Exception $e) {
  904. return error($e->getMessage());
  905. }
  906. }
  907. // 删除聊天
  908. public function delChat()
  909. {
  910. $param = $this->request->param();
  911. $user_id = $this->userInfo['user_id'];
  912. $is_group = $param['is_group'] ?: 0;
  913. $id = $param['id'];
  914. $data=[
  915. 'user_id'=>$user_id,
  916. 'is_group'=>$is_group,
  917. 'to_user'=>$id
  918. ];
  919. ChatDelog::create($data);
  920. ChatDelog::updateCache($user_id);
  921. return success('');
  922. }
  923. // 向用户发送消息
  924. public function sendToMsg(){
  925. $param=$this->request->param();
  926. $toContactId=$param['toContactId'];
  927. $type=$param['type'];
  928. $status=$param['status'];
  929. $event=$param['event'] ?? 'calling';
  930. if($event=='calling'){
  931. $status=3;
  932. }
  933. $sdp=$param['sdp'] ?? '';
  934. $iceCandidate=$param['iceCandidate'] ?? '';
  935. $callTime=$param['callTime'] ?? '';
  936. $msg_id=$param['msg_id'] ?? '';
  937. $id=$param['id'] ?? '';
  938. $code=($param['code'] ?? '') ?: 901;
  939. // 如果该用户不在线,则发送忙线
  940. Gateway::$registerAddress = config('gateway.registerAddress');
  941. if(!Gateway::isUidOnline($toContactId)){
  942. $toContactId=$this->userInfo['user_id'];
  943. $code=907;
  944. $event='busy';
  945. sleep(1);
  946. }
  947. switch($code){
  948. case 902:
  949. $content=lang('webRtc.cancel');
  950. break;
  951. case 903:
  952. $content=lang('webRtc.refuse');
  953. break;
  954. case 905:
  955. $content=lang('webRtc.notConnected');
  956. break;
  957. case 906:
  958. $content=lang('webRtc.duration',['time'=>date("i:s",$callTime)]);
  959. break;
  960. case 907:
  961. $content=lang('webRtc.busy');
  962. break;
  963. case 908:
  964. $content=lang('webRtc.other');
  965. break;
  966. default:
  967. $content=$type==1 ?lang('webRtc.video') : lang('webRtc.audio');
  968. break;
  969. }
  970. switch($event){
  971. case 'calling':
  972. $content=$type==1 ?lang('webRtc.video'): lang('webRtc.audio');
  973. break;
  974. case 'acceptRtc':
  975. $content=lang('webRtc.answer');
  976. break;
  977. case 'iceCandidate':
  978. $content=lang('webRtc.exchange');
  979. break;
  980. }
  981. $userInfo=$this->userInfo;
  982. $userInfo['id']=$userInfo['user_id'];
  983. $user = new User();
  984. $data=[
  985. 'id'=>$id,
  986. 'msg_id'=>$msg_id,
  987. 'sendTime'=>time()*1000,
  988. 'toContactId'=>$toContactId,
  989. 'content'=>$content,
  990. 'type'=>'webrtc',
  991. 'status'=>'succeed',
  992. 'is_group'=>0,
  993. 'is_read'=>0,
  994. 'fromUser'=>$userInfo,
  995. 'at'=>[],
  996. 'extends'=>[
  997. 'type'=>$type, //通话类型,1视频,0语音。
  998. 'status'=>$status, //,1拨打方,2接听方
  999. 'event'=>$event,
  1000. 'callTime'=>$callTime,
  1001. 'sdp'=>$sdp,
  1002. 'code'=>$code, //通话状态:呼叫901,取消902,拒绝903,接听904,未接通905,接通后挂断906,忙线907,其他端操作908
  1003. 'iceCandidate'=>$iceCandidate,
  1004. 'isMobile'=>$this->request->isMobile() ? 1 : 0,
  1005. ]
  1006. ];
  1007. if($event=='calling'){
  1008. $chat_identify=chat_identify($userInfo['id'],$toContactId);
  1009. $msg=[
  1010. 'from_user'=>$userInfo['id'],
  1011. 'to_user'=>$toContactId,
  1012. 'id'=>$id,
  1013. 'content'=>str_encipher($content),
  1014. 'chat_identify'=>$chat_identify,
  1015. 'create_time'=>time(),
  1016. 'type'=>$data['type'],
  1017. 'is_group'=>0,
  1018. 'is_read'=>0,
  1019. 'extends'=>$data['extends'],
  1020. ];
  1021. $message=new Message();
  1022. $message->update(['is_last'=>0],['chat_identify'=>$chat_identify]);
  1023. $message->save($msg);
  1024. $msg_id=$message->msg_id;
  1025. $data['msg_id']=$msg_id;
  1026. // 将接收人设置为发送人才能定位到该消息
  1027. $data['toContactId']=$userInfo['id'];
  1028. $data['toUser']=$toContactId;
  1029. }elseif($event=='hangup'){
  1030. $message=Message::where(['id'=>$id])->find();
  1031. if(!$message){
  1032. return error(lang('webRtc.fail'));
  1033. }
  1034. if($message){
  1035. $message->content=str_encipher($content);
  1036. $extends=$message->extends;
  1037. $extends['code']=$code;
  1038. $extends['callTime']=$callTime;
  1039. $message->extends=$extends;
  1040. $message->save();
  1041. }
  1042. }
  1043. wsSendMsg($toContactId,'webrtc',$data);
  1044. $wsData=$data;
  1045. if(in_array($event,['calling','acceptRtc','hangup'])){
  1046. if(in_array($event,['acceptRtc','hangup'])){
  1047. $data['extends']['event']='otherOpt'; //其他端操作
  1048. }
  1049. $data['toContactId']=$toContactId;
  1050. $data['contactInfo']=$user->setContact($toContactId,0,'webrtc',$content) ? : [];
  1051. wsSendMsg($userInfo['id'],'webrtc',$data);
  1052. }
  1053. return success('',$wsData);
  1054. }
  1055. // 修改密码
  1056. public function editPassword()
  1057. {
  1058. if(env('app.demon_mode',false)){
  1059. return warning(lang('system.demoMode'));
  1060. }
  1061. $user_id = $this->userInfo['user_id'];
  1062. $user=User::find($user_id);
  1063. if(!$user){
  1064. return warning(lang('user.exist'));
  1065. }
  1066. $account=$user->account;
  1067. $code=$this->request->param('code','');
  1068. $originalPassword = $this->request->param('originalPassword', '');
  1069. if($code){
  1070. if(Cache::get($account)!=$code){
  1071. return warning(lang('user.codeErr'));
  1072. }
  1073. }elseif($originalPassword){
  1074. if(password_hash_tp($originalPassword,$user->salt)!= $user->password){
  1075. return warning(lang('user.passErr'));
  1076. }
  1077. }else{
  1078. return warning(lang('system.parameterError'));
  1079. }
  1080. try{
  1081. $password = $this->request->param('password','');
  1082. if($password){
  1083. $salt=$user->salt;
  1084. $user->password= password_hash_tp($password,$salt);
  1085. }
  1086. $user->save();
  1087. return success(lang('system.editOk'));
  1088. }catch (\Exception $e){
  1089. return error(lang('system.editFail'));
  1090. }
  1091. }
  1092. // 修改用户信息
  1093. public function updateUserInfo(){
  1094. try{
  1095. $data = $this->request->param();
  1096. $user=User::find($this->uid);
  1097. if(!$user){
  1098. return warning(lang('user.exist'));
  1099. }
  1100. // 接入用户名检测服务
  1101. event('GreenText',['content'=>$data['realname'],'service'=>"nickname_detection"]);
  1102. // 个性签名检测服务
  1103. event('GreenText',['content'=>$data['motto'],'service'=>"comment_detection"]);
  1104. $user->realname =$data['realname'];
  1105. $user->email =$data['email'];
  1106. $user->motto=$data['motto'];
  1107. $user->sex =$data['sex'];
  1108. $user->name_py= pinyin_sentence($data['realname']);
  1109. $user->save();
  1110. return success(lang('system.editOk'), $data);
  1111. }catch (\Exception $e){
  1112. return error($e->getMessage());
  1113. }
  1114. }
  1115. // 修改账户
  1116. public function editAccount(){
  1117. if(env('app.demon_mode',false)){
  1118. return warning(lang('system.demoMode'));
  1119. }
  1120. $code=$this->request->param('code','');
  1121. $newCode=$this->request->param('newCode','');
  1122. $account=$this->request->param('account','');
  1123. $isUser=User::where('account',$account)->find();
  1124. if($isUser){
  1125. return warning(lang('user.already'));
  1126. }
  1127. $user=User::find($this->uid);
  1128. if(!$user){
  1129. return warning(lang('user.exist'));
  1130. }
  1131. // 如果已经认证过了,则需要验证验证码
  1132. if($user->is_auth){
  1133. if(Cache::get($user->account)!=$code){
  1134. return warning(lang('user.codeErr'));
  1135. }
  1136. }
  1137. if(Cache::get($account)!=$newCode){
  1138. return warning(lang('user.newCodeErr'));
  1139. }
  1140. try{
  1141. $user->account=$account;
  1142. $user->is_auth=1;
  1143. $user->save();
  1144. return success(lang('system.editOk'));
  1145. }catch (\Exception $e){
  1146. return error(lang('system.editFail'));
  1147. }
  1148. }
  1149. // 阅读@消息
  1150. public function readAtMsg(){
  1151. $param = $this->request->param();
  1152. $atList=Db::name('message')->where(['chat_identify'=>$param['toContactId'],'is_group'=>1])->whereFindInSet('at',$this->userInfo['user_id'])->order('msg_id desc')->select();
  1153. $atData=$this->recombileMsg($atList,false);
  1154. Message::setAtRead($atData,$this->userInfo['user_id']);
  1155. // $message=Message::where('msg_id',$param['msg_id'])->select();
  1156. // $atList=($message ?? null) ? explode(',',$message): [];
  1157. // // 两个数组取差集
  1158. // $newAtList = array_diff($atList, [$this->userInfo['user_id']]);
  1159. // Message::where('msg_id',$param['msg_id'])->update(['at'=>implode(',',$newAtList)]);
  1160. return success('');
  1161. }
  1162. // 获取系统公告
  1163. public function getAdminNotice(){
  1164. $data=Message::where(['chat_identify'=>'admin_notice'])->order('msg_id desc')->find();
  1165. $extends=$data['extends'] ?? [];
  1166. if(!$extends){
  1167. $extends['title']='';
  1168. }
  1169. $createTime=$data['create_time'] ?? 0;
  1170. if(!$createTime){
  1171. $extends['create_time']=$createTime;
  1172. }else{
  1173. $extends['create_time']=is_string($data['create_time']) ? strtotime($data['create_time']) : $data['create_time'];
  1174. }
  1175. return success('',$extends);
  1176. }
  1177. // 双向删除消息
  1178. public function delMessage(){
  1179. $param = $this->request->param();
  1180. $id = $param['id'];
  1181. if(!$this->globalConfig['chatInfo']['dbDelMsg']){
  1182. return warning(lang('system.notAuth'));
  1183. }
  1184. $message = Message::where(['id' => $id])->find();
  1185. if ($message) {
  1186. if($message['from_user']!=$this->userInfo['user_id']){
  1187. return warning(lang('system.notAuth'));
  1188. }
  1189. Message::where(['id' => $id])->delete();
  1190. // 如果是最后一条消息,需要将上一条设置为最后一条
  1191. if($message['is_last']){
  1192. Message::where(['chat_identify'=>$message['chat_identify']])->order('msg_id desc')->limit(1)->update(['is_last'=>1]);
  1193. }
  1194. $toContactId = $message['to_user'];
  1195. if ($message['is_group'] == 1) {
  1196. $toContactId = explode('-', $message['chat_identify'])[1];
  1197. }
  1198. wsSendMsg($toContactId, 'delMessage', $message, $message['is_group']);
  1199. return success('');
  1200. } else {
  1201. return warning(lang('im.exist'));
  1202. }
  1203. }
  1204. }