Im.php 47 KB

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