kuaifan 5 years ago
parent
commit
c637a95ad5

+ 1 - 0
app/Http/Controllers/Api/ChatController.php

@@ -102,6 +102,7 @@ class ChatController extends Controller
         $lists = Base::getPageList($lists, false);
         //
         foreach ($lists['lists'] AS $key => $item) {
+            $lists['lists'][$key]['userimg'] = Users::userimg($item['username']);
             $lists['lists'][$key]['message'] = Base::string2array($item['message']);
         }
         //

+ 0 - 3
app/Http/Controllers/Api/UsersController.php

@@ -313,9 +313,6 @@ class UsersController extends Controller
         }
         //用户名
         $username = trim(Request::input('username'));
-        if (in_array($username, ['__loadIng'])) {
-            return Base::retError('用户名包含非法字符串!');
-        }
         if (strlen($username) < 2) {
             return Base::retError('用户名不可以少于2个字符!');
         } elseif (strlen($username) > 16) {

+ 4 - 3
app/Module/Chat.php

@@ -2,11 +2,8 @@
 
 namespace App\Module;
 
-use App\Model\DBCache;
 use Cache;
 use DB;
-use Request;
-use Session;
 
 /**
  * Class Chat
@@ -77,6 +74,10 @@ class Chat
         }
         //
         $indate = abs($message['indate'] - time()) > 30 ? time() : $message['indate'];
+        if (isset($message['id'])) unset($message['id']);
+        if (isset($message['username'])) unset($message['username']);
+        if (isset($message['userimg'])) unset($message['userimg']);
+        if (isset($message['indate'])) unset($message['indate']);
         $inArray = [
             'did' => $dialog['id'],
             'username' => $username,

+ 4 - 4
app/Module/Users.php

@@ -222,15 +222,15 @@ class Users
 
     /**
      * 用户头像,不存在时返回默认
-     * @param string|int $var 头像地址 或 会员ID
+     * @param string $var 头像地址 或 会员用户名
      * @return \Illuminate\Contracts\Routing\UrlGenerator|string
      */
     public static function userimg($var) {
-        if (Base::isNumber($var)) {
+        if (!Base::strExists($var, '.')) {
             if (empty($var)) {
                 $var = "";
-            }else{
-                $userInfo = self::userid2basic($var);
+            } else {
+                $userInfo = self::username2basic($var);
                 $var = $userInfo['userimg'];
             }
         }

+ 4 - 4
resources/assets/js/main/components/UserView.vue

@@ -57,7 +57,7 @@
             getUserData(num, cacheTime) {
                 let keyName = '__name:' + this.username.substring(0, 1) + '__';
                 let localData = $A.jsonParse(window.localStorage[keyName]);
-                if (localData.__loadIng === true) {
+                if (window.__userViewNetworking === true) {
                     if (num < 100) {
                         setTimeout(() => {
                             this.getUserData(num + 1, cacheTime)
@@ -77,7 +77,7 @@
                     return;
                 }
                 //
-                localData.__loadIng = true;
+                window.__userViewNetworking = true;
                 $A.aAjax({
                     url: 'users/basic',
                     data: {
@@ -87,8 +87,8 @@
                         localData[this.username].success = false;
                         localData[this.username].update = 0;
                         localData[this.username].data = {};
-                        localData.__loadIng = false;
                         window.localStorage[keyName] = $A.jsonStringify(localData);
+                        window.__userViewNetworking = false;
                     },
                     success: (res) => {
                         if (res.ret === 1) {
@@ -104,8 +104,8 @@
                             localData[this.username].update = 0;
                             localData[this.username].data = {};
                         }
-                        localData.__loadIng = false;
                         window.localStorage[keyName] = $A.jsonStringify(localData);
+                        window.__userViewNetworking = false;
                     }
                 });
             }

+ 15 - 1
resources/assets/js/main/components/chat/Index.vue

@@ -541,6 +541,9 @@
                             res.data.lists.reverse().forEach((item) => {
                                 this.addMessageData(Object.assign(item.message, {
                                     id: item.id,
+                                    username: item.username,
+                                    userimg: item.userimg,
+                                    indate: item.indate,
                                 }));
                             });
                             this.messageNoDataText = this.$L("没有相关的数据");
@@ -639,6 +642,18 @@
 
             addMessageData(data, animation = false) {
                 data.self = data.username === this.userInfo.username;
+                let sikp = false;
+                if (data.id) {
+                    this.messageLists.some((item, index) => {
+                        if (item.id == data.id) {
+                            this.messageLists.splice(index, 1, data);
+                            return sikp = true;
+                        }
+                    });
+                    if (sikp) {
+                        return;
+                    }
+                }
                 this.messageLists.push(data);
                 //
                 if (this.autoBottom) {
@@ -662,7 +677,6 @@
                 let text = this.messageText.trim();
                 if ($A.count(text) > 0) {
                     let data = {
-                        id: 0,
                         type: 'text',
                         username: this.userInfo.username,
                         userimg: this.userInfo.userimg,

+ 1 - 0
resources/assets/js/main/components/chat/message.vue

@@ -128,6 +128,7 @@
             height: 38px;
             margin-left: 8px;
             margin-right: 8px;
+            border-radius: 3px;
         }
         .item-error {
             cursor: pointer;

+ 12 - 4
resources/assets/js/main/main.js

@@ -393,18 +393,26 @@ import '../../sass/main.scss';
              * @param target    接收方的标识,type=all时此项无效
              * @param content   发送内容
              * @param callback  发送回调
+             * @param isAgain
              */
-            sendTo(type, target, content, callback) {
+            sendTo(type, target, content, callback, isAgain = false) {
                 if (this.__instance === null) {
-                    console.log("[WS] 未初始化连接");
-                    return;
+                    console.log("[WS] 服务未连接");
+                    if (isAgain === true) {
+                        typeof callback === "function" && callback({status: 0, message: '服务未连接'});
+                        return;
+                    }
+                    this.connection();
+                    setTimeout(() => { this.sendTo(type, target, content, callback, true); }, 6000);
                 }
                 if (this.__connected === false) {
                     console.log("[WS] 未连接成功");
+                    typeof callback === "function" && callback({status: 0, message: '未连接成功'});
                     return;
                 }
                 if (['user', 'all'].indexOf(type) === -1) {
                     console.log("[WS] 错误的消息类型-" + type);
+                    typeof callback === "function" && callback({status: 0, message: '错误的消息类型-' + type});
                     return;
                 }
                 let messageId = '';
@@ -429,7 +437,7 @@ import '../../sass/main.scss';
              */
             close() {
                 if (this.__instance === null) {
-                    console.log("[WS] 未初始化连接");
+                    console.log("[WS] 服务未连接");
                     return;
                 }
                 if (this.__connected === false) {