Im.php 48 KB

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