lgsso-sdk 1.2.7 → 1.3.0
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/lgsso-sdk.cjs +249 -113
- package/dist/lgsso-sdk.esm.js +249 -113
- package/dist/lgsso-sdk.js +249 -113
- package/dist/lgsso-sdk.min.js +1 -1
- package/package.json +1 -1
- package/src/sso.js +213 -98
- package/src/utils.js +36 -15
package/dist/lgsso-sdk.js
CHANGED
|
@@ -7,8 +7,8 @@
|
|
|
7
7
|
/*
|
|
8
8
|
* @Author: Robin LEI
|
|
9
9
|
* @Date: 2025-08-21 15:09:15
|
|
10
|
-
* @LastEditTime:
|
|
11
|
-
* @FilePath: \
|
|
10
|
+
* @LastEditTime: 2026-01-23 11:28:26
|
|
11
|
+
* @FilePath: \lims-frontd:\业务代码\中联钢信\五服一管\lg-ssosdk\src\utils.js
|
|
12
12
|
*/
|
|
13
13
|
/**
|
|
14
14
|
* 检查是否在浏览器环境
|
|
@@ -44,38 +44,58 @@
|
|
|
44
44
|
}
|
|
45
45
|
|
|
46
46
|
/**
|
|
47
|
-
* 移除URL
|
|
48
|
-
* @param {string}
|
|
47
|
+
* 移除URL中的指定参数(支持单个或多个)
|
|
48
|
+
* @param {string|string[]} names - 要删除的参数名(单个字符串或字符串数组)
|
|
49
49
|
*/
|
|
50
|
-
function removeQueryParam(
|
|
50
|
+
function removeQueryParam(names) {
|
|
51
51
|
if (!isBrowser()) return;
|
|
52
52
|
|
|
53
|
+
// 统一转为数组,兼容单个参数的调用方式
|
|
54
|
+
const paramNames = Array.isArray(names) ? names : [names];
|
|
55
|
+
if (paramNames.length === 0) return; // 无参数需删除时直接返回
|
|
56
|
+
|
|
53
57
|
const url = new URL(window.location.href);
|
|
54
|
-
let
|
|
58
|
+
let newUrl;
|
|
55
59
|
|
|
56
60
|
// 处理hash模式(参数在#之后)
|
|
57
61
|
if (url.hash.includes('?')) {
|
|
58
62
|
const [hashPath, hashQuery] = url.hash.split('?');
|
|
59
|
-
params = new URLSearchParams(hashQuery);
|
|
63
|
+
const params = new URLSearchParams(hashQuery);
|
|
64
|
+
|
|
65
|
+
// 批量删除参数
|
|
66
|
+
paramNames.forEach(name => {
|
|
67
|
+
if (params.has(name)) {
|
|
68
|
+
params.delete(name);
|
|
69
|
+
}
|
|
70
|
+
});
|
|
60
71
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
72
|
+
// 重构hash部分
|
|
73
|
+
const newHash = params.toString() ? `${hashPath}?${params.toString()}` : hashPath;
|
|
74
|
+
if (newHash !== url.hash) { // 只有hash变化时才更新URL
|
|
64
75
|
url.hash = newHash;
|
|
65
76
|
newUrl = url.toString();
|
|
66
77
|
}
|
|
67
78
|
}
|
|
68
79
|
// 处理history模式(参数在?之后)
|
|
69
80
|
else {
|
|
70
|
-
params = new URLSearchParams(url.search);
|
|
71
|
-
|
|
72
|
-
|
|
81
|
+
const params = new URLSearchParams(url.search);
|
|
82
|
+
let hasChanged = false;
|
|
83
|
+
|
|
84
|
+
// 批量删除参数
|
|
85
|
+
paramNames.forEach(name => {
|
|
86
|
+
if (params.has(name)) {
|
|
87
|
+
params.delete(name);
|
|
88
|
+
hasChanged = true;
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
// 只有参数变化时才更新URL
|
|
93
|
+
if (hasChanged) {
|
|
73
94
|
url.search = params.toString();
|
|
74
95
|
newUrl = url.toString();
|
|
75
96
|
}
|
|
76
97
|
}
|
|
77
|
-
|
|
78
|
-
// 更新URL
|
|
98
|
+
// 更新URL(仅当URL有变化时)
|
|
79
99
|
if (newUrl && newUrl !== window.location.href) {
|
|
80
100
|
window.location.replace(newUrl);
|
|
81
101
|
}
|
|
@@ -98,6 +118,7 @@
|
|
|
98
118
|
*/
|
|
99
119
|
function mergeConfigs(defaults, options) {
|
|
100
120
|
if (!options) return { ...defaults };
|
|
121
|
+
|
|
101
122
|
const merged = { ...defaults };
|
|
102
123
|
for (const key in options) {
|
|
103
124
|
if (options.hasOwnProperty(key)) {
|
|
@@ -4042,6 +4063,93 @@
|
|
|
4042
4063
|
}
|
|
4043
4064
|
}
|
|
4044
4065
|
|
|
4066
|
+
// ===== Cookie操作工具函数 =====
|
|
4067
|
+
/**
|
|
4068
|
+
* 获取指定名称的Cookie值
|
|
4069
|
+
* @param {string} name Cookie名称
|
|
4070
|
+
* @returns {string|null} Cookie值
|
|
4071
|
+
*/
|
|
4072
|
+
function getCookie(name) {
|
|
4073
|
+
if (!isBrowser()) return null;
|
|
4074
|
+
const match = document.cookie.match(new RegExp(`(^| )${name}=([^;]+)`));
|
|
4075
|
+
return match ? decodeURIComponent(match[2]) : null;
|
|
4076
|
+
}
|
|
4077
|
+
|
|
4078
|
+
/**
|
|
4079
|
+
* 设置Cookie(支持指定根域名)
|
|
4080
|
+
* @param {string} name Cookie名称
|
|
4081
|
+
* @param {string} value Cookie值
|
|
4082
|
+
* @param {Object} options 配置(expires: 过期天数/Date, path: 路径, domain: 域名)
|
|
4083
|
+
*/
|
|
4084
|
+
function setCookie(name, value, options = {}) {
|
|
4085
|
+
if (!isBrowser()) return;
|
|
4086
|
+
let cookieStr = `${name}=${encodeURIComponent(value)}`;
|
|
4087
|
+
|
|
4088
|
+
// 路径:默认根路径,确保全站可用
|
|
4089
|
+
cookieStr += `; path=${options.path || '/'}`;
|
|
4090
|
+
|
|
4091
|
+
// 域名:指定根域名.zlgx.com(适配你的场景)
|
|
4092
|
+
// 优先级:用户传入的domain > 默认根域名.zlgx.com
|
|
4093
|
+
const targetDomain = options.domain || '.zlgx.com';
|
|
4094
|
+
cookieStr += `; domain=${targetDomain}`;
|
|
4095
|
+
|
|
4096
|
+
// 过期时间(默认1天)
|
|
4097
|
+
if (options.expires) {
|
|
4098
|
+
const expires = typeof options.expires === 'number'
|
|
4099
|
+
? new Date(Date.now() + options.expires * 86400000)
|
|
4100
|
+
: options.expires;
|
|
4101
|
+
cookieStr += `; expires=${expires.toUTCString()}`;
|
|
4102
|
+
}
|
|
4103
|
+
|
|
4104
|
+
// 可选:HTTPS环境下添加Secure(仅HTTPS传输)
|
|
4105
|
+
if (options.secure || window.location.protocol === 'https:') {
|
|
4106
|
+
cookieStr += '; Secure';
|
|
4107
|
+
}
|
|
4108
|
+
|
|
4109
|
+
// 可选:防止XSS攻击的HttpOnly(注意:HttpOnly的Cookie无法通过JS读取)
|
|
4110
|
+
if (options.httpOnly) {
|
|
4111
|
+
cookieStr += '; HttpOnly';
|
|
4112
|
+
}
|
|
4113
|
+
|
|
4114
|
+
document.cookie = cookieStr;
|
|
4115
|
+
}
|
|
4116
|
+
|
|
4117
|
+
function removeCookie(name, options = {}) {
|
|
4118
|
+
if (!isBrowser()) return;
|
|
4119
|
+
setCookie(
|
|
4120
|
+
name,
|
|
4121
|
+
'',
|
|
4122
|
+
{
|
|
4123
|
+
expires: -1, // 立即过期
|
|
4124
|
+
path: options.path || '/',
|
|
4125
|
+
domain: options.domain || '.zlgx.com' // 必须和设置时一致
|
|
4126
|
+
}
|
|
4127
|
+
);
|
|
4128
|
+
}
|
|
4129
|
+
|
|
4130
|
+
// 调用示例:删除根域名下的platType
|
|
4131
|
+
removeCookie('platType', { domain: '.zlgx.com' });
|
|
4132
|
+
|
|
4133
|
+
/**
|
|
4134
|
+
* 给重定向地址拼接platType参数
|
|
4135
|
+
* @param {string} redirectUrl 原始重定向地址
|
|
4136
|
+
* @param {string} platType platType值
|
|
4137
|
+
* @param {string} platTypeKey 参数名(默认platType)
|
|
4138
|
+
* @returns {string} 拼接后的重定向地址
|
|
4139
|
+
*/
|
|
4140
|
+
function addPlatTypeToRedirectUrl(redirectUrl, platType, platTypeKey = 'platType') {
|
|
4141
|
+
if (!redirectUrl || !platType) return redirectUrl;
|
|
4142
|
+
try {
|
|
4143
|
+
const url = new URL(redirectUrl);
|
|
4144
|
+
url.searchParams.set(platTypeKey, platType);
|
|
4145
|
+
return url.toString();
|
|
4146
|
+
} catch (e) {
|
|
4147
|
+
// 兼容非标准URL的情况(比如相对路径)
|
|
4148
|
+
const separator = redirectUrl.includes('?') ? '&' : '?';
|
|
4149
|
+
return `${redirectUrl}${separator}${platTypeKey}=${platType}`;
|
|
4150
|
+
}
|
|
4151
|
+
}
|
|
4152
|
+
|
|
4045
4153
|
// 默认配置
|
|
4046
4154
|
const DEFAULT_CONFIG = {
|
|
4047
4155
|
accessCodeKey: 'accessCode',
|
|
@@ -4052,13 +4160,14 @@
|
|
|
4052
4160
|
refreshCodeApi: '',
|
|
4053
4161
|
logoutApi: '',
|
|
4054
4162
|
logOutUrl: '',
|
|
4055
|
-
storage: localStorage,
|
|
4163
|
+
storage: localStorage,
|
|
4056
4164
|
oldPwdKey: 'oldPwd',
|
|
4057
4165
|
newPwdKey: 'newPwd',
|
|
4058
4166
|
changePasswordApi: '',
|
|
4059
4167
|
sendCaptchaCodeApi: '',
|
|
4060
4168
|
typeKey: 'type',
|
|
4061
|
-
codeKey: 'code'
|
|
4169
|
+
codeKey: 'code',
|
|
4170
|
+
platTypeKey: 'platType' // platType参数名配置
|
|
4062
4171
|
};
|
|
4063
4172
|
|
|
4064
4173
|
let config = null;
|
|
@@ -4081,46 +4190,35 @@
|
|
|
4081
4190
|
}
|
|
4082
4191
|
|
|
4083
4192
|
/**
|
|
4084
|
-
* 判断是否是iOS
|
|
4193
|
+
* 判断是否是iOS移动端
|
|
4085
4194
|
* @returns {boolean} 是否为iOS环境
|
|
4086
4195
|
*/
|
|
4087
4196
|
function isIOS() {
|
|
4088
|
-
|
|
4089
|
-
// if (!isBrowser()) return false;
|
|
4197
|
+
if (!isBrowser()) return false;
|
|
4090
4198
|
|
|
4091
4199
|
const userAgent = navigator.userAgent.toLowerCase();
|
|
4092
|
-
const platform = navigator.platform.toLowerCase();
|
|
4200
|
+
const platform = navigator.platform.toLowerCase();
|
|
4093
4201
|
const maxTouchPoints = navigator.maxTouchPoints || 0;
|
|
4094
|
-
const screenRatio = screen.width / screen.height;
|
|
4202
|
+
const screenRatio = screen.width / screen.height;
|
|
4095
4203
|
|
|
4096
|
-
// ========== 核心检测规则 ==========
|
|
4097
|
-
// 1. 基础规则:iPhone/iPod 直接命中(无兼容问题)
|
|
4098
4204
|
const isIphoneIpod = /iphone|ipod/.test(userAgent) && !window.MSStream;
|
|
4099
|
-
|
|
4100
|
-
// 2. iPad 专属检测(覆盖所有iPad机型,含iPad Air)
|
|
4101
4205
|
const isIpad = (
|
|
4102
|
-
// 场景1:老版本iPadOS/原生标识(platform含ipad)
|
|
4103
4206
|
platform.includes('ipad') ||
|
|
4104
|
-
// 场景2:iPadOS 13+ 伪装Mac(UA含macintosh + 触摸+ 非Mac平台)
|
|
4105
4207
|
(
|
|
4106
|
-
userAgent.includes('macintosh') &&
|
|
4107
|
-
maxTouchPoints > 0 &&
|
|
4108
|
-
!platform.includes('mac') &&
|
|
4208
|
+
userAgent.includes('macintosh') &&
|
|
4209
|
+
maxTouchPoints > 0 &&
|
|
4210
|
+
!platform.includes('mac') &&
|
|
4109
4211
|
!window.MSStream
|
|
4110
4212
|
) ||
|
|
4111
|
-
// 场景3:新版本iPadOS(UA直接含ipados,如iPad Air搭载的iPadOS 15+)
|
|
4112
4213
|
userAgent.includes('ipados') ||
|
|
4113
|
-
// 场景4:极端场景(第三方浏览器如Chrome for iPad Air)
|
|
4114
4214
|
(
|
|
4115
|
-
maxTouchPoints >= 5 &&
|
|
4116
|
-
!platform.includes('android') &&
|
|
4117
|
-
(screenRatio > 0.7 && screenRatio < 1.4)
|
|
4215
|
+
maxTouchPoints >= 5 &&
|
|
4216
|
+
!platform.includes('android') &&
|
|
4217
|
+
(screenRatio > 0.7 && screenRatio < 1.4)
|
|
4118
4218
|
)
|
|
4119
4219
|
);
|
|
4120
4220
|
|
|
4121
|
-
|
|
4122
|
-
const result = isIphoneIpod || isIpad;
|
|
4123
|
-
return result;
|
|
4221
|
+
return isIphoneIpod || isIpad;
|
|
4124
4222
|
}
|
|
4125
4223
|
|
|
4126
4224
|
/**
|
|
@@ -4140,9 +4238,17 @@
|
|
|
4140
4238
|
config = mergeConfigs(DEFAULT_CONFIG, options);
|
|
4141
4239
|
validateConfig(config);
|
|
4142
4240
|
|
|
4241
|
+
// 初始化时检测URL中的platType并存入Cookie
|
|
4242
|
+
if (isBrowser()) {
|
|
4243
|
+
const platTypeFromUrl = getQueryParam(config.platTypeKey);
|
|
4244
|
+
if (platTypeFromUrl) {
|
|
4245
|
+
setCookie(config.platTypeKey, platTypeFromUrl);
|
|
4246
|
+
// 可选:移除URL中的platType(避免重复显示)
|
|
4247
|
+
}
|
|
4248
|
+
}
|
|
4249
|
+
|
|
4143
4250
|
const accessCode = getQueryParam(config.accessCodeKey);
|
|
4144
4251
|
|
|
4145
|
-
// 验证必要的API配置
|
|
4146
4252
|
if (!config.tokenApi) {
|
|
4147
4253
|
return { code: -100, msg: '缺少tokenApi配置', success: false };
|
|
4148
4254
|
}
|
|
@@ -4161,14 +4267,25 @@
|
|
|
4161
4267
|
|
|
4162
4268
|
if (result.code === 0 && result.data) {
|
|
4163
4269
|
config.storage.setItem(config.tokenKey, result.data);
|
|
4164
|
-
removeQueryParam(config.accessCodeKey);
|
|
4270
|
+
removeQueryParam([config.accessCodeKey,config.platTypeKey]);
|
|
4165
4271
|
}
|
|
4166
4272
|
return result;
|
|
4167
4273
|
}
|
|
4168
4274
|
// 如果没有token,跳转到登录页
|
|
4169
4275
|
else if (!this.getToken()) {
|
|
4170
4276
|
if (isBrowser() && config.logOutUrl) {
|
|
4171
|
-
|
|
4277
|
+
// ===== 核心修正:platType拼到redirect_uri里 =====
|
|
4278
|
+
const platTypeFromCookie = getCookie(config.platTypeKey);
|
|
4279
|
+
// 1. 获取原始重定向地址
|
|
4280
|
+
let redirectUri = getCurrentUrlWithParams();
|
|
4281
|
+
// 2. 给重定向地址加platType参数
|
|
4282
|
+
if (platTypeFromCookie) {
|
|
4283
|
+
redirectUri = addPlatTypeToRedirectUrl(redirectUri, platTypeFromCookie, config.platTypeKey);
|
|
4284
|
+
}
|
|
4285
|
+
// 3. 构建登录页URL(仅带redirect_uri参数)
|
|
4286
|
+
let loginUrl = new URL(config.logOutUrl);
|
|
4287
|
+
loginUrl.searchParams.set('redirect_uri', encodeURIComponent(redirectUri));
|
|
4288
|
+
window.location.href = loginUrl.toString();
|
|
4172
4289
|
}
|
|
4173
4290
|
}
|
|
4174
4291
|
|
|
@@ -4200,33 +4317,21 @@
|
|
|
4200
4317
|
* @returns {Promise<Object>} 接口返回结果
|
|
4201
4318
|
*/
|
|
4202
4319
|
async logout() {
|
|
4203
|
-
// 1. 前置校验:初始化状态
|
|
4204
4320
|
if (!config) {
|
|
4205
4321
|
return { code: -101, msg: '请先调用init方法初始化', success: false };
|
|
4206
4322
|
}
|
|
4207
4323
|
|
|
4208
|
-
// 2. 工具函数:统一构建登录跳转URL(确保完整编码当前URL的所有参数/hash)
|
|
4209
|
-
const buildLoginUrl = () => {
|
|
4210
|
-
if (!config.logOutUrl || !isBrowser()) return '';
|
|
4211
|
-
// 关键:获取当前完整URL(含query、hash)并完整编码,避免参数丢失
|
|
4212
|
-
const currentFullUrl = window.location.href;
|
|
4213
|
-
const encodedRedirectUri = encodeURIComponent(currentFullUrl);
|
|
4214
|
-
return `${config.logOutUrl}?redirect_uri=${encodedRedirectUri}`;
|
|
4215
|
-
};
|
|
4216
|
-
|
|
4217
|
-
// 3. 校验logoutApi配置(仅接口调用时需要,跳转登录页不受此影响)
|
|
4218
4324
|
if (!config.logoutApi) {
|
|
4219
|
-
// 无logoutApi时仍清除token并跳转登录页,保证基础退出逻辑
|
|
4220
|
-
this.removeToken();
|
|
4221
|
-
const loginUrl = buildLoginUrl();
|
|
4222
|
-
if (loginUrl) window.location.href = loginUrl;
|
|
4223
4325
|
return { code: -102, msg: '未配置logoutApi', success: false };
|
|
4224
4326
|
}
|
|
4225
4327
|
|
|
4328
|
+
// 获取并删除Cookie中的platType
|
|
4329
|
+
const platType = getCookie(config.platTypeKey);
|
|
4330
|
+
removeCookie(config.platTypeKey);
|
|
4331
|
+
|
|
4226
4332
|
const token = this.getToken();
|
|
4227
4333
|
if (token) {
|
|
4228
4334
|
try {
|
|
4229
|
-
// 调用退出接口
|
|
4230
4335
|
const result = await request(
|
|
4231
4336
|
config.logoutApi,
|
|
4232
4337
|
{
|
|
@@ -4235,37 +4340,48 @@
|
|
|
4235
4340
|
timeout: config.timeout
|
|
4236
4341
|
}
|
|
4237
4342
|
);
|
|
4238
|
-
// 无论接口返回结果如何,都清除token
|
|
4239
4343
|
this.removeToken();
|
|
4240
4344
|
|
|
4241
|
-
|
|
4242
|
-
|
|
4243
|
-
|
|
4345
|
+
if (isBrowser() && config.logOutUrl) {
|
|
4346
|
+
// ===== 核心修正:platType拼到redirect_uri里 =====
|
|
4347
|
+
// 1. 获取原始重定向地址
|
|
4348
|
+
let redirectUri = getCurrentUrlWithParams();
|
|
4349
|
+
// 2. 给重定向地址加platType参数
|
|
4350
|
+
if (platType) {
|
|
4351
|
+
redirectUri = addPlatTypeToRedirectUrl(redirectUri, platType, config.platTypeKey);
|
|
4352
|
+
}
|
|
4353
|
+
// 3. 构建登录页URL
|
|
4354
|
+
let logoutUrl = new URL(config.logOutUrl);
|
|
4355
|
+
logoutUrl.searchParams.set('redirect_uri', encodeURIComponent(redirectUri));
|
|
4356
|
+
window.location.href = logoutUrl.toString();
|
|
4357
|
+
}
|
|
4244
4358
|
return result;
|
|
4245
4359
|
} catch (error) {
|
|
4246
|
-
// 接口调用失败,仍清除token并跳转登录页
|
|
4247
|
-
this.removeToken();
|
|
4248
|
-
const loginUrl = buildLoginUrl();
|
|
4249
|
-
if (loginUrl) window.location.href = loginUrl;
|
|
4250
4360
|
return { code: -103, msg: `退出失败: ${error.message}`, success: false };
|
|
4251
4361
|
}
|
|
4252
4362
|
} else {
|
|
4253
|
-
// 无token时直接清除(兜底)并跳转登录页
|
|
4254
4363
|
this.removeToken();
|
|
4255
|
-
|
|
4256
|
-
|
|
4364
|
+
if (isBrowser() && config.logOutUrl) {
|
|
4365
|
+
// ===== 核心修正:platType拼到redirect_uri里 =====
|
|
4366
|
+
let redirectUri = getCurrentUrlWithParams();
|
|
4367
|
+
if (platType) {
|
|
4368
|
+
redirectUri = addPlatTypeToRedirectUrl(redirectUri, platType, config.platTypeKey);
|
|
4369
|
+
}
|
|
4370
|
+
let logoutUrl = new URL(config.logOutUrl);
|
|
4371
|
+
logoutUrl.searchParams.set('redirect_uri', encodeURIComponent(redirectUri));
|
|
4372
|
+
window.location.href = logoutUrl.toString();
|
|
4373
|
+
}
|
|
4257
4374
|
return { code: 0, msg: '已成功清除token', success: true };
|
|
4258
4375
|
}
|
|
4259
4376
|
},
|
|
4260
4377
|
|
|
4261
4378
|
/**
|
|
4262
4379
|
* 用token换取新accessCode并跳转到指定URL
|
|
4263
|
-
* @param {string} redirectUrl -
|
|
4380
|
+
* @param {string} redirectUrl - 目标跳转地址
|
|
4264
4381
|
* @param {string} target - 当前页面:_self、新页面打开:_blank,默认当前页_self
|
|
4265
4382
|
* @returns {Promise<Object>} 接口返回结果
|
|
4266
4383
|
*/
|
|
4267
4384
|
async toUrl(redirectUrl, target = '_self') {
|
|
4268
|
-
// 1. 前置校验:初始化状态、参数合法性
|
|
4269
4385
|
if (!config) {
|
|
4270
4386
|
return { code: -101, msg: '请先调用init方法初始化', success: false };
|
|
4271
4387
|
}
|
|
@@ -4274,45 +4390,49 @@
|
|
|
4274
4390
|
return { code: -104, msg: '请提供跳转地址', success: false };
|
|
4275
4391
|
}
|
|
4276
4392
|
|
|
4277
|
-
//
|
|
4278
|
-
|
|
4279
|
-
|
|
4280
|
-
// ========== 保留:platType=screen强制_self逻辑 ==========
|
|
4281
|
-
const isPlatTypeScreen = currentPlatType === 'screen';
|
|
4282
|
-
|
|
4283
|
-
// 2. 处理target参数:platType=screen > iOS > 传入的target
|
|
4284
|
-
const finalTarget = isPlatTypeScreen
|
|
4285
|
-
? '_self' // platType=screen时强制_self
|
|
4286
|
-
: (isIOS() ? '_self' : target); // 否则沿用原iOS判断逻辑
|
|
4287
|
-
|
|
4288
|
-
if (!['_self', '_blank'].includes(finalTarget)) {
|
|
4289
|
-
return { code: -108, msg: 'target参数必须是"_self"或"_blank"', success: false };
|
|
4393
|
+
// iOS强制_self
|
|
4394
|
+
if (isIOS()) {
|
|
4395
|
+
target = '_self';
|
|
4290
4396
|
}
|
|
4291
4397
|
|
|
4292
|
-
//
|
|
4293
|
-
const
|
|
4294
|
-
|
|
4295
|
-
|
|
4296
|
-
|
|
4297
|
-
|
|
4298
|
-
|
|
4398
|
+
// platType=screen时强制_self
|
|
4399
|
+
const platType = getCookie(config.platTypeKey);
|
|
4400
|
+
if (platType === 'screen') {
|
|
4401
|
+
target = '_self';
|
|
4402
|
+
}
|
|
4403
|
+
|
|
4404
|
+
// 验证target参数
|
|
4405
|
+
if (!['_self', '_blank'].includes(target)) {
|
|
4406
|
+
return { code: -108, msg: 'target参数必须是"_self"或"_blank"', success: false };
|
|
4407
|
+
}
|
|
4299
4408
|
|
|
4300
|
-
// 4. 校验refreshCodeApi配置
|
|
4301
4409
|
if (!config.refreshCodeApi) {
|
|
4302
4410
|
return { code: -105, msg: '未配置refreshCodeApi', success: false };
|
|
4303
4411
|
}
|
|
4304
4412
|
|
|
4305
|
-
// 5. 获取token,无token则直接跳登录页
|
|
4306
4413
|
const token = this.getToken();
|
|
4307
4414
|
if (!token) {
|
|
4308
4415
|
if (isBrowser() && config.logOutUrl) {
|
|
4309
|
-
|
|
4310
|
-
|
|
4416
|
+
// ===== 核心修正:platType拼到redirect_uri里 =====
|
|
4417
|
+
// 1. 给目标跳转地址加platType
|
|
4418
|
+
let newRedirectUrl = redirectUrl;
|
|
4419
|
+
if (platType) {
|
|
4420
|
+
newRedirectUrl = addPlatTypeToRedirectUrl(newRedirectUrl, platType, config.platTypeKey);
|
|
4421
|
+
}
|
|
4422
|
+
// 2. 构建登录页URL
|
|
4423
|
+
let loginUrl = new URL(config.logOutUrl);
|
|
4424
|
+
loginUrl.searchParams.set('redirect_uri', encodeURIComponent(newRedirectUrl));
|
|
4425
|
+
const loginUrlStr = loginUrl.toString();
|
|
4426
|
+
|
|
4427
|
+
if (target === '_blank') {
|
|
4428
|
+
window.open(loginUrlStr, '_blank');
|
|
4429
|
+
} else {
|
|
4430
|
+
window.location.href = loginUrlStr;
|
|
4431
|
+
}
|
|
4311
4432
|
}
|
|
4312
4433
|
return { code: -106, msg: '未找到有效token', success: false };
|
|
4313
4434
|
}
|
|
4314
4435
|
|
|
4315
|
-
// 6. 有token则尝试换取accessCode并跳转
|
|
4316
4436
|
try {
|
|
4317
4437
|
const result = await request(
|
|
4318
4438
|
config.refreshCodeApi,
|
|
@@ -4324,31 +4444,48 @@
|
|
|
4324
4444
|
);
|
|
4325
4445
|
|
|
4326
4446
|
if (result.code === 0 && result.data && isBrowser()) {
|
|
4327
|
-
//
|
|
4447
|
+
// toUrl跳转目标地址时,仅携带accessCode,不携带platType
|
|
4328
4448
|
const url = new URL(redirectUrl);
|
|
4329
|
-
// 1. 如果当前页面有platType,拼接到目标URL(覆盖原有platType)
|
|
4330
|
-
if (currentPlatType) {
|
|
4331
|
-
url.searchParams.set('platType', currentPlatType);
|
|
4332
|
-
}
|
|
4333
|
-
// 2. 拼接accessCode(保留原有逻辑)
|
|
4334
4449
|
url.searchParams.set(config.accessCodeKey, result.data);
|
|
4335
4450
|
const targetUrl = url.toString();
|
|
4336
4451
|
|
|
4337
|
-
|
|
4452
|
+
if (target === '_blank') {
|
|
4453
|
+
window.open(targetUrl, '_blank');
|
|
4454
|
+
} else {
|
|
4455
|
+
window.location.href = targetUrl;
|
|
4456
|
+
}
|
|
4338
4457
|
} else {
|
|
4339
|
-
//
|
|
4340
|
-
|
|
4341
|
-
|
|
4342
|
-
|
|
4458
|
+
// ===== 核心修正:platType拼到redirect_uri里 =====
|
|
4459
|
+
let newRedirectUrl = redirectUrl;
|
|
4460
|
+
if (platType) {
|
|
4461
|
+
newRedirectUrl = addPlatTypeToRedirectUrl(newRedirectUrl, platType, config.platTypeKey);
|
|
4462
|
+
}
|
|
4463
|
+
let loginUrl = new URL(config.logOutUrl);
|
|
4464
|
+
loginUrl.searchParams.set('redirect_uri', encodeURIComponent(newRedirectUrl));
|
|
4465
|
+
const loginUrlStr = loginUrl.toString();
|
|
4466
|
+
|
|
4467
|
+
if (target === '_blank') {
|
|
4468
|
+
window.open(loginUrlStr, '_blank');
|
|
4469
|
+
} else {
|
|
4470
|
+
window.location.href = loginUrlStr;
|
|
4343
4471
|
}
|
|
4344
4472
|
}
|
|
4345
4473
|
|
|
4346
4474
|
return result;
|
|
4347
4475
|
} catch (error) {
|
|
4348
|
-
//
|
|
4349
|
-
|
|
4350
|
-
|
|
4351
|
-
|
|
4476
|
+
// ===== 核心修正:platType拼到redirect_uri里 =====
|
|
4477
|
+
let newRedirectUrl = redirectUrl;
|
|
4478
|
+
if (platType) {
|
|
4479
|
+
newRedirectUrl = addPlatTypeToRedirectUrl(newRedirectUrl, platType, config.platTypeKey);
|
|
4480
|
+
}
|
|
4481
|
+
let loginUrl = new URL(config.logOutUrl);
|
|
4482
|
+
loginUrl.searchParams.set('redirect_uri', encodeURIComponent(newRedirectUrl));
|
|
4483
|
+
const loginUrlStr = loginUrl.toString();
|
|
4484
|
+
|
|
4485
|
+
if (target === '_blank') {
|
|
4486
|
+
window.open(loginUrlStr, '_blank');
|
|
4487
|
+
} else {
|
|
4488
|
+
window.location.href = loginUrlStr;
|
|
4352
4489
|
}
|
|
4353
4490
|
return { code: -107, msg: `跳转失败: ${error.message}`, success: false };
|
|
4354
4491
|
}
|
|
@@ -4387,7 +4524,7 @@
|
|
|
4387
4524
|
* @returns {Object|null} 当前配置
|
|
4388
4525
|
*/
|
|
4389
4526
|
getConfig() {
|
|
4390
|
-
return { ...config };
|
|
4527
|
+
return { ...config };
|
|
4391
4528
|
},
|
|
4392
4529
|
|
|
4393
4530
|
async changePassword(fromData = {}) {
|
|
@@ -4416,7 +4553,6 @@
|
|
|
4416
4553
|
|
|
4417
4554
|
async getPhoneCode() {
|
|
4418
4555
|
const token = this.getToken();
|
|
4419
|
-
// 修复笔误:sendCaptchaCode → sendCaptchaCodeApi
|
|
4420
4556
|
return request(
|
|
4421
4557
|
config.sendCaptchaCodeApi,
|
|
4422
4558
|
{
|