kuaifan 5 éve
szülő
commit
51c954c5b2

+ 17 - 2
app/Http/Controllers/Api/UsersController.php

@@ -114,11 +114,26 @@ class UsersController extends Controller
     /**
      * 获取指定会员基本信息
      *
-     * @apiParam {String} username           会员用户名
+     * @apiParam {String|jsonArray} username          会员用户名(多个格式:jsonArray,一次最多30个)
      */
     public function basic()
     {
-        return Base::retSuccess('success', Users::username2basic(trim(Request::input('username'))));
+        $username = trim(Request::input('username'));
+        $array = Base::json2array($username);
+        if (empty($array)) {
+            $array[] = $username;
+        }
+        if (count($array) > 50) {
+            return Base::retError(['一次最多只能获取%条数据!', 50]);
+        }
+        $retArray = [];
+        foreach ($array AS $name) {
+            $basic = Users::username2basic($name);
+            if ($basic) {
+                $retArray[] = $basic;
+            }
+        }
+        return Base::retSuccess('success', $retArray);
     }
 
     /**

+ 13 - 68
resources/assets/js/main/components/UserView.vue

@@ -42,84 +42,29 @@
             }
         },
         mounted() {
-            this.getUserData(0, 300);
+            this.getUserData(300);
         },
         watch: {
             username() {
-                this.getUserData(0, 300);
+                this.getUserData(300);
             },
         },
         methods: {
             popperShow() {
-                this.getUserData(0, 60)
+                this.getUserData(30)
             },
 
-            getUserData(num, cacheTime) {
-                let keyName = '__userName:' + this.username.substring(0, 1) + '__';
-                let localData = $A.jsonParse(window.localStorage[keyName]);
-                //
-                if (window.__userViewNetworking === true) {
-                    if (num == 0) {
-                        if (typeof localData[this.username] !== "object") {
-                            localData[this.username] = {};
-                        }
-                        if (localData[this.username].success === true) {
-                            this.nickname = localData[this.username].data.nickname;
-                            this.profession = localData[this.username].data.profession;
-                        }
+            getUserData(cacheTime) {
+                $A.getUserBasic(this.username, (data, success) => {
+                    if (success) {
+                        this.nickname = data.nickname;
+                        this.profession = data.profession;
+                    } else {
+                        this.nickname = '';
+                        this.profession = '';
                     }
-                    if (num < 100) {
-                        setTimeout(() => {
-                            this.getUserData(num + 1, cacheTime)
-                        }, 500);
-                    }
-                    return;
-                }
-                //
-                if (typeof localData[this.username] !== "object") {
-                    localData[this.username] = {};
-                }
-                if (localData[this.username].success === true) {
-                    this.nickname = localData[this.username].data.nickname;
-                    this.profession = localData[this.username].data.profession;
-                    this.$emit("on-result", localData[this.username].data);
-                    if (localData[this.username].update + cacheTime > Math.round(new Date().getTime() / 1000)) {
-                        return;
-                    }
-                }
-                //
-                window.__userViewNetworking = true;
-                $A.aAjax({
-                    url: 'users/basic',
-                    data: {
-                        username: this.username,
-                    },
-                    error: () => {
-                        localData[this.username].success = false;
-                        localData[this.username].update = 0;
-                        localData[this.username].data = {};
-                        window.localStorage[keyName] = $A.jsonStringify(localData);
-                        window.__userViewNetworking = false;
-                    },
-                    success: (res) => {
-                        if (res.ret === 1) {
-                            this.nickname = res.data.nickname;
-                            this.profession = res.data.profession;
-                            localData[this.username].success = true;
-                            localData[this.username].update = Math.round(new Date().getTime() / 1000);
-                            localData[this.username].data = res.data;
-                        } else {
-                            this.nickname = '';
-                            this.profession = '';
-                            localData[this.username].success = false;
-                            localData[this.username].update = 0;
-                            localData[this.username].data = {};
-                        }
-                        this.$emit("on-result", localData[this.username].data);
-                        window.localStorage[keyName] = $A.jsonStringify(localData);
-                        window.__userViewNetworking = false;
-                    }
-                });
+                    this.$emit("on-result", data);
+                }, cacheTime);
             }
         }
     }

+ 105 - 0
resources/assets/js/main/main.js

@@ -181,6 +181,111 @@ import '../../sass/main.scss';
         },
 
         /**
+         * 根据用户名获取用户基本信息
+         * @param username
+         * @param callback
+         * @param cacheTime
+         */
+        getUserBasic(username, callback, cacheTime = 300) {
+            if (typeof callback !== "function") {
+                return;
+            }
+            if (!username) {
+                callback({}, false);
+                return;
+            }
+            //
+            let keyName = '__userName:' + username.substring(0, 1) + '__';
+            let localData = $A.jsonParse(window.localStorage[keyName]);
+            if ($A.getObject(localData, username + '.success') === true) {
+                callback(localData[username].data, true);
+                if (localData[username].update + cacheTime > Math.round(new Date().getTime() / 1000)) {
+                    return;
+                }
+            }
+            //
+            $A.__userBasicObject.push({
+                username: username,
+                callback: callback
+            });
+            $A.__userBasicEvent();
+        },
+        __userBasicEvent() {
+            if ($A.__userBasicLoading === true) {
+                return;
+            }
+            $A.__userBasicLoading = true;
+            //
+            let userArray = [];
+            $A.__userBasicObject.some((item) => {
+                userArray.push(item.username);
+                if (userArray.length >= 30) {
+                    return true;
+                }
+            });
+            //
+            $A.aAjax({
+                url: 'users/basic',
+                data: {
+                    username: $A.jsonStringify(userArray),
+                },
+                error: () => {
+                    userArray.forEach((username) => {
+                        let tmpLists = $A.__userBasicObject.filter((item) => { return item.username == username });
+                        tmpLists.forEach((item) => {
+                            if (typeof item.callback === "function") {
+                                item.callback({}, false);
+                                item.callback = null;
+                            }
+                        });
+                    });
+                    //
+                    $A.__userBasicLoading = false;
+                    $A.__userBasicObject = $A.__userBasicObject.filter((item) => { return typeof item.callback === "function"});
+                    if ($A.__userBasicObject.length > 0) {
+                        $A.__userBasicEvent();
+                    }
+                },
+                success: (res) => {
+                    if (res.ret === 1) {
+                        res.data.forEach((data) => {
+                            let keyName = '__userName:' + data.username.substring(0, 1) + '__';
+                            let localData = $A.jsonParse(window.localStorage[keyName]);
+                            localData[data.username] = {
+                                success: true,
+                                update: Math.round(new Date().getTime() / 1000),
+                                data: data
+                            };
+                            window.localStorage[keyName] = $A.jsonStringify(localData);
+                        });
+                    }
+                    userArray.forEach((username) => {
+                        let tmpLists = $A.__userBasicObject.filter((item) => { return item.username == username });
+                        tmpLists.forEach((item) => {
+                            if (typeof item.callback === "function") {
+                                let info = res.data.filter((data) => { return data.username == username });
+                                if (info.length === 0) {
+                                    item.callback({}, false);
+                                } else {
+                                    item.callback(info[0], true);
+                                }
+                                item.callback = null;
+                            }
+                        });
+                    });
+                    //
+                    $A.__userBasicLoading = false;
+                    $A.__userBasicObject = $A.__userBasicObject.filter((item) => { return typeof item.callback === "function"});
+                    if ($A.__userBasicObject.length > 0) {
+                        $A.__userBasicEvent();
+                    }
+                }
+            });
+        },
+        __userBasicLoading: false,
+        __userBasicObject: [],
+
+        /**
          * 打开登录页面
          */
         userLogout() {

+ 1 - 0
resources/lang/en/general.php

@@ -113,4 +113,5 @@ return [
     "不能操作自己!" => "Can't operate oneself!",
     "成员不存在!" => "Member does not exist!",
     "操作成功!" => "Operation successful!",
+    "一次最多只能获取%条数据!" => "A maximum of % data can be retrieved at one time!",
 ];

+ 2 - 2
resources/notify/chrome/js/base.js

@@ -56,13 +56,13 @@
      * @param url
      * @returns {string}
      */
-    getHostname(url) {
+    getHost(url) {
         if (/^chrome:\/\//.test(url)) {
             return "";
         }
         try {
             var info = new URL(url);
-            return info.hostname;
+            return info.host || info.hostname;
         } catch (err) {
             console.log(err);
         }

+ 2 - 2
resources/notify/chrome/js/notify.js

@@ -63,7 +63,7 @@ const getBadgeNum = function () {
                     case 'user':
                         instances[key].unread++;
                         chrome.tabs.query({active: true, currentWindow: true}, function (tabs) {
-                            if ($A.getHostname(tabs[0].url) != key) {
+                            if ($A.getHost(tabs[0].url) != key) {
                                 var url = 'http://' + key + '/#/todo?token=' + encodeURIComponent(instances[key].token) + '&open=chat';
                                 $A.showNotify(key, {
                                     body: $A.getMsgDesc(body),
@@ -100,7 +100,7 @@ chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
     var configLists;
     if (request.act === "config") {
         if (sender.tab) {
-            var hostname = $A.getHostname(sender.tab.url);
+            var hostname = $A.getHost(sender.tab.url);
             if (hostname) {
                 configLists = $A.jsonParse($A.getStorage("configLists"), {});
                 if (typeof configLists[hostname] !== "object") {

+ 1 - 1
resources/notify/chrome/js/popup.js

@@ -51,7 +51,7 @@ function showLists(lists) {
         chrome.tabs.query({}, function (tabs) {
             var has = false;
             tabs.some(function (item) {
-                if ($A.getHostname(item.url) == index) {
+                if ($A.getHost(item.url) == index) {
                     chrome.windows.update(item.windowId, {focused: true});
                     chrome.tabs.highlight({tabs: item.index, windowId: item.windowId});
                     chrome.tabs.update({url: $A.urlAddParams($A.removeURLParameter(item.url, ['open', 'rand']), {open: 'chat', rand: Math.round(new Date().getTime())})});