MessageService.php 25 KB

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