Im.php 48 KB

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