MessageService.php 26 KB

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