@sunggang/ui-lib 0.4.58 → 0.4.60
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/LiffProvider.cjs.js +114 -129
- package/LiffProvider.esm.js +114 -130
- package/package.json +1 -1
- package/src/lib/Provider/LiffProvider.d.ts +5 -1
package/LiffProvider.cjs.js
CHANGED
|
@@ -453,6 +453,115 @@ function _ts_generator(thisArg, body) {
|
|
|
453
453
|
}
|
|
454
454
|
}
|
|
455
455
|
var LiffContext = /*#__PURE__*/ React.createContext(undefined);
|
|
456
|
+
// 外部可直接 import 的 helper:嘗試由 getLiff helper 或 dynamic import 取得 SDK,並在必要時啟動 init
|
|
457
|
+
function fetchLiffSdk(liffId) {
|
|
458
|
+
return _fetchLiffSdk.apply(this, arguments);
|
|
459
|
+
}
|
|
460
|
+
function _fetchLiffSdk() {
|
|
461
|
+
_fetchLiffSdk = _async_to_generator(function(liffId) {
|
|
462
|
+
var sdk, mod, e, e1, e2;
|
|
463
|
+
return _ts_generator(this, function(_state) {
|
|
464
|
+
switch(_state.label){
|
|
465
|
+
case 0:
|
|
466
|
+
if (typeof window === "undefined") return [
|
|
467
|
+
2,
|
|
468
|
+
null
|
|
469
|
+
];
|
|
470
|
+
_state.label = 1;
|
|
471
|
+
case 1:
|
|
472
|
+
_state.trys.push([
|
|
473
|
+
1,
|
|
474
|
+
10,
|
|
475
|
+
,
|
|
476
|
+
11
|
|
477
|
+
]);
|
|
478
|
+
sdk = null;
|
|
479
|
+
_state.label = 2;
|
|
480
|
+
case 2:
|
|
481
|
+
_state.trys.push([
|
|
482
|
+
2,
|
|
483
|
+
4,
|
|
484
|
+
,
|
|
485
|
+
5
|
|
486
|
+
]);
|
|
487
|
+
return [
|
|
488
|
+
4,
|
|
489
|
+
Promise.resolve().then(function () { return require('./index.cjs3.js'); })
|
|
490
|
+
];
|
|
491
|
+
case 3:
|
|
492
|
+
mod = _state.sent();
|
|
493
|
+
sdk = mod && (mod.default || mod.liff) ? mod.default || mod.liff : mod;
|
|
494
|
+
return [
|
|
495
|
+
3,
|
|
496
|
+
5
|
|
497
|
+
];
|
|
498
|
+
case 4:
|
|
499
|
+
e = _state.sent();
|
|
500
|
+
console.warn("dynamic import @line/liff failed", e);
|
|
501
|
+
sdk = null;
|
|
502
|
+
return [
|
|
503
|
+
3,
|
|
504
|
+
5
|
|
505
|
+
];
|
|
506
|
+
case 5:
|
|
507
|
+
if (!sdk) return [
|
|
508
|
+
2,
|
|
509
|
+
null
|
|
510
|
+
];
|
|
511
|
+
if (!(typeof sdk.init === "function" && !sdk._initialized && liffId)) return [
|
|
512
|
+
3,
|
|
513
|
+
9
|
|
514
|
+
];
|
|
515
|
+
_state.label = 6;
|
|
516
|
+
case 6:
|
|
517
|
+
_state.trys.push([
|
|
518
|
+
6,
|
|
519
|
+
8,
|
|
520
|
+
,
|
|
521
|
+
9
|
|
522
|
+
]);
|
|
523
|
+
return [
|
|
524
|
+
4,
|
|
525
|
+
sdk.init({
|
|
526
|
+
liffId: liffId,
|
|
527
|
+
withLoginOnExternalBrowser: false
|
|
528
|
+
})
|
|
529
|
+
];
|
|
530
|
+
case 7:
|
|
531
|
+
_state.sent();
|
|
532
|
+
sdk._initialized = true;
|
|
533
|
+
return [
|
|
534
|
+
3,
|
|
535
|
+
9
|
|
536
|
+
];
|
|
537
|
+
case 8:
|
|
538
|
+
e1 = _state.sent();
|
|
539
|
+
console.warn("liff.init failed or already initialized", e1);
|
|
540
|
+
return [
|
|
541
|
+
3,
|
|
542
|
+
9
|
|
543
|
+
];
|
|
544
|
+
case 9:
|
|
545
|
+
return [
|
|
546
|
+
2,
|
|
547
|
+
sdk
|
|
548
|
+
];
|
|
549
|
+
case 10:
|
|
550
|
+
e2 = _state.sent();
|
|
551
|
+
console.warn("fetchLiffSdk error", e2);
|
|
552
|
+
return [
|
|
553
|
+
2,
|
|
554
|
+
null
|
|
555
|
+
];
|
|
556
|
+
case 11:
|
|
557
|
+
return [
|
|
558
|
+
2
|
|
559
|
+
];
|
|
560
|
+
}
|
|
561
|
+
});
|
|
562
|
+
});
|
|
563
|
+
return _fetchLiffSdk.apply(this, arguments);
|
|
564
|
+
}
|
|
456
565
|
var LiffProvider = function(param) {
|
|
457
566
|
var children = param.children, liffId = param.liffId, _param_localStorageKey = param.localStorageKey, localStorageKey = _param_localStorageKey === void 0 ? "one-gobo-global-login" : _param_localStorageKey, checkTokenUrl = param.checkTokenUrl, loginUrl = param.loginUrl, accountLoginUrl = param.accountLoginUrl, memberInfoUrl = param.memberInfoUrl, _param_openInApp = param.openInApp, openInApp = _param_openInApp === void 0 ? false : _param_openInApp, _param_ignoreCheckAddFriend = param.ignoreCheckAddFriend, ignoreCheckAddFriend = _param_ignoreCheckAddFriend === void 0 ? false : _param_ignoreCheckAddFriend, _param_ignoreRoute = param.ignoreRoute, ignoreRoute = _param_ignoreRoute === void 0 ? false : _param_ignoreRoute, _param_loginByUser = param.loginByUser, loginByUser = _param_loginByUser === void 0 ? false : _param_loginByUser, _param_liffLogin = param.liffLogin, liffLogin = _param_liffLogin === void 0 ? false : _param_liffLogin, customerRedirectUrl = param.customerRedirectUrl, inviteCode = param.inviteCode, handleLoginError = param.handleLoginError;
|
|
458
567
|
var _useState = _sliced_to_array(React.useState(null), 2), liffObject = _useState[0], setLiffObject = _useState[1];
|
|
@@ -465,76 +574,8 @@ var LiffProvider = function(param) {
|
|
|
465
574
|
var _useState7 = _sliced_to_array(React.useState(null), 2), loginType = _useState7[0], setLoginType = _useState7[1];
|
|
466
575
|
// userInfo 的 localStorage key
|
|
467
576
|
var userInfoStorageKey = "".concat(localStorageKey, "-userInfo");
|
|
468
|
-
// liff profile 的 localStorage key
|
|
469
|
-
var liffUserInfoStorageKey = "".concat(localStorageKey, "-liffUserInfo");
|
|
470
|
-
// 從 localStorage 載入 userInfo
|
|
471
|
-
var loadUserInfoFromStorage = function() {
|
|
472
|
-
try {
|
|
473
|
-
var storedUserInfo = window.localStorage.getItem(userInfoStorageKey);
|
|
474
|
-
if (storedUserInfo) {
|
|
475
|
-
var parsedUserInfo = JSON.parse(storedUserInfo);
|
|
476
|
-
setUserInfo(parsedUserInfo);
|
|
477
|
-
return parsedUserInfo;
|
|
478
|
-
}
|
|
479
|
-
} catch (err) {
|
|
480
|
-
console.error("載入 userInfo 失敗:", err);
|
|
481
|
-
}
|
|
482
|
-
return null;
|
|
483
|
-
};
|
|
484
|
-
// 儲存 userInfo 到 localStorage
|
|
485
|
-
var saveUserInfoToStorage = function(info) {
|
|
486
|
-
try {
|
|
487
|
-
window.localStorage.setItem(userInfoStorageKey, JSON.stringify(info));
|
|
488
|
-
} catch (err) {
|
|
489
|
-
console.error("儲存 userInfo 失敗:", err);
|
|
490
|
-
}
|
|
491
|
-
};
|
|
492
|
-
// 從 localStorage 載入 LIFF profile
|
|
493
|
-
var loadLiffUserInfoFromStorage = function() {
|
|
494
|
-
try {
|
|
495
|
-
var stored = window.localStorage.getItem(liffUserInfoStorageKey);
|
|
496
|
-
if (stored) {
|
|
497
|
-
var parsed = JSON.parse(stored);
|
|
498
|
-
setLiffProfile(parsed);
|
|
499
|
-
return parsed;
|
|
500
|
-
}
|
|
501
|
-
} catch (err) {
|
|
502
|
-
console.error("載入 liffUserInfo 失敗:", err);
|
|
503
|
-
}
|
|
504
|
-
return null;
|
|
505
|
-
};
|
|
506
|
-
// 儲存 LIFF profile 到 localStorage
|
|
507
|
-
var saveLiffUserInfoToStorage = function(info) {
|
|
508
|
-
try {
|
|
509
|
-
window.localStorage.setItem(liffUserInfoStorageKey, JSON.stringify(info));
|
|
510
|
-
} catch (err) {
|
|
511
|
-
console.error("儲存 liffUserInfo 失敗:", err);
|
|
512
|
-
}
|
|
513
|
-
};
|
|
514
|
-
// 移除 LIFF profile 的 localStorage
|
|
515
|
-
var removeLiffUserInfoFromStorage = function() {
|
|
516
|
-
try {
|
|
517
|
-
window.localStorage.removeItem(liffUserInfoStorageKey);
|
|
518
|
-
} catch (err) {
|
|
519
|
-
// ignore
|
|
520
|
-
}
|
|
521
|
-
};
|
|
522
|
-
// 移除 LIFF initialized flag
|
|
523
|
-
var removeLiffInitializedFromStorage = function() {
|
|
524
|
-
try {
|
|
525
|
-
window.localStorage.removeItem("".concat(localStorageKey, "-liffInitialized"));
|
|
526
|
-
} catch (err) {
|
|
527
|
-
// ignore
|
|
528
|
-
}
|
|
529
|
-
};
|
|
530
577
|
// 清除 LIFF 相關快取與狀態(暴露給外部 as needed)
|
|
531
578
|
var clearLiffData = function() {
|
|
532
|
-
try {
|
|
533
|
-
removeLiffUserInfoFromStorage();
|
|
534
|
-
removeLiffInitializedFromStorage();
|
|
535
|
-
} catch (e) {
|
|
536
|
-
// ignore
|
|
537
|
-
}
|
|
538
579
|
setLiffProfile(null);
|
|
539
580
|
setLiffObject(null);
|
|
540
581
|
};
|
|
@@ -564,7 +605,6 @@ var LiffProvider = function(param) {
|
|
|
564
605
|
case 2:
|
|
565
606
|
response = _state.sent();
|
|
566
607
|
setUserInfo(response);
|
|
567
|
-
saveUserInfoToStorage(response);
|
|
568
608
|
return [
|
|
569
609
|
3,
|
|
570
610
|
4
|
|
@@ -733,7 +773,6 @@ var LiffProvider = function(param) {
|
|
|
733
773
|
case 5:
|
|
734
774
|
// 登入沒有回傳 token,視為登入失敗 — 清除 LIFF 快取
|
|
735
775
|
setUserInfo(response);
|
|
736
|
-
saveUserInfoToStorage(response);
|
|
737
776
|
try {
|
|
738
777
|
clearLiffData();
|
|
739
778
|
} catch (e) {
|
|
@@ -794,9 +833,6 @@ var LiffProvider = function(param) {
|
|
|
794
833
|
originalFetch = window.fetch;
|
|
795
834
|
window.fetch = customFetch;
|
|
796
835
|
if (openInApp && !(liff === null || liff === void 0 ? void 0 : liff.isInClient()) && (liff === null || liff === void 0 ? void 0 : liff.getOS()) !== "web") window.location.href = "line://app/".concat(liffId);
|
|
797
|
-
if (isValid) return [
|
|
798
|
-
2
|
|
799
|
-
];
|
|
800
836
|
return [
|
|
801
837
|
4,
|
|
802
838
|
liff.init({
|
|
@@ -843,7 +879,6 @@ var LiffProvider = function(param) {
|
|
|
843
879
|
case 4:
|
|
844
880
|
profile = _state.sent();
|
|
845
881
|
setLiffProfile(profile);
|
|
846
|
-
saveLiffUserInfoToStorage(profile);
|
|
847
882
|
return [
|
|
848
883
|
3,
|
|
849
884
|
6
|
|
@@ -1097,11 +1132,6 @@ var LiffProvider = function(param) {
|
|
|
1097
1132
|
return trimmed.toLowerCase().startsWith("bearer ") ? trimmed : "Bearer ".concat(trimmed);
|
|
1098
1133
|
};
|
|
1099
1134
|
var searchParams = typeof window !== "undefined" ? new URLSearchParams(window.location.search) : null;
|
|
1100
|
-
// 初始載入時從 localStorage 載入
|
|
1101
|
-
React.useEffect(function() {
|
|
1102
|
-
loadUserInfoFromStorage();
|
|
1103
|
-
loadLiffUserInfoFromStorage();
|
|
1104
|
-
}, []);
|
|
1105
1135
|
// 客戶端初始化 LIFF
|
|
1106
1136
|
React.useEffect(function() {
|
|
1107
1137
|
var checkAndInitialize = function() {
|
|
@@ -1181,56 +1211,10 @@ var LiffProvider = function(param) {
|
|
|
1181
1211
|
var contextValue = React.useMemo(function() {
|
|
1182
1212
|
return {
|
|
1183
1213
|
liffObject: liffObject,
|
|
1184
|
-
//
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
var mod, sdk, e;
|
|
1189
|
-
return _ts_generator(this, function(_state) {
|
|
1190
|
-
switch(_state.label){
|
|
1191
|
-
case 0:
|
|
1192
|
-
if (liffObject) return [
|
|
1193
|
-
2,
|
|
1194
|
-
liffObject
|
|
1195
|
-
];
|
|
1196
|
-
if (typeof window === "undefined") return [
|
|
1197
|
-
2,
|
|
1198
|
-
null
|
|
1199
|
-
];
|
|
1200
|
-
_state.label = 1;
|
|
1201
|
-
case 1:
|
|
1202
|
-
_state.trys.push([
|
|
1203
|
-
1,
|
|
1204
|
-
3,
|
|
1205
|
-
,
|
|
1206
|
-
4
|
|
1207
|
-
]);
|
|
1208
|
-
return [
|
|
1209
|
-
4,
|
|
1210
|
-
Promise.resolve().then(function () { return require('./index.cjs3.js'); })
|
|
1211
|
-
];
|
|
1212
|
-
case 2:
|
|
1213
|
-
mod = _state.sent();
|
|
1214
|
-
sdk = mod === null || mod === void 0 ? void 0 : mod.liff;
|
|
1215
|
-
if (sdk) setLiffObject(sdk);
|
|
1216
|
-
return [
|
|
1217
|
-
2,
|
|
1218
|
-
sdk || null
|
|
1219
|
-
];
|
|
1220
|
-
case 3:
|
|
1221
|
-
e = _state.sent();
|
|
1222
|
-
console.error("lazy import @line/liff failed", e);
|
|
1223
|
-
return [
|
|
1224
|
-
2,
|
|
1225
|
-
null
|
|
1226
|
-
];
|
|
1227
|
-
case 4:
|
|
1228
|
-
return [
|
|
1229
|
-
2
|
|
1230
|
-
];
|
|
1231
|
-
}
|
|
1232
|
-
});
|
|
1233
|
-
}),
|
|
1214
|
+
// helper:透過 fetchLiffSdk 執行更完整的 fetch + optional init
|
|
1215
|
+
fetchLiffSdk: function(liffId) {
|
|
1216
|
+
return fetchLiffSdk(liffId);
|
|
1217
|
+
},
|
|
1234
1218
|
loginData: loginData,
|
|
1235
1219
|
userInfo: userInfo,
|
|
1236
1220
|
accessToken: accessToken,
|
|
@@ -1270,4 +1254,5 @@ var useLiffContext = function() {
|
|
|
1270
1254
|
|
|
1271
1255
|
exports.LiffContext = LiffContext;
|
|
1272
1256
|
exports.LiffProvider = LiffProvider;
|
|
1257
|
+
exports.fetchLiffSdk = fetchLiffSdk;
|
|
1273
1258
|
exports.useLiffContext = useLiffContext;
|
package/LiffProvider.esm.js
CHANGED
|
@@ -449,6 +449,115 @@ function _ts_generator(thisArg, body) {
|
|
|
449
449
|
}
|
|
450
450
|
}
|
|
451
451
|
var LiffContext = /*#__PURE__*/ createContext(undefined);
|
|
452
|
+
// 外部可直接 import 的 helper:嘗試由 getLiff helper 或 dynamic import 取得 SDK,並在必要時啟動 init
|
|
453
|
+
function fetchLiffSdk(liffId) {
|
|
454
|
+
return _fetchLiffSdk.apply(this, arguments);
|
|
455
|
+
}
|
|
456
|
+
function _fetchLiffSdk() {
|
|
457
|
+
_fetchLiffSdk = _async_to_generator(function(liffId) {
|
|
458
|
+
var sdk, mod, e, e1, e2;
|
|
459
|
+
return _ts_generator(this, function(_state) {
|
|
460
|
+
switch(_state.label){
|
|
461
|
+
case 0:
|
|
462
|
+
if (typeof window === "undefined") return [
|
|
463
|
+
2,
|
|
464
|
+
null
|
|
465
|
+
];
|
|
466
|
+
_state.label = 1;
|
|
467
|
+
case 1:
|
|
468
|
+
_state.trys.push([
|
|
469
|
+
1,
|
|
470
|
+
10,
|
|
471
|
+
,
|
|
472
|
+
11
|
|
473
|
+
]);
|
|
474
|
+
sdk = null;
|
|
475
|
+
_state.label = 2;
|
|
476
|
+
case 2:
|
|
477
|
+
_state.trys.push([
|
|
478
|
+
2,
|
|
479
|
+
4,
|
|
480
|
+
,
|
|
481
|
+
5
|
|
482
|
+
]);
|
|
483
|
+
return [
|
|
484
|
+
4,
|
|
485
|
+
import('./index.esm3.js')
|
|
486
|
+
];
|
|
487
|
+
case 3:
|
|
488
|
+
mod = _state.sent();
|
|
489
|
+
sdk = mod && (mod.default || mod.liff) ? mod.default || mod.liff : mod;
|
|
490
|
+
return [
|
|
491
|
+
3,
|
|
492
|
+
5
|
|
493
|
+
];
|
|
494
|
+
case 4:
|
|
495
|
+
e = _state.sent();
|
|
496
|
+
console.warn("dynamic import @line/liff failed", e);
|
|
497
|
+
sdk = null;
|
|
498
|
+
return [
|
|
499
|
+
3,
|
|
500
|
+
5
|
|
501
|
+
];
|
|
502
|
+
case 5:
|
|
503
|
+
if (!sdk) return [
|
|
504
|
+
2,
|
|
505
|
+
null
|
|
506
|
+
];
|
|
507
|
+
if (!(typeof sdk.init === "function" && !sdk._initialized && liffId)) return [
|
|
508
|
+
3,
|
|
509
|
+
9
|
|
510
|
+
];
|
|
511
|
+
_state.label = 6;
|
|
512
|
+
case 6:
|
|
513
|
+
_state.trys.push([
|
|
514
|
+
6,
|
|
515
|
+
8,
|
|
516
|
+
,
|
|
517
|
+
9
|
|
518
|
+
]);
|
|
519
|
+
return [
|
|
520
|
+
4,
|
|
521
|
+
sdk.init({
|
|
522
|
+
liffId: liffId,
|
|
523
|
+
withLoginOnExternalBrowser: false
|
|
524
|
+
})
|
|
525
|
+
];
|
|
526
|
+
case 7:
|
|
527
|
+
_state.sent();
|
|
528
|
+
sdk._initialized = true;
|
|
529
|
+
return [
|
|
530
|
+
3,
|
|
531
|
+
9
|
|
532
|
+
];
|
|
533
|
+
case 8:
|
|
534
|
+
e1 = _state.sent();
|
|
535
|
+
console.warn("liff.init failed or already initialized", e1);
|
|
536
|
+
return [
|
|
537
|
+
3,
|
|
538
|
+
9
|
|
539
|
+
];
|
|
540
|
+
case 9:
|
|
541
|
+
return [
|
|
542
|
+
2,
|
|
543
|
+
sdk
|
|
544
|
+
];
|
|
545
|
+
case 10:
|
|
546
|
+
e2 = _state.sent();
|
|
547
|
+
console.warn("fetchLiffSdk error", e2);
|
|
548
|
+
return [
|
|
549
|
+
2,
|
|
550
|
+
null
|
|
551
|
+
];
|
|
552
|
+
case 11:
|
|
553
|
+
return [
|
|
554
|
+
2
|
|
555
|
+
];
|
|
556
|
+
}
|
|
557
|
+
});
|
|
558
|
+
});
|
|
559
|
+
return _fetchLiffSdk.apply(this, arguments);
|
|
560
|
+
}
|
|
452
561
|
var LiffProvider = function(param) {
|
|
453
562
|
var children = param.children, liffId = param.liffId, _param_localStorageKey = param.localStorageKey, localStorageKey = _param_localStorageKey === void 0 ? "one-gobo-global-login" : _param_localStorageKey, checkTokenUrl = param.checkTokenUrl, loginUrl = param.loginUrl, accountLoginUrl = param.accountLoginUrl, memberInfoUrl = param.memberInfoUrl, _param_openInApp = param.openInApp, openInApp = _param_openInApp === void 0 ? false : _param_openInApp, _param_ignoreCheckAddFriend = param.ignoreCheckAddFriend, ignoreCheckAddFriend = _param_ignoreCheckAddFriend === void 0 ? false : _param_ignoreCheckAddFriend, _param_ignoreRoute = param.ignoreRoute, ignoreRoute = _param_ignoreRoute === void 0 ? false : _param_ignoreRoute, _param_loginByUser = param.loginByUser, loginByUser = _param_loginByUser === void 0 ? false : _param_loginByUser, _param_liffLogin = param.liffLogin, liffLogin = _param_liffLogin === void 0 ? false : _param_liffLogin, customerRedirectUrl = param.customerRedirectUrl, inviteCode = param.inviteCode, handleLoginError = param.handleLoginError;
|
|
454
563
|
var _useState = _sliced_to_array(useState(null), 2), liffObject = _useState[0], setLiffObject = _useState[1];
|
|
@@ -461,76 +570,8 @@ var LiffProvider = function(param) {
|
|
|
461
570
|
var _useState7 = _sliced_to_array(useState(null), 2), loginType = _useState7[0], setLoginType = _useState7[1];
|
|
462
571
|
// userInfo 的 localStorage key
|
|
463
572
|
var userInfoStorageKey = "".concat(localStorageKey, "-userInfo");
|
|
464
|
-
// liff profile 的 localStorage key
|
|
465
|
-
var liffUserInfoStorageKey = "".concat(localStorageKey, "-liffUserInfo");
|
|
466
|
-
// 從 localStorage 載入 userInfo
|
|
467
|
-
var loadUserInfoFromStorage = function() {
|
|
468
|
-
try {
|
|
469
|
-
var storedUserInfo = window.localStorage.getItem(userInfoStorageKey);
|
|
470
|
-
if (storedUserInfo) {
|
|
471
|
-
var parsedUserInfo = JSON.parse(storedUserInfo);
|
|
472
|
-
setUserInfo(parsedUserInfo);
|
|
473
|
-
return parsedUserInfo;
|
|
474
|
-
}
|
|
475
|
-
} catch (err) {
|
|
476
|
-
console.error("載入 userInfo 失敗:", err);
|
|
477
|
-
}
|
|
478
|
-
return null;
|
|
479
|
-
};
|
|
480
|
-
// 儲存 userInfo 到 localStorage
|
|
481
|
-
var saveUserInfoToStorage = function(info) {
|
|
482
|
-
try {
|
|
483
|
-
window.localStorage.setItem(userInfoStorageKey, JSON.stringify(info));
|
|
484
|
-
} catch (err) {
|
|
485
|
-
console.error("儲存 userInfo 失敗:", err);
|
|
486
|
-
}
|
|
487
|
-
};
|
|
488
|
-
// 從 localStorage 載入 LIFF profile
|
|
489
|
-
var loadLiffUserInfoFromStorage = function() {
|
|
490
|
-
try {
|
|
491
|
-
var stored = window.localStorage.getItem(liffUserInfoStorageKey);
|
|
492
|
-
if (stored) {
|
|
493
|
-
var parsed = JSON.parse(stored);
|
|
494
|
-
setLiffProfile(parsed);
|
|
495
|
-
return parsed;
|
|
496
|
-
}
|
|
497
|
-
} catch (err) {
|
|
498
|
-
console.error("載入 liffUserInfo 失敗:", err);
|
|
499
|
-
}
|
|
500
|
-
return null;
|
|
501
|
-
};
|
|
502
|
-
// 儲存 LIFF profile 到 localStorage
|
|
503
|
-
var saveLiffUserInfoToStorage = function(info) {
|
|
504
|
-
try {
|
|
505
|
-
window.localStorage.setItem(liffUserInfoStorageKey, JSON.stringify(info));
|
|
506
|
-
} catch (err) {
|
|
507
|
-
console.error("儲存 liffUserInfo 失敗:", err);
|
|
508
|
-
}
|
|
509
|
-
};
|
|
510
|
-
// 移除 LIFF profile 的 localStorage
|
|
511
|
-
var removeLiffUserInfoFromStorage = function() {
|
|
512
|
-
try {
|
|
513
|
-
window.localStorage.removeItem(liffUserInfoStorageKey);
|
|
514
|
-
} catch (err) {
|
|
515
|
-
// ignore
|
|
516
|
-
}
|
|
517
|
-
};
|
|
518
|
-
// 移除 LIFF initialized flag
|
|
519
|
-
var removeLiffInitializedFromStorage = function() {
|
|
520
|
-
try {
|
|
521
|
-
window.localStorage.removeItem("".concat(localStorageKey, "-liffInitialized"));
|
|
522
|
-
} catch (err) {
|
|
523
|
-
// ignore
|
|
524
|
-
}
|
|
525
|
-
};
|
|
526
573
|
// 清除 LIFF 相關快取與狀態(暴露給外部 as needed)
|
|
527
574
|
var clearLiffData = function() {
|
|
528
|
-
try {
|
|
529
|
-
removeLiffUserInfoFromStorage();
|
|
530
|
-
removeLiffInitializedFromStorage();
|
|
531
|
-
} catch (e) {
|
|
532
|
-
// ignore
|
|
533
|
-
}
|
|
534
575
|
setLiffProfile(null);
|
|
535
576
|
setLiffObject(null);
|
|
536
577
|
};
|
|
@@ -560,7 +601,6 @@ var LiffProvider = function(param) {
|
|
|
560
601
|
case 2:
|
|
561
602
|
response = _state.sent();
|
|
562
603
|
setUserInfo(response);
|
|
563
|
-
saveUserInfoToStorage(response);
|
|
564
604
|
return [
|
|
565
605
|
3,
|
|
566
606
|
4
|
|
@@ -729,7 +769,6 @@ var LiffProvider = function(param) {
|
|
|
729
769
|
case 5:
|
|
730
770
|
// 登入沒有回傳 token,視為登入失敗 — 清除 LIFF 快取
|
|
731
771
|
setUserInfo(response);
|
|
732
|
-
saveUserInfoToStorage(response);
|
|
733
772
|
try {
|
|
734
773
|
clearLiffData();
|
|
735
774
|
} catch (e) {
|
|
@@ -790,9 +829,6 @@ var LiffProvider = function(param) {
|
|
|
790
829
|
originalFetch = window.fetch;
|
|
791
830
|
window.fetch = customFetch;
|
|
792
831
|
if (openInApp && !(liff === null || liff === void 0 ? void 0 : liff.isInClient()) && (liff === null || liff === void 0 ? void 0 : liff.getOS()) !== "web") window.location.href = "line://app/".concat(liffId);
|
|
793
|
-
if (isValid) return [
|
|
794
|
-
2
|
|
795
|
-
];
|
|
796
832
|
return [
|
|
797
833
|
4,
|
|
798
834
|
liff.init({
|
|
@@ -839,7 +875,6 @@ var LiffProvider = function(param) {
|
|
|
839
875
|
case 4:
|
|
840
876
|
profile = _state.sent();
|
|
841
877
|
setLiffProfile(profile);
|
|
842
|
-
saveLiffUserInfoToStorage(profile);
|
|
843
878
|
return [
|
|
844
879
|
3,
|
|
845
880
|
6
|
|
@@ -1093,11 +1128,6 @@ var LiffProvider = function(param) {
|
|
|
1093
1128
|
return trimmed.toLowerCase().startsWith("bearer ") ? trimmed : "Bearer ".concat(trimmed);
|
|
1094
1129
|
};
|
|
1095
1130
|
var searchParams = typeof window !== "undefined" ? new URLSearchParams(window.location.search) : null;
|
|
1096
|
-
// 初始載入時從 localStorage 載入
|
|
1097
|
-
useEffect(function() {
|
|
1098
|
-
loadUserInfoFromStorage();
|
|
1099
|
-
loadLiffUserInfoFromStorage();
|
|
1100
|
-
}, []);
|
|
1101
1131
|
// 客戶端初始化 LIFF
|
|
1102
1132
|
useEffect(function() {
|
|
1103
1133
|
var checkAndInitialize = function() {
|
|
@@ -1177,56 +1207,10 @@ var LiffProvider = function(param) {
|
|
|
1177
1207
|
var contextValue = useMemo(function() {
|
|
1178
1208
|
return {
|
|
1179
1209
|
liffObject: liffObject,
|
|
1180
|
-
//
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
var mod, sdk, e;
|
|
1185
|
-
return _ts_generator(this, function(_state) {
|
|
1186
|
-
switch(_state.label){
|
|
1187
|
-
case 0:
|
|
1188
|
-
if (liffObject) return [
|
|
1189
|
-
2,
|
|
1190
|
-
liffObject
|
|
1191
|
-
];
|
|
1192
|
-
if (typeof window === "undefined") return [
|
|
1193
|
-
2,
|
|
1194
|
-
null
|
|
1195
|
-
];
|
|
1196
|
-
_state.label = 1;
|
|
1197
|
-
case 1:
|
|
1198
|
-
_state.trys.push([
|
|
1199
|
-
1,
|
|
1200
|
-
3,
|
|
1201
|
-
,
|
|
1202
|
-
4
|
|
1203
|
-
]);
|
|
1204
|
-
return [
|
|
1205
|
-
4,
|
|
1206
|
-
import('./index.esm3.js')
|
|
1207
|
-
];
|
|
1208
|
-
case 2:
|
|
1209
|
-
mod = _state.sent();
|
|
1210
|
-
sdk = mod === null || mod === void 0 ? void 0 : mod.liff;
|
|
1211
|
-
if (sdk) setLiffObject(sdk);
|
|
1212
|
-
return [
|
|
1213
|
-
2,
|
|
1214
|
-
sdk || null
|
|
1215
|
-
];
|
|
1216
|
-
case 3:
|
|
1217
|
-
e = _state.sent();
|
|
1218
|
-
console.error("lazy import @line/liff failed", e);
|
|
1219
|
-
return [
|
|
1220
|
-
2,
|
|
1221
|
-
null
|
|
1222
|
-
];
|
|
1223
|
-
case 4:
|
|
1224
|
-
return [
|
|
1225
|
-
2
|
|
1226
|
-
];
|
|
1227
|
-
}
|
|
1228
|
-
});
|
|
1229
|
-
}),
|
|
1210
|
+
// helper:透過 fetchLiffSdk 執行更完整的 fetch + optional init
|
|
1211
|
+
fetchLiffSdk: function(liffId) {
|
|
1212
|
+
return fetchLiffSdk(liffId);
|
|
1213
|
+
},
|
|
1230
1214
|
loginData: loginData,
|
|
1231
1215
|
userInfo: userInfo,
|
|
1232
1216
|
accessToken: accessToken,
|
|
@@ -1264,4 +1248,4 @@ var useLiffContext = function() {
|
|
|
1264
1248
|
return context;
|
|
1265
1249
|
};
|
|
1266
1250
|
|
|
1267
|
-
export { LiffContext, LiffProvider, useLiffContext };
|
|
1251
|
+
export { LiffContext, LiffProvider, fetchLiffSdk, useLiffContext };
|
package/package.json
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
2
|
type LiffContextType = {
|
|
3
|
-
|
|
3
|
+
fetchLiffSdk?: (liffId?: string) => Promise<{
|
|
4
|
+
sdk: any;
|
|
5
|
+
isLoggedIn: boolean | null;
|
|
6
|
+
} | null>;
|
|
4
7
|
liffObject: any;
|
|
5
8
|
loginData: any;
|
|
6
9
|
userInfo?: any;
|
|
@@ -14,6 +17,7 @@ type LiffContextType = {
|
|
|
14
17
|
accountLogin: (username: string, password: string) => Promise<void>;
|
|
15
18
|
};
|
|
16
19
|
export declare const LiffContext: React.Context<LiffContextType | undefined>;
|
|
20
|
+
export declare function fetchLiffSdk(liffId?: string): Promise<any>;
|
|
17
21
|
export declare const LiffProvider: ({ children, liffId, localStorageKey, checkTokenUrl, loginUrl, accountLoginUrl, memberInfoUrl, openInApp, ignoreCheckAddFriend, ignoreRoute, loginByUser, liffLogin, customerRedirectUrl, inviteCode, handleLoginError, }: {
|
|
18
22
|
children: React.ReactNode;
|
|
19
23
|
liffId: string;
|