xtj-login-kit 0.3.1
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.
- package/dist/chunks/index-Cuix-7nW.js +949 -0
- package/dist/chunks/ui-C58EkuFV.js +268 -0
- package/dist/index.d.ts +101 -0
- package/dist/index.js +7 -0
- package/dist/react.d.ts +25 -0
- package/dist/react.js +297 -0
- package/dist/session.js +9 -0
- package/dist/vue.d.ts +26 -0
- package/dist/vue.js +261 -0
- package/package.json +54 -0
package/dist/react.js
ADDED
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
import { forwardRef, useRef, useState, useMemo, useEffect, useImperativeHandle, createElement } from "react";
|
|
2
|
+
import { L as LOGIN_STATUS, c as createLoginSession, t as toLoginError } from "./chunks/index-Cuix-7nW.js";
|
|
3
|
+
import { g as getLoginRootStyle, L as LOGIN_MOTION_STYLES, i as isMiniProgramMethod, M as MINI_PROGRAM_LAUNCH_RECOVERY_MS, a as getMiniProgramPresentation, b as getMiniProgramIconStyle, c as getMiniProgramButtonStyle, d as getQrPresentation, e as getQrImageStyle, q as qrIconStyle, f as getQrLabelStyle, h as qrActionStyle, j as qrOverlayStyle, k as qrContentStyle } from "./chunks/ui-C58EkuFV.js";
|
|
4
|
+
const QrLoginView = ({ status, onRetry, active }) => {
|
|
5
|
+
const children = [];
|
|
6
|
+
const presentation = getQrPresentation(status, active);
|
|
7
|
+
if (status.loginImg) {
|
|
8
|
+
children.push(createElement("img", {
|
|
9
|
+
key: "qr",
|
|
10
|
+
className: "xtj-login__qr",
|
|
11
|
+
src: status.loginImg,
|
|
12
|
+
alt: "登录二维码",
|
|
13
|
+
style: getQrImageStyle(Boolean(presentation))
|
|
14
|
+
}));
|
|
15
|
+
}
|
|
16
|
+
if (presentation) {
|
|
17
|
+
const stateContent = [];
|
|
18
|
+
if (presentation.icon) {
|
|
19
|
+
const iconClassName = [
|
|
20
|
+
"xtj-login__state-icon",
|
|
21
|
+
presentation.iconState ? `xtj-login__state-icon--${presentation.iconState}` : ""
|
|
22
|
+
].filter(Boolean).join(" ");
|
|
23
|
+
stateContent.push(createElement("img", {
|
|
24
|
+
key: "icon",
|
|
25
|
+
className: iconClassName,
|
|
26
|
+
src: presentation.icon,
|
|
27
|
+
alt: "",
|
|
28
|
+
style: qrIconStyle,
|
|
29
|
+
"aria-hidden": "true"
|
|
30
|
+
}));
|
|
31
|
+
}
|
|
32
|
+
stateContent.push(createElement("span", {
|
|
33
|
+
key: "label",
|
|
34
|
+
className: "xtj-login__state-label",
|
|
35
|
+
style: getQrLabelStyle(Boolean(presentation.icon)),
|
|
36
|
+
role: "status",
|
|
37
|
+
"aria-live": "polite"
|
|
38
|
+
}, presentation.label));
|
|
39
|
+
if (presentation.action === "retry") {
|
|
40
|
+
children.push(createElement("button", {
|
|
41
|
+
key: "state",
|
|
42
|
+
type: "button",
|
|
43
|
+
className: "xtj-login__state xtj-login__state--action",
|
|
44
|
+
style: qrActionStyle,
|
|
45
|
+
onClick: onRetry,
|
|
46
|
+
"aria-label": presentation.label === "二维码已过期" ? "二维码已过期,点击重新获取" : presentation.label
|
|
47
|
+
}, stateContent));
|
|
48
|
+
} else {
|
|
49
|
+
children.push(createElement("div", {
|
|
50
|
+
key: "state",
|
|
51
|
+
className: "xtj-login__state",
|
|
52
|
+
style: qrOverlayStyle
|
|
53
|
+
}, stateContent));
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return createElement("div", { className: "xtj-login__content", style: qrContentStyle }, children);
|
|
57
|
+
};
|
|
58
|
+
const MiniProgramLoginView = ({ status, onRetry, onMiniProgram, miniProgramPhase, active }) => {
|
|
59
|
+
const presentation = getMiniProgramPresentation(status, miniProgramPhase, active);
|
|
60
|
+
const onClick = presentation.action === "retry" ? onRetry : presentation.action === "open" ? onMiniProgram : void 0;
|
|
61
|
+
const children = [
|
|
62
|
+
createElement("img", {
|
|
63
|
+
key: "icon",
|
|
64
|
+
className: [
|
|
65
|
+
"xtj-login__mini-icon",
|
|
66
|
+
presentation.iconState ? `xtj-login__mini-icon--${presentation.iconState}` : ""
|
|
67
|
+
].filter(Boolean).join(" "),
|
|
68
|
+
src: presentation.icon,
|
|
69
|
+
alt: "",
|
|
70
|
+
style: getMiniProgramIconStyle(presentation),
|
|
71
|
+
"aria-hidden": "true"
|
|
72
|
+
}),
|
|
73
|
+
createElement("span", { key: "label" }, presentation.label)
|
|
74
|
+
];
|
|
75
|
+
return createElement("button", {
|
|
76
|
+
type: "button",
|
|
77
|
+
className: `xtj-login__mini-button xtj-login__mini-button--${presentation.tone}`,
|
|
78
|
+
style: getMiniProgramButtonStyle(presentation),
|
|
79
|
+
onClick,
|
|
80
|
+
disabled: presentation.disabled,
|
|
81
|
+
"aria-live": "polite"
|
|
82
|
+
}, children);
|
|
83
|
+
};
|
|
84
|
+
const LoginView = (props) => {
|
|
85
|
+
if (isMiniProgramMethod(props.loginMethod)) return createElement(MiniProgramLoginView, props);
|
|
86
|
+
return createElement(QrLoginView, props);
|
|
87
|
+
};
|
|
88
|
+
const Login = forwardRef(function Login2(props, forwardedRef) {
|
|
89
|
+
const active = props.active !== false;
|
|
90
|
+
const hostRef = useRef(null);
|
|
91
|
+
const sessionRef = useRef(null);
|
|
92
|
+
const successRef = useRef(props.onSuccess);
|
|
93
|
+
const errorRef = useRef(props.onError);
|
|
94
|
+
const statusRef = useRef(props.onStatus);
|
|
95
|
+
const miniProgramPhaseRef = useRef("idle");
|
|
96
|
+
const miniProgramRecoveryTimerRef = useRef(null);
|
|
97
|
+
const miniProgramRecoveryAtRef = useRef(0);
|
|
98
|
+
const initializationErrorRef = useRef(null);
|
|
99
|
+
const [status, setStatus] = useState({ kind: LOGIN_STATUS.IDLE, platform: props.platform || "web" });
|
|
100
|
+
const [miniProgramPhase, setMiniProgramPhase] = useState("idle");
|
|
101
|
+
const gatewayOptions = useMemo(() => {
|
|
102
|
+
if (!props.gatewayOptions) return void 0;
|
|
103
|
+
const paths = props.gatewayOptions.paths;
|
|
104
|
+
return {
|
|
105
|
+
baseUrl: props.gatewayOptions.baseUrl,
|
|
106
|
+
codeType: props.gatewayOptions.codeType,
|
|
107
|
+
schemeTarget: props.gatewayOptions.schemeTarget,
|
|
108
|
+
schemeMiniPath: props.gatewayOptions.schemeMiniPath,
|
|
109
|
+
requestTimeoutMs: props.gatewayOptions.requestTimeoutMs,
|
|
110
|
+
fetchImpl: props.gatewayOptions.fetchImpl,
|
|
111
|
+
paths: paths ? {
|
|
112
|
+
genLoginId: paths.genLoginId,
|
|
113
|
+
checkLogin: paths.checkLogin,
|
|
114
|
+
miniProgramScheme: paths.miniProgramScheme
|
|
115
|
+
} : void 0
|
|
116
|
+
};
|
|
117
|
+
}, [
|
|
118
|
+
Boolean(props.gatewayOptions),
|
|
119
|
+
props.gatewayOptions?.baseUrl,
|
|
120
|
+
props.gatewayOptions?.codeType,
|
|
121
|
+
props.gatewayOptions?.schemeTarget,
|
|
122
|
+
props.gatewayOptions?.schemeMiniPath,
|
|
123
|
+
props.gatewayOptions?.requestTimeoutMs,
|
|
124
|
+
props.gatewayOptions?.fetchImpl,
|
|
125
|
+
props.gatewayOptions?.paths?.genLoginId,
|
|
126
|
+
props.gatewayOptions?.paths?.checkLogin,
|
|
127
|
+
props.gatewayOptions?.paths?.miniProgramScheme
|
|
128
|
+
]);
|
|
129
|
+
useEffect(() => {
|
|
130
|
+
successRef.current = props.onSuccess;
|
|
131
|
+
errorRef.current = props.onError;
|
|
132
|
+
statusRef.current = props.onStatus;
|
|
133
|
+
}, [props.onSuccess, props.onError, props.onStatus]);
|
|
134
|
+
useEffect(() => {
|
|
135
|
+
miniProgramPhaseRef.current = "idle";
|
|
136
|
+
miniProgramRecoveryAtRef.current = 0;
|
|
137
|
+
setMiniProgramPhase("idle");
|
|
138
|
+
if (!active) {
|
|
139
|
+
initializationErrorRef.current = null;
|
|
140
|
+
sessionRef.current = null;
|
|
141
|
+
setStatus({ kind: LOGIN_STATUS.IDLE, platform: props.platform || "web" });
|
|
142
|
+
return void 0;
|
|
143
|
+
}
|
|
144
|
+
let session;
|
|
145
|
+
try {
|
|
146
|
+
session = createLoginSession({
|
|
147
|
+
gatewayOptions,
|
|
148
|
+
appCode: props.appCode,
|
|
149
|
+
platform: props.platform,
|
|
150
|
+
loginMethod: props.loginMethod,
|
|
151
|
+
pollIntervalMs: props.pollIntervalMs,
|
|
152
|
+
requestTimeoutMs: props.requestTimeoutMs,
|
|
153
|
+
timeoutMs: props.timeoutMs,
|
|
154
|
+
debug: props.debug,
|
|
155
|
+
windowRef: props.windowRef,
|
|
156
|
+
documentRef: props.documentRef,
|
|
157
|
+
onStatus: (next) => {
|
|
158
|
+
setStatus(next);
|
|
159
|
+
if (next.kind === LOGIN_STATUS.QR_READY) {
|
|
160
|
+
if (miniProgramPhaseRef.current === "awaiting" && Date.now() >= miniProgramRecoveryAtRef.current && session.canRetryMiniProgramLaunch()) {
|
|
161
|
+
miniProgramPhaseRef.current = "retry-launch";
|
|
162
|
+
setMiniProgramPhase("retry-launch");
|
|
163
|
+
} else if (miniProgramPhaseRef.current === "retry-launch" && session.canRetryMiniProgramLaunch() === false) {
|
|
164
|
+
miniProgramPhaseRef.current = "awaiting";
|
|
165
|
+
setMiniProgramPhase("awaiting");
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
statusRef.current?.(next);
|
|
169
|
+
},
|
|
170
|
+
onSuccess: (result) => successRef.current?.(result),
|
|
171
|
+
onError: (error) => errorRef.current?.(error)
|
|
172
|
+
});
|
|
173
|
+
sessionRef.current = session;
|
|
174
|
+
session.mount(hostRef.current);
|
|
175
|
+
initializationErrorRef.current = null;
|
|
176
|
+
} catch (error) {
|
|
177
|
+
try {
|
|
178
|
+
session?.unmount();
|
|
179
|
+
} catch {
|
|
180
|
+
}
|
|
181
|
+
const result = toLoginError(error, "init", false);
|
|
182
|
+
const next = { kind: LOGIN_STATUS.ERROR, platform: props.platform || "web", ...result };
|
|
183
|
+
const signature = `${result.code}:${result.message}`;
|
|
184
|
+
sessionRef.current = null;
|
|
185
|
+
setStatus(next);
|
|
186
|
+
if (initializationErrorRef.current !== signature) {
|
|
187
|
+
initializationErrorRef.current = signature;
|
|
188
|
+
try {
|
|
189
|
+
statusRef.current?.(next);
|
|
190
|
+
} catch {
|
|
191
|
+
}
|
|
192
|
+
try {
|
|
193
|
+
errorRef.current?.(result);
|
|
194
|
+
} catch {
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
return void 0;
|
|
198
|
+
}
|
|
199
|
+
return () => {
|
|
200
|
+
if (miniProgramRecoveryTimerRef.current !== null) {
|
|
201
|
+
clearTimeout(miniProgramRecoveryTimerRef.current);
|
|
202
|
+
miniProgramRecoveryTimerRef.current = null;
|
|
203
|
+
}
|
|
204
|
+
session.unmount();
|
|
205
|
+
if (sessionRef.current === session) sessionRef.current = null;
|
|
206
|
+
};
|
|
207
|
+
}, [
|
|
208
|
+
active,
|
|
209
|
+
gatewayOptions,
|
|
210
|
+
props.appCode,
|
|
211
|
+
props.platform,
|
|
212
|
+
props.loginMethod,
|
|
213
|
+
props.pollIntervalMs,
|
|
214
|
+
props.requestTimeoutMs,
|
|
215
|
+
props.timeoutMs,
|
|
216
|
+
props.debug,
|
|
217
|
+
props.windowRef,
|
|
218
|
+
props.documentRef
|
|
219
|
+
]);
|
|
220
|
+
const updateMiniProgramPhase = (next) => {
|
|
221
|
+
miniProgramPhaseRef.current = next;
|
|
222
|
+
setMiniProgramPhase(next);
|
|
223
|
+
};
|
|
224
|
+
const resetMiniProgramPhase = () => {
|
|
225
|
+
if (miniProgramRecoveryTimerRef.current !== null) clearTimeout(miniProgramRecoveryTimerRef.current);
|
|
226
|
+
miniProgramRecoveryTimerRef.current = null;
|
|
227
|
+
miniProgramRecoveryAtRef.current = 0;
|
|
228
|
+
updateMiniProgramPhase("idle");
|
|
229
|
+
};
|
|
230
|
+
const retry = () => {
|
|
231
|
+
resetMiniProgramPhase();
|
|
232
|
+
if (!active) return Promise.resolve(null);
|
|
233
|
+
return sessionRef.current?.retry() ?? Promise.resolve(null);
|
|
234
|
+
};
|
|
235
|
+
const cancel = (reason) => {
|
|
236
|
+
resetMiniProgramPhase();
|
|
237
|
+
sessionRef.current?.cancel(reason);
|
|
238
|
+
};
|
|
239
|
+
const openMiniProgram = async () => {
|
|
240
|
+
if (!["idle", "retry-launch"].includes(miniProgramPhaseRef.current)) return;
|
|
241
|
+
const session = sessionRef.current;
|
|
242
|
+
if (!session) return;
|
|
243
|
+
updateMiniProgramPhase("launching");
|
|
244
|
+
try {
|
|
245
|
+
await session.openMiniProgram();
|
|
246
|
+
if (sessionRef.current === session) {
|
|
247
|
+
updateMiniProgramPhase("awaiting");
|
|
248
|
+
miniProgramRecoveryAtRef.current = Date.now() + MINI_PROGRAM_LAUNCH_RECOVERY_MS;
|
|
249
|
+
if (miniProgramRecoveryTimerRef.current !== null) clearTimeout(miniProgramRecoveryTimerRef.current);
|
|
250
|
+
miniProgramRecoveryTimerRef.current = setTimeout(() => {
|
|
251
|
+
miniProgramRecoveryTimerRef.current = null;
|
|
252
|
+
const documentRef = props.documentRef || (typeof document === "undefined" ? null : document);
|
|
253
|
+
if (sessionRef.current === session && miniProgramPhaseRef.current === "awaiting" && session.canRetryMiniProgramLaunch() && documentRef?.visibilityState !== "hidden") {
|
|
254
|
+
updateMiniProgramPhase("retry-launch");
|
|
255
|
+
}
|
|
256
|
+
}, MINI_PROGRAM_LAUNCH_RECOVERY_MS);
|
|
257
|
+
}
|
|
258
|
+
} catch {
|
|
259
|
+
if (sessionRef.current === session) updateMiniProgramPhase("idle");
|
|
260
|
+
}
|
|
261
|
+
};
|
|
262
|
+
useImperativeHandle(forwardedRef, () => ({
|
|
263
|
+
get element() {
|
|
264
|
+
return hostRef.current;
|
|
265
|
+
},
|
|
266
|
+
retry,
|
|
267
|
+
cancel
|
|
268
|
+
}));
|
|
269
|
+
const loginMethod = props.loginMethod || "qr";
|
|
270
|
+
const className = ["xtj-login", `xtj-login--${loginMethod}`, props.className].filter(Boolean).join(" ");
|
|
271
|
+
return createElement("div", {
|
|
272
|
+
ref: hostRef,
|
|
273
|
+
className,
|
|
274
|
+
style: { ...getLoginRootStyle(loginMethod), ...props.style },
|
|
275
|
+
"data-state": status.kind,
|
|
276
|
+
"data-login-method": loginMethod,
|
|
277
|
+
"data-active": active ? "true" : "false"
|
|
278
|
+
}, [
|
|
279
|
+
createElement("style", {
|
|
280
|
+
key: "motion-styles",
|
|
281
|
+
"data-xtj-login-styles": "motion"
|
|
282
|
+
}, LOGIN_MOTION_STYLES),
|
|
283
|
+
createElement(LoginView, {
|
|
284
|
+
key: "view",
|
|
285
|
+
status,
|
|
286
|
+
active,
|
|
287
|
+
loginMethod,
|
|
288
|
+
onRetry: retry,
|
|
289
|
+
onMiniProgram: openMiniProgram,
|
|
290
|
+
miniProgramPhase
|
|
291
|
+
})
|
|
292
|
+
]);
|
|
293
|
+
});
|
|
294
|
+
Login.displayName = "XTJLogin";
|
|
295
|
+
export {
|
|
296
|
+
Login
|
|
297
|
+
};
|
package/dist/session.js
ADDED
package/dist/vue.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { LoginError, LoginGatewayOptions, LoginHandle, LoginResult, LoginStatus } from './index.js'
|
|
2
|
+
|
|
3
|
+
export type { LoginHandle } from './index.js'
|
|
4
|
+
|
|
5
|
+
export type LoginProps = {
|
|
6
|
+
appCode: string | number
|
|
7
|
+
active?: boolean
|
|
8
|
+
className?: string
|
|
9
|
+
class?: string
|
|
10
|
+
style?: string | Record<string, string> | Array<string | Record<string, string>>
|
|
11
|
+
platform?: 'web' | 'mobile-web' | string
|
|
12
|
+
loginMethod?: 'qr' | 'mini-program' | string
|
|
13
|
+
pollIntervalMs?: number
|
|
14
|
+
requestTimeoutMs?: number
|
|
15
|
+
timeoutMs?: number
|
|
16
|
+
debug?: boolean
|
|
17
|
+
gatewayOptions: LoginGatewayOptions
|
|
18
|
+
windowRef?: Window
|
|
19
|
+
documentRef?: Document
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export const Login: import('vue').DefineComponent<LoginProps, LoginHandle, {}, {}, {}, {}, {}, {
|
|
23
|
+
success: (result: LoginResult) => void
|
|
24
|
+
error: (error: LoginError) => void
|
|
25
|
+
status: (status: LoginStatus) => void
|
|
26
|
+
}>
|
package/dist/vue.js
ADDED
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
import { defineComponent, ref, onMounted, watch, onUnmounted, h } from "vue";
|
|
2
|
+
import { L as LOGIN_STATUS, c as createLoginSession, t as toLoginError } from "./chunks/index-Cuix-7nW.js";
|
|
3
|
+
import { g as getLoginRootStyle, L as LOGIN_MOTION_STYLES, i as isMiniProgramMethod, M as MINI_PROGRAM_LAUNCH_RECOVERY_MS, a as getMiniProgramPresentation, c as getMiniProgramButtonStyle, b as getMiniProgramIconStyle, d as getQrPresentation, e as getQrImageStyle, q as qrIconStyle, f as getQrLabelStyle, h as qrActionStyle, j as qrOverlayStyle, k as qrContentStyle } from "./chunks/ui-C58EkuFV.js";
|
|
4
|
+
const QrLoginView = (status, retry, active) => {
|
|
5
|
+
const children = [];
|
|
6
|
+
const presentation = getQrPresentation(status, active);
|
|
7
|
+
if (status.loginImg) {
|
|
8
|
+
children.push(h("img", {
|
|
9
|
+
class: "xtj-login__qr",
|
|
10
|
+
src: status.loginImg,
|
|
11
|
+
alt: "登录二维码",
|
|
12
|
+
style: getQrImageStyle(Boolean(presentation))
|
|
13
|
+
}));
|
|
14
|
+
}
|
|
15
|
+
if (presentation) {
|
|
16
|
+
const stateContent = [];
|
|
17
|
+
if (presentation.icon) {
|
|
18
|
+
stateContent.push(h("img", {
|
|
19
|
+
class: [
|
|
20
|
+
"xtj-login__state-icon",
|
|
21
|
+
presentation.iconState ? `xtj-login__state-icon--${presentation.iconState}` : ""
|
|
22
|
+
],
|
|
23
|
+
src: presentation.icon,
|
|
24
|
+
alt: "",
|
|
25
|
+
style: qrIconStyle,
|
|
26
|
+
"aria-hidden": "true"
|
|
27
|
+
}));
|
|
28
|
+
}
|
|
29
|
+
stateContent.push(h("span", {
|
|
30
|
+
class: "xtj-login__state-label",
|
|
31
|
+
style: getQrLabelStyle(Boolean(presentation.icon)),
|
|
32
|
+
role: "status",
|
|
33
|
+
"aria-live": "polite"
|
|
34
|
+
}, presentation.label));
|
|
35
|
+
if (presentation.action === "retry") {
|
|
36
|
+
children.push(h("button", {
|
|
37
|
+
class: "xtj-login__state xtj-login__state--action",
|
|
38
|
+
type: "button",
|
|
39
|
+
style: qrActionStyle,
|
|
40
|
+
onClick: retry,
|
|
41
|
+
"aria-label": presentation.label === "二维码已过期" ? "二维码已过期,点击重新获取" : presentation.label
|
|
42
|
+
}, stateContent));
|
|
43
|
+
} else {
|
|
44
|
+
children.push(h("div", {
|
|
45
|
+
class: "xtj-login__state",
|
|
46
|
+
style: qrOverlayStyle
|
|
47
|
+
}, stateContent));
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return h("div", { class: "xtj-login__content", style: qrContentStyle }, children);
|
|
51
|
+
};
|
|
52
|
+
const MiniProgramLoginView = (status, retry, openMiniProgram, miniProgramPhase, active) => {
|
|
53
|
+
const presentation = getMiniProgramPresentation(status, miniProgramPhase, active);
|
|
54
|
+
const onClick = presentation.action === "retry" ? retry : presentation.action === "open" ? openMiniProgram : void 0;
|
|
55
|
+
return h("button", {
|
|
56
|
+
class: [`xtj-login__mini-button`, `xtj-login__mini-button--${presentation.tone}`],
|
|
57
|
+
type: "button",
|
|
58
|
+
style: getMiniProgramButtonStyle(presentation),
|
|
59
|
+
onClick,
|
|
60
|
+
disabled: presentation.disabled,
|
|
61
|
+
"aria-live": "polite"
|
|
62
|
+
}, [
|
|
63
|
+
h("img", {
|
|
64
|
+
class: [
|
|
65
|
+
"xtj-login__mini-icon",
|
|
66
|
+
presentation.iconState ? `xtj-login__mini-icon--${presentation.iconState}` : ""
|
|
67
|
+
],
|
|
68
|
+
src: presentation.icon,
|
|
69
|
+
alt: "",
|
|
70
|
+
style: getMiniProgramIconStyle(presentation),
|
|
71
|
+
"aria-hidden": "true"
|
|
72
|
+
}),
|
|
73
|
+
h("span", presentation.label)
|
|
74
|
+
]);
|
|
75
|
+
};
|
|
76
|
+
const LoginView = (status, loginMethod, retry, openMiniProgram, miniProgramPhase, active) => {
|
|
77
|
+
if (isMiniProgramMethod(loginMethod)) {
|
|
78
|
+
return MiniProgramLoginView(status, retry, openMiniProgram, miniProgramPhase, active);
|
|
79
|
+
}
|
|
80
|
+
return QrLoginView(status, retry, active);
|
|
81
|
+
};
|
|
82
|
+
const Login = defineComponent({
|
|
83
|
+
name: "XTJLogin",
|
|
84
|
+
props: {
|
|
85
|
+
appCode: { type: [String, Number], required: true },
|
|
86
|
+
active: { type: Boolean, default: true },
|
|
87
|
+
className: { type: String, default: "" },
|
|
88
|
+
class: { type: String, default: "" },
|
|
89
|
+
style: { type: [String, Object, Array], default: void 0 },
|
|
90
|
+
platform: { type: String, default: "web" },
|
|
91
|
+
loginMethod: { type: String, default: "qr" },
|
|
92
|
+
pollIntervalMs: { type: Number, default: void 0 },
|
|
93
|
+
requestTimeoutMs: { type: Number, default: void 0 },
|
|
94
|
+
timeoutMs: { type: Number, default: void 0 },
|
|
95
|
+
debug: { type: Boolean, default: false },
|
|
96
|
+
gatewayOptions: { type: Object, required: true },
|
|
97
|
+
windowRef: { type: Object, default: void 0 },
|
|
98
|
+
documentRef: { type: Object, default: void 0 }
|
|
99
|
+
},
|
|
100
|
+
emits: ["success", "error", "status"],
|
|
101
|
+
setup(props, { emit, expose }) {
|
|
102
|
+
const hostRef = ref(null);
|
|
103
|
+
const status = ref({ kind: LOGIN_STATUS.IDLE, platform: props.platform });
|
|
104
|
+
const miniProgramPhase = ref("idle");
|
|
105
|
+
let miniProgramRecoveryTimer = null;
|
|
106
|
+
let miniProgramRecoveryAt = 0;
|
|
107
|
+
let session = null;
|
|
108
|
+
const createSession = () => {
|
|
109
|
+
if (!props.active) {
|
|
110
|
+
session = null;
|
|
111
|
+
status.value = { kind: LOGIN_STATUS.IDLE, platform: props.platform };
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
try {
|
|
115
|
+
session = createLoginSession({
|
|
116
|
+
gatewayOptions: props.gatewayOptions,
|
|
117
|
+
appCode: props.appCode,
|
|
118
|
+
platform: props.platform,
|
|
119
|
+
loginMethod: props.loginMethod,
|
|
120
|
+
pollIntervalMs: props.pollIntervalMs,
|
|
121
|
+
requestTimeoutMs: props.requestTimeoutMs,
|
|
122
|
+
timeoutMs: props.timeoutMs,
|
|
123
|
+
debug: props.debug,
|
|
124
|
+
windowRef: props.windowRef,
|
|
125
|
+
documentRef: props.documentRef,
|
|
126
|
+
onStatus: (next) => {
|
|
127
|
+
status.value = next;
|
|
128
|
+
if (next.kind === LOGIN_STATUS.QR_READY) {
|
|
129
|
+
if (miniProgramPhase.value === "awaiting" && Date.now() >= miniProgramRecoveryAt && session.canRetryMiniProgramLaunch()) {
|
|
130
|
+
miniProgramPhase.value = "retry-launch";
|
|
131
|
+
} else if (miniProgramPhase.value === "retry-launch" && session.canRetryMiniProgramLaunch() === false) {
|
|
132
|
+
miniProgramPhase.value = "awaiting";
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
emit("status", next);
|
|
136
|
+
},
|
|
137
|
+
onSuccess: (result) => emit("success", result),
|
|
138
|
+
onError: (error) => emit("error", error)
|
|
139
|
+
});
|
|
140
|
+
session.mount(hostRef.value);
|
|
141
|
+
} catch (error) {
|
|
142
|
+
try {
|
|
143
|
+
session?.unmount();
|
|
144
|
+
} catch {
|
|
145
|
+
}
|
|
146
|
+
const result = toLoginError(error, "init", false);
|
|
147
|
+
const next = { kind: LOGIN_STATUS.ERROR, platform: props.platform, ...result };
|
|
148
|
+
session = null;
|
|
149
|
+
status.value = next;
|
|
150
|
+
emit("status", next);
|
|
151
|
+
emit("error", result);
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
};
|
|
155
|
+
const openMiniProgram = async () => {
|
|
156
|
+
if (!["idle", "retry-launch"].includes(miniProgramPhase.value)) return;
|
|
157
|
+
const activeSession = session;
|
|
158
|
+
if (!activeSession) return;
|
|
159
|
+
miniProgramPhase.value = "launching";
|
|
160
|
+
try {
|
|
161
|
+
await activeSession.openMiniProgram();
|
|
162
|
+
if (session === activeSession) {
|
|
163
|
+
miniProgramPhase.value = "awaiting";
|
|
164
|
+
miniProgramRecoveryAt = Date.now() + MINI_PROGRAM_LAUNCH_RECOVERY_MS;
|
|
165
|
+
if (miniProgramRecoveryTimer !== null) clearTimeout(miniProgramRecoveryTimer);
|
|
166
|
+
miniProgramRecoveryTimer = setTimeout(() => {
|
|
167
|
+
miniProgramRecoveryTimer = null;
|
|
168
|
+
const documentRef = props.documentRef || (typeof document === "undefined" ? null : document);
|
|
169
|
+
if (session === activeSession && miniProgramPhase.value === "awaiting" && activeSession.canRetryMiniProgramLaunch() && documentRef?.visibilityState !== "hidden") {
|
|
170
|
+
miniProgramPhase.value = "retry-launch";
|
|
171
|
+
}
|
|
172
|
+
}, MINI_PROGRAM_LAUNCH_RECOVERY_MS);
|
|
173
|
+
}
|
|
174
|
+
} catch {
|
|
175
|
+
if (session === activeSession) miniProgramPhase.value = "idle";
|
|
176
|
+
}
|
|
177
|
+
};
|
|
178
|
+
const resetMiniProgramPhase = () => {
|
|
179
|
+
if (miniProgramRecoveryTimer !== null) clearTimeout(miniProgramRecoveryTimer);
|
|
180
|
+
miniProgramRecoveryTimer = null;
|
|
181
|
+
miniProgramRecoveryAt = 0;
|
|
182
|
+
miniProgramPhase.value = "idle";
|
|
183
|
+
};
|
|
184
|
+
const retry = () => {
|
|
185
|
+
resetMiniProgramPhase();
|
|
186
|
+
if (!props.active) return Promise.resolve(null);
|
|
187
|
+
return session?.retry() ?? Promise.resolve(null);
|
|
188
|
+
};
|
|
189
|
+
const cancel = (reason) => {
|
|
190
|
+
resetMiniProgramPhase();
|
|
191
|
+
session?.cancel(reason);
|
|
192
|
+
};
|
|
193
|
+
expose({
|
|
194
|
+
get element() {
|
|
195
|
+
return hostRef.value;
|
|
196
|
+
},
|
|
197
|
+
retry,
|
|
198
|
+
cancel
|
|
199
|
+
});
|
|
200
|
+
onMounted(() => {
|
|
201
|
+
createSession();
|
|
202
|
+
});
|
|
203
|
+
watch(
|
|
204
|
+
[
|
|
205
|
+
() => props.active,
|
|
206
|
+
() => props.appCode,
|
|
207
|
+
() => props.platform,
|
|
208
|
+
() => props.loginMethod,
|
|
209
|
+
() => props.pollIntervalMs,
|
|
210
|
+
() => props.requestTimeoutMs,
|
|
211
|
+
() => props.timeoutMs,
|
|
212
|
+
() => props.debug,
|
|
213
|
+
() => props.windowRef,
|
|
214
|
+
() => props.documentRef,
|
|
215
|
+
() => Boolean(props.gatewayOptions),
|
|
216
|
+
() => props.gatewayOptions?.baseUrl,
|
|
217
|
+
() => props.gatewayOptions?.codeType,
|
|
218
|
+
() => props.gatewayOptions?.schemeTarget,
|
|
219
|
+
() => props.gatewayOptions?.schemeMiniPath,
|
|
220
|
+
() => props.gatewayOptions?.requestTimeoutMs,
|
|
221
|
+
() => props.gatewayOptions?.fetchImpl,
|
|
222
|
+
() => props.gatewayOptions?.paths?.genLoginId,
|
|
223
|
+
() => props.gatewayOptions?.paths?.checkLogin,
|
|
224
|
+
() => props.gatewayOptions?.paths?.miniProgramScheme
|
|
225
|
+
],
|
|
226
|
+
() => {
|
|
227
|
+
session?.unmount();
|
|
228
|
+
session = null;
|
|
229
|
+
if (miniProgramRecoveryTimer !== null) clearTimeout(miniProgramRecoveryTimer);
|
|
230
|
+
miniProgramRecoveryTimer = null;
|
|
231
|
+
miniProgramRecoveryAt = 0;
|
|
232
|
+
miniProgramPhase.value = "idle";
|
|
233
|
+
createSession();
|
|
234
|
+
}
|
|
235
|
+
);
|
|
236
|
+
onUnmounted(() => {
|
|
237
|
+
if (miniProgramRecoveryTimer !== null) clearTimeout(miniProgramRecoveryTimer);
|
|
238
|
+
miniProgramRecoveryTimer = null;
|
|
239
|
+
miniProgramRecoveryAt = 0;
|
|
240
|
+
session?.unmount();
|
|
241
|
+
session = null;
|
|
242
|
+
});
|
|
243
|
+
return () => {
|
|
244
|
+
const loginMethod = props.loginMethod || "qr";
|
|
245
|
+
return h("div", {
|
|
246
|
+
ref: hostRef,
|
|
247
|
+
class: ["xtj-login", `xtj-login--${loginMethod}`, props.className || props.class].filter(Boolean),
|
|
248
|
+
style: [getLoginRootStyle(loginMethod), props.style],
|
|
249
|
+
"data-state": status.value.kind,
|
|
250
|
+
"data-login-method": loginMethod,
|
|
251
|
+
"data-active": props.active ? "true" : "false"
|
|
252
|
+
}, [
|
|
253
|
+
h("style", { "data-xtj-login-styles": "motion" }, LOGIN_MOTION_STYLES),
|
|
254
|
+
LoginView(status.value, loginMethod, retry, openMiniProgram, miniProgramPhase.value, props.active)
|
|
255
|
+
]);
|
|
256
|
+
};
|
|
257
|
+
}
|
|
258
|
+
});
|
|
259
|
+
export {
|
|
260
|
+
Login
|
|
261
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "xtj-login-kit",
|
|
3
|
+
"version": "0.3.1",
|
|
4
|
+
"description": "可嵌入业务页面的星途径 Web 统一登录组件库",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js",
|
|
13
|
+
"default": "./dist/index.js"
|
|
14
|
+
},
|
|
15
|
+
"./react": {
|
|
16
|
+
"types": "./dist/react.d.ts",
|
|
17
|
+
"import": "./dist/react.js",
|
|
18
|
+
"default": "./dist/react.js"
|
|
19
|
+
},
|
|
20
|
+
"./vue": {
|
|
21
|
+
"types": "./dist/vue.d.ts",
|
|
22
|
+
"import": "./dist/vue.js",
|
|
23
|
+
"default": "./dist/vue.js"
|
|
24
|
+
},
|
|
25
|
+
"./session": {
|
|
26
|
+
"types": "./dist/index.d.ts",
|
|
27
|
+
"import": "./dist/session.js",
|
|
28
|
+
"default": "./dist/session.js"
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
"files": [
|
|
32
|
+
"dist"
|
|
33
|
+
],
|
|
34
|
+
"sideEffects": false,
|
|
35
|
+
"engines": {
|
|
36
|
+
"node": ">=18"
|
|
37
|
+
},
|
|
38
|
+
"peerDependencies": {
|
|
39
|
+
"react": ">=18",
|
|
40
|
+
"vue": ">=3"
|
|
41
|
+
},
|
|
42
|
+
"peerDependenciesMeta": {
|
|
43
|
+
"react": {
|
|
44
|
+
"optional": true
|
|
45
|
+
},
|
|
46
|
+
"vue": {
|
|
47
|
+
"optional": true
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
"publishConfig": {
|
|
51
|
+
"access": "public",
|
|
52
|
+
"registry": "https://registry.npmjs.org/"
|
|
53
|
+
}
|
|
54
|
+
}
|