MessageService.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services\Api;
  12. use App\Models\ImChatModel;
  13. use App\Models\MerchantModel;
  14. use App\Models\MessageModel;
  15. use App\Services\BaseService;
  16. use App\Services\ConfigService;
  17. use App\Services\RedisService;
  18. /**
  19. * 站内消息服务管理-服务类
  20. * @author laravel开发员
  21. * @since 2020/11/11
  22. * Class MessageService
  23. * @package App\Services\Api
  24. */
  25. class MessageService extends BaseService
  26. {
  27. // 静态对象
  28. protected static $instance = null;
  29. /**
  30. * 构造函数
  31. * @author laravel开发员
  32. * @since 2020/11/11
  33. * MessageService constructor.
  34. */
  35. public function __construct()
  36. {
  37. $this->model = new MessageModel();
  38. }
  39. /**
  40. * 静态入口
  41. * @return static|null
  42. */
  43. public static function make()
  44. {
  45. if (!self::$instance) {
  46. self::$instance = (new static());
  47. }
  48. return self::$instance;
  49. }
  50. /**
  51. * 消息列表
  52. * @param $userId
  53. * @param $params
  54. * @param int $pageSize
  55. * @return array
  56. */
  57. public function getDataList($userId, $params, $pageSize = 0)
  58. {
  59. $page = request()->post('page', 1);
  60. $cacheKey = "caches:messages:history_{$page}_" . md5($userId . json_encode($params) . $pageSize);
  61. $datas = RedisService::get($cacheKey);
  62. if ($datas) {
  63. return $datas;
  64. }
  65. $where = ['a.status' => 1, 'a.mark' => 1];
  66. $field = ['a.id', 'a.title', 'a.type', 'a.msg_type', 'a.chat_type', 'a.description', 'a.content', 'a.from_user_name', 'a.from_user_avatar', 'a.from_uid', 'a.to_user_name', 'a.to_user_avatar', 'a.to_uid', 'a.create_time', 'a.is_read', 'a.pages', 'a.status'];
  67. $datas = $this->model->from('message as a')
  68. ->leftJoin('member as b', 'b.id', '=', 'a.from_uid')
  69. ->leftJoin('member as c', 'c.id', '=', 'a.to_uid')
  70. ->where($where)
  71. ->where(function ($query) use ($params, $userId) {
  72. $fromUid = isset($params['from_uid']) ? intval($params['from_uid']) : 0;
  73. if ($fromUid) {
  74. $query->where('a.from_uid', $fromUid);
  75. }
  76. $type = isset($params['type']) ? intval($params['type']) : 0;
  77. if ($type) {
  78. $query->where('a.type', $type);
  79. }
  80. if ($type != 9) {
  81. $query->where('a.to_uid', $userId);
  82. }
  83. $chatType = isset($params['chat_type']) ? intval($params['chat_type']) : 0;
  84. if ($chatType) {
  85. $query->where('a.chat_type', $chatType);
  86. }
  87. $chatKey = isset($params['chat_key']) ? trim($params['chat_key']) : '';
  88. if ($chatKey) {
  89. $query->where('a.chat_key', $chatKey);
  90. }
  91. })->select($field)
  92. ->orderBy('a.create_time', 'desc')
  93. ->orderBy('a.id', 'desc')
  94. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  95. $datas = $datas ? $datas->toArray() : [];
  96. if ($datas) {
  97. $ids = [];
  98. foreach ($datas['data'] as &$item) {
  99. $ids[] = $item['id'];
  100. $item['time_text'] = isset($item['create_time']) && $item['create_time'] ? dateForWeek($item['create_time']) : '';
  101. $item['content'] = $item['msg_type'] == 2 ? get_images_preview(json_decode($item['content'], true), '') : $item['content'];
  102. $item['from_user_avatar'] = $item['from_user_avatar'] ? get_image_url($item['from_user_avatar']) : get_image_url('/images/member/logo.png');
  103. $item['to_user_avatar'] = $item['to_user_avatar'] ? get_image_url($item['to_user_avatar']) : get_image_url('/images/member/logo.png');
  104. }
  105. unset($item);
  106. // 更新已读
  107. if ($ids) {
  108. $this->model->whereIn('id', $ids)->update(['is_read' => 1, 'update_time' => time()]);
  109. }
  110. arsort($datas['data'], true);
  111. $datas['data'] = array_reverse($datas['data'], false);
  112. RedisService::set($cacheKey, $datas, rand(3, 5));
  113. }
  114. return [
  115. 'list' => isset($datas['data']) ? $datas['data'] : [],
  116. 'total' => isset($datas['total']) ? $datas['total'] : 0,
  117. 'pageSize' => $pageSize
  118. ];
  119. }
  120. /**
  121. * 获取站内消息窗口列表
  122. * @param $userId
  123. * @return array|mixed
  124. */
  125. public function getGroupList($userId, $cache = false)
  126. {
  127. $cachekey = "caches:message:topList_{$userId}";
  128. $datas = RedisService::get($cachekey);
  129. if ($datas && $cache) {
  130. $datas['cache'] = true;
  131. return $datas;
  132. }
  133. $types = [1, 4, 5];
  134. $setting = MemberSettingService::make()->getSetting($userId);
  135. $pushStatus = false;
  136. if ($setting) {
  137. foreach ($setting as $k => $v) {
  138. if ($v == 1) {
  139. if ($k == 'receive_app') {
  140. $pushStatus = true;
  141. } else if ($k == 'receive_order') {
  142. $types[] = 2;
  143. } else if ($k == 'receive_account') {
  144. $types[] = 3;
  145. }
  146. }
  147. }
  148. asort($types);
  149. } else {
  150. $pushStatus = true;
  151. $types = [1, 2, 3, 4, 5];
  152. }
  153. // 关闭系统APP消息
  154. if (!$pushStatus) {
  155. return ['list' => [], 'total' => 0, 'types' => []];
  156. }
  157. $field = ['title', 'type', 'to_uid', 'description', 'is_read', 'create_time'];
  158. $datas = $this->model->where(['to_uid' => $userId, 'status' => 1, 'mark' => 1])
  159. ->whereIn('type', $types)
  160. ->groupBy('type')
  161. ->orderBy('type', 'asc')
  162. ->orderBy('is_read', 'desc')
  163. ->orderBy('create_time', 'desc')
  164. ->select($field)
  165. ->get();
  166. $datas = $datas ? $datas->toArray() : [];
  167. $total = 0;
  168. if ($datas) {
  169. $titles = [1 => '公告通知', 2 => '订单通知', 3 => '账户通知', 4 => '客服通知', 5 => '互动消息'];
  170. foreach ($datas as &$item) {
  171. $item['title'] = isset($titles[$item['type']]) ? $titles[$item['type']] : '消息通知';
  172. $data = $this->getNewMessage($item['type'], 0, $userId);
  173. $item['description'] = isset($data['description']) && $data['description'] ? $data['description'] : (isset($data['content']) ? mb_substr($data['content'], 0, 56, 'utf-8') . '...' : lang('有新消息'));
  174. $item['create_time'] = isset($data['create_time']) && $data['create_time'] ? $data['create_time'] : '';
  175. $item['time_text'] = isset($item['create_time']) && $item['create_time'] ? dateFormat($item['create_time']) : '';
  176. $item['create_time'] = isset($item['create_time']) ? datetime($item['create_time'], 'Y-m-d H.i.s') : '';
  177. $unread = $this->getUnreadCount($userId, 0, $item['type']);
  178. $item['unread'] = intval($unread);
  179. $total += intval($unread);
  180. }
  181. RedisService::set($cachekey, ['list' => $datas, 'total' => $total, 'types' => $types], rand(2, 3));
  182. }
  183. return ['list' => $datas, 'total' => $total, 'types' => $types];
  184. }
  185. /**
  186. * 聊天分组消息
  187. * @param $userId
  188. * @param $params
  189. * @param int $pageSize
  190. * @return array
  191. */
  192. public function getDataListFromatKey($userId, $params, $pageSize = 0)
  193. {
  194. $page = request()->post('page', 1);
  195. $cacheKey = "caches:m_chat:{$page}_" . md5($userId . json_encode($params) . $pageSize);
  196. $datas = RedisService::get($cacheKey);
  197. $data = isset($datas['data']) ? $datas['data'] : [];
  198. if ($datas && $data) {
  199. return [
  200. 'unread' => isset($datas['unReadCount']) ? $datas['unReadCount'] : 0,
  201. 'total' => isset($datas['total']) ? $datas['total'] : 0,
  202. 'list' => $data,
  203. 'pageSize' => $pageSize,
  204. 'cache' => true,
  205. ];
  206. }
  207. // 不接收则不显示聊天消息
  208. $receiveCustom = MemberSettingService::make()->getSetting($userId, 'receive_custom', 1);
  209. if ($receiveCustom != 1) {
  210. return [
  211. 'unread' => 0,
  212. 'total' => 0,
  213. 'list' => [],
  214. 'pageSize' => $pageSize,
  215. 'close' => true,
  216. ];
  217. }
  218. $where = ['a.type' => 9, 'a.status' => 1, 'a.mark' => 1];
  219. $expire = ConfigService::make()->getConfigByCode('chat_log_expire');
  220. $expire = $expire ? $expire * 86400 : 60 * 86400;
  221. $field = ['a.id', 'a.title', 'a.type', 'a.msg_type', 'a.chat_key', 'a.chat_type', 'a.description', 'a.content', 'a.from_user_name', 'a.from_user_avatar', 'a.from_uid', 'a.to_user_name', 'a.to_user_avatar', 'a.to_uid', 'a.create_time', 'a.is_read', 'a.status'];
  222. $datas = $this->model->from('message as a')
  223. ->where($where)
  224. ->where('a.chat_key', '>', 0)
  225. ->where('a.create_time', '>=', time() - $expire)
  226. ->where(function ($query) use ($params, $userId) {
  227. $chatKey = isset($params['chat_key']) ? trim($params['chat_key']) : '';
  228. if ($chatKey) {
  229. $query->where('a.chat_key', $chatKey);
  230. }
  231. $isRead = isset($params['is_read']) ? intval($params['is_read']) : 0;
  232. if ($isRead) {
  233. $query->where('a.is_read', $isRead);
  234. }
  235. $chatType = isset($params['chat_type']) ? intval($params['chat_type']) : 0;
  236. if ($chatType) {
  237. $query->where('a.chat_type', $chatType);
  238. }
  239. if ($userId) {
  240. $query->where(function ($query) use ($userId) {
  241. $query->where(['a.from_uid' => $userId])->orWhere(['a.to_uid' => $userId]);
  242. });
  243. }
  244. })
  245. ->select($field)
  246. ->groupBy('chat_key')
  247. ->orderBy('a.create_time', 'desc')
  248. ->orderBy('a.id', 'desc')
  249. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  250. $datas = $datas ? $datas->toArray() : [];
  251. $unReadCount = 0;
  252. if ($datas) {
  253. foreach ($datas['data'] as &$item) {
  254. $item['from_user_avatar'] = isset($item['from_user_avatar']) && $item['from_user_avatar'] ? get_image_url($item['from_user_avatar']) : get_image_url('/images/member/logo.png');
  255. $item['to_user_avatar'] = isset($item['to_user_avatar']) && $item['to_user_avatar'] ? get_image_url($item['to_user_avatar']) : get_image_url('/images/member/logo.png');
  256. $data = $this->getNewMessage(0, $item['chat_key']);
  257. $item['description'] = isset($data['description']) && $data['description'] ? $data['description'] : (isset($data['content']) ? mb_substr($data['content'], 0, 30, 'utf-8') : lang('有新消息'));
  258. $item['create_time'] = isset($data['create_time']) && $data['create_time'] ? $data['create_time'] : '';
  259. $item['time_text'] = isset($item['create_time']) && $item['create_time'] ? dateFormat($item['create_time']) : '';
  260. $item['create_time'] = isset($item['create_time']) ? datetime($item['create_time'], 'Y-m-d H.i.s') : '';
  261. $item['unread'] = $this->getUnreadCount($userId, $item['chat_key'], 0);
  262. if ($item['from_uid'] == $userId) {
  263. $item['from_user_name'] = $item['to_user_name'];
  264. $item['from_user_avatar'] = $item['to_user_avatar'];
  265. $item['tuid'] = $item['to_uid'];
  266. } else {
  267. $item['tuid'] = $item['from_uid'];
  268. }
  269. $unReadCount += intval($item['unread']);
  270. }
  271. unset($item);
  272. $datas['unReadCount'] = $unReadCount;
  273. RedisService::set($cacheKey, $datas, rand(3, 5));
  274. }
  275. return [
  276. 'unread' => $unReadCount,
  277. 'total' => isset($datas['total']) ? $datas['total'] : 0,
  278. 'list' => isset($datas['data']) ? $datas['data'] : [],
  279. 'pageSize' => $pageSize,
  280. 'cache' => false,
  281. ];
  282. }
  283. /**
  284. * 获取最新消息
  285. * @param $chatKey
  286. * @return mixed
  287. */
  288. public function getNewMessage($type = 0, $chatKey = 0, $userId = 0)
  289. {
  290. $cacheKey = "caches:messages:new_{$type}_{$chatKey}_{$userId}";
  291. $data = RedisService::get($cacheKey);
  292. if ($data) {
  293. return $data;
  294. }
  295. $where = ['status' => 1, 'mark' => 1];
  296. if ($type) {
  297. $where['type'] = $type;
  298. }
  299. if ($chatKey) {
  300. $where['chat_key'] = $chatKey;
  301. }
  302. if ($userId) {
  303. $where['to_uid'] = $userId;
  304. }
  305. $data = $this->model->where($where)->select('id', 'title', 'description', 'msg_type', 'content', 'create_time')
  306. ->orderBy('create_time', 'desc')
  307. ->orderBy('id', 'desc')
  308. ->first();
  309. $data = $data ? $data->toArray() : [];
  310. if ($data) {
  311. if ($data['msg_type'] == 2) {
  312. $data['description'] = '[图片]';
  313. } else if ($data['msg_type'] == 3) {
  314. $data['description'] = '[视频聊天]';
  315. } else if ($data['msg_type'] == 4) {
  316. $data['description'] = '[直播间分享]';
  317. }
  318. RedisService::set($cacheKey, $data, rand(3, 5));
  319. }
  320. return $data;
  321. }
  322. /**
  323. * 获取未读消息数量
  324. * @param $userId
  325. * @return array|mixed
  326. */
  327. public function getBarCount($userId, $type = 0)
  328. {
  329. $cacheKey = "caches:barCount:{$userId}_{$type}";
  330. $data = RedisService::get($cacheKey);
  331. if ($data) {
  332. return $data;
  333. }
  334. $data = $this->getUnreadCount($userId, 0, $type);
  335. RedisService::set($cacheKey, $data, rand(3, 5));
  336. return $data;
  337. }
  338. /**
  339. * 推送站内消息
  340. * @param $userId 用户ID
  341. * @param $title 标题
  342. * @param $message 消息内容
  343. * @param int $type 类型:1-公告,2-订单,3-账户,4-客服,5-动态
  344. * @return false
  345. */
  346. public function pushMessage($userId, $title, $message, $type = 1, $fromUid = 0)
  347. {
  348. $types = [4, 5];
  349. $pushStatus = false;
  350. $datas = MemberSettingService::make()->getSetting($userId);
  351. if ($datas) {
  352. foreach ($datas as $k => $v) {
  353. if ($v == 1) {
  354. if ($k == 'receive_app') {
  355. $pushStatus = true;
  356. $types[] = 1;
  357. } else if ($k == 'receive_order') {
  358. $types[] = 2;
  359. } else if ($k == 'receive_account') {
  360. $types[] = 3;
  361. }
  362. }
  363. }
  364. } else {
  365. $pushStatus = true;
  366. $types = [1, 2, 3, 4, 5];
  367. }
  368. if ($userId && $pushStatus && in_array($type, $types)) {
  369. $appName = ConfigService::make()->getConfigByCode('app_name', '星链社交');
  370. $log = [
  371. 'from_uid' => $fromUid,
  372. 'to_uid' => $userId,
  373. 'from_user_name' => $appName,
  374. 'chat_type' => 0,
  375. 'msg_type' => 1,
  376. 'type' => $type,
  377. 'title' => $title,
  378. 'description' => $title,
  379. 'content' => $message,
  380. 'chat_key' => "0{$userId}",
  381. 'create_time' => time(),
  382. 'status' => 1,
  383. 'mark' => 1,
  384. ];
  385. return MessageModel::insert($log);
  386. }
  387. return false;
  388. }
  389. /**
  390. * 获取
  391. * @param $userId
  392. * @return array|mixed
  393. */
  394. public function getUnreadCount($userId, $chatKey = 0, $type = 0)
  395. {
  396. $cacheKey = "caches:messages:un_{$userId}_{$chatKey}_{$type}";
  397. $data = RedisService::get($cacheKey);
  398. if (RedisService::exists($cacheKey)) {
  399. return $data;
  400. }
  401. $where = ['to_uid' => $userId, 'status' => 1, 'is_read' => 2, 'mark' => 1];
  402. if ($type > 0) {
  403. $where['type'] = $type;
  404. }
  405. if ($chatKey) {
  406. $where['chat_key'] = $chatKey;
  407. }
  408. $data = $this->model->where($where)->count('id');
  409. RedisService::set($cacheKey, $data, rand(3, 5));
  410. return $data;
  411. }
  412. /**
  413. * 验证发送消息或者内容是否有屏蔽词
  414. * @param $message 消息或内容
  415. * @param $type 是否返回屏蔽后内容:1-是
  416. * @return bool
  417. */
  418. public function filterMessage($message, $type = 1)
  419. {
  420. $filter = ConfigService::make()->getConfigByCode('chat_sensitive');
  421. $filter = !empty($filter) ? explode('、', $filter) : [];
  422. $filter = array_filter($filter);
  423. if ($filter) {
  424. if ($type != 2) {
  425. foreach ($filter as $kw) {
  426. $message = preg_replace("/{$kw}/", '***', $message);
  427. }
  428. } else {
  429. foreach ($filter as $kw) {
  430. if (preg_match("/{$kw}/", $message)) {
  431. return false;
  432. }
  433. }
  434. }
  435. // 手机号、邮箱、网址
  436. if ($type == 1) {
  437. $message = preg_replace("/^(1[3-9][0-9]{9})$/", '***', $message);
  438. $message = preg_replace("/([a-z0-9&\-_.]+@[\w\-_]+([\w\-.]+)?\.[\w\-]+)/is", '***', $message);
  439. $message = str_replace(['https:', 'http:', '.com', '.cn', '.net', '.top', 'www.'], '***', $message);
  440. $message = preg_replace("/([a-zA-Z0-9][a-zA-Z0-9\_]{6,20})/", '***', $message);
  441. $message = preg_replace("/\*{3}\*{1,}/", '***', $message);
  442. }
  443. }
  444. return $type == 3 && $message === '***' ? '' : $message;
  445. }
  446. /**
  447. * 已读
  448. * @param $userId 用户ID
  449. * @param $chatKey 聊天窗口ID
  450. * @return bool
  451. */
  452. public function setRead($userId, $type = 0, $chatKey = '')
  453. {
  454. $where = ['to_uid' => $userId];
  455. if ($type) {
  456. $where['type'] = $type;
  457. }
  458. if ($chatKey) {
  459. $where['chat_key'] = $chatKey;
  460. }
  461. $this->model->where($where)->update(['is_read' => 1, 'update_time' => time()]);
  462. // 清除缓存
  463. RedisService::keyDel("caches:messages:bar*");
  464. RedisService::keyDel("caches:messages:new_*");
  465. RedisService::keyDel("caches:messages:un_*");
  466. RedisService::keyDel("caches:messages:topList_{$userId}");
  467. return true;
  468. }
  469. /**
  470. * 隐藏
  471. * @param $userId
  472. * @param int $expire
  473. * @return mixed
  474. */
  475. public function setHide($userId, $type)
  476. {
  477. $this->model->where(['to_uid' => $userId, 'type' => $type, 'status' => 1, 'mark' => 1])
  478. ->update(['update_time' => time(), 'is_read' => 1, 'status' => 3]);
  479. // 清除缓存
  480. RedisService::keyDel("caches:messages:bar*");
  481. RedisService::keyDel("caches:messages:new_*");
  482. RedisService::keyDel("caches:messages:un_*");
  483. RedisService::keyDel("caches:messages:topList_{$userId}");
  484. return true;
  485. }
  486. /**
  487. * 清除历史
  488. * @param $userId
  489. * @param int $expire
  490. * @return mixed
  491. */
  492. public function clear($userId, $chatKey = 0, $type = 0)
  493. {
  494. $where = ['to_uid' => $userId, 'mark' => 1];
  495. if ($type) {
  496. $where['type'] = $type;
  497. }
  498. if ($chatKey) {
  499. $where['chat_key'] = $chatKey;
  500. }
  501. $this->model->where($where)
  502. ->update(['update_time' => time(), 'mark' => 0]);
  503. // 清除缓存
  504. RedisService::keyDel("caches:messages:bar*");
  505. RedisService::keyDel("caches:messages:new_*");
  506. RedisService::keyDel("caches:messages:un_*");
  507. RedisService::keyDel("caches:messages:topList_{$userId}");
  508. return true;
  509. }
  510. /**
  511. * 清除历史
  512. * @param $userId
  513. * @param int $expire
  514. * @return mixed
  515. */
  516. public function clearAll($userId)
  517. {
  518. $expire = ConfigService::make()->getConfigByCode('chat_log_expire');
  519. $expire = $expire > 0 ? $expire : 60;
  520. $this->model->where(['to_uid' => $userId, 'mark' => 0])
  521. ->where('update_time', '<', time() - 7 * 86400)
  522. ->delete();
  523. // 清除缓存
  524. RedisService::keyDel("caches:messages:bar*");
  525. RedisService::keyDel("caches:messages:new_*");
  526. RedisService::keyDel("caches:messages:topList_{$userId}");
  527. return $this->model->where(['to_uid' => $userId, 'mark' => 1])
  528. ->where('create_time', '<', time() - $expire * 86400)
  529. ->update(['update_time' => time(), 'mark' => 0]);
  530. }
  531. }