Im.php 47 KB

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