@yuntower/yuntower-account-web-sdk 0.0.7 → 0.0.9

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/README.md CHANGED
@@ -1,20 +1,20 @@
1
- # YunTowerAccount-WebSDK
2
-
3
- 云塔开放认证平台官方Web SDK
4
-
5
- Apache License 2.0
6
-
7
- - 官网:[https://account.yuntower.com](https://account.yuntower.cn)
8
- - 文档:[https://account.yuntower.com/docs](https://account.yuntower.cn/docs)
9
- - NPM:[https://www.npmjs.com/package/@yuntower/yuntower-account-web-sdk](https://www.npmjs.com/package/yuntower-account-web-sdk)
10
-
11
-
12
- ```
13
- // For versions <= 0.0.6
14
- npm i yuntower-account-web-sdk
15
-
16
- // For versions >= 0.0.7
17
- npm i yuntower/yuntower-account-web-sdk
18
- ```
19
-
1
+ # YunTowerAccount-WebSDK
2
+
3
+ 云塔开放认证平台官方Web SDK
4
+
5
+ Apache License 2.0
6
+
7
+ - 官网:[https://account.yuntower.com](https://account.yuntower.cn)
8
+ - 文档:[https://account.yuntower.com/docs](https://account.yuntower.cn/docs)
9
+ - NPM:[https://www.npmjs.com/package/@yuntower/yuntower-account-web-sdk](https://www.npmjs.com/package/yuntower-account-web-sdk)
10
+
11
+
12
+ ```
13
+ // For versions <= 0.0.6
14
+ npm i yuntower-account-web-sdk
15
+
16
+ // For versions >= 0.0.7
17
+ npm i yuntower/yuntower-account-web-sdk
18
+ ```
19
+
20
20
  Provide by YunTower
@@ -0,0 +1,61 @@
1
+ /**
2
+ * 云塔账号通行证 WEB SDK
3
+ * @author YunTower
4
+ * @version 0.0.9
5
+ * @license MIT
6
+ * @see https://github.com/YunTower/YunTowerAccount-WebSDK
7
+ */
8
+ declare class YunTowerAccountSDK {
9
+ private authStatus;
10
+ private readonly config;
11
+ private static readonly ALLOWED_SCOPES;
12
+ private static readonly ALLOWED_TYPES;
13
+ private static readonly DEFAULT_AUTH_URL;
14
+ private static readonly ALLOWED_ORIGINS;
15
+ constructor({ type, appid, redirectUrl, state, scope }: ConstructorParams);
16
+ /**
17
+ * 验证构造函数参数
18
+ */
19
+ private validateParams;
20
+ /**
21
+ * 生成授权URL
22
+ */
23
+ private buildAuthUrl;
24
+ /**
25
+ * 验证消息来源
26
+ */
27
+ private isValidOrigin;
28
+ /**
29
+ * 处理授权消息
30
+ */
31
+ private handleAuthMessage;
32
+ /**
33
+ * 设置消息监听器
34
+ */
35
+ private setupMessageListener;
36
+ /**
37
+ * 加载授权窗口 (iframe模式)
38
+ * @param elementId 目标元素的ID
39
+ * @param style 自定义样式
40
+ * @param callback 回调函数
41
+ */
42
+ loadAuthWindow(elementId: string, style: string | undefined, callback: (response: CallbackResponse) => void): void;
43
+ /**
44
+ * 开启授权窗口
45
+ * @param callback 回调函数
46
+ */
47
+ openAuthWindow(callback?: (response: CallbackResponse) => void): void;
48
+ /**
49
+ * 获取当前授权状态
50
+ */
51
+ getAuthStatus(): boolean;
52
+ /**
53
+ * 重置授权状态
54
+ */
55
+ resetAuthStatus(): void;
56
+ /**
57
+ * 获取当前配置
58
+ */
59
+ getConfig(): Readonly<SDKConfig>;
60
+ }
61
+ export default YunTowerAccountSDK;
package/dist/index.js ADDED
@@ -0,0 +1,193 @@
1
+ /**
2
+ * 云塔账号通行证 WEB SDK
3
+ * @author YunTower
4
+ * @version 0.0.9
5
+ * @license MIT
6
+ * @see https://github.com/YunTower/YunTowerAccount-WebSDK
7
+ */
8
+ class YunTowerAccountSDK {
9
+ authStatus;
10
+ config;
11
+ static ALLOWED_SCOPES = [
12
+ 'user:profile',
13
+ 'user:email',
14
+ 'connect:codemao_uid',
15
+ 'connect:pgaot_uid',
16
+ 'connect:dao3_uid',
17
+ ];
18
+ static ALLOWED_TYPES = ['window', 'redirect', 'iframe'];
19
+ static DEFAULT_AUTH_URL = 'http://localhost:5173';
20
+ static ALLOWED_ORIGINS = [
21
+ 'account.yuntower.cn',
22
+ 'account.yuntower.com',
23
+ 'localhost:5173',
24
+ ];
25
+ constructor({ type, appid, redirectUrl, state, scope = ['user:profile'] }) {
26
+ this.validateParams({ type, appid, scope });
27
+ this.authStatus = false;
28
+ this.config = {
29
+ authUrl: YunTowerAccountSDK.DEFAULT_AUTH_URL,
30
+ allowedOrigins: [...YunTowerAccountSDK.ALLOWED_ORIGINS],
31
+ type,
32
+ appid,
33
+ scope,
34
+ redirectUrl,
35
+ state,
36
+ };
37
+ }
38
+ /**
39
+ * 验证构造函数参数
40
+ */
41
+ validateParams({ type, appid, scope, }) {
42
+ if (!type || !appid || !scope?.length) {
43
+ throw new Error('[YunTowerAccountSDK] 参数缺失: type、appid 和 scope 为必填项');
44
+ }
45
+ if (!YunTowerAccountSDK.ALLOWED_TYPES.includes(type)) {
46
+ throw new Error(`[YunTowerAccountSDK] type参数错误,支持的类型: ${YunTowerAccountSDK.ALLOWED_TYPES.join(', ')}`);
47
+ }
48
+ for (const item of scope) {
49
+ if (!YunTowerAccountSDK.ALLOWED_SCOPES.includes(item)) {
50
+ throw new Error(`[YunTowerAccountSDK] scope参数错误,目前只支持: ${YunTowerAccountSDK.ALLOWED_SCOPES.join(', ')}`);
51
+ }
52
+ }
53
+ }
54
+ /**
55
+ * 生成授权URL
56
+ */
57
+ buildAuthUrl() {
58
+ const params = new URLSearchParams({
59
+ type: this.config.type,
60
+ appid: this.config.appid,
61
+ scope: this.config.scope.join(','),
62
+ });
63
+ if (this.config.redirectUrl) {
64
+ params.append('redirect_url', this.config.redirectUrl);
65
+ }
66
+ if (this.config.state) {
67
+ params.append('state', this.config.state);
68
+ }
69
+ return `${this.config.authUrl}/auth/app?${params.toString()}`;
70
+ }
71
+ /**
72
+ * 验证消息来源
73
+ */
74
+ isValidOrigin(origin) {
75
+ const normalizedOrigin = origin.replace(/^https?:\/\//, '');
76
+ return this.config.allowedOrigins.includes(normalizedOrigin);
77
+ }
78
+ /**
79
+ * 处理授权消息
80
+ */
81
+ handleAuthMessage(event, callback) {
82
+ if (!this.isValidOrigin(event.origin)) {
83
+ return false;
84
+ }
85
+ if (event.data?.action === 'status') {
86
+ const { status, data, msg } = event.data;
87
+ if (status === 'success') {
88
+ this.authStatus = true;
89
+ }
90
+ callback({
91
+ event: 'auth',
92
+ status,
93
+ data,
94
+ msg,
95
+ });
96
+ return true;
97
+ }
98
+ return false;
99
+ }
100
+ /**
101
+ * 设置消息监听器
102
+ */
103
+ setupMessageListener(callback) {
104
+ const messageListener = (event) => {
105
+ if (this.handleAuthMessage(event, callback)) {
106
+ cleanup();
107
+ }
108
+ };
109
+ const cleanup = () => {
110
+ window.removeEventListener('message', messageListener);
111
+ };
112
+ window.addEventListener('message', messageListener);
113
+ return cleanup;
114
+ }
115
+ /**
116
+ * 加载授权窗口 (iframe模式)
117
+ * @param elementId 目标元素的ID
118
+ * @param style 自定义样式
119
+ * @param callback 回调函数
120
+ */
121
+ loadAuthWindow(elementId, style = '', callback) {
122
+ if (this.config.type !== 'iframe') {
123
+ throw new Error('[YunTowerAccountSDK] 此方法仅支持iframe类型');
124
+ }
125
+ const iframe = document.getElementById(elementId);
126
+ if (!iframe) {
127
+ throw new Error('[YunTowerAccountSDK] 未找到目标元素');
128
+ }
129
+ const defaultStyle = 'height: 640px; width: 400px; border: unset; border-radius: 5px';
130
+ iframe.src = this.buildAuthUrl();
131
+ iframe.style.cssText = style || defaultStyle;
132
+ this.setupMessageListener(callback);
133
+ }
134
+ /**
135
+ * 开启授权窗口
136
+ * @param callback 回调函数
137
+ */
138
+ openAuthWindow(callback = () => { }) {
139
+ const authUrl = this.buildAuthUrl();
140
+ if (this.config.type === 'redirect') {
141
+ window.location.href = authUrl;
142
+ return;
143
+ }
144
+ if (this.config.type === 'window') {
145
+ const authWindow = window.open(authUrl, '_blank', 'width=500,height=600');
146
+ if (!authWindow) {
147
+ throw new Error('[YunTowerAccountSDK] 无法打开授权窗口,可能被浏览器拦截');
148
+ }
149
+ const cleanup = this.setupMessageListener(callback);
150
+ // 监听窗口关闭状态
151
+ const checkInterval = setInterval(() => {
152
+ if (authWindow.closed) {
153
+ clearInterval(checkInterval);
154
+ cleanup();
155
+ callback({
156
+ event: 'closed',
157
+ status: 'success',
158
+ });
159
+ }
160
+ else {
161
+ // 发送状态检查消息
162
+ try {
163
+ authWindow.postMessage({ action: 'status' }, '*');
164
+ }
165
+ catch {
166
+ // 窗口可能已经关闭,忽略错误
167
+ }
168
+ }
169
+ }, 3000);
170
+ return;
171
+ }
172
+ throw new Error('[YunTowerAccountSDK] 不支持的授权类型');
173
+ }
174
+ /**
175
+ * 获取当前授权状态
176
+ */
177
+ getAuthStatus() {
178
+ return this.authStatus;
179
+ }
180
+ /**
181
+ * 重置授权状态
182
+ */
183
+ resetAuthStatus() {
184
+ this.authStatus = false;
185
+ }
186
+ /**
187
+ * 获取当前配置
188
+ */
189
+ getConfig() {
190
+ return Object.freeze({ ...this.config });
191
+ }
192
+ }
193
+ export default YunTowerAccountSDK;
package/package.json CHANGED
@@ -1,14 +1,26 @@
1
1
  {
2
2
  "name": "@yuntower/yuntower-account-web-sdk",
3
3
  "description": "YunTower Account Web SDK",
4
- "version": "0.0.7",
4
+ "version": "0.0.9",
5
5
  "private": false,
6
6
  "author": "yuntower",
7
7
  "license": "Apache-2.0",
8
8
  "dependencies": {
9
9
  "typescript": "^5.5.4"
10
10
  },
11
- "main": "index.js",
11
+ "main": "dist/index.js",
12
+ "module": "dist/index.js",
13
+ "types": "dist/index.d.ts",
14
+ "exports": {
15
+ ".": {
16
+ "require": "./dist/index.js",
17
+ "import": "./dist/index.js",
18
+ "types": "./dist/index.d.ts"
19
+ }
20
+ },
21
+ "files": [
22
+ "dist"
23
+ ],
12
24
  "scripts": {
13
25
  "test": "echo \"Error: no test specified\" && exit 1"
14
26
  },
package/index.js DELETED
@@ -1,134 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- var YunTowerAccountSDK = /** @class */ (function () {
4
- function YunTowerAccountSDK(_a) {
5
- var type = _a.type, appid = _a.appid, _b = _a.redirect_url, redirect_url = _b === void 0 ? null : _b, _c = _a.state, state = _c === void 0 ? null : _c, _d = _a.scope, scope = _d === void 0 ? 'user_profile' : _d;
6
- if (!appid || !scope) {
7
- console.error('[YunTowerAccountSDK] 参数缺失');
8
- }
9
- if (!['window', 'redirect', 'iframe'].includes(type)) {
10
- console.error('[YunTowerAccountSDK] [type]参数错误');
11
- }
12
- if (!['user_profile'].includes(scope)) {
13
- console.error('[YunTowerAccountSDK] [scope]参数错误,目前只支持[user_profile]');
14
- }
15
- this.auth_status = false;
16
- this.config = {
17
- auth: 'https://account.yuntower.com',
18
- origin_white_list: ['account.yuntower.cn', 'account.yuntower.com'],
19
- type: type,
20
- appid: appid,
21
- scope: scope,
22
- redirect_url: redirect_url,
23
- state: state
24
- };
25
- }
26
- /**
27
- * 加载授权窗口
28
- *
29
- * @param {string} id 目标元素的ID
30
- * @param {string} style 样式
31
- * @param callback
32
- */
33
- YunTowerAccountSDK.prototype.loadAuthWindow = function (id, style, callback) {
34
- var _this = this;
35
- if (style === void 0) { style = ''; }
36
- var auth_path = "".concat(this.config.auth, "/auth/app?type=").concat(this.config.type, "&appid=").concat(this.config.appid, "&redirect_url=").concat(this.config.redirect_url, "&scope=").concat(this.config.scope, "&state=").concat(this.config.state);
37
- var iframe = document.getElementById(id);
38
- if (!iframe) {
39
- console.error('[YunTowerAccountSDK] 未找到id元素');
40
- return;
41
- }
42
- if (this.config.type !== 'iframe') {
43
- console.error('[YunTowerAccountSDK] type 参数错误,仅支持[iframe]类型');
44
- return;
45
- }
46
- if (style === '') {
47
- style = "height: 366px; width: 400px; border: unset; border-radius: 5px";
48
- }
49
- iframe.setAttribute('src', auth_path);
50
- iframe.setAttribute('style', style);
51
- // 监听来自子页面的消息
52
- var messageListener = function (event) {
53
- var _a, _b, _c, _d;
54
- var origin = event.origin.replace(/^https?:\/\//, '');
55
- if (!_this.config.origin_white_list.includes(origin))
56
- return;
57
- // 授权成功
58
- if (((_a = event.data) === null || _a === void 0 ? void 0 : _a.action) === 'status') {
59
- window.removeEventListener('message', messageListener);
60
- if (((_b = event.data) === null || _b === void 0 ? void 0 : _b.status) === 'success') {
61
- _this.auth_status = true;
62
- callback({
63
- event: 'auth',
64
- status: (_c = event.data) === null || _c === void 0 ? void 0 : _c.status,
65
- data: event.data.data
66
- });
67
- }
68
- else {
69
- callback({
70
- event: 'auth',
71
- status: (_d = event.data) === null || _d === void 0 ? void 0 : _d.status,
72
- msg: event.data.msg
73
- });
74
- }
75
- }
76
- };
77
- window.addEventListener('message', messageListener);
78
- };
79
- /**
80
- * 开启授权窗口
81
- * @param {*} callback
82
- */
83
- YunTowerAccountSDK.prototype.openAuthWindow = function (callback) {
84
- var _this = this;
85
- var auth_path = "".concat(this.config.auth, "/auth/app?type=").concat(this.config.type, "&appid=").concat(this.config.appid, "&redirect_url=").concat(this.config.redirect_url, "&scope=").concat(this.config.scope, "&state=").concat(this.config.state);
86
- if (this.config.type == 'redirect') {
87
- window.location.href = auth_path;
88
- return false;
89
- }
90
- var child = window.open(auth_path, '_blank', 'width=500,height=600');
91
- // 监听来自子页面的消息
92
- var messageListener = function (event) {
93
- var _a, _b, _c, _d;
94
- var origin = event.origin.replace(/^https?:\/\//, '');
95
- if (!_this.config.origin_white_list.includes(origin))
96
- return;
97
- // 授权成功
98
- if (((_a = event.data) === null || _a === void 0 ? void 0 : _a.action) === 'status') {
99
- window.removeEventListener('message', messageListener);
100
- if (((_b = event.data) === null || _b === void 0 ? void 0 : _b.status) === 'success') {
101
- _this.auth_status = true;
102
- callback({
103
- event: 'auth',
104
- status: (_c = event.data) === null || _c === void 0 ? void 0 : _c.status,
105
- data: event.data.data
106
- });
107
- }
108
- else {
109
- callback({
110
- event: 'auth',
111
- status: (_d = event.data) === null || _d === void 0 ? void 0 : _d.status,
112
- msg: event.data.msg
113
- });
114
- }
115
- child === null || child === void 0 ? void 0 : child.close();
116
- }
117
- };
118
- window.addEventListener('message', messageListener);
119
- if (child && !child.closed) {
120
- var timer_1 = setInterval(function () {
121
- if (child.closed) {
122
- clearInterval(timer_1);
123
- callback({
124
- event: 'closed',
125
- status: 'success'
126
- });
127
- }
128
- child.postMessage({ action: 'status' }, '*');
129
- }, 3000);
130
- }
131
- };
132
- return YunTowerAccountSDK;
133
- }());
134
- exports.default = YunTowerAccountSDK;
package/src/index.js DELETED
@@ -1,134 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- var YunTowerAccountSDK = /** @class */ (function () {
4
- function YunTowerAccountSDK(_a) {
5
- var type = _a.type, appid = _a.appid, _b = _a.redirect_url, redirect_url = _b === void 0 ? null : _b, _c = _a.state, state = _c === void 0 ? null : _c, _d = _a.scope, scope = _d === void 0 ? 'user_profile' : _d;
6
- if (!appid || !scope) {
7
- console.error('[YunTowerAccountSDK] 参数缺失');
8
- }
9
- if (!['window', 'redirect', 'iframe'].includes(type)) {
10
- console.error('[YunTowerAccountSDK] [type]参数错误');
11
- }
12
- if (!['user_profile'].includes(scope)) {
13
- console.error('[YunTowerAccountSDK] [scope]参数错误,目前只支持[user_profile]');
14
- }
15
- this.auth_status = false;
16
- this.config = {
17
- auth: 'https://account.yuntower.com',
18
- origin_white_list: ['account.yuntower.cn', 'account.yuntower.com'],
19
- type: type,
20
- appid: appid,
21
- scope: scope,
22
- redirect_url: redirect_url,
23
- state: state
24
- };
25
- }
26
- /**
27
- * 加载授权窗口
28
- *
29
- * @param {string} id 目标元素的ID
30
- * @param {string} style 样式
31
- * @param callback
32
- */
33
- YunTowerAccountSDK.prototype.loadAuthWindow = function (id, style, callback) {
34
- var _this = this;
35
- if (style === void 0) { style = ''; }
36
- var auth_path = "".concat(this.config.auth, "/auth/app?type=").concat(this.config.type, "&appid=").concat(this.config.appid, "&redirect_url=").concat(this.config.redirect_url, "&scope=").concat(this.config.scope, "&state=").concat(this.config.state);
37
- var iframe = document.getElementById(id);
38
- if (!iframe) {
39
- console.error('[YunTowerAccountSDK] 未找到id元素');
40
- return;
41
- }
42
- if (this.config.type !== 'iframe') {
43
- console.error('[YunTowerAccountSDK] type 参数错误,仅支持[iframe]类型');
44
- return;
45
- }
46
- if (style === '') {
47
- style = "height: 366px; width: 400px; border: unset; border-radius: 5px";
48
- }
49
- iframe.setAttribute('src', auth_path);
50
- iframe.setAttribute('style', style);
51
- // 监听来自子页面的消息
52
- var messageListener = function (event) {
53
- var _a, _b, _c, _d;
54
- var origin = event.origin.replace(/^https?:\/\//, '');
55
- if (!_this.config.origin_white_list.includes(origin))
56
- return;
57
- // 授权成功
58
- if (((_a = event.data) === null || _a === void 0 ? void 0 : _a.action) === 'status') {
59
- window.removeEventListener('message', messageListener);
60
- if (((_b = event.data) === null || _b === void 0 ? void 0 : _b.status) === 'success') {
61
- _this.auth_status = true;
62
- callback({
63
- event: 'auth',
64
- status: (_c = event.data) === null || _c === void 0 ? void 0 : _c.status,
65
- data: event.data.data
66
- });
67
- }
68
- else {
69
- callback({
70
- event: 'auth',
71
- status: (_d = event.data) === null || _d === void 0 ? void 0 : _d.status,
72
- msg: event.data.msg
73
- });
74
- }
75
- }
76
- };
77
- window.addEventListener('message', messageListener);
78
- };
79
- /**
80
- * 开启授权窗口
81
- * @param {*} callback
82
- */
83
- YunTowerAccountSDK.prototype.openAuthWindow = function (callback) {
84
- var _this = this;
85
- var auth_path = "".concat(this.config.auth, "/auth/app?type=").concat(this.config.type, "&appid=").concat(this.config.appid, "&redirect_url=").concat(this.config.redirect_url, "&scope=").concat(this.config.scope, "&state=").concat(this.config.state);
86
- if (this.config.type == 'redirect') {
87
- window.location.href = auth_path;
88
- return false;
89
- }
90
- var child = window.open(auth_path, '_blank', 'width=500,height=600');
91
- // 监听来自子页面的消息
92
- var messageListener = function (event) {
93
- var _a, _b, _c, _d;
94
- var origin = event.origin.replace(/^https?:\/\//, '');
95
- if (!_this.config.origin_white_list.includes(origin))
96
- return;
97
- // 授权成功
98
- if (((_a = event.data) === null || _a === void 0 ? void 0 : _a.action) === 'status') {
99
- window.removeEventListener('message', messageListener);
100
- if (((_b = event.data) === null || _b === void 0 ? void 0 : _b.status) === 'success') {
101
- _this.auth_status = true;
102
- callback({
103
- event: 'auth',
104
- status: (_c = event.data) === null || _c === void 0 ? void 0 : _c.status,
105
- data: event.data.data
106
- });
107
- }
108
- else {
109
- callback({
110
- event: 'auth',
111
- status: (_d = event.data) === null || _d === void 0 ? void 0 : _d.status,
112
- msg: event.data.msg
113
- });
114
- }
115
- child === null || child === void 0 ? void 0 : child.close();
116
- }
117
- };
118
- window.addEventListener('message', messageListener);
119
- if (child && !child.closed) {
120
- var timer_1 = setInterval(function () {
121
- if (child.closed) {
122
- clearInterval(timer_1);
123
- callback({
124
- event: 'closed',
125
- status: 'success'
126
- });
127
- }
128
- child.postMessage({ action: 'status' }, '*');
129
- }, 3000);
130
- }
131
- };
132
- return YunTowerAccountSDK;
133
- }());
134
- exports.default = YunTowerAccountSDK;
package/src/index.ts DELETED
@@ -1,181 +0,0 @@
1
- class YunTowerAccountSDK {
2
- auth_status: boolean;
3
- config: {
4
- auth: string,
5
- origin_white_list: string[];
6
- type: 'window' | 'redirect' | 'iframe';
7
- appid: string;
8
- scope: string | '';
9
- redirect_url: null | string;
10
- state: null | string;
11
- };
12
-
13
- constructor({
14
- type,
15
- appid,
16
- redirect_url = null,
17
- state = null,
18
- scope = 'user_profile'
19
- }: {
20
- type: 'window' | 'redirect' | 'iframe';
21
- appid: string;
22
- scope: string | 'user_profile';
23
- redirect_url?: null | string;
24
- state?: null | string;
25
- }) {
26
- if (!appid || !scope) {
27
- console.error('[YunTowerAccountSDK] 参数缺失');
28
- }
29
-
30
- if (!['window', 'redirect', 'iframe'].includes(type)) {
31
- console.error('[YunTowerAccountSDK] [type]参数错误');
32
- }
33
-
34
- if (!['user_profile'].includes(scope)) {
35
- console.error('[YunTowerAccountSDK] [scope]参数错误,目前只支持[user_profile]');
36
- }
37
-
38
- this.auth_status = false;
39
- this.config = {
40
- auth: 'https://account.yuntower.com',
41
- origin_white_list: ['account.yuntower.cn', 'account.yuntower.com'],
42
- type,
43
- appid,
44
- scope,
45
- redirect_url,
46
- state
47
- };
48
- }
49
-
50
- /**
51
- * 加载授权窗口
52
- *
53
- * @param {string} id 目标元素的ID
54
- * @param {string} style 样式
55
- * @param callback
56
- */
57
- loadAuthWindow(id: string, style: string = '', callback: (arg0: {
58
- event: string;
59
- status: 'success' | 'failed' | 'error' | 'noLogin' | 'denied';
60
- data?: any;
61
- msg?: string;
62
- }) => void) {
63
- const auth_path = `${this.config.auth}/auth/app?type=${this.config.type}&appid=${this.config.appid}&redirect_url=${this.config.redirect_url}&scope=${this.config.scope}&state=${this.config.state}`;
64
- const iframe = document.getElementById(id);
65
- if (!iframe) {
66
- console.error('[YunTowerAccountSDK] 未找到id元素');
67
- return;
68
- }
69
-
70
- if (this.config.type !== 'iframe') {
71
- console.error('[YunTowerAccountSDK] type 参数错误,仅支持[iframe]类型');
72
- return;
73
- }
74
-
75
- if (style === '') {
76
- style = "height: 366px; width: 400px; border: unset; border-radius: 5px"
77
- }
78
-
79
-
80
- iframe.setAttribute('src', auth_path);
81
- iframe.setAttribute('style', style);
82
-
83
- // 监听来自子页面的消息
84
- const messageListener = (event: MessageEvent) => {
85
- const origin = event.origin.replace(/^https?:\/\//, '');
86
-
87
- if (!this.config.origin_white_list.includes(origin)) return;
88
-
89
- // 授权成功
90
- if (event.data?.action === 'status') {
91
- window.removeEventListener('message', messageListener);
92
- if (event.data?.status === 'success') {
93
- this.auth_status = true;
94
- callback({
95
- event: 'auth',
96
- status: event.data?.status,
97
- data: event.data.data
98
- });
99
- } else {
100
- callback({
101
- event: 'auth',
102
- status: event.data?.status,
103
- msg: event.data.msg
104
- });
105
- }
106
- }
107
- };
108
-
109
- window.addEventListener('message', messageListener);
110
- }
111
-
112
- /**
113
- * 开启授权窗口
114
- * @param {*} callback
115
- */
116
- openAuthWindow(callback: (arg0: {
117
- event: string;
118
- status: 'success' | 'failed' | 'error' | 'noLogin' | 'denied';
119
- data?: any;
120
- msg?: string;
121
- }) => void) {
122
- const auth_path = `${this.config.auth}/auth/app?type=${this.config.type}&appid=${this.config.appid}&redirect_url=${this.config.redirect_url}&scope=${this.config.scope}&state=${this.config.state}`;
123
-
124
- if (this.config.type == 'redirect') {
125
- window.location.href = auth_path;
126
- return false;
127
- }
128
-
129
- let child = window.open(
130
- auth_path,
131
- '_blank',
132
- 'width=500,height=600'
133
- );
134
-
135
-
136
- // 监听来自子页面的消息
137
- const messageListener = (event: MessageEvent) => {
138
- const origin = event.origin.replace(/^https?:\/\//, '');
139
-
140
- if (!this.config.origin_white_list.includes(origin)) return;
141
-
142
- // 授权成功
143
- if (event.data?.action === 'status') {
144
- window.removeEventListener('message', messageListener);
145
- if (event.data?.status === 'success') {
146
- this.auth_status = true;
147
- callback({
148
- event: 'auth',
149
- status: event.data?.status,
150
- data: event.data.data
151
- });
152
- } else {
153
- callback({
154
- event: 'auth',
155
- status: event.data?.status,
156
- msg: event.data.msg
157
- });
158
- }
159
- child?.close();
160
- }
161
- };
162
-
163
- window.addEventListener('message', messageListener);
164
-
165
-
166
- if (child && !child.closed) {
167
- const timer = setInterval(() => {
168
- if (child.closed) {
169
- clearInterval(timer);
170
- callback({
171
- event: 'closed',
172
- status: 'success'
173
- });
174
- }
175
- child.postMessage({action: 'status'}, '*');
176
- }, 3000);
177
- }
178
- }
179
- }
180
-
181
- export default YunTowerAccountSDK;
package/tsconfig.json DELETED
@@ -1,17 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "target": "es5",
4
- "module": "commonjs",
5
- "lib": ["es2020", "dom", "es2015.promise"],
6
- "strict": true,
7
- "esModuleInterop": true,
8
- "outDir": "./dist"
9
- },
10
- "include": [
11
- "src/**/*"
12
- ],
13
- "exclude": [
14
- "node_modules",
15
- "**/*.spec.ts"
16
- ]
17
- }