@jnrs/vue-core 1.2.3 → 1.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,2 +1,3 @@
1
1
  export { useReactivityTableHeight } from './useReactivityTableHeight';
2
2
  export { useMouseSelection } from './useMouseSelection';
3
+ export { useWebSocket } from './useWebSocket';
@@ -1,5 +1,129 @@
1
- import { a as s, u as t } from "../useMouseSelection-v7Ud0vBn.js";
1
+ import { c as G, u as K } from "../useMouseSelection-DU3qQWIm.js";
2
+ import { ref as p, onDeactivated as x, onActivated as A, onUnmounted as D } from "vue";
3
+ import { ElMessage as v } from "element-plus";
4
+ import { a as L, u as _ } from "../index-KdP2kDTZ.js";
5
+ const { token: H } = L(), { isMock: R } = _();
6
+ function z(O) {
7
+ const {
8
+ url: S,
9
+ token: k = H,
10
+ connectionTimeoutDuration: C = 6e4,
11
+ heartbeat: b = !1,
12
+ heartbeatInterval: T = 3e4,
13
+ heartbeatTimeout: N = 1e4,
14
+ showLog: i = !1,
15
+ showMessage: c = !0,
16
+ onOpen: E,
17
+ onMessage: M,
18
+ onClose: I,
19
+ onError: y
20
+ } = O, e = p(null), f = p(!0), s = p(!1), u = p("未连接");
21
+ let h = "", o = "", r = null, a = null, t = null;
22
+ R && (h = S.startsWith("/mock") ? S : "/mock" + S);
23
+ const g = () => {
24
+ r && clearTimeout(r), a && clearTimeout(a), r = null, a = null;
25
+ }, W = () => {
26
+ if (!(!e.value || e.value.readyState !== WebSocket.OPEN))
27
+ try {
28
+ e.value.send(JSON.stringify({ type: "ping" })), a = setTimeout(() => {
29
+ o = "WebSocket 连接不稳定,正在重连...", i && console.warn(o), c && v({
30
+ message: o,
31
+ type: "warning",
32
+ grouping: !0,
33
+ showClose: !0,
34
+ duration: 3e3
35
+ }), P();
36
+ }, N);
37
+ } catch (n) {
38
+ console.error("发送 WebSocket ping 失败:", n);
39
+ }
40
+ }, P = () => {
41
+ d(), setTimeout(w, 2e3);
42
+ }, w = () => {
43
+ if (e.value?.readyState === WebSocket.OPEN) return;
44
+ e.value && e.value.close(), g(), t && clearTimeout(t);
45
+ const n = k ? [k] : void 0;
46
+ e.value = new WebSocket(h, n), t = setTimeout(() => {
47
+ e.value && e.value.readyState !== WebSocket.OPEN && (o = "WebSocket 连接超时", f.value = !1, s.value = !1, u.value = "连接超时", i && console.error(o), c && v({
48
+ message: o,
49
+ type: "error",
50
+ grouping: !0,
51
+ showClose: !0,
52
+ duration: 0
53
+ }), e.value?.close(), e.value = null, y?.(new Event("timeout")));
54
+ }, C), e.value.onopen = () => {
55
+ t && (clearTimeout(t), t = null), f.value = !1, s.value = !0, u.value = "连接成功", o = "WebSocket 连接成功", i && console.log(o), c && v({
56
+ message: o,
57
+ type: "success",
58
+ grouping: !0,
59
+ showClose: !0,
60
+ duration: 3e3
61
+ }), E?.(), b && (g(), r = setInterval(W, T));
62
+ }, e.value.onmessage = (l) => {
63
+ if (u.value = "数据接收中", typeof l.data == "string")
64
+ try {
65
+ if (JSON.parse(l.data).type === "pong") {
66
+ a && (clearTimeout(a), a = null);
67
+ return;
68
+ }
69
+ } catch {
70
+ }
71
+ try {
72
+ const m = JSON.parse(l.data);
73
+ M?.(m);
74
+ } catch (m) {
75
+ console.error("WebSocket 消息解析失败:", l.data, m);
76
+ }
77
+ }, e.value.onclose = () => {
78
+ f.value = !1, s.value = !1, u.value = "连接已关闭", g(), t && (clearTimeout(t), t = null), o = "WebSocket 连接已关闭", i && console.log(o), c && v({
79
+ message: o,
80
+ type: "warning",
81
+ grouping: !0,
82
+ showClose: !0,
83
+ duration: 3e3
84
+ }), I?.();
85
+ }, e.value.onerror = (l) => {
86
+ f.value = !1, s.value = !1, u.value = "连接失败", t && (clearTimeout(t), t = null), o = "WebSocket 连接失败", i && console.error(o, l), c && v({
87
+ message: o,
88
+ type: "error",
89
+ grouping: !0,
90
+ showClose: !0,
91
+ duration: 3e3
92
+ }), y?.(l);
93
+ };
94
+ }, J = (n) => {
95
+ if (!e.value || e.value.readyState !== WebSocket.OPEN) {
96
+ o = "WebSocket 未连接,无法发送消息", i && console.error(o), c && v({
97
+ message: o,
98
+ type: "error",
99
+ grouping: !0,
100
+ showClose: !0,
101
+ duration: 3e3
102
+ });
103
+ return;
104
+ }
105
+ const l = typeof n == "string" ? n : JSON.stringify(n);
106
+ e.value.send(l);
107
+ }, d = () => {
108
+ e.value && (e.value.close(), e.value = null), f.value = !1, s.value = !1, u.value = "手动关闭连接", g(), t && (clearTimeout(t), t = null);
109
+ };
110
+ return w(), x(() => {
111
+ r && (clearInterval(r), r = null), a && (clearTimeout(a), a = null);
112
+ }), A(() => {
113
+ s.value && b && (r = setInterval(W, T));
114
+ }), D(() => {
115
+ d();
116
+ }), {
117
+ ws: e,
118
+ isLoading: f,
119
+ isConnected: s,
120
+ wsStateInfo: u,
121
+ sendMessage: J,
122
+ closeWebSocket: d
123
+ };
124
+ }
2
125
  export {
3
- s as useMouseSelection,
4
- t as useReactivityTableHeight
126
+ G as useMouseSelection,
127
+ K as useReactivityTableHeight,
128
+ z as useWebSocket
5
129
  };
