@yuntower/yuntower-account-web-sdk 0.0.13 → 0.0.15

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/index.d.ts CHANGED
@@ -32,9 +32,11 @@ declare class YunTowerAccountSDK {
32
32
  private setupMessageListener;
33
33
  /**
34
34
  * 弹窗授权模式
35
- * @param callback 授权回调函数
35
+ * @param optionsOrCallback 配置(可选)或回调;传 { autoCloseOnFinish: false } 可关闭「成功/失败时自动关窗」
36
+ * @param callback 授权回调(当第一个参数为 options 时必传)
37
+ * @returns 返回 { close },由接入方在需要时调用 close() 关闭授权窗口
36
38
  */
37
- window(callback?: (response: CallbackResponse) => void): void;
39
+ window(optionsOrCallback?: WindowOptions | ((response: CallbackResponse) => void), callback?: (response: CallbackResponse) => void): WindowController;
38
40
  /**
39
41
  * 重定向授权模式
40
42
  * @param redirectUrl 授权完成后的重定向URL
package/dist/index.js CHANGED
@@ -72,15 +72,19 @@ class YunTowerAccountSDK {
72
72
  if (!this.isValidOrigin(event.origin)) {
73
73
  return false;
74
74
  }
75
- if (event.data?.action === "status") {
76
- const { status, data, msg } = event.data;
75
+ const data = event.data;
76
+ const isStatusMessage = data?.action === "status" || data?.type === "status";
77
+ if (isStatusMessage) {
78
+ const status = data.status;
79
+ const msg = data.msg;
80
+ const responseData = data.data;
77
81
  if (status === "success") {
78
82
  this.authStatus = true;
79
83
  }
80
84
  callback({
81
85
  event: "auth",
82
86
  status,
83
- data,
87
+ data: responseData,
84
88
  msg,
85
89
  });
86
90
  return true;
@@ -104,35 +108,74 @@ class YunTowerAccountSDK {
104
108
  }
105
109
  /**
106
110
  * 弹窗授权模式
107
- * @param callback 授权回调函数
111
+ * @param optionsOrCallback 配置(可选)或回调;传 { autoCloseOnFinish: false } 可关闭「成功/失败时自动关窗」
112
+ * @param callback 授权回调(当第一个参数为 options 时必传)
113
+ * @returns 返回 { close },由接入方在需要时调用 close() 关闭授权窗口
108
114
  */
109
- window(callback = () => { }) {
115
+ window(optionsOrCallback, callback) {
116
+ const options = optionsOrCallback && typeof optionsOrCallback === "object"
117
+ ? optionsOrCallback
118
+ : {};
119
+ const userCallback = typeof optionsOrCallback === "function"
120
+ ? optionsOrCallback
121
+ : callback ?? (() => { });
122
+ const autoCloseOnFinish = options.autoCloseOnFinish !== false;
110
123
  const authUrl = this.buildAuthUrl("window");
111
124
  const authWindow = window.open(authUrl, "_blank", "width=500,height=600");
112
125
  if (!authWindow) {
113
126
  throw new Error("[YunTowerAccountSDK] 无法打开授权窗口,可能被浏览器拦截");
114
127
  }
115
- const cleanup = this.setupMessageListener(callback);
116
- // 监听窗口关闭状态
117
- const checkInterval = setInterval(() => {
128
+ let intervalId = null;
129
+ const cleanup = this.setupMessageListener((response) => {
130
+ if (intervalId != null) {
131
+ clearInterval(intervalId);
132
+ intervalId = null;
133
+ }
134
+ cleanup();
135
+ if (autoCloseOnFinish && authWindow && !authWindow.closed) {
136
+ // 延后关窗,避免部分浏览器在 message 回调里同步 close 不生效
137
+ setTimeout(() => {
138
+ if (authWindow && !authWindow.closed) {
139
+ authWindow.close();
140
+ }
141
+ }, 0);
142
+ }
143
+ userCallback(response);
144
+ });
145
+ // 轮询授权窗状态,间隔 1s 便于尽快收到授权页回复
146
+ intervalId = setInterval(() => {
118
147
  if (authWindow.closed) {
119
- clearInterval(checkInterval);
148
+ if (intervalId != null) {
149
+ clearInterval(intervalId);
150
+ intervalId = null;
151
+ }
120
152
  cleanup();
121
- callback({
153
+ userCallback({
122
154
  event: "closed",
123
155
  status: "success",
124
156
  });
125
157
  }
126
158
  else {
127
- // 发送状态检查消息
128
159
  try {
129
160
  authWindow.postMessage({ action: "status" }, "*");
130
161
  }
131
162
  catch {
132
- // 窗口可能已经关闭,忽略错误
163
+ // 窗口可能已关闭,忽略
133
164
  }
134
165
  }
135
- }, 3000);
166
+ }, 1000);
167
+ return {
168
+ close() {
169
+ if (intervalId != null) {
170
+ clearInterval(intervalId);
171
+ intervalId = null;
172
+ }
173
+ cleanup();
174
+ if (authWindow && !authWindow.closed) {
175
+ authWindow.close();
176
+ }
177
+ },
178
+ };
136
179
  }
137
180
  /**
138
181
  * 重定向授权模式
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@yuntower/yuntower-account-web-sdk",
3
3
  "description": "YunTower Account Web SDK",
4
- "version": "0.0.13",
4
+ "version": "0.0.15",
5
5
  "private": false,
6
6
  "author": "yuntower",
7
7
  "license": "Apache-2.0",