MessageService.php 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989
  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\GoodsModel;
  13. use App\Models\GroupUserSettingModel;
  14. use App\Models\LiveChatModel;
  15. use App\Models\MemberModel;
  16. use App\Models\MessageModel;
  17. use App\Models\VideoCollectModel;
  18. use App\Models\VideoModel;
  19. use App\Services\BaseService;
  20. use App\Services\ConfigService;
  21. use App\Services\RedisService;
  22. use Illuminate\Support\Facades\DB;
  23. /**
  24. * 站内消息服务管理-服务类
  25. * @author laravel开发员
  26. * @since 2020/11/11
  27. * Class MessageService
  28. * @package App\Services\Api
  29. */
  30. class MessageService extends BaseService
  31. {
  32. // 静态对象
  33. protected static $instance = null;
  34. /**
  35. * 构造函数
  36. * @author laravel开发员
  37. * @since 2020/11/11
  38. * MessageService constructor.
  39. */
  40. public function __construct()
  41. {
  42. $this->model = new MessageModel();
  43. }
  44. /**
  45. * 静态入口
  46. * @return static|null
  47. */
  48. public static function make()
  49. {
  50. if (!self::$instance) {
  51. self::$instance = (new static());
  52. }
  53. return self::$instance;
  54. }
  55. /**
  56. * 消息列表
  57. * @param $userId
  58. * @param $params
  59. * @param int $pageSize
  60. * @return array
  61. */
  62. public function getDataList($userId, $params, $pageSize = 0)
  63. {
  64. $page = request()->post('page', 1);
  65. $cacheKey = "caches:messages:history_{$page}_" . md5($userId . json_encode($params) . $pageSize);
  66. $datas = RedisService::get($cacheKey);
  67. if ($datas) {
  68. return $datas;
  69. }
  70. $where = ['a.status' => 1, 'a.mark' => 1];
  71. $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.group_id', 'a.from_uid', 'a.to_user_name', 'a.to_user_avatar','a.video_id','a.goods_id','a.live_id', 'a.to_uid', 'a.create_time', 'a.is_read', 'a.pages', 'a.status','d.nickname'];
  72. $model = $this->model->with(['goods','video','live'])->from('message as a')
  73. ->leftJoin('member as b', 'b.id', '=', 'a.from_uid')
  74. ->leftJoin('member as c', 'c.id', '=', 'a.to_uid')
  75. ->leftJoin('groups_user_setting as d',function($join){
  76. $join->on('d.user_id', '=', 'a.from_uid')->on('d.group_id','a.group_id');
  77. })
  78. ->where($where)
  79. ->where(function ($query) use ($params, $userId) {
  80. $fromUid = isset($params['from_uid']) ? intval($params['from_uid']) : 0;
  81. if ($fromUid) {
  82. $query->where('a.from_uid', $fromUid);
  83. }
  84. $type = isset($params['type']) ? intval($params['type']) : 0;
  85. if ($type) {
  86. $query->where('a.type', $type);
  87. }
  88. if ($type != 9) {
  89. $query->where('a.to_uid', $userId);
  90. }else {
  91. $groupId = isset($params['group_id']) ? intval($params['group_id']) : 0;
  92. $clearTime = GroupUserSettingModel::where(['user_id'=>$userId,'group_id'=>$groupId])->value('clear_time');
  93. $clearTime = $clearTime? $clearTime : 0;
  94. if ($groupId) {
  95. $query->where('a.group_id', $groupId)->where(function($query) use($clearTime){
  96. $query->whereRaw("lev_a.create_time >= {$clearTime}");
  97. });
  98. }else{
  99. $query->where(function ($query) use ($userId) {
  100. $query->where(function($query) use($userId){
  101. $query->where(['a.from_uid' => $userId,'a.from_show'=>1]);
  102. })->orWhere(function($query) use($userId){
  103. $query->where(['a.to_uid' => $userId,'a.to_show'=>1]);
  104. });
  105. });
  106. }
  107. }
  108. $chatType = isset($params['chat_type']) ? intval($params['chat_type']) : 0;
  109. if ($chatType) {
  110. $query->where('a.chat_type', $chatType);
  111. }
  112. $chatKey = isset($params['chat_key']) ? trim($params['chat_key']) : '';
  113. if ($chatKey) {
  114. $query->where('a.chat_key', $chatKey);
  115. }
  116. });
  117. $countModel = clone $model;
  118. $datas = $model->select($field)
  119. ->orderBy('a.create_time', 'desc')
  120. ->orderBy('a.id', 'desc')
  121. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  122. $datas = $datas ? $datas->toArray() : [];
  123. if ($datas) {
  124. $ids = [];
  125. $awardWaitRate = ConfigService::make()->getConfigByCode('shop_award_score_rate', 0);
  126. $awardWaitRate = $awardWaitRate > 0 && $awardWaitRate < 1000 ? $awardWaitRate : 0;
  127. $waitRate = ConfigService::make()->getConfigByCode('day_wait_score_rate', 0);
  128. $waitRate = $waitRate > 0 && $waitRate < 100 ? $waitRate : 0;
  129. foreach ($datas['data'] as &$item) {
  130. // 已读
  131. if($item['to_uid'] == $userId){
  132. $ids[] = $item['id'];
  133. }
  134. $item['show_time']= 0;
  135. if($item['create_time'] < time() - 3600 ){
  136. $item['show_time'] = 1;
  137. }
  138. if($item['nickname']){
  139. $item['from_user_name'] = $item['nickname'];
  140. }
  141. $item['time_text'] = isset($item['create_time']) && $item['create_time'] ? dateForWeek($item['create_time']) : '';
  142. $item['content'] = $item['msg_type'] == 2 ? get_images_preview(json_decode($item['content'], true), '') : str_replace("\n","<br>", $item['content']);
  143. $item['from_user_avatar'] = $item['from_user_avatar'] ? get_image_url($item['from_user_avatar']) : get_image_url('/images/member/logo.png');
  144. $item['to_user_avatar'] = $item['to_user_avatar'] ? get_image_url($item['to_user_avatar']) : get_image_url('/images/member/logo.png');
  145. if(isset($item['video']) && !is_null($item['video'])){
  146. $item['video']['thumb'] = get_image_url($item['video']['thumb']);
  147. $item['video']['file_url'] = get_image_url($item['video']['file_url']);
  148. }
  149. if(isset($item['goods']) && !is_null($item['goods'])){
  150. $item['goods']['main_img'] = get_image_url($item['goods']['main_img']);
  151. if (isset($item['goods']['retail_price']) && $item['goods']['retail_price']) {
  152. $item['goods']['retail_price1'] = $item['goods']['retail_price'];
  153. $item['goods']['retail_price'] = GoodsService::make()->getRealSalePrice($item['goods']['cost_price']);
  154. $item['goods']['wait_score'] = moneyFormat($item['goods']['retail_price'] * $awardWaitRate / 100 * $waitRate / 100, 2);
  155. }
  156. }
  157. $item['live'] = !is_null($item['live'])? [] : $item['live'];
  158. }
  159. unset($item);
  160. // 更新已读
  161. $type = isset($params['type'])? $params['type'] : 0;
  162. $chatKey = isset($params['chat_key']) ? trim($params['chat_key']) : '';
  163. $groupId = isset($params['group_id']) ? intval($params['group_id']) : 0;
  164. if($groupId>0){
  165. $this->model->where(['group_id'=> $groupId])->update(['is_read' => 1, 'update_time' => time()]);
  166. RedisService::clear("caches:message:topList_{$userId}");
  167. RedisService::keyDel("caches:messages:un_*");
  168. }
  169. else if($type == 9){
  170. $this->model->where(['to_uid'=> $userId,'chat_key'=>$chatKey,'type'=>9])->update(['is_read' => 1, 'update_time' => time()]);
  171. RedisService::clear("caches:message:topList_{$userId}");
  172. RedisService::keyDel("caches:messages:un_*");
  173. }
  174. else if ($ids) {
  175. $this->model->whereIn('id', $ids)->update(['is_read' => 1, 'update_time' => time()]);
  176. RedisService::clear("caches:message:topList_{$userId}");
  177. RedisService::keyDel("caches:messages:un_*");
  178. }
  179. $datas['unread'] = $countModel->where(['a.is_read'=>2])->count('a.id');
  180. arsort($datas['data'], true);
  181. $sort = isset($params['sort'])? $params['sort'] : 1;
  182. if($sort != 2){
  183. $datas['data'] = array_reverse($datas['data'], false);
  184. }
  185. RedisService::set($cacheKey, $datas, rand(3, 5));
  186. }
  187. return [
  188. 'list' => isset($datas['data']) ? $datas['data'] : [],
  189. 'total' => isset($datas['total']) ? $datas['total'] : 0,
  190. 'unread' => isset($datas['unread']) ? $datas['unread'] : 0,
  191. 'pageSize' => $pageSize
  192. ];
  193. }
  194. /**
  195. * 直播间消息列表
  196. * @param $userId
  197. * @param $params
  198. * @param int $pageSize
  199. * @return array
  200. */
  201. public function getLiveDataList($userId, $params, $pageSize = 0)
  202. {
  203. $page = request()->post('page', 1);
  204. $liveId = request()->post('live_id', 0);
  205. $cacheKey = "caches:messages:live_{$liveId}_{$page}_" . md5(json_encode($params) . $pageSize);
  206. $datas = RedisService::get($cacheKey);
  207. if ($datas) {
  208. return $datas;
  209. }
  210. $where = ['a.status' => 1, 'a.mark' => 1];
  211. $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'];
  212. $datas = LiveChatModel::with(['member'])->from('live_chat as a')
  213. ->leftJoin('member as b', 'b.id', '=', 'a.from_uid')
  214. ->leftJoin('member as c', 'c.id', '=', 'a.to_uid')
  215. ->where($where)
  216. ->where(function ($query) use ($params, $userId) {
  217. $fromUid = isset($params['from_uid']) ? intval($params['from_uid']) : 0;
  218. if ($fromUid) {
  219. $query->where('a.from_uid', $fromUid);
  220. }
  221. $liveId = isset($params['live_id']) ? intval($params['live_id']) : 0;
  222. if ($liveId) {
  223. $query->where('a.live_id', $liveId);
  224. }
  225. $chatKey = isset($params['chat_key']) ? trim($params['chat_key']) : '';
  226. if ($chatKey) {
  227. $query->where('a.chat_key', $chatKey);
  228. }
  229. })->select($field)
  230. ->orderBy('a.create_time', 'desc')
  231. ->orderBy('a.id', 'desc')
  232. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  233. $datas = $datas ? $datas->toArray() : [];
  234. if ($datas) {
  235. foreach ($datas['data'] as &$item) {
  236. $item['time_text'] = isset($item['create_time']) && $item['create_time'] ? dateForWeek($item['create_time']) : '';
  237. $item['member']['avatar'] = $item['from_user_avatar']? get_image_url($item['from_user_avatar']) : '/images/member/logo.png';
  238. }
  239. unset($item);
  240. arsort($datas['data'], true);
  241. $datas['data'] = array_reverse($datas['data'], false);
  242. RedisService::set($cacheKey, $datas, rand(3, 5));
  243. }
  244. return [
  245. 'list' => isset($datas['data']) ? $datas['data'] : [],
  246. 'total' => isset($datas['total']) ? $datas['total'] : 0,
  247. 'pageSize' => $pageSize
  248. ];
  249. }
  250. /**
  251. * 获取站内消息窗口列表
  252. * @param $userId
  253. * @return array|mixed
  254. */
  255. public function getGroupList($userId, $cache = false)
  256. {
  257. $cachekey = "caches:message:topList_{$userId}";
  258. $datas = RedisService::get($cachekey);
  259. if ($datas && $cache) {
  260. $datas['cache'] = true;
  261. return $datas;
  262. }
  263. $types = [1, 4, 5];
  264. $setting = MemberSettingService::make()->getSetting($userId);
  265. $pushStatus = false;
  266. if ($setting) {
  267. foreach ($setting as $k => $v) {
  268. if ($v == 1) {
  269. if ($k == 'receive_app') {
  270. $pushStatus = true;
  271. } else if ($k == 'receive_order') {
  272. $types[] = 2;
  273. } else if ($k == 'receive_account') {
  274. $types[] = 3;
  275. }
  276. }
  277. }
  278. asort($types);
  279. } else {
  280. $pushStatus = true;
  281. $types = [1, 2, 3, 4, 5];
  282. }
  283. // 关闭系统APP消息
  284. if (!$pushStatus) {
  285. return ['list' => [], 'total' => 0, 'types' => []];
  286. }
  287. $field = ['title', 'type', 'to_uid', 'description', 'is_read', 'create_time'];
  288. $datas = $this->model->where(['to_uid' => $userId, 'status' => 1, 'mark' => 1])
  289. ->whereIn('type', $types)
  290. ->groupBy('type')
  291. ->orderBy('type', 'asc')
  292. ->orderBy('is_read', 'desc')
  293. ->orderBy('create_time', 'desc')
  294. ->select($field)
  295. ->get();
  296. $datas = $datas ? $datas->toArray() : [];
  297. $total = 0;
  298. if ($datas) {
  299. $titles = [1 => '公告通知', 2 => '订单通知', 3 => '账户通知', 4 => '客服通知', 5 => '互动消息'];
  300. foreach ($datas as &$item) {
  301. $item['title'] = isset($titles[$item['type']]) ? $titles[$item['type']] : '消息通知';
  302. $data = $this->getNewMessage($item['type'], 0, $userId);
  303. $item['description'] = isset($data['description']) && $data['description'] ? $data['description'] : (isset($data['content']) ? mb_substr($data['content'], 0, 56, 'utf-8') . '...' : lang('有新消息'));
  304. $item['create_time'] = isset($data['create_time']) && $data['create_time'] ? $data['create_time'] : '';
  305. $item['time_text'] = isset($item['create_time']) && $item['create_time'] ? dateFormat($item['create_time']) : '';
  306. $item['create_time'] = isset($item['create_time']) ? datetime($item['create_time'], 'Y-m-d H.i.s') : '';
  307. $unread = $this->getUnreadCount($userId, 0, $item['type']);
  308. $item['unread'] = intval($unread);
  309. $total += intval($unread);
  310. }
  311. RedisService::set($cachekey, ['list' => $datas, 'total' => $total, 'types' => $types], rand(2, 3));
  312. }
  313. return ['list' => $datas, 'total' => $total, 'types' => $types];
  314. }
  315. /**
  316. * 聊天分组消息
  317. * @param $userId
  318. * @param $params
  319. * @param int $pageSize
  320. * @return array
  321. */
  322. public function getDataListFromatKey($userId, $params, $pageSize = 0)
  323. {
  324. $page = request()->post('page', 1);
  325. $cacheKey = "caches:m_chat:{$page}_" . md5($userId . json_encode($params) . $pageSize);
  326. $datas = RedisService::get($cacheKey);
  327. $data = isset($datas['data']) ? $datas['data'] : [];
  328. if ($datas && $data) {
  329. return [
  330. 'unread' => isset($datas['unReadCount']) ? $datas['unReadCount'] : 0,
  331. 'total' => isset($datas['total']) ? $datas['total'] : 0,
  332. 'list' => $data,
  333. 'pageSize' => $pageSize,
  334. 'cache' => true,
  335. ];
  336. }
  337. // 不接收则不显示聊天消息
  338. $receiveCustom = MemberSettingService::make()->getSetting($userId, 'receive_custom', 1);
  339. if ($receiveCustom != 1) {
  340. return [
  341. 'unread' => 0,
  342. 'total' => 0,
  343. 'list' => [],
  344. 'pageSize' => $pageSize,
  345. 'close' => true,
  346. ];
  347. }
  348. $where = ['a.type' => 9, 'a.status' => 1, 'a.mark' => 1];
  349. $expire = ConfigService::make()->getConfigByCode('chat_log_expire');
  350. $expire = $expire ? $expire * 86400 : 60 * 86400;
  351. $field = ['a.id', 'a.title', 'a.type', 'a.msg_type','a.to_show', 'a.chat_key', 'a.chat_type', 'a.description','a.group_id', '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','b.name as group_name','b.thumb as group_thumb','b.group_users'];
  352. $datas = $this->model->from('message as a')
  353. ->leftJoin('groups as b', function($join) use($userId){
  354. $join->on('b.id','=','a.group_id')->where('a.group_id','>',0);
  355. })
  356. ->leftJoin('groups_user_setting as c',function($join) use($userId){
  357. $join->on('c.group_id','a.group_id')->where('c.user_id', '=', $userId);
  358. })
  359. ->where($where)
  360. ->where('a.chat_key', '!=', '')
  361. ->where('a.create_time', '>=', time() - $expire)
  362. ->where(function ($query) use ($params, $userId) {
  363. $chatKey = isset($params['chat_key']) ? trim($params['chat_key']) : '';
  364. if ($chatKey) {
  365. $query->where('a.chat_key', $chatKey);
  366. }
  367. $isRead = isset($params['is_read']) ? intval($params['is_read']) : 0;
  368. if ($isRead) {
  369. $query->where('a.is_read', $isRead);
  370. }
  371. $chatType = isset($params['chat_type']) ? intval($params['chat_type']) : 0;
  372. if ($chatType) {
  373. $query->where('a.chat_type', $chatType);
  374. }
  375. if ($userId) {
  376. $query->where(function ($query) use ($userId) {
  377. $query->where(function($query) use($userId){
  378. $query->where(['a.from_uid' => $userId,'a.from_show'=>1]);
  379. })->orWhere(function($query) use($userId){
  380. $query->where(['a.to_uid' => $userId,'a.to_show'=>1]);
  381. })->orWhere(function($query) use($userId){
  382. $query->where('a.group_id','>',0)->where(function($query){
  383. $query->whereRaw('lev_a.create_time >= lev_c.clear_time and lev_c.clear_time > 0')->orWhere('c.clear_time',0)->orWhereNull('c.id');
  384. })->whereRaw('FIND_IN_SET(?,lev_b.group_users)', $userId);
  385. });
  386. });
  387. }
  388. })
  389. ->select($field)
  390. ->groupBy('chat_key')
  391. ->orderBy('a.create_time', 'desc')
  392. ->orderBy('a.id', 'desc')
  393. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  394. $datas = $datas ? $datas->toArray() : [];
  395. $unReadCount = 0;
  396. if ($datas) {
  397. $messageList = [];
  398. foreach ($datas['data'] as $k => &$item) {
  399. $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');
  400. $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');
  401. $data = $this->getNewMessage($item['type'], $item['chat_key'], $userId, $item['group_id']);
  402. $item['description'] = isset($data['description']) && $data['description'] ? $data['description'] : (isset($data['content']) ? mb_substr($data['content'], 0, 30, 'utf-8') : ($data? web_lang('有新消息') : ''));
  403. $item['create_time'] = isset($data['create_time']) && $data['create_time'] ? $data['create_time'] : $item['create_time'];
  404. $item['msg_type'] = isset($data['msg_type']) && $data['msg_type']? $data['msg_type'] : $item['msg_type'];
  405. $item['time_text'] = isset($item['create_time']) && $item['create_time'] ? dateFormat($item['create_time']) : '';
  406. $timeKey = $item['create_time']? strtotime($item['create_time']) : time();
  407. $item['create_time'] = isset($item['create_time']) ? datetime($item['create_time'], 'Y-m-d H.i.s') : '';
  408. $item['unread'] = $this->getUnreadCount($userId, $item['chat_key'], 0);
  409. if ($item['from_uid'] == $userId) {
  410. $item['from_user_name'] = $item['to_user_name'];
  411. $item['from_user_avatar'] = $item['to_user_avatar'];
  412. $item['tuid'] = $item['to_uid'];
  413. } else {
  414. $item['tuid'] = $item['from_uid'];
  415. }
  416. if($item['group_id']>0){
  417. $groupUsers = $item['group_users']? explode(',', $item['group_users']) : [];
  418. $item['group_user_count'] = $groupUsers? count($groupUsers) : 1;
  419. $item['from_user_name'] = $item['group_name']? $item['group_name'] : '';
  420. $item['from_user_avatar'] = $item['group_thumb']? get_image_url($item['group_thumb']) : '';
  421. if($item['from_user_avatar'] == '' ){
  422. $userIds = $groupUsers? array_slice($groupUsers,0,9) : [$userId];
  423. $item['group_users'] = GroupService::make()->getGroupUsers($item['id'], $userIds);
  424. }
  425. }else{
  426. $item['group_users'] = [];
  427. }
  428. $unReadCount += intval($item['unread']);
  429. $messageList[$timeKey.'_'.$k] = $item;
  430. }
  431. unset($item);
  432. krsort($messageList);
  433. $datas['data'] = array_values($messageList);
  434. $datas['unReadCount'] = $unReadCount;
  435. RedisService::set($cacheKey, $datas, rand(3, 5));
  436. }
  437. return [
  438. 'unread' => $unReadCount,
  439. 'total' => isset($datas['total']) ? $datas['total'] : 0,
  440. 'list' => isset($datas['data']) ? $datas['data'] : [],
  441. 'pageSize' => $pageSize,
  442. 'cache' => false,
  443. ];
  444. }
  445. /**
  446. * 获取最新消息
  447. * @param $chatKey
  448. * @return mixed
  449. */
  450. public function getNewMessage($type = 0, $chatKey = 0, $userId = 0, $groupId=0)
  451. {
  452. $cacheKey = "caches:messages:new_{$type}_{$chatKey}_{$userId}_{$groupId}";
  453. $data = RedisService::get($cacheKey);
  454. if ($data) {
  455. return $data;
  456. }
  457. $where = ['status' => 1, 'mark' => 1];
  458. if ($type) {
  459. $where['type'] = $type;
  460. }
  461. if ($chatKey) {
  462. $where['chat_key'] = $chatKey;
  463. }
  464. if ($userId && $chatKey==0) {
  465. $where['to_uid'] = $userId;
  466. }
  467. if($groupId){
  468. $clearTime = GroupUserSettingModel::where(['user_id'=>$userId,'group_id'=>$groupId])->value('clear_time');
  469. $clearTime = $clearTime? $clearTime : 0;
  470. $data = $this->model->where($where)
  471. ->where('create_time','>=', $clearTime)
  472. ->select('id', 'title', 'description', 'msg_type', 'content', 'create_time')
  473. ->orderBy('create_time', 'desc')
  474. ->orderBy('id', 'desc')
  475. ->first();
  476. }else{
  477. $data = $this->model->where($where)->select('id', 'title', 'description', 'msg_type', 'content', 'create_time')
  478. ->orderBy('create_time', 'desc')
  479. ->orderBy('id', 'desc')
  480. ->first();
  481. }
  482. $data = $data ? $data->toArray() : [];
  483. if ($data) {
  484. if ($data['msg_type'] == 2) {
  485. $data['description'] = '[图片]';
  486. } else if ($data['msg_type'] == 3) {
  487. $data['description'] = '[视频聊天]';
  488. } else if ($data['msg_type'] == 4) {
  489. $data['description'] = '[直播间分享]';
  490. }else if($data['msg_type'] == 6){
  491. $data['description'] = '[群消息]';
  492. }
  493. RedisService::set($cacheKey, $data, rand(3, 5));
  494. }
  495. return $data;
  496. }
  497. /**
  498. * 获取未读消息数量
  499. * @param $userId
  500. * @return array|mixed
  501. */
  502. public function getBarCount($userId, $type = 0)
  503. {
  504. $cacheKey = "caches:barCount:{$userId}_{$type}";
  505. $data = RedisService::get($cacheKey);
  506. if ($data) {
  507. return $data;
  508. }
  509. $data = $this->getUnreadCount($userId, 0, $type);
  510. RedisService::set($cacheKey, $data, rand(3, 5));
  511. return $data;
  512. }
  513. /**
  514. * 推送站内消息
  515. * @param $userId 用户ID
  516. * @param $title 标题
  517. * @param $message 消息内容
  518. * @param int $type 类型:1-公告,2-订单,3-账户,4-客服,5-动态
  519. * @return false
  520. */
  521. public function pushMessage($userId, $title, $message, $type = 1, $fromUid = 0, $pageUrl='')
  522. {
  523. $types = [4, 5];
  524. $pushStatus = false;
  525. $datas = MemberSettingService::make()->getSetting($userId);
  526. if ($datas) {
  527. foreach ($datas as $k => $v) {
  528. if ($v == 1) {
  529. if ($k == 'receive_app') {
  530. $pushStatus = true;
  531. $types[] = 1;
  532. } else if ($k == 'receive_order') {
  533. $types[] = 2;
  534. } else if ($k == 'receive_account') {
  535. $types[] = 3;
  536. }
  537. }
  538. }
  539. } else {
  540. $pushStatus = true;
  541. $types = [1, 2, 3, 4, 5];
  542. }
  543. if ($userId && $pushStatus && in_array($type, $types)) {
  544. $appName = ConfigService::make()->getConfigByCode('app_name', '星链社交');
  545. $log = [
  546. 'from_uid' => $fromUid,
  547. 'to_uid' => $userId,
  548. 'from_user_name' => $appName,
  549. 'chat_type' => 0,
  550. 'msg_type' => 1,
  551. 'type' => $type,
  552. 'title' => $title,
  553. 'description' => $title,
  554. 'content' => $message,
  555. 'pages' => $pageUrl?$pageUrl:'',
  556. 'chat_key' => "0{$userId}",
  557. 'create_time' => time(),
  558. 'status' => 1,
  559. 'mark' => 1,
  560. ];
  561. return MessageModel::insert($log);
  562. }
  563. return false;
  564. }
  565. /**
  566. * 推送群聊消息
  567. * @param $fromUId 发送用户ID
  568. * @param $groupId 群ID
  569. * @param $title 标题
  570. * @param $message 消息内容
  571. * @param int $type 类型:1-公告,2-订单,3-账户,4-客服,5-动态
  572. * @return false
  573. */
  574. public function pushGroupMessage($groupId, $title, $message, $msgType = 1, $fromUid = 0, $pageUrl='')
  575. {
  576. $types = [6, 7];
  577. if ($groupId && in_array($msgType, $types)) {
  578. $appName = ConfigService::make()->getConfigByCode('app_name', '星链社交');
  579. $log = [
  580. 'from_uid' => $fromUid,
  581. 'to_uid' => 0,
  582. 'group_id' => $groupId,
  583. 'from_user_name' => $appName,
  584. 'chat_type' => 0,
  585. 'msg_type' => $msgType,
  586. 'type' => 9,
  587. 'title' => $title,
  588. 'description' => $title,
  589. 'content' => $message,
  590. 'pages' => $pageUrl?$pageUrl:'',
  591. 'chat_key' => "group-{$groupId}",
  592. 'create_time' => time(),
  593. 'status' => 1,
  594. 'mark' => 1,
  595. ];
  596. return MessageModel::insert($log);
  597. }
  598. return false;
  599. }
  600. /**
  601. * 获取
  602. * @param $userId
  603. * @return array|mixed
  604. */
  605. public function getUnreadCount($userId, $chatKey = 0, $type = 0)
  606. {
  607. $cacheKey = "caches:messages:un_{$userId}_{$chatKey}_{$type}";
  608. $data = RedisService::get($cacheKey);
  609. if (RedisService::exists($cacheKey)) {
  610. return $data;
  611. }
  612. $where = ['a.to_show'=>1, 'a.status' => 1, 'a.mark' => 1];
  613. if ($type > 0) {
  614. $where['a.type'] = $type;
  615. }
  616. if ($chatKey) {
  617. $where['a.chat_key'] = $chatKey;
  618. }
  619. $expire = ConfigService::make()->getConfigByCode('chat_log_expire');
  620. $expire = $expire ? $expire * 86400 : 60 * 86400;
  621. $data = $this->model->from('message as a')
  622. ->leftJoin('groups as b', function($join) use($userId){
  623. $join->on('b.id','=','a.group_id');
  624. })
  625. ->leftJoin('groups_user_setting as c', function($join) use($userId){
  626. $join->on('c.group_id','=','a.group_id')->where('c.user_id','=', $userId);
  627. })
  628. ->where(function($query) use($userId){
  629. $query->where(function ($query) use ($userId) {
  630. if($userId){
  631. $query->where(function($query) use($userId){
  632. $query->where(['a.to_uid' => $userId,'a.to_show'=>1,'a.is_read' => 2])->where('a.group_id', 0);
  633. })->orWhere(function($query) use($userId){
  634. $query->where('a.group_id','>',0)->whereNotIn('a.from_uid',[$userId])->where(function($query){
  635. $query->whereRaw('lev_a.create_time >= lev_c.read_time and lev_c.read_time > 0')->orWhere('c.read_time',0)->orWhereNull('c.id');
  636. })->where(function($query){
  637. $query->where(['a.from_show'=>1])->orWhere(['a.to_show'=>1]);
  638. })->whereRaw('FIND_IN_SET(?,lev_b.group_users)', $userId);
  639. });
  640. }
  641. });
  642. })
  643. ->where('a.create_time', '>=', time() - $expire)
  644. ->where($where)
  645. ->count('a.id');
  646. RedisService::set($cacheKey, $data, rand(2, 3));
  647. return $data;
  648. }
  649. /**
  650. * 验证发送消息或者内容是否有屏蔽词
  651. * @param $message 消息或内容
  652. * @param $type 是否返回屏蔽后内容:1-是
  653. * @return bool
  654. */
  655. public function filterMessage($message, $type = 1)
  656. {
  657. $filter = ConfigService::make()->getConfigByCode('chat_sensitive');
  658. $filter = !empty($filter) ? explode('、', $filter) : [];
  659. $filter = array_filter($filter);
  660. if ($filter) {
  661. if ($type != 2) {
  662. foreach ($filter as $kw) {
  663. $message = preg_replace("/{$kw}/", '***', $message);
  664. }
  665. } else {
  666. foreach ($filter as $kw) {
  667. if (preg_match("/{$kw}/", $message)) {
  668. return false;
  669. }
  670. }
  671. }
  672. // 手机号、邮箱、网址
  673. if ($type == 1) {
  674. $message = preg_replace("/^(1[3-9][0-9]{9})$/", '***', $message);
  675. $message = preg_replace("/([a-z0-9&\-_.]+@[\w\-_]+([\w\-.]+)?\.[\w\-]+)/is", '***', $message);
  676. $message = str_replace(['https:', 'http:', '.com', '.cn', '.net', '.top', 'www.'], '***', $message);
  677. $message = preg_replace("/([a-zA-Z0-9][a-zA-Z0-9\_]{6,20})/", '***', $message);
  678. $message = preg_replace("/\*{3}\*{1,}/", '***', $message);
  679. }
  680. }
  681. return $type == 3 && $message === '***' ? '' : $message;
  682. }
  683. /**
  684. * 已读
  685. * @param $userId 用户ID
  686. * @param $chatKey 聊天窗口ID
  687. * @return bool
  688. */
  689. public function setRead($userId, $type = 0, $chatKey = '', $groupId=0)
  690. {
  691. $where = [];
  692. if(!$groupId){
  693. $where['to_uid'] = $userId;
  694. }else{
  695. $where['group_id'] = $groupId;
  696. }
  697. if ($type) {
  698. $where['type'] = $type;
  699. }
  700. if ($chatKey) {
  701. $where['chat_key'] = $chatKey;
  702. }
  703. $this->model->where($where)->update(['is_read' => 1, 'update_time' => time()]);
  704. // 清除缓存
  705. RedisService::keyDel("caches:messages:bar*");
  706. RedisService::keyDel("caches:messages:new_*");
  707. RedisService::keyDel("caches:messages:un_*");
  708. RedisService::keyDel("caches:messages:topList_{$userId}");
  709. return true;
  710. }
  711. /**
  712. * 隐藏
  713. * @param $userId
  714. * @param int $expire
  715. * @return mixed
  716. */
  717. public function setHide($userId, $type)
  718. {
  719. $this->model->where(['to_uid' => $userId, 'type' => $type, 'status' => 1, 'mark' => 1])
  720. ->update(['update_time' => time(),'to_show'=>2, 'is_read' => 1, 'status' => 3]);
  721. // 清除缓存
  722. RedisService::keyDel("caches:messages:bar*");
  723. RedisService::keyDel("caches:messages:new_*");
  724. RedisService::keyDel("caches:messages:un_*");
  725. RedisService::keyDel("caches:messages:topList_{$userId}");
  726. return true;
  727. }
  728. /**
  729. * 分享
  730. * @param $userId
  731. * @param $params
  732. * @return false|int[]
  733. */
  734. public function share($userId, $params)
  735. {
  736. $shareUserId = isset($params['user_id'])? intval($params['user_id']) : 0;
  737. $groupId = isset($params['group_id'])? intval($params['group_id']) : 0;
  738. $videoId = isset($params['video_id'])? intval($params['video_id']) : 0;
  739. $liveId = isset($params['live_id'])? intval($params['live_id']) : 0;
  740. $goodsId = isset($params['goods_id'])? intval($params['goods_id']) : 0;
  741. $type = isset($params['type'])? intval($params['type']) : 1; // 1-用户私聊分享,2-商家私聊分享,3-承兑商私聊分享,0-群聊分享
  742. $message = isset($params['message'])? trim($params['message']) : '';
  743. $userInfo = MemberModel::where(['id'=> $userId,'mark'=>1])
  744. ->select(['id','nickname','usdt','pay_password','avatar','status'])
  745. ->first();
  746. $status = isset($userInfo['status'])? $userInfo['status'] : 0;
  747. if(empty($userInfo) || $status != 1){
  748. $this->error = 2024;
  749. return false;
  750. }
  751. $shareInfo = [];
  752. if($shareUserId){
  753. $shareInfo = MemberModel::where(['id'=> $shareUserId,'mark'=>1])
  754. ->select(['id','nickname','usdt','pay_password','avatar','status'])
  755. ->first();
  756. }
  757. $msgData = [
  758. 'from_uid' => $userId,
  759. 'to_uid' => $shareUserId,
  760. 'type' => 9,
  761. 'title' => '分享',
  762. 'msg_type' => $goodsId? 5 : 8,
  763. 'chat_type' => $type,
  764. 'from_user_name' => $userInfo['nickname'],
  765. 'to_user_name' => $shareInfo? $shareInfo['nickname'] : '',
  766. 'from_user_avatar' => $userInfo? $userInfo['avatar'] : '',
  767. 'to_user_avatar' => $shareInfo? $shareInfo['avatar'] : '',
  768. 'description' => $goodsId?'商品分享':($liveId? '直播分享': '视频分享'),
  769. 'content' => $message,
  770. 'goods_id' => $goodsId,
  771. 'group_id' => $groupId,
  772. 'live_id' => $liveId,
  773. 'video_id' => $videoId,
  774. 'chat_key' => $type==0? "group-{$groupId}" : getChatKey($userId, $shareUserId),
  775. 'create_time' => time(),
  776. 'update_time' => time(),
  777. 'is_read' => 2,
  778. 'status' => 1
  779. ];
  780. DB::beginTransaction();
  781. if(!$this->model->insertGetid($msgData)){
  782. DB::rollBack();
  783. $this->error = 1070;
  784. return false;
  785. }
  786. // 视频分享
  787. $num = 1;
  788. if($videoId){
  789. $videoInfo = VideoModel::where(['id'=> $videoId])->first();
  790. if(empty($videoInfo)){
  791. DB::rollBack();
  792. $this->error = 1070;
  793. return false;
  794. }
  795. $data = [
  796. 'user_id'=> $userId,
  797. 'type'=> 4,
  798. 'collect_id'=> $videoId,
  799. 'collect_uid'=> isset($videoInfo['user_id'])? $videoInfo['user_id'] : 0,
  800. 'tags'=> isset($videoInfo['tags'])&&$videoInfo['tags']? $videoInfo['tags'] : '',
  801. 'create_time'=> time(),
  802. 'status'=> 1,
  803. ];
  804. if(!VideoCollectModel::insertGetId($data)){
  805. DB::rollBack();
  806. $this->error = 1070;
  807. return false;
  808. }
  809. $updateData = ['share_num'=> DB::raw("share_num + 1"),'update_time'=>time()];
  810. if(!VideoModel::where(['id'=> $videoId])->update($updateData)){
  811. DB::rollBack();
  812. $this->error = 1070;
  813. return false;
  814. }
  815. $num = $videoInfo['share_num']+1;
  816. }
  817. // 商品分享
  818. else if($goodsId){
  819. $updateData = ['share_num'=> DB::raw("share_num + 1"),'update_time'=>time()];
  820. if(!GoodsModel::where(['id'=> $goodsId])->update($updateData)){
  821. DB::rollBack();
  822. $this->error = 1070;
  823. return false;
  824. }
  825. $num = (int)VideoModel::where(['id'=> $videoId])->value('share_num');
  826. }
  827. DB::commit();
  828. $this->error = 1069;
  829. return ['num'=> $num];
  830. }
  831. /**
  832. * 清除历史
  833. * @param $userId
  834. * @param int $expire
  835. * @return mixed
  836. */
  837. public function clear($userId, $chatKey = 0, $type = 0, $groupId=0)
  838. {
  839. $where = ['mark' => 1];
  840. if($groupId){
  841. $where['group_id'] = $groupId;
  842. }else{
  843. $where['to_uid'] = $userId;
  844. }
  845. if ($type) {
  846. $where['type'] = $type;
  847. }
  848. if ($chatKey) {
  849. $where['chat_key'] = $chatKey;
  850. }
  851. $this->model->where($where)
  852. ->update(['update_time' => time(), 'mark' => 0]);
  853. // 清除缓存
  854. RedisService::keyDel("caches:messages:bar*");
  855. RedisService::keyDel("caches:messages:new_*");
  856. RedisService::keyDel("caches:messages:un_*");
  857. RedisService::keyDel("caches:messages:topList_{$userId}");
  858. return true;
  859. }
  860. /**
  861. * 清除历史
  862. * @param $userId
  863. * @param int $expire
  864. * @return mixed
  865. */
  866. public function clearAll($userId, $type=0)
  867. {
  868. $expire = ConfigService::make()->getConfigByCode('chat_log_expire');
  869. $expire = $expire > 0 ? $expire : 60;
  870. $this->model->where(['to_uid' => $userId, 'mark' => 0])
  871. ->where('update_time', '<', time() - $expire * 86400)
  872. ->delete();
  873. // 清除缓存
  874. RedisService::keyDel("caches:messages:bar*");
  875. RedisService::keyDel("caches:messages:new_*");
  876. RedisService::keyDel("caches:messages:topList_{$userId}");
  877. // 站内消息发给我的全删
  878. $this->model->whereNotIn('type',[9])->where(function($query) use($userId, $type){
  879. $query->where(['to_uid'=> $userId,'mark'=>1]);
  880. if($type>0){
  881. $query->where('type', $type);
  882. }
  883. })->update(['update_time' => time(), 'mark' => 0]);
  884. if($type==9 || !$type){
  885. // 聊天消息我发的全删
  886. $this->model->where(function($query) use($userId){
  887. $query->where(['type'=>9,'from_uid'=> $userId,'mark'=>1]);
  888. })->update(['update_time' => time(), 'mark' => 0]);
  889. // 聊天消息发给我的,接收方全隐藏
  890. $this->model->where(function($query) use($userId){
  891. $query->where(['type'=>9,'to_uid'=> $userId,'mark'=>1]);
  892. })->update(['update_time' => time(),'is_read'=>1, 'to_show' => 2]);
  893. }
  894. return true;
  895. }
  896. }