@@ -0,0 +1,117 @@
1
+ /**
2
+ * @Author : TanRui
3
+ * @WeChat : Tan578853789
4
+ * @File : useWebSocket.ts
5
+ * @Date : 2026/01/06
6
+ * @Desc. : WebSocket 封装(支持心跳机制,前端发送 {type: 'ping'},后端需返回 {type: 'pong'})
7
+ */
8
+ type WebSocketRawMessage = unknown;
9
+ interface UseWebSocketOptions {
10
+ /**
11
+ * WebSocket 地址
12
+ */
13
+ url: string;
14
+ /**
15
+ * 网络超时时间(ms,默认 60s)
16
+ */
17
+ connectionTimeoutDuration?: number;
18
+ /**
19
+ * 认证信息
20
+ */
21
+ token?: string;
22
+ /**
23
+ * 是否启用心跳(默认 false)
24
+ */
25
+ heartbeat?: boolean;
26
+ /**
27
+ * 心跳间隔(ms,默认 30s)
28
+ */
29
+ heartbeatInterval?: number;
30
+ /**
31
+ * 心跳超时时间(ms,默认 10s)
32
+ */
33
+ heartbeatTimeout?: number;
34
+ /**
35
+ * 是否打印控制台信息(默认 false)
36
+ */
37
+ showLog?: boolean;
38
+ /**
39
+ * 是否显示消息弹框(默认 true)
40
+ */
41
+ showMessage?: boolean;
42
+ onOpen?: () => void;
43
+ onMessage?: (data: WebSocketRawMessage) => void;
44
+ onClose?: () => void;
45
+ onError?: (error: Event) => void;
46
+ }
47
+ /**
48
+ * useWebSocket
49
+ * @param options - WebSocket 配置项
50
+ * @returns {WebSocket | null} - ws 实例
51
+ * @returns {boolean} - isLoading 是否正在加载中
52
+ * @returns {boolean} - isConnected 是否已连接
53
+ * @returns {string} - wsStateInfo 连接状态信息
54
+ * @returns {void} - sendMessage 发送消息函数
55
+ * @returns {void} - closeWebSocket 关闭连接函数
56
+ */
57
+ export declare function useWebSocket(options: UseWebSocketOptions): {
58
+ ws: import('vue').Ref<{
59
+ binaryType: BinaryType;
60
+ readonly bufferedAmount: number;
61
+ readonly extensions: string;
62
+ onclose: ((this: WebSocket, ev: CloseEvent) => any) | null;
63
+ onerror: ((this: WebSocket, ev: Event) => any) | null;
64
+ onmessage: ((this: WebSocket, ev: MessageEvent) => any) | null;
65
+ onopen: ((this: WebSocket, ev: Event) => any) | null;
66
+ readonly protocol: string;
67
+ readonly readyState: number;
68
+ readonly url: string;
69
+ close: (code?: number, reason?: string) => void;
70
+ send: (data: string | ArrayBufferLike | Blob | ArrayBufferView) => void;
71
+ readonly CONNECTING: 0;
72
+ readonly OPEN: 1;
73
+ readonly CLOSING: 2;
74
+ readonly CLOSED: 3;
75
+ addEventListener: {
76
+ <K extends keyof WebSocketEventMap>(type: K, listener: (this: WebSocket, ev: WebSocketEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void;
77
+ (type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void;
78
+ };
79
+ removeEventListener: {
80
+ <K extends keyof WebSocketEventMap>(type: K, listener: (this: WebSocket, ev: WebSocketEventMap[K]) => any, options?: boolean | EventListenerOptions): void;
81
+ (type: string, listener: EventListenerOrEventListenerObject, options?: boolean | EventListenerOptions): void;
82
+ };
83
+ dispatchEvent: (event: Event) => boolean;
84
+ } | null, WebSocket | {
85
+ binaryType: BinaryType;
86
+ readonly bufferedAmount: number;
87
+ readonly extensions: string;
88
+ onclose: ((this: WebSocket, ev: CloseEvent) => any) | null;
89
+ onerror: ((this: WebSocket, ev: Event) => any) | null;
90
+ onmessage: ((this: WebSocket, ev: MessageEvent) => any) | null;
91
+ onopen: ((this: WebSocket, ev: Event) => any) | null;
92
+ readonly protocol: string;
93
+ readonly readyState: number;
94
+ readonly url: string;
95
+ close: (code?: number, reason?: string) => void;
96
+ send: (data: string | ArrayBufferLike | Blob | ArrayBufferView) => void;
97
+ readonly CONNECTING: 0;
98
+ readonly OPEN: 1;
99
+ readonly CLOSING: 2;
100
+ readonly CLOSED: 3;
101
+ addEventListener: {
102
+ <K extends keyof WebSocketEventMap>(type: K, listener: (this: WebSocket, ev: WebSocketEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void;
103
+ (type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void;
104
+ };
105
+ removeEventListener: {
106
+ <K extends keyof WebSocketEventMap>(type: K, listener: (this: WebSocket, ev: WebSocketEventMap[K]) => any, options?: boolean | EventListenerOptions): void;
107
+ (type: string, listener: EventListenerOrEventListenerObject, options?: boolean | EventListenerOptions): void;
108
+ };
109
+ dispatchEvent: (event: Event) => boolean;
110
+ } | null>;
111
+ isLoading: import('vue').Ref<boolean, boolean>;
112
+ isConnected: import('vue').Ref<boolean, boolean>;
113
+ wsStateInfo: import('vue').Ref<string, string>;
114
+ sendMessage: (data: string | Record<string, unknown>) => void;
115
+ closeWebSocket: () => void;
116
+ };
117
+ export {};
@@ -3301,8 +3301,8 @@ const K = Xt.t, nr = ({ options: s, responseMap: e, handleMessageFn: t = ut, han
3301
3301
  message: K("axios.create.noToken"),
3302
3302
  type: "error",
3303
3303
  grouping: !0,
3304
- duration: 5e3,
3305
- showClose: !0
3304
+ showClose: !0,
3305
+ duration: 5e3
3306
3306
  }), Promise.reject({
3307
3307
  message: K("axios.create.noToken")
3308
3308
  }));
@@ -3310,8 +3310,8 @@ const K = Xt.t, nr = ({ options: s, responseMap: e, handleMessageFn: t = ut, han
3310
3310
  message: K("axios.create.errorRsp"),
3311
3311
  type: "warning",
3312
3312
  grouping: !0,
3313
- duration: 5e3,
3314
- showClose: !0
3313
+ showClose: !0,
3314
+ duration: 5e3
3315
3315
  }), Promise.reject(l))), a.interceptors.response.use((l) => {
3316
3316
  if (!rr(l.data) && !ir(l.data))
3317
3317
  return l.data;
@@ -3320,8 +3320,8 @@ const K = Xt.t, nr = ({ options: s, responseMap: e, handleMessageFn: t = ut, han
3320
3320
  message: f || K("axios.create.errorMsg"),
3321
3321
  type: "warning",
3322
3322
  grouping: !0,
3323
- duration: 5e3,
3324
- showClose: !0
3323
+ showClose: !0,
3324
+ duration: 5e3
3325
3325
  }), Promise.reject(l.data));
3326
3326
  }, (l) => {
3327
3327
  let u = "", c = "", f = "";
@@ -3335,8 +3335,8 @@ const K = Xt.t, nr = ({ options: s, responseMap: e, handleMessageFn: t = ut, han
3335
3335
  message: u,
3336
3336
  type: "error",
3337
3337
  grouping: !0,
3338
- duration: 5e3,
3339
- showClose: !0
3338
+ showClose: !0,
3339
+ duration: 5e3
3340
3340
  }), Promise.reject({
3341
3341
  fillUrl: l.config?.baseURL + l.config?.url,
3342
3342
  message: u,
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { c as r } from "./index-Ci9uljtG.js";
2
2
  import { i } from "./index-KdP2kDTZ.js";
3
- import { i as t } from "./index-CWC6ffog.js";
3
+ import { i as t } from "./index-BVrPZ7_E.js";
4
4
  console.log(
5
5
  "%c✨ 欢迎使用 @jnrs/vue-core",
6
6
  'background: #42B883; color: #39495C; font-weight: bold; padding: 4px 8px; border-radius: 4px; font-family: "Helvetica Neue", sans-serif;'
@@ -1,4 +1,4 @@
1
- import { a as o } from "../index-CWC6ffog.js";
1
+ import { a as o } from "../index-BVrPZ7_E.js";
2
2
  export {
3
3
  o as axiosRequest
4
4
  };
@@ -196,9 +196,9 @@ const ge = (e) => {
196
196
  };
197
197
  export {
198
198
  T as S,
199
- he as a,
199
+ Q as a,
200
200
  K as b,
201
- Q as c,
201
+ he as c,
202
202
  de as d,
203
203
  N as f,
204
204
  M as i,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jnrs/vue-core",
3
- "version": "1.2.3",
3
+ "version": "1.2.4",
4
4
  "description": "巨能前端工程化开发,Vue 专用核心功能包。",
5
5
  "keywords": [
6
6
  "jnrs",
@@ -62,7 +62,7 @@
62
62
  "vue": "^3.5.22",
63
63
  "vue-router": "^4.5.1",
64
64
  "vue-i18n": "^9.14.5",
65
- "@jnrs/shared": "1.1.8"
65
+ "@jnrs/shared": "1.1.9"
66
66
  },
67
67
  "devDependencies": {
68
68
  "@vitejs/plugin-vue": "^6.0.1",