Procházet zdrojové kódy

新增知识库协作更新通知

kuaifan před 5 roky
rodič
revize
6b06da7e8c

+ 37 - 0
app/Http/Controllers/Api/DocsController.php

@@ -5,7 +5,10 @@ namespace App\Http\Controllers\Api;
 use App\Http\Controllers\Controller;
 use App\Module\Base;
 use App\Module\Users;
+use App\Tasks\PushTask;
+use Cache;
 use DB;
+use Hhxsv5\LaravelS\Swoole\Task\Task;
 use Request;
 
 /**
@@ -384,6 +387,40 @@ class DocsController extends Controller
             'username' => $user['username'],
             'indate' => Base::time()
         ]);
+        //通知正在编辑的成员
+        $sid = $id;
+        $array = Base::json2array(Cache::get("docs::" . $sid));
+        if ($array) {
+            foreach ($array as $uname => $vbody) {
+                if (intval($vbody['indate']) + 20 < time()) {
+                    unset($array[$uname]);
+                }
+            }
+        }
+        $pushLists = [];
+        foreach ($array AS $tuser) {
+            $uLists = Base::DBC2A(DB::table('ws')->select(['fd', 'username', 'channel'])->where('username', $tuser['username'])->get());
+            foreach ($uLists AS $item) {
+                if ($item['username'] == $user['username']) {
+                    continue;
+                }
+                $pushLists[] = [
+                    'fd' => $item['fd'],
+                    'msg' => [
+                        'messageType' => 'docs',
+                        'body' => [
+                            'type' => 'update',
+                            'sid' => $sid,
+                            'nickname' => $user['nickname'] ?: $user['username'],
+                            'time' => time(),
+                        ]
+                    ]
+                ];
+            }
+        }
+        $pushTask = new PushTask($pushLists);
+        Task::deliver($pushTask);
+        //
         return Base::retSuccess('保存成功!');
     }
 }

+ 1 - 1
app/Module/Chat.php

@@ -196,7 +196,7 @@ class Chat
             $array = [];
         }
         //messageType来自客户端(前端->后端):refresh/unread/read/roger/user/info/team/docs
-        //messageType来自服务端(后端->前端):error/open/kick/user/back/unread
+        //messageType来自服务端(后端->前端):error/open/kick/user/back/unread/docs
         if (!isset($array['messageType'])) $array['messageType'] = '';  //消息类型
         if (!isset($array['messageId'])) $array['messageId'] = '';      //消息ID(用于back给客户端)
         if (!isset($array['contentId'])) $array['contentId'] = 0;       //消息数据ID(用于roger给服务端)

+ 41 - 15
app/Services/WebSocketService.php

@@ -300,23 +300,49 @@ class WebSocketService implements WebSocketHandlerInterface
              */
             case 'docs':
                 $back['message'] = [];
-                if ($data['body']['type'] === 'enter') {
-                    $sid = intval($data['body']['sid']);
-                    if ($sid > 0) {
-                        $array = Base::json2array(Cache::get("docs::" . $sid));
-                        if ($array) {
-                            foreach ($array AS $uname => $vbody) {
-                                if (intval($vbody['indate']) + 10 < time()) {
-                                    unset($array[$uname]);
-                                }
-                            }
+                $body = $data['body'];
+                $type = $body['type'];
+                $sid = intval($body['sid']);
+                if ($sid <= 0) {
+                    return;
+                }
+                $array = Base::json2array(Cache::get("docs::" . $sid));
+                if ($array) {
+                    foreach ($array as $uname => $vbody) {
+                        if (intval($vbody['indate']) + 20 < time()) {
+                            unset($array[$uname]);
+                        }
+                    }
+                }
+                if ($type == 'enter' || $type == 'refresh') {
+                    $array[$body['username']] = $body;
+                } elseif ($type == 'quit') {
+                    unset($array[$body['username']]);
+                }
+                //
+                Cache::put("docs::" . $sid, Base::array2json($array), 30);
+                ksort($array);
+                $back['message'] = array_values($array);
+                //
+                if ($type == 'enter' || $type == 'quit') {
+                    $pushLists = [];
+                    foreach ($back['message'] AS $tuser) {
+                        foreach ($this->getUserOfName($tuser['username']) AS $item) {
+                            $pushLists[] = [
+                                'fd' => $item['fd'],
+                                'msg' => [
+                                    'messageType' => 'docs',
+                                    'body' => [
+                                        'type' => 'users',
+                                        'sid' => $sid,
+                                        'lists' => $back['message']
+                                    ]
+                                ]
+                            ];
                         }
-                        $array[$data['body']['username']] = $data['body'];
-                        Cache::put("docs::" . $sid, Base::array2json($array), 30);
-                        //
-                        ksort($array);
-                        $back['message'] = array_values($array);
                     }
+                    $pushTask = new PushTask($pushLists);
+                    Task::deliver($pushTask);
                 }
                 break;
         }