Chat.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace App\Module;
  3. use Cache;
  4. use DB;
  5. /**
  6. * Class Chat
  7. * @package App\Module
  8. */
  9. class Chat
  10. {
  11. /**
  12. * 打开对话(创建对话)
  13. * @param string $username 发送者用户名
  14. * @param string $receive 接受者用户名
  15. * @return mixed
  16. */
  17. public static function openDialog($username, $receive)
  18. {
  19. if (!$username || !$receive) {
  20. return Base::retError('参数错误!');
  21. }
  22. $cacheKey = $username . "@" . $receive;
  23. $result = Cache::remember($cacheKey, now()->addMinutes(10), function() use ($receive, $username) {
  24. $row = Base::DBC2A(DB::table('chat_dialog')->where([
  25. 'user1' => $username,
  26. 'user2' => $receive,
  27. ])->first());
  28. if ($row) {
  29. $row['recField'] = 2; //接受信息用户的字段位置
  30. return Base::retSuccess('already1', $row);
  31. }
  32. $row = Base::DBC2A(DB::table('chat_dialog')->where([
  33. 'user1' => $receive,
  34. 'user2' => $username,
  35. ])->first());
  36. if ($row) {
  37. $row['recField'] = 1;
  38. return Base::retSuccess('already2', $row);
  39. }
  40. //
  41. DB::table('chat_dialog')->insert([
  42. 'user1' => $username,
  43. 'user2' => $receive,
  44. 'indate' => Base::time()
  45. ]);
  46. $row = Base::DBC2A(DB::table('chat_dialog')->where([
  47. 'user1' => $username,
  48. 'user2' => $receive,
  49. ])->first());
  50. if ($row) {
  51. $row['recField'] = 2;
  52. return Base::retSuccess('success', $row);
  53. }
  54. //
  55. return Base::retError('系统繁忙,请稍后再试!');
  56. });
  57. if (Base::isError($result)) {
  58. Cache::forget($cacheKey);
  59. }
  60. return $result;
  61. }
  62. /**
  63. * 保存对话消息
  64. * @param string $username 发送者用户名
  65. * @param string $receive 接受者用户名
  66. * @param array $message
  67. * @return mixed
  68. */
  69. public static function saveMessage($username, $receive, $message)
  70. {
  71. $dialog = self::openDialog($username, $receive);
  72. if (Base::isError($dialog)) {
  73. return $dialog;
  74. } else {
  75. $dialog = $dialog['data'];
  76. }
  77. //
  78. $indate = abs($message['indate'] - time()) > 30 ? time() : $message['indate'];
  79. if (isset($message['id'])) unset($message['id']);
  80. if (isset($message['username'])) unset($message['username']);
  81. if (isset($message['userimg'])) unset($message['userimg']);
  82. if (isset($message['indate'])) unset($message['indate']);
  83. $inArray = [
  84. 'did' => $dialog['id'],
  85. 'username' => $username,
  86. 'receive' => $receive,
  87. 'message' => Base::array2string($message),
  88. 'indate' => $indate
  89. ];
  90. //
  91. switch ($message['type']) {
  92. case 'text':
  93. $lastText = $message['text'];
  94. break;
  95. case 'image':
  96. $lastText = '[图片]';
  97. break;
  98. default:
  99. $lastText = '[未知类型]';
  100. break;
  101. }
  102. if ($lastText) {
  103. $upArray = Base::DBUP([
  104. ($dialog['recField'] == 1 ? 'unread1' : 'unread2') => 1,
  105. ]);
  106. $upArray['lasttext'] = $lastText;
  107. $upArray['lastdate'] = $indate;
  108. DB::table('chat_dialog')->where('id', $dialog['id'])->update($upArray);
  109. }
  110. $inArray['id'] = DB::table('chat_msg')->insertGetId($inArray);
  111. $inArray['message'] = $message;
  112. //
  113. return Base::retSuccess('success', $inArray);
  114. }
  115. }