沈丘营商办后台前端项目
346149741
2024-06-18 9fb6a0ff49c2af567be2e3adaf93c4c301b3f102
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// import axios from "axios";
import useWebSockStore from "@/store/modules/websock";
 
import { WebSockCode, useType } from "./index";
export default useType;
/**
 *webSockHelper 辅助类
 */
export class webSockHelper extends WebSockCode {
  useSockStore = useWebSockStore();
  constructor(_useType: useType, url: string, userId?: string) {
    super(url, _useType, userId || "", "");
    this.useSockStore.webSock = this.settingLong();
    this.onMessage = this.onMessageWebsock;
  }
  /**
   * 初始化连接
   */
  init = (_useType?: useType, fnc?: () => void) => {
    if (!this.useSockStore.webSock || _useType == useType.WEB) {
      this.createdWebSock(this.jointUrl());
      this.onMessage = this.onMessageWebsock;
      this.useSockStore.webSock = this.webSock;
      if (fnc != null) {
        this.updataSessions();
        fnc();
      }
    }
    if (_useType == useType.LCON) {
      // console.log("创建长连接");
      this.useSockStore.webSock = this.settingLong();
    }
    return this.useSockStore.webSock;
  };
 
  private onMessageWebsock = (e?: any) => {
    let data = JSON.parse(e.data);
    // console.log("sdasdsd", data);
    if (data.id == this.useSockStore.sessionObj.id && this.useSockStore.isopen) {
      this.getMsgList(1);
    } else {
      // console.log("更细未读数量", data);
      let index = this.useSockStore.sessionList.findIndex((f) => f.id == data.id); //更新会话对象
      // console.log("更细未读数量1", index);
      if (index > -1) {
        this.useSockStore.sessionList[index].unReadCount = data.unReadCount;
        this.useSockStore.updateUnread();
      } else {
        this.updataSessions();
      }
    }
    // return data;
  };
  /**
   * 是否可以加载数据
   */
  private isLoadData = (type = 0) => {
    // console.log("type=======>", type);
    if (type == 1) {
      return true;
    }
    //当前会话不能为空
    if (!this.useSockStore.sessionObj.toUserId || !this.useSockStore.sessionObj.userId) {
      return false;
    }
    if (this.pageNum > 1) {
      if (this.useSockStore.msgList.length == this.total) {
        this.pageNum = 1;
        return false;
      }
    }
    return true;
  };
 
  /**
   * 加载消息记录
   * @param type 1 推送
   * @returns
   */
  public getMsgList = (type = 0) => {
    return new Promise<void>(async (resolve, reject) => {
      if (this.isLoadData(type)) {
        this.getMessages(type).then((res: any[]) => {
          let _rows = res;
          if (type == 0) {
            let list = _rows!.reverse();
            if (this.pageNum > 1 && list.length > 0) {
              this.useSockStore.msgList = list.concat(this.useSockStore.msgList); //
            } else {
              this.useSockStore.msgList = list;
              this.toScrollBottom();
            }
          } else {
            //获取推送的消息
            if (_rows.length > 0) {
              this.useSockStore.msgList.push(_rows[0]);
              this.toScrollBottom();
            }
          }
          if (_rows.length > 0 && this.pageNum == 1) {
            this.updataSession();
          }
          resolve();
        });
      } else {
        reject("加载异常");
      }
    });
  };
 
  /**
   * 更细会话未读数量
   * @returns
   */
  public updataSession = () => {
    return new Promise<any>(async (resolve, reject) => {
      this.getSessionInfo()
        .then((data: any) => {
          this.updataUnReadCount(data);
          resolve(data);
        })
        .catch((e) => {
          reject(e);
        });
    });
  };
  /**
   * 更新未读数量角标
   */
  private updataUnReadCount = (data: any) => {
    let index = this.useSockStore.sessionList.findIndex((f) => f.id == data.id);
    if (index > -1) this.useSockStore.sessionList[index].unReadCount = data.unReadCount;
    this.useSockStore.updateUnread();
  };
 
  /**
   * 发送消息
   */
  public sendMsg = (content: string) => {
    return new Promise<void>(async (resolve, reject) => {
      this.send(content)
        .then((msgobj: any) => {
          this.useSockStore.msgList.push(msgobj);
          this.toScrollBottom();
          resolve();
        })
        .catch((e) => {
          reject(e);
        });
    });
  };
 
  /**
   * 关闭连接
   */
  public breakConnect = () => {
    this.offConcat().then(() => {
      this.useSockStore.webSock = undefined;
    });
  };
 
  /**
   * 更新消息数据
   */
  public updataMessages = () => {
    this.getMsgList().then(() => {
      this.mountNextPage(this.getMsgList);
    });
    // return this.getMsgList();
  };
 
  /**
   * 加载会话列表
   */
  public updataSessions = () => {
    return new Promise<any>(async (resolve, reject) => {
      this.getSessions()
        .then((res: any[]) => {
          this.useSockStore.sessionList = res;
          this.useSockStore.updateUnread();
          resolve(res);
        })
        .catch((e) => {
          reject(e);
        });
    });
  };
}