Im.php 48 KB

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