Im.php 48 KB

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