ImChatService.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  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\AccountLogModel;
  13. use App\Models\ImChatModel;
  14. use App\Models\ImChatParamsModel;
  15. use App\Models\MemberModel;
  16. use App\Services\BaseService;
  17. use App\Services\ConfigService;
  18. use App\Services\RedisService;
  19. use Illuminate\Support\Facades\DB;
  20. /**
  21. * 聊天服务管理-服务类
  22. * @author laravel开发员
  23. * @since 2020/11/11
  24. * Class ImChatService
  25. * @package App\Services\Api
  26. */
  27. class ImChatService extends BaseService
  28. {
  29. // 静态对象
  30. protected static $instance = null;
  31. /**
  32. * 构造函数
  33. * @author laravel开发员
  34. * @since 2020/11/11
  35. * ImChatService constructor.
  36. */
  37. public function __construct()
  38. {
  39. $this->model = new ImChatModel();
  40. }
  41. /**
  42. * 静态入口
  43. * @return static|null
  44. */
  45. public static function make()
  46. {
  47. if (!self::$instance) {
  48. self::$instance = (new static());
  49. }
  50. return self::$instance;
  51. }
  52. /**
  53. * 消息列表
  54. * @param $userId
  55. * @param $params
  56. * @param int $pageSize
  57. * @return array
  58. */
  59. public function getDataList($userId, $params, $pageSize=0)
  60. {
  61. $page = request()->post('page', 1);
  62. $cacheKey = "caches:chats:history_{$page}_".md5($userId.json_encode($params). $pageSize);
  63. $datas = RedisService::get($cacheKey);
  64. $data = isset($datas['data'])? $datas['data'] : [];
  65. if($datas && $data) {
  66. return [
  67. 'total'=> isset($datas['total'])? $datas['total'] : 0,
  68. 'list'=> $data,
  69. 'pageSize'=> $pageSize,
  70. 'cache'=> true,
  71. ];
  72. }
  73. $where = ['a.status'=> 1,'a.mark'=>1];
  74. $expire = ConfigService::make()->getConfigByCode('chat_log_expire');
  75. $expire = $expire? $expire*86400 : 60*86400;
  76. $field = ['a.id','a.chat_key','a.message','a.from_user_id','a.to_user_id','a.is_connect','a.video_time','a.msg_type','a.create_time','a.is_read','a.status','b.avatar as from_avatar','b.nickname as from_nickname','c.avatar as to_avatar','c.nickname as to_nickname'];
  77. $datas = $this->model->from('imchat as a')
  78. ->leftJoin('member as b','b.id','=','a.from_user_id')
  79. ->leftJoin('member as c','c.id','=','a.to_user_id')
  80. ->where(function($query) use($userId){
  81. if($userId){
  82. $query->where('a.from_user_id', $userId)->orWhere('a.to_user_id', $userId);
  83. }
  84. })
  85. ->where($where)
  86. ->where('a.create_time','>=', time() - $expire)
  87. ->where(function($query) use($params){
  88. $chatKey = isset($params['chat_key'])? trim($params['chat_key']) : '';
  89. if($chatKey){
  90. $query->where('a.chat_key', $chatKey);
  91. }
  92. $isRead = isset($params['is_read'])? intval($params['is_read']) : 0;
  93. if($isRead){
  94. $query->where('a.is_read', $isRead);
  95. }
  96. $isRead = isset($params['is_read'])? intval($params['is_read']) : 0;
  97. if($isRead){
  98. $query->where('a.is_read', $isRead);
  99. }
  100. $isShow = isset($params['is_show'])? intval($params['is_show']) : 0;
  101. if($isShow){
  102. $query->where('a.is_show', $isRead);
  103. }
  104. })
  105. ->select($field)
  106. ->orderBy('a.create_time','desc')
  107. ->orderBy('a.id','desc')
  108. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  109. $datas = $datas ? $datas->toArray() : [];
  110. if ($datas) {
  111. $chatKey = '';
  112. foreach ($datas['data'] as $k=> &$item) {
  113. $item['show_time'] = $k == 0? 1 : 0;
  114. $chatKey = isset($item['chat_key'])? $item['chat_key'] : '';
  115. $item['time_text'] = isset($item['create_time']) && $item['create_time']? dateFormat($item['create_time']) : '';
  116. $item['from_avatar'] = isset($item['from_avatar']) && $item['from_avatar'] ? get_image_url($item['from_avatar']) : get_image_url('/images/member/logo.png');
  117. $item['to_avatar'] = isset($item['to_avatar']) && $item['to_avatar'] ? get_image_url($item['to_avatar']) : get_image_url('/images/member/logo.png');
  118. $info = MerchantService::make()->getCacheInfoByUser($item['from_user_id'], 'id,name,logo,status');
  119. $status = isset($info['status'])? $info['status'] : 0;
  120. if($info && $status==2){
  121. $item['from_avatar'] = $info['logo'];
  122. $item['from_nickname'] = $info['name'];
  123. }
  124. // 商家
  125. $info = MerchantService::make()->getCacheInfoByUser($item['to_user_id'], 'id,name,logo,status');
  126. $status = isset($info['status'])? $info['status'] : 0;
  127. if($info && $status==2){
  128. $item['to_avatar'] = $info['logo']?$info['logo']:get_image_url('/images/member/logo.png');
  129. $item['to_nickname'] = $info['name'];
  130. }
  131. if(empty($item['from_avatar'])){
  132. $item['from_suffix_name'] = mb_substr($item['from_nickname'],0,1);
  133. }
  134. if(empty($item['to_avatar'])){
  135. $item['to_suffix_name'] = mb_substr($item['to_nickname'],0,1);
  136. }
  137. $item['video_time_text'] = '';
  138. if(isset($item['video_time']) && $item['video_time']){
  139. $data = [];
  140. $time = intval($item['video_time']);
  141. if($time>3600){
  142. $hour = intval($time/3600);
  143. $data[] = $hour<10?'0'.$hour:$hour;
  144. }
  145. if($time%3600 > 0){
  146. $minute = intval($time%3600/60);
  147. $data[] = $minute<10?'0'.$minute:$minute;
  148. }
  149. if($time%60 > 0){
  150. $second = intval($time%60);
  151. $data[] = $second<10?'0'.$second:$second;
  152. }
  153. $item['video_time_text'] = $data? implode(':', $data) : '';
  154. }
  155. }
  156. unset($item);
  157. // 已读处理
  158. if($chatKey && $userId){
  159. $this->model->where(['chat_key'=> $chatKey,'to_user_id'=> $userId,'mark'=>1,'status'=>1])->update(['update_time'=>time(),'is_read'=>1]);
  160. // 清除缓存
  161. RedisService::keyDel("caches:chats:history*");
  162. RedisService::keyDel("caches:chats:group*");
  163. RedisService::keyDel("caches:messages:bar*");
  164. }
  165. arsort($datas['data'], true);
  166. $datas['data'] = array_reverse($datas['data'], false);
  167. RedisService::set($cacheKey, $datas, rand(3, 5));
  168. }
  169. return [
  170. 'total'=> isset($datas['total'])? $datas['total'] : 0,
  171. 'list'=> isset($datas['data'])? $datas['data'] : [],
  172. 'pageSize'=> $pageSize,
  173. 'cache'=> false,
  174. ];
  175. }
  176. /**
  177. * 消息列表
  178. * @param $userId
  179. * @param $params
  180. * @param int $pageSize
  181. * @return array
  182. */
  183. public function getDataListFromatKey($userId, $params, $pageSize=0)
  184. {
  185. $page = request()->post('page', 1);
  186. $cacheKey = "caches:chats:group_{$page}_".md5($userId.json_encode($params).$pageSize);
  187. $datas = RedisService::get($cacheKey);
  188. $data = isset($datas['data'])? $datas['data'] : [];
  189. if($datas && $data) {
  190. return [
  191. 'unread'=> isset($datas['unReadCount'])? $datas['unReadCount'] : 0,
  192. 'total'=> isset($datas['total'])? $datas['total'] : 0,
  193. 'list'=> $data,
  194. 'pageSize'=> $pageSize,
  195. 'cache'=> true,
  196. ];
  197. }
  198. $where = ['a.status'=> 1,'a.mark'=>1];
  199. $expire = ConfigService::make()->getConfigByCode('chat_log_expire');
  200. $expire = $expire? $expire*86400 : 60*86400;
  201. $field = ['a.id','a.chat_key','a.from_user_id','a.to_user_id','a.msg_type','a.create_time','a.video_time','a.is_connect','a.is_read','a.from_is_show','a.to_is_show','a.status','b.avatar as from_avatar','b.nickname as from_nickname','c.avatar as to_avatar','c.nickname as to_nickname'];
  202. $datas = $this->model->from('imchat as a')
  203. ->leftJoin('member as b','b.id','=','a.from_user_id')
  204. ->leftJoin('member as c','c.id','=','a.to_user_id')
  205. ->where(function($query) use($userId){
  206. if($userId){
  207. $query->where(['a.from_user_id'=>$userId,'a.from_is_show'=>1])
  208. ->orWhere(function($query) use($userId){
  209. $query->where(['a.to_user_id'=>$userId,'a.to_is_show'=>1]);
  210. });
  211. }
  212. })
  213. ->where($where)
  214. ->where('a.chat_key','>', 0)
  215. ->where('a.create_time','>=', time() - $expire)
  216. ->where(function($query) use($params){
  217. $chatKey = isset($params['chat_key'])? trim($params['chat_key']) : '';
  218. if($chatKey){
  219. $query->where('a.chat_key', $chatKey);
  220. }
  221. $isRead = isset($params['is_read'])? intval($params['is_read']) : 0;
  222. if($isRead){
  223. $query->where('a.is_read', $isRead);
  224. }
  225. $isRead = isset($params['is_read'])? intval($params['is_read']) : 0;
  226. if($isRead){
  227. $query->where('a.is_read', $isRead);
  228. }
  229. })
  230. ->select($field)
  231. ->groupBy('chat_key')
  232. // ->orderBy('a.is_read','desc')
  233. ->orderBy('a.create_time','desc')
  234. ->orderBy('a.id','desc')
  235. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  236. $datas = $datas ? $datas->toArray() : [];
  237. $unReadCount = 0;
  238. if ($datas) {
  239. foreach ($datas['data'] as &$item) {
  240. $item['from_avatar'] = isset($item['from_avatar']) && $item['from_avatar'] ? get_image_url($item['from_avatar']) : get_image_url('/images/member/logo.png');
  241. $item['to_avatar'] = isset($item['to_avatar']) && $item['to_avatar'] ? get_image_url($item['to_avatar']) : get_image_url('/images/member/logo.png');
  242. $data = $this->getNewChat($item['chat_key']);
  243. $item['description'] = isset($data['description']) && $data['description']? $data['description'] : (isset($data['message'])? mb_substr($data['message'],0,20,'utf-8'):'有新消息');
  244. $item['create_time'] = isset($data['create_time']) && $data['create_time']? $data['create_time'] : '';
  245. $item['time_text'] = isset($item['create_time']) && $item['create_time'] ? dateFormat($item['create_time']) : '';
  246. $item['create_time'] = isset($item['create_time']) ? datetime($item['create_time'], 'Y-m-d H.i.s') : '';
  247. if($item['from_user_id'] == $userId){
  248. $item['avatar'] = $item['to_avatar'];
  249. $item['nickname'] = $item['to_nickname'];
  250. $info = MerchantService::make()->getCacheInfoByUser($item['to_user_id'], 'id,name,logo,status');
  251. $status = isset($info['status'])? $info['status'] : 0;
  252. if($info && $status==2){
  253. $item['avatar'] = $info['logo']?$info['logo']:get_image_url('/images/member/logo.png');
  254. $item['nickname'] = $info['name'];
  255. }
  256. $item['unread'] = $this->getUnreadCount($userId, $item['chat_key']);
  257. $item['touid'] = $item['to_user_id'];
  258. }else{
  259. $item['avatar'] = $item['from_avatar'];
  260. $item['nickname'] = $item['from_nickname'];
  261. $info = MerchantService::make()->getCacheInfoByUser($item['from_user_id'], 'id,name,logo,status');
  262. $status = isset($info['status'])? $info['status'] : 0;
  263. if($info && $status==2){
  264. $item['avatar'] = $info['logo']?$info['logo'] : get_image_url('/images/member/logo.png');
  265. $item['nickname'] = $info['name'];
  266. }
  267. $item['unread'] = $this->getUnreadCount($userId,$item['chat_key']);
  268. $item['touid'] = $item['from_user_id'];
  269. }
  270. $item['video_time_text'] = '';
  271. if(isset($item['video_time']) && $item['video_time']){
  272. $data = [];
  273. $time = intval($item['video_time']);
  274. if($time>3600){
  275. $hour = intval($time/3600);
  276. $data[] = $hour<10?'0'.$hour:$hour;
  277. }
  278. if($time%3600 > 0){
  279. $minute = intval($time%3600/60);
  280. $data[] = $minute<10?'0'.$minute:$minute;
  281. }
  282. if($time%60 > 0){
  283. $second = intval($time%60);
  284. $data[] = $second<10?'0'.$second:$second;
  285. }
  286. $item['video_time_text'] = $data? implode(':', $data) : '';
  287. }
  288. $unReadCount += intval($item['unread']);
  289. }
  290. unset($item);
  291. $datas['unReadCount'] = $unReadCount;
  292. RedisService::set($cacheKey, $datas, rand(3, 5));
  293. }
  294. return [
  295. 'unread'=> $unReadCount,
  296. 'total'=> isset($datas['total'])? $datas['total'] : 0,
  297. 'list'=> isset($datas['data'])? $datas['data'] : [],
  298. 'pageSize'=> $pageSize,
  299. 'cache'=> false,
  300. ];
  301. }
  302. /**
  303. * 获取最新消息
  304. * @param $chatKey
  305. * @return mixed
  306. */
  307. public function getNewChat($chatKey)
  308. {
  309. $cacheKey = "caches:chats:new_{$chatKey}";
  310. $data = RedisService::get($cacheKey);
  311. if($data){
  312. return $data;
  313. }
  314. $where = ['chat_key'=>$chatKey,'status'=>1,'mark'=>1];
  315. $data = $this->model->where($where)->select('id','description','message','create_time')
  316. //->orderBy('is_read','desc')
  317. ->orderBy('create_time','desc')
  318. ->orderBy('id','desc')
  319. ->first();
  320. $data = $data? $data->toArray() : [];
  321. if($data){
  322. RedisService::set($cacheKey, $data, rand(3,5));
  323. }
  324. return $data;
  325. }
  326. /**
  327. * 未读聊天消息数量
  328. * @param $userId
  329. * @return array|mixed
  330. */
  331. public function getUnreadCount($userId, $chatKey=0)
  332. {
  333. $cacheKey = "caches:chats:unReadCount:{$userId}_{$chatKey}";
  334. $data = RedisService::get($cacheKey);
  335. if($data){
  336. return intval($data);
  337. }
  338. $where = ['to_user_id'=> $userId,'status'=>1,'to_is_show'=>1,'is_read'=>2,'mark'=>1];
  339. if($chatKey){
  340. $where['chat_key'] = $chatKey;
  341. }
  342. $data = $this->model->where($where)->count('id');
  343. $data = $data? intval($data) : 0;
  344. RedisService::set($cacheKey, $data, rand(3, 5));
  345. return $data;
  346. }
  347. /**
  348. * 今日消息数量
  349. * @param $userId
  350. * @return array|mixed
  351. */
  352. public function getCount($userId, $chatKey=0)
  353. {
  354. $where = ['to_user_id'=> $userId,'status'=>1,'to_is_show'=>1,'mark'=>1];
  355. if($chatKey){
  356. $where['chat_key'] = $chatKey;
  357. }
  358. $data = $this->model->where($where)->where('create_time','>=', time() - 2 * 86400)->count('id');
  359. $data = $data? intval($data) : 0;
  360. return $data;
  361. }
  362. /**
  363. * 验证是否还有聊天次数或者时长
  364. * @param $fromUserId 发送或发起用户ID
  365. * @param $toUserId 聊天对方用户ID
  366. * @param int $chatType 聊天类型:1-文字类型,2-视频/语音
  367. * @return mixed
  368. */
  369. public function checkChat($fromUserId, $toUserId, $chatType=1)
  370. {
  371. // 如果是客服,发送不限制
  372. $checkIsCustom = MemberModel::where(['id'=> $fromUserId,'mark'=>1])->value('is_custom');
  373. if($checkIsCustom == 1){
  374. return true;
  375. }
  376. $params = ImChatParamsModel::where(['from_user_id'=> $fromUserId, 'to_user_id'=> $toUserId,'mark'=>1])
  377. ->select(['id','chat_num','chat_time'])
  378. ->first();
  379. $params = $params? $params->toArray() : [];
  380. $chatNum = isset($params['chat_num'])? intval($params['chat_num']) : 0;
  381. $chatTime = isset($params['chat_time'])? intval($params['chat_time']) : 0;
  382. // 未购买过次数的获取系统默认免费参数
  383. if(empty($params)) {
  384. $chatNum = ConfigService::make()->getConfigByCode('chat_free_num');
  385. $chatTime = ConfigService::make()->getConfigByCode('chat_free_time');
  386. $chatTime = $chatTime*60;
  387. if($chatNum>0) {
  388. $data = [
  389. 'from_user_id'=> $fromUserId,
  390. 'to_user_id'=> $toUserId,
  391. 'chat_num'=> $chatNum>0? intval($chatNum) : 0,
  392. 'chat_time'=> $chatTime>0? intval($chatTime) : 0,
  393. 'update_time'=> time(),
  394. 'remark'=> '更新系统免费参数',
  395. 'mark'=> 1
  396. ];
  397. ImChatParamsModel::insert($data);
  398. }
  399. }
  400. return $chatType==1? $chatNum : $chatTime;
  401. }
  402. /**
  403. * 更新聊天参数
  404. * @param $fromUserId
  405. * @param $toUserId
  406. * @param $num
  407. * @param int $chatType
  408. */
  409. public function updateChatParams($fromUserId, $toUserId, $num=1, $chatType=1)
  410. {
  411. if(empty($fromUserId) || empty($toUserId)){
  412. return false;
  413. }
  414. if(!$id = ImChatParamsModel::where(['from_user_id'=> $fromUserId, 'to_user_id'=> $toUserId,'mark'=>1])->value('id'))
  415. {
  416. $chatNum = ConfigService::make()->getConfigByCode('chat_free_num');
  417. $chatTime = ConfigService::make()->getConfigByCode('chat_free_time');
  418. $chatTime = $chatTime*60;
  419. $data = [
  420. 'from_user_id'=> $fromUserId,
  421. 'to_user_id'=> $toUserId,
  422. 'chat_num'=> $chatNum>0? intval($chatNum) : 0,
  423. 'chat_time'=> $chatTime>0? intval($chatTime) : 0,
  424. 'update_time'=> time(),
  425. 'remark'=> '更新系统免费参数',
  426. 'mark'=> 1
  427. ];
  428. return ImChatParamsModel::insertGetId($data);
  429. }else{
  430. if($chatType == 1){
  431. return ImChatParamsModel::where(['from_user_id'=> $fromUserId, 'to_user_id'=> $toUserId,'mark'=>1])
  432. ->decrement('chat_num', $num);
  433. }else{
  434. return ImChatParamsModel::where(['from_user_id'=> $fromUserId, 'to_user_id'=> $toUserId,'mark'=>1])
  435. ->decrement('chat_time', $num);
  436. }
  437. }
  438. }
  439. /**
  440. * 已读
  441. * @param $userId 用户ID
  442. * @param $chatKey 聊天窗口ID
  443. * @return bool
  444. */
  445. public function setRead($userId, $chatKey)
  446. {
  447. $this->model->where(['chat_key'=> $chatKey])
  448. ->where(['to_user_id'=>$userId])
  449. ->update(['is_read'=>1,'update_time'=>time()]);
  450. // 清除缓存
  451. RedisService::keyDel("caches:chats:history*");
  452. RedisService::keyDel("caches:chats:group*");
  453. RedisService::keyDel("caches:messages:bar*");
  454. return true;
  455. }
  456. /**
  457. * 隐藏
  458. * @param $userId 用户ID
  459. * @param $chatKey 聊天窗口ID
  460. * @return bool
  461. */
  462. public function setHide($userId, $chatKey)
  463. {
  464. $this->model->where(['chat_key'=> $chatKey])
  465. ->where(['from_user_id'=>$userId])
  466. ->update(['from_is_show'=>2,'is_read'=>1,'update_time'=>time()]);
  467. $this->model->where(['chat_key'=> $chatKey])
  468. ->where(['to_user_id'=>$userId])
  469. ->update(['to_is_show'=>2,'is_read'=>1,'update_time'=>time()]);
  470. // 清除缓存
  471. RedisService::keyDel("caches:chats:history*");
  472. RedisService::keyDel("caches:chats:group*");
  473. RedisService::keyDel("caches:messages:bar*");
  474. return true;
  475. }
  476. /**
  477. * 按窗口清除
  478. * @param $userId 用户ID
  479. * @param $chatKey 聊天窗口KEY
  480. * @return bool
  481. */
  482. public function clear($userId, $chatKey)
  483. {
  484. $this->model->where(['from_user_id'=>$userId,'chat_key'=>$chatKey])->update(['from_is_show'=>2,'is_read'=>1,'update_time'=>time()]);
  485. $this->model->where(['to_user_id'=>$userId,'chat_key'=>$chatKey])->update(['to_is_show'=>2,'is_read'=>1,'update_time'=>time()]);
  486. return true;
  487. }
  488. /**
  489. * 清除历史
  490. * @param $userId
  491. * @param $type
  492. * @return mixed
  493. */
  494. public function clearAll($userId, $type=1)
  495. {
  496. if($type == 1){
  497. // 列表窗
  498. $this->model->where(['from_user_id'=>$userId])->update(['from_is_show'=>2,'is_read'=>1,'update_time'=>time()]);
  499. $this->model->where(['to_user_id'=>$userId])->update(['to_is_show'=>2,'is_read'=>1,'update_time'=>time()]);
  500. }else{
  501. // 删除
  502. $this->model->where('mark', 1)->where(function($query) use($userId){
  503. $query->where(['from_user_id'=>$userId])->whereOr(['to_user_id'=>$userId]);
  504. })->update(['mark'=>0,'update_time'=>time()]);
  505. }
  506. // 删除历史
  507. $this->model->where('mark', 0)->where('update_time','<', time() - 7*86400)->delete();
  508. // 站内消息
  509. MessageService::make()->clearAll($userId);
  510. return true;
  511. }
  512. /**
  513. * 聊天付费
  514. * @param $userId
  515. * @param $params
  516. * @return bool
  517. */
  518. public function buyChat($userId, $params)
  519. {
  520. $userInfo = MemberService::make()->getCacheInfo(['id'=>$userId,'status'=>1]);
  521. $balance = isset($userInfo['balance'])? floatval($userInfo['balance']) : 0;
  522. if(empty($userInfo)){
  523. $this->error = 2401;
  524. return false;
  525. }
  526. $chatType = isset($params['type'])? intval($params['type']) : 0;
  527. $configKey = $chatType==1? 'chat_buy_num_money':'chat_buy_time_money';
  528. $config = ConfigService::make()->getConfigByCode($configKey);
  529. $config = $config? explode('/', $config) : [];
  530. $money = isset($config[0])?floatval($config[0]) : 0;
  531. $num = isset($config[1])?intval($config[1]) : 0;
  532. $fromUserId = isset($params['from_user_id'])? intval($params['from_user_id']) : 0;
  533. $toUserId = isset($params['to_user_id'])? intval($params['to_user_id']) : 0;
  534. if($money<=0 || $num<=0 || $toUserId<=0 || $fromUserId<=0){
  535. $this->error = 2402;
  536. return false;
  537. }
  538. // 验证余额是否足够
  539. if($balance < $money){
  540. $this->error = 2403;
  541. return false;
  542. }
  543. // 购买数量
  544. DB::beginTransaction();
  545. if(!MemberModel::where(['id'=> $userId])->decrement('balance', $money)){
  546. DB::rollBack();
  547. $this->error = 2404;
  548. return false;
  549. }
  550. // 账户明细
  551. $orderNo = get_order_num('CH');
  552. $data = [
  553. 'user_id'=> $userId,
  554. 'source_order_no'=> $orderNo,
  555. 'source_uid'=> $toUserId,
  556. 'type'=> 8,
  557. 'coin_type'=> 4,
  558. 'user_type'=> 1,
  559. 'money'=> $money,
  560. 'balance'=> $balance,
  561. 'create_time'=> time(),
  562. 'update_time'=> time(),
  563. 'remark'=> $chatType==1? "聊天付费购买{$num}次":"视频聊天付费购买{$num}分钟",
  564. 'status'=> 1,
  565. ];
  566. if(!AccountLogModel::insertGetId($data)){
  567. DB::rollBack();
  568. $this->error = 2405;
  569. return false;
  570. }
  571. // 聊天参数处理
  572. if($chatType == 1){
  573. $data = ['chat_num'=> DB::raw("chat_num + {$num}"),'update_time'=> time()];
  574. if(!ImChatParamsModel::where(['from_user_id'=> $fromUserId, 'to_user_id'=> $toUserId,'mark'=>1])
  575. ->update($data)){
  576. DB::rollBack();
  577. $this->error = 2405;
  578. return false;
  579. }
  580. }else{
  581. $time = $num*60;
  582. $data = ['chat_time'=> DB::raw("chat_time + {$time}"),'update_time'=> time()];
  583. if(!ImChatParamsModel::where(['from_user_id'=> $fromUserId, 'to_user_id'=> $toUserId,'mark'=>1])
  584. ->update($data)){
  585. DB::rollBack();
  586. $this->error = 2406;
  587. return false;
  588. }
  589. }
  590. $this->error = 2407;
  591. DB::commit();
  592. $log = [
  593. 'user_id'=> 0,
  594. 'merch_id'=> 0,
  595. 'source_uid'=> $userId,
  596. 'source_order_no'=> $orderNo,
  597. 'type'=> 13,
  598. 'coin_type'=> 2,
  599. 'user_type'=> 5,
  600. 'money'=> $money,
  601. 'balance'=> 0,
  602. 'create_time'=> time(),
  603. 'update_time'=> time(),
  604. 'remark'=> '平台聊天收入',
  605. 'status'=>1,
  606. 'mark'=>1
  607. ];
  608. if(!AccountLogModel::insertGetId($log)){
  609. $this->error = 2641;
  610. return false;
  611. }
  612. // 平台流水
  613. FinanceService::make()->saveLog(0, $money, 1, 1);
  614. return true;
  615. }
  616. /**
  617. * 待通知消息
  618. * @param $noticeUserId
  619. * @return array|mixed
  620. */
  621. public function countByWait($noticeUserId)
  622. {
  623. $cacheKey = "caches:chat:counts:user_{$noticeUserId}";
  624. $count = RedisService::get($cacheKey);
  625. if($count){
  626. return $count;
  627. }
  628. $where = ['to_user_id'=> $noticeUserId,'status'=>1,'msg_type'=> 1,'mark'=>1];
  629. $count = $this->model->where($where)->where('create_time','>=',time() - 86400)->count('id');
  630. RedisService::set($cacheKey, $count, rand(3,5));
  631. return $count;
  632. }
  633. }