Im.php 46 KB

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