Chat.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. if (mb_strlen($message['text']) > 20000) {
  111. return Base::retError("发送内容长度已超出最大限制!");
  112. }
  113. $field = ($dialog['recField'] == 1 ? 'unread1' : 'unread2');
  114. $unread = intval(DB::table('chat_dialog')->where('id', $dialog['id'])->value($field));
  115. $lastText = self::messageDesc($message);
  116. if ($lastText) {
  117. $upArray = [];
  118. if ($username != $receive) {
  119. $upArray = Base::DBUP([
  120. $field => 1,
  121. ]);
  122. $unread += 1;
  123. }
  124. $upArray['lasttext'] = mb_substr($lastText, 0, 100);
  125. $upArray['lastdate'] = $indate;
  126. if ($dialog['del1']) {
  127. $upArray['del1'] = 0;
  128. }
  129. if ($dialog['del2']) {
  130. $upArray['del2'] = 0;
  131. }
  132. DB::table('chat_dialog')->where('id', $dialog['id'])->update($upArray);
  133. if ($dialog['del1'] || $dialog['del2']) {
  134. Chat::forgetDialog($dialog['user1'], $dialog['user2']);
  135. }
  136. }
  137. $inArray['id'] = DB::table('chat_msg')->insertGetId($inArray);
  138. $inArray['message'] = $message;
  139. $inArray['unread'] = $unread;
  140. //
  141. return Base::retSuccess('success', $inArray);
  142. }
  143. /**
  144. * 格式化信息(来自接收)
  145. * @param $data
  146. * @return array
  147. */
  148. public static function formatMsgReceive($data) {
  149. return self::formatMsgData(Base::json2array($data));
  150. }
  151. /**
  152. * 格式化信息(用于发送)
  153. * @param $array
  154. * @return string
  155. */
  156. public static function formatMsgSend($array) {
  157. return Base::array2json(self::formatMsgData($array));
  158. }
  159. /**
  160. * 格式化信息
  161. * @param array $array
  162. * @return array
  163. */
  164. public static function formatMsgData($array = []) {
  165. if (!is_array($array)) {
  166. $array = [];
  167. }
  168. //messageType来自客户端(前端->后端):refresh/unread/read/roger/user/info/team/docs/appActivity
  169. //messageType来自服务端(后端->前端):error/open/kick/user/back/unread/docs
  170. if (!isset($array['messageType'])) $array['messageType'] = ''; //消息类型
  171. if (!isset($array['messageId'])) $array['messageId'] = ''; //消息ID(用于back给客户端)
  172. if (!isset($array['contentId'])) $array['contentId'] = 0; //消息数据ID(用于roger给服务端)
  173. if (!isset($array['channel'])) $array['channel'] = ''; //渠道(用于多端登录)
  174. if (!isset($array['username'])) $array['username'] = ''; //发送者
  175. if (!isset($array['target'])) $array['target'] = null; //接受者
  176. if (!isset($array['body'])) $array['body'] = []; //正文内容
  177. if (!isset($array['time'])) $array['time'] = time(); //时间
  178. //
  179. $array['contentId'] = intval($array['contentId']);
  180. if (!is_array($array['body']) || empty($array['body'])) $array['body'] = ['_' => time()];
  181. return $array;
  182. }
  183. /**
  184. * 获取跟任务有关系的(在线)用户(关注的、在项目里的、负责人、创建者)
  185. * @param $taskId
  186. * @return array
  187. */
  188. public static function getTaskUsers($taskId)
  189. {
  190. $tmpLists = Project::taskSomeUsers($taskId);
  191. if (empty($tmpLists)) {
  192. return [];
  193. }
  194. //
  195. return Base::DBC2A(DB::table('ws')->select(['fd', 'username', 'channel'])->where([
  196. ['update', '>', time() - 600],
  197. ])->whereIn('username', array_values(array_unique($tmpLists)))->get());
  198. }
  199. /**
  200. * 获取消息简述
  201. * @param array $message
  202. * @return mixed|string
  203. */
  204. public static function messageDesc($message)
  205. {
  206. switch ($message['type']) {
  207. case 'text':
  208. $lastText = $message['text'];
  209. break;
  210. case 'image':
  211. $lastText = '[图片]';
  212. break;
  213. case 'taskB':
  214. $lastText = $message['text'] . " [来自关注任务]";
  215. break;
  216. case 'report':
  217. $lastText = $message['text'] . " [来自工作报告]";
  218. break;
  219. case 'video':
  220. $lastText = '[视频通话]';
  221. break;
  222. case 'voice':
  223. $lastText = '[语音通话]';
  224. break;
  225. default:
  226. $lastText = '[未知类型]';
  227. break;
  228. }
  229. return $lastText;
  230. }
  231. }