Chat.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. * @param bool $forceRefresh 是否强制刷新缓存
  16. * @return mixed
  17. */
  18. public static function openDialog($username, $receive, $forceRefresh = false)
  19. {
  20. if (!$username || !$receive) {
  21. return Base::retError('参数错误!');
  22. }
  23. $cacheKey = $username . "@" . $receive;
  24. if ($forceRefresh === true) {
  25. Cache::forget($cacheKey);
  26. }
  27. $result = Cache::remember($cacheKey, now()->addMinutes(10), function() use ($receive, $username) {
  28. $row = Base::DBC2A(DB::table('chat_dialog')->where([
  29. 'user1' => $username,
  30. 'user2' => $receive,
  31. ])->first());
  32. if ($row) {
  33. $row['recField'] = 2; //接受者的字段位置
  34. return Base::retSuccess('already1', $row);
  35. }
  36. $row = Base::DBC2A(DB::table('chat_dialog')->where([
  37. 'user1' => $receive,
  38. 'user2' => $username,
  39. ])->first());
  40. if ($row) {
  41. $row['recField'] = 1;
  42. return Base::retSuccess('already2', $row);
  43. }
  44. //
  45. DB::table('chat_dialog')->insert([
  46. 'user1' => $username,
  47. 'user2' => $receive,
  48. 'indate' => Base::time()
  49. ]);
  50. $row = Base::DBC2A(DB::table('chat_dialog')->where([
  51. 'user1' => $username,
  52. 'user2' => $receive,
  53. ])->first());
  54. if ($row) {
  55. $row['recField'] = 2;
  56. return Base::retSuccess('success', $row);
  57. }
  58. //
  59. return Base::retError('系统繁忙,请稍后再试!');
  60. });
  61. if (Base::isError($result)) {
  62. Cache::forget($cacheKey);
  63. }
  64. return $result;
  65. }
  66. /**
  67. * 删除对话缓存
  68. * @param $username
  69. * @param $receive
  70. * @return bool
  71. */
  72. public static function forgetDialog($username, $receive)
  73. {
  74. if (!$username || !$receive) {
  75. return false;
  76. }
  77. Cache::forget($username . "@" . $receive);
  78. Cache::forget($receive . "@" . $username);
  79. return true;
  80. }
  81. /**
  82. * 保存对话消息
  83. * @param string $username 发送者用户名
  84. * @param string $receive 接受者用户名
  85. * @param array $message
  86. * @return mixed
  87. */
  88. public static function saveMessage($username, $receive, $message)
  89. {
  90. $dialog = self::openDialog($username, $receive);
  91. if (Base::isError($dialog)) {
  92. return $dialog;
  93. } else {
  94. $dialog = $dialog['data'];
  95. }
  96. //
  97. $indate = abs($message['indate'] - time()) > 60 ? time() : $message['indate'];
  98. if (isset($message['id'])) unset($message['id']);
  99. if (isset($message['username'])) unset($message['username']);
  100. if (isset($message['userimg'])) unset($message['userimg']);
  101. if (isset($message['indate'])) unset($message['indate']);
  102. $inArray = [
  103. 'did' => $dialog['id'],
  104. 'username' => $username,
  105. 'receive' => $receive,
  106. 'message' => Base::array2string($message),
  107. 'indate' => $indate
  108. ];
  109. //
  110. switch ($message['type']) {
  111. case 'text':
  112. $lastText = $message['text'];
  113. break;
  114. case 'image':
  115. $lastText = '[图片]';
  116. break;
  117. case 'taskB':
  118. $lastText = $message['text'] . " [来自关注任务]";
  119. break;
  120. case 'report':
  121. $lastText = $message['text'] . " [来自工作报告]";
  122. break;
  123. case 'video':
  124. $lastText = '[视频通话]';
  125. break;
  126. case 'voice':
  127. $lastText = '[语音通话]';
  128. break;
  129. default:
  130. $lastText = '[未知类型]';
  131. break;
  132. }
  133. if (mb_strlen($message['text']) > 20000) {
  134. return Base::retError("发送内容长度已超出最大限制!");
  135. }
  136. $field = ($dialog['recField'] == 1 ? 'unread1' : 'unread2');
  137. $unread = intval(DB::table('chat_dialog')->where('id', $dialog['id'])->value($field));
  138. if ($lastText) {
  139. $upArray = [];
  140. if ($username != $receive) {
  141. $upArray = Base::DBUP([
  142. $field => 1,
  143. ]);
  144. $unread += 1;
  145. }
  146. $upArray['lasttext'] = mb_substr($lastText, 0, 100);
  147. $upArray['lastdate'] = $indate;
  148. if ($dialog['del1']) {
  149. $upArray['del1'] = 0;
  150. }
  151. if ($dialog['del2']) {
  152. $upArray['del2'] = 0;
  153. }
  154. DB::table('chat_dialog')->where('id', $dialog['id'])->update($upArray);
  155. if ($dialog['del1'] || $dialog['del2']) {
  156. Chat::forgetDialog($dialog['user1'], $dialog['user2']);
  157. }
  158. }
  159. $inArray['id'] = DB::table('chat_msg')->insertGetId($inArray);
  160. $inArray['message'] = $message;
  161. $inArray['unread'] = $unread;
  162. //
  163. return Base::retSuccess('success', $inArray);
  164. }
  165. /**
  166. * 格式化信息(来自接收)
  167. * @param $data
  168. * @return array
  169. */
  170. public static function formatMsgReceive($data) {
  171. return self::formatMsgData(Base::json2array($data));
  172. }
  173. /**
  174. * 格式化信息(用于发送)
  175. * @param $array
  176. * @return string
  177. */
  178. public static function formatMsgSend($array) {
  179. return Base::array2json(self::formatMsgData($array));
  180. }
  181. /**
  182. * 格式化信息
  183. * @param array $array
  184. * @return array
  185. */
  186. public static function formatMsgData($array = []) {
  187. if (!is_array($array)) {
  188. $array = [];
  189. }
  190. //messageType来自客户端(前端->后端):refresh/unread/read/roger/user/info/team/docs
  191. //messageType来自服务端(后端->前端):error/open/kick/user/back/unread
  192. if (!isset($array['messageType'])) $array['messageType'] = ''; //消息类型
  193. if (!isset($array['messageId'])) $array['messageId'] = ''; //消息ID(用于back给客户端)
  194. if (!isset($array['contentId'])) $array['contentId'] = 0; //消息数据ID(用于roger给服务端)
  195. if (!isset($array['channel'])) $array['channel'] = ''; //渠道(用于多端登录)
  196. if (!isset($array['username'])) $array['username'] = ''; //发送者
  197. if (!isset($array['target'])) $array['target'] = null; //接受者
  198. if (!isset($array['body'])) $array['body'] = []; //正文内容
  199. if (!isset($array['time'])) $array['time'] = time(); //时间
  200. //
  201. $array['contentId'] = intval($array['contentId']);
  202. if (!is_array($array['body']) || empty($array['body'])) $array['body'] = ['_' => time()];
  203. return $array;
  204. }
  205. }