@yuntower/yuntower-account-web-sdk 0.0.7 → 0.0.8
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 +43 -0
- package/{src/index.ts → dist/index.js} +127 -181
- package/package.json +14 -2
- package/index.js +0 -134
- package/src/index.js +0 -134
- package/tsconfig.json +0 -17
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
declare 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
|
+
constructor({ type, appid, redirect_url, state, scope }: {
|
|
13
|
+
type: 'window' | 'redirect' | 'iframe';
|
|
14
|
+
appid: string;
|
|
15
|
+
scope: string | 'user_profile';
|
|
16
|
+
redirect_url?: null | string;
|
|
17
|
+
state?: null | string;
|
|
18
|
+
});
|
|
19
|
+
/**
|
|
20
|
+
* 加载授权窗口
|
|
21
|
+
*
|
|
22
|
+
* @param {string} id 目标元素的ID
|
|
23
|
+
* @param {string} style 样式
|
|
24
|
+
* @param callback
|
|
25
|
+
*/
|
|
26
|
+
loadAuthWindow(id: string, style: string | undefined, callback: (arg0: {
|
|
27
|
+
event: string;
|
|
28
|
+
status: 'success' | 'failed' | 'error' | 'noLogin' | 'denied';
|
|
29
|
+
data?: any;
|
|
30
|
+
msg?: string;
|
|
31
|
+
}) => void): void;
|
|
32
|
+
/**
|
|
33
|
+
* 开启授权窗口
|
|
34
|
+
* @param {*} callback
|
|
35
|
+
*/
|
|
36
|
+
openAuthWindow(callback: (arg0: {
|
|
37
|
+
event: string;
|
|
38
|
+
status: 'success' | 'failed' | 'error' | 'noLogin' | 'denied';
|
|
39
|
+
data?: any;
|
|
40
|
+
msg?: string;
|
|
41
|
+
}) => void): false | undefined;
|
|
42
|
+
}
|
|
43
|
+
export default YunTowerAccountSDK;
|
|
@@ -1,181 +1,127 @@
|
|
|
1
|
-
class YunTowerAccountSDK {
|
|
2
|
-
auth_status
|
|
3
|
-
config
|
|
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
|
-
this.config
|
|
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
|
-
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;
|
|
1
|
+
class YunTowerAccountSDK {
|
|
2
|
+
auth_status;
|
|
3
|
+
config;
|
|
4
|
+
constructor({ type, appid, redirect_url = null, state = null, scope = 'user_profile' }) {
|
|
5
|
+
if (!appid || !scope) {
|
|
6
|
+
console.error('[YunTowerAccountSDK] 参数缺失');
|
|
7
|
+
}
|
|
8
|
+
if (!['window', 'redirect', 'iframe'].includes(type)) {
|
|
9
|
+
console.error('[YunTowerAccountSDK] [type]参数错误');
|
|
10
|
+
}
|
|
11
|
+
if (!['user_profile'].includes(scope)) {
|
|
12
|
+
console.error('[YunTowerAccountSDK] [scope]参数错误,目前只支持[user_profile]');
|
|
13
|
+
}
|
|
14
|
+
this.auth_status = false;
|
|
15
|
+
this.config = {
|
|
16
|
+
auth: 'https://account.yuntower.com',
|
|
17
|
+
origin_white_list: ['account.yuntower.cn', 'account.yuntower.com'],
|
|
18
|
+
type,
|
|
19
|
+
appid,
|
|
20
|
+
scope,
|
|
21
|
+
redirect_url,
|
|
22
|
+
state
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* 加载授权窗口
|
|
27
|
+
*
|
|
28
|
+
* @param {string} id 目标元素的ID
|
|
29
|
+
* @param {string} style 样式
|
|
30
|
+
* @param callback
|
|
31
|
+
*/
|
|
32
|
+
loadAuthWindow(id, style = '', callback) {
|
|
33
|
+
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}`;
|
|
34
|
+
const iframe = document.getElementById(id);
|
|
35
|
+
if (!iframe) {
|
|
36
|
+
console.error('[YunTowerAccountSDK] 未找到id元素');
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
if (this.config.type !== 'iframe') {
|
|
40
|
+
console.error('[YunTowerAccountSDK] type 参数错误,仅支持[iframe]类型');
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
if (style === '') {
|
|
44
|
+
style = "height: 366px; width: 400px; border: unset; border-radius: 5px";
|
|
45
|
+
}
|
|
46
|
+
iframe.setAttribute('src', auth_path);
|
|
47
|
+
iframe.setAttribute('style', style);
|
|
48
|
+
// 监听来自子页面的消息
|
|
49
|
+
const messageListener = (event) => {
|
|
50
|
+
const origin = event.origin.replace(/^https?:\/\//, '');
|
|
51
|
+
if (!this.config.origin_white_list.includes(origin))
|
|
52
|
+
return;
|
|
53
|
+
// 授权成功
|
|
54
|
+
if (event.data?.action === 'status') {
|
|
55
|
+
window.removeEventListener('message', messageListener);
|
|
56
|
+
if (event.data?.status === 'success') {
|
|
57
|
+
this.auth_status = true;
|
|
58
|
+
callback({
|
|
59
|
+
event: 'auth',
|
|
60
|
+
status: event.data?.status,
|
|
61
|
+
data: event.data.data
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
callback({
|
|
66
|
+
event: 'auth',
|
|
67
|
+
status: event.data?.status,
|
|
68
|
+
msg: event.data.msg
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
window.addEventListener('message', messageListener);
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* 开启授权窗口
|
|
77
|
+
* @param {*} callback
|
|
78
|
+
*/
|
|
79
|
+
openAuthWindow(callback) {
|
|
80
|
+
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}`;
|
|
81
|
+
if (this.config.type == 'redirect') {
|
|
82
|
+
window.location.href = auth_path;
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
|
+
let child = window.open(auth_path, '_blank', 'width=500,height=600');
|
|
86
|
+
// 监听来自子页面的消息
|
|
87
|
+
const messageListener = (event) => {
|
|
88
|
+
const origin = event.origin.replace(/^https?:\/\//, '');
|
|
89
|
+
if (!this.config.origin_white_list.includes(origin))
|
|
90
|
+
return;
|
|
91
|
+
// 授权成功
|
|
92
|
+
if (event.data?.action === 'status') {
|
|
93
|
+
window.removeEventListener('message', messageListener);
|
|
94
|
+
if (event.data?.status === 'success') {
|
|
95
|
+
this.auth_status = true;
|
|
96
|
+
callback({
|
|
97
|
+
event: 'auth',
|
|
98
|
+
status: event.data?.status,
|
|
99
|
+
data: event.data.data
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
else {
|
|
103
|
+
callback({
|
|
104
|
+
event: 'auth',
|
|
105
|
+
status: event.data?.status,
|
|
106
|
+
msg: event.data.msg
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
child?.close();
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
window.addEventListener('message', messageListener);
|
|
113
|
+
if (child && !child.closed) {
|
|
114
|
+
const timer = setInterval(() => {
|
|
115
|
+
if (child.closed) {
|
|
116
|
+
clearInterval(timer);
|
|
117
|
+
callback({
|
|
118
|
+
event: 'closed',
|
|
119
|
+
status: 'success'
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
child.postMessage({ action: 'status' }, '*');
|
|
123
|
+
}, 3000);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
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.
|
|
4
|
+
"version": "0.0.8",
|
|
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/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
|
-
}
|