@wapri/gbase 1.0.1-beta.0 → 2.0.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/LICENSE +21 -0
- package/README.md +1 -1
- package/dist/index.d.ts +0 -2
- package/dist/index.js +0 -2
- package/dist/storage.js +5 -5
- package/package.json +11 -11
- package/dist/auth.d.ts +0 -40
- package/dist/auth.js +0 -128
- package/dist/platform.d.ts +0 -10
- package/dist/platform.js +0 -12
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 wapri contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -63,7 +63,7 @@ Mozilla/5.0 (Linux; Android 10; HarmonyOS; LIO-AL10; HMSCore 6.3.0.311) AppleWeb
|
|
|
63
63
|
```ts
|
|
64
64
|
import { isEmail, isPhone } from '@wapri/gbase'
|
|
65
65
|
|
|
66
|
-
isEmail('user@
|
|
66
|
+
isEmail('user@wapri.com') // true
|
|
67
67
|
isPhone('13800138000') // true
|
|
68
68
|
```
|
|
69
69
|
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
package/dist/storage.js
CHANGED
|
@@ -5,16 +5,16 @@ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _d
|
|
|
5
5
|
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
6
6
|
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
|
|
7
7
|
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
8
|
-
/**
|
|
9
|
-
* 在 App 壳未提供浏览器存储时,回退到内存存储,避免读写时抛出异常。
|
|
8
|
+
/**
|
|
9
|
+
* 在 App 壳未提供浏览器存储时,回退到内存存储,避免读写时抛出异常。
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
12
|
var noSupportLS = function noSupportLS(type) {
|
|
13
13
|
try {
|
|
14
14
|
var _storage = globalThis[type];
|
|
15
|
-
_storage.setItem('
|
|
16
|
-
_storage.getItem('
|
|
17
|
-
_storage.removeItem('
|
|
15
|
+
_storage.setItem('_wapri-test-key', 'miao');
|
|
16
|
+
_storage.getItem('_wapri-test-key');
|
|
17
|
+
_storage.removeItem('_wapri-test-key');
|
|
18
18
|
return false;
|
|
19
19
|
} catch (_unused) {
|
|
20
20
|
return true;
|
package/package.json
CHANGED
|
@@ -1,16 +1,20 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wapri/gbase",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "前端基础公共库",
|
|
5
5
|
"keywords": [],
|
|
6
|
-
"license": "
|
|
7
|
-
"author": "
|
|
8
|
-
"sideEffects": false,
|
|
9
|
-
"module": "dist/index.js",
|
|
10
|
-
"types": "dist/index.d.ts",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"author": "wapri contributors",
|
|
11
8
|
"files": [
|
|
12
9
|
"dist"
|
|
13
10
|
],
|
|
11
|
+
"sideEffects": false,
|
|
12
|
+
"module": "dist/index.js",
|
|
13
|
+
"types": "dist/index.d.ts",
|
|
14
|
+
"publishConfig": {
|
|
15
|
+
"access": "public",
|
|
16
|
+
"registry": "https://registry.npmjs.org/"
|
|
17
|
+
},
|
|
14
18
|
"dependencies": {
|
|
15
19
|
"copy-to-clipboard": "^4.0.2",
|
|
16
20
|
"currency.js": "^2.0.4",
|
|
@@ -24,13 +28,9 @@
|
|
|
24
28
|
"@types/js-cookie": "^3.0.6",
|
|
25
29
|
"@types/lodash-es": "^4.17.12"
|
|
26
30
|
},
|
|
27
|
-
"publishConfig": {
|
|
28
|
-
"access": "public",
|
|
29
|
-
"registry": "https://registry.npmjs.org/"
|
|
30
|
-
},
|
|
31
31
|
"peerDependenciesMeta": {},
|
|
32
32
|
"scripts": {
|
|
33
|
-
"build": "father build",
|
|
33
|
+
"build": "node ../../scripts/clean-dist.mjs && father build",
|
|
34
34
|
"dev": "father dev"
|
|
35
35
|
}
|
|
36
36
|
}
|
package/dist/auth.d.ts
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import { type PlatformType } from './platform';
|
|
2
|
-
export declare const systemType: {
|
|
3
|
-
readonly PLATFORM: "PLATFORM";
|
|
4
|
-
readonly MERCHANT: "MERCHANT";
|
|
5
|
-
readonly MAITIAN: "MAITIAN";
|
|
6
|
-
};
|
|
7
|
-
export type SystemType = (typeof systemType)[keyof typeof systemType];
|
|
8
|
-
/** 读取当前登录主体类型(内存态,由 Account 实例写入)。 */
|
|
9
|
-
export declare const getCurrentUserType: () => SystemType | '';
|
|
10
|
-
/** 写入当前登录主体类型(内存态,由 Account 实例调用)。 */
|
|
11
|
-
export declare const setCurrentUserType: (userType: SystemType | '') => void;
|
|
12
|
-
/**
|
|
13
|
-
* 身份判断使用的平台参数名。
|
|
14
|
-
*
|
|
15
|
-
* 加 `kaimai-` 前缀,避免和业务自身常见的 `platform` 参数撞车:
|
|
16
|
-
* 业务链接里带一个无关的 `platform=xxx` 就会把登录主体判错。
|
|
17
|
-
*/
|
|
18
|
-
export declare const PLATFORM_URL_PARAM = "kaimai-platform";
|
|
19
|
-
/** 读取 URL 上的平台参数;未传或非法时返回空字符串。 */
|
|
20
|
-
export declare const getUrlPlatform: () => PlatformType | '';
|
|
21
|
-
/**
|
|
22
|
-
* 按 location.origin 识别部署域:
|
|
23
|
-
* - `admin-xxx.xxx.com` / `admin.xxx.com` → 平台端
|
|
24
|
-
* - `merchant-xxx.xxx.com` / `merchant.xxx.com` → 商家端
|
|
25
|
-
*/
|
|
26
|
-
export declare const resolveOriginPlatform: (origin: string) => PlatformType | '';
|
|
27
|
-
/** 读取当前 location.origin 对应的平台标识。 */
|
|
28
|
-
export declare const getOriginPlatform: () => PlatformType | '';
|
|
29
|
-
/**
|
|
30
|
-
* 判断当前是否为平台端。
|
|
31
|
-
*
|
|
32
|
-
* 优先级:部署域(location.origin)> URL `kaimai-platform` 参数 > Account 实例写入的当前主体。
|
|
33
|
-
* 部署域命中时以域名锁定为准,避免在 admin/merchant 业务域上被 URL 参数串用。
|
|
34
|
-
*/
|
|
35
|
-
export declare const systemPlatform: () => boolean;
|
|
36
|
-
export declare const systemMerchant: () => boolean;
|
|
37
|
-
export declare const systemMaitian: () => boolean;
|
|
38
|
-
export declare const getToken: () => string | null;
|
|
39
|
-
export declare const setToken: (token: string) => void;
|
|
40
|
-
export declare const removeToken: () => void;
|
package/dist/auth.js
DELETED
|
@@ -1,128 +0,0 @@
|
|
|
1
|
-
import { default as Cookies } from 'js-cookie';
|
|
2
|
-
import { isPlatformType, platformType } from "./platform";
|
|
3
|
-
import { storage } from "./storage";
|
|
4
|
-
import { getUrlParam } from "./url";
|
|
5
|
-
var TokenKey = 'Admin-Token';
|
|
6
|
-
var tokenKeyMerchant = 'Admin-Token-MERCHANT';
|
|
7
|
-
var tokenKeyPlatform = 'Admin-Token-PLATFORM';
|
|
8
|
-
var tokenKeyMaitian = 'Admin-Token-MAITIAN';
|
|
9
|
-
export var systemType = {
|
|
10
|
-
PLATFORM: 'PLATFORM',
|
|
11
|
-
MERCHANT: 'MERCHANT',
|
|
12
|
-
MAITIAN: 'MAITIAN'
|
|
13
|
-
};
|
|
14
|
-
/**
|
|
15
|
-
* 当前登录主体,只保存在内存中。
|
|
16
|
-
*
|
|
17
|
-
* 主体归属由 `@wapri/gaccount` 的 Account 实例按自身 platform 决定,
|
|
18
|
-
* 不再落 storage:避免多平台页面共用同一份持久化主体而读错 Token。
|
|
19
|
-
*/
|
|
20
|
-
var currentUserType = '';
|
|
21
|
-
|
|
22
|
-
/** 读取当前登录主体类型(内存态,由 Account 实例写入)。 */
|
|
23
|
-
export var getCurrentUserType = function getCurrentUserType() {
|
|
24
|
-
return currentUserType;
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
/** 写入当前登录主体类型(内存态,由 Account 实例调用)。 */
|
|
28
|
-
export var setCurrentUserType = function setCurrentUserType(userType) {
|
|
29
|
-
currentUserType = userType;
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* 网关 JWT 较长,不能放 Cookie:浏览器会自动带上 Admin-Token,
|
|
34
|
-
* 与 Authorization: Bearer 同时出现时权限中心会返回 401。
|
|
35
|
-
* Token 仅存 localStorage,请求时由 axios 写入 Authorization。
|
|
36
|
-
*/
|
|
37
|
-
function clearLegacyTokenCookie() {
|
|
38
|
-
Cookies.remove(TokenKey);
|
|
39
|
-
Cookies.remove(TokenKey, {
|
|
40
|
-
path: '/'
|
|
41
|
-
});
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* 身份判断使用的平台参数名。
|
|
46
|
-
*
|
|
47
|
-
* 加 `kaimai-` 前缀,避免和业务自身常见的 `platform` 参数撞车:
|
|
48
|
-
* 业务链接里带一个无关的 `platform=xxx` 就会把登录主体判错。
|
|
49
|
-
*/
|
|
50
|
-
export var PLATFORM_URL_PARAM = 'kaimai-platform';
|
|
51
|
-
|
|
52
|
-
/** 读取 URL 上的平台参数;未传或非法时返回空字符串。 */
|
|
53
|
-
export var getUrlPlatform = function getUrlPlatform() {
|
|
54
|
-
if (typeof window === 'undefined') return '';
|
|
55
|
-
var value = String(getUrlParam(PLATFORM_URL_PARAM) || '').toLowerCase();
|
|
56
|
-
return isPlatformType(value) ? value : '';
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* 按 location.origin 识别部署域:
|
|
61
|
-
* - `admin-xxx.xxx.com` / `admin.xxx.com` → 平台端
|
|
62
|
-
* - `merchant-xxx.xxx.com` / `merchant.xxx.com` → 商家端
|
|
63
|
-
*/
|
|
64
|
-
export var resolveOriginPlatform = function resolveOriginPlatform(origin) {
|
|
65
|
-
var normalized = origin.toLowerCase();
|
|
66
|
-
if (/^https?:\/\/admin[.-]/.test(normalized)) return platformType.KAIMAI_ADMIN;
|
|
67
|
-
if (/^https?:\/\/merchant[.-]/.test(normalized)) return platformType.KAIMAI_MERCHANT;
|
|
68
|
-
return '';
|
|
69
|
-
};
|
|
70
|
-
|
|
71
|
-
/** 读取当前 location.origin 对应的平台标识。 */
|
|
72
|
-
export var getOriginPlatform = function getOriginPlatform() {
|
|
73
|
-
if (typeof window === 'undefined') return '';
|
|
74
|
-
return resolveOriginPlatform(window.location.origin);
|
|
75
|
-
};
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* 判断当前是否为平台端。
|
|
79
|
-
*
|
|
80
|
-
* 优先级:部署域(location.origin)> URL `kaimai-platform` 参数 > Account 实例写入的当前主体。
|
|
81
|
-
* 部署域命中时以域名锁定为准,避免在 admin/merchant 业务域上被 URL 参数串用。
|
|
82
|
-
*/
|
|
83
|
-
export var systemPlatform = function systemPlatform() {
|
|
84
|
-
var originPlatform = getOriginPlatform();
|
|
85
|
-
if (originPlatform) return originPlatform === platformType.KAIMAI_ADMIN;
|
|
86
|
-
var urlPlatform = getUrlPlatform();
|
|
87
|
-
if (urlPlatform) return urlPlatform === platformType.KAIMAI_ADMIN;
|
|
88
|
-
return currentUserType === systemType.PLATFORM;
|
|
89
|
-
};
|
|
90
|
-
export var systemMerchant = function systemMerchant() {
|
|
91
|
-
var originPlatform = getOriginPlatform();
|
|
92
|
-
if (originPlatform) return originPlatform === platformType.KAIMAI_MERCHANT;
|
|
93
|
-
var urlPlatform = getUrlPlatform();
|
|
94
|
-
if (urlPlatform) return urlPlatform === platformType.KAIMAI_MERCHANT;
|
|
95
|
-
return currentUserType === systemType.MERCHANT;
|
|
96
|
-
};
|
|
97
|
-
export var systemMaitian = function systemMaitian() {
|
|
98
|
-
var originPlatform = getOriginPlatform();
|
|
99
|
-
if (originPlatform) return originPlatform === platformType.MAITIAN;
|
|
100
|
-
var urlPlatform = getUrlPlatform();
|
|
101
|
-
if (urlPlatform) return urlPlatform === platformType.MAITIAN;
|
|
102
|
-
return currentUserType === systemType.MAITIAN;
|
|
103
|
-
};
|
|
104
|
-
var getTokenKey = function getTokenKey() {
|
|
105
|
-
if (systemPlatform()) {
|
|
106
|
-
return tokenKeyPlatform;
|
|
107
|
-
}
|
|
108
|
-
if (systemMerchant()) {
|
|
109
|
-
return tokenKeyMerchant;
|
|
110
|
-
}
|
|
111
|
-
if (systemMaitian()) {
|
|
112
|
-
return tokenKeyMaitian;
|
|
113
|
-
}
|
|
114
|
-
return TokenKey;
|
|
115
|
-
};
|
|
116
|
-
export var getToken = function getToken() {
|
|
117
|
-
// 历史版本会把 token 写进 Cookie,每次读取时顺带清掉,避免继续随请求发出
|
|
118
|
-
clearLegacyTokenCookie();
|
|
119
|
-
return storage.getItem(getTokenKey());
|
|
120
|
-
};
|
|
121
|
-
export var setToken = function setToken(token) {
|
|
122
|
-
clearLegacyTokenCookie();
|
|
123
|
-
storage.setItem(getTokenKey(), token);
|
|
124
|
-
};
|
|
125
|
-
export var removeToken = function removeToken() {
|
|
126
|
-
clearLegacyTokenCookie();
|
|
127
|
-
storage.removeItem(getTokenKey());
|
|
128
|
-
};
|
package/dist/platform.d.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
/** 业务平台标识,同时用于账号中心 URL 的 platform 参数。 */
|
|
2
|
-
export declare const platformType: {
|
|
3
|
-
readonly KAIMAI_ADMIN: "kaimai-admin";
|
|
4
|
-
readonly KAIMAI_MERCHANT: "kaimai-merchant";
|
|
5
|
-
readonly MAITIAN: "maitian";
|
|
6
|
-
};
|
|
7
|
-
export type PlatformType = (typeof platformType)[keyof typeof platformType];
|
|
8
|
-
export declare const platformTypes: PlatformType[];
|
|
9
|
-
/** 判断值是否为受支持的平台标识。 */
|
|
10
|
-
export declare const isPlatformType: (value: unknown) => value is PlatformType;
|
package/dist/platform.js
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
/** 业务平台标识,同时用于账号中心 URL 的 platform 参数。 */
|
|
2
|
-
export var platformType = {
|
|
3
|
-
KAIMAI_ADMIN: 'kaimai-admin',
|
|
4
|
-
KAIMAI_MERCHANT: 'kaimai-merchant',
|
|
5
|
-
MAITIAN: 'maitian'
|
|
6
|
-
};
|
|
7
|
-
export var platformTypes = Object.values(platformType);
|
|
8
|
-
|
|
9
|
-
/** 判断值是否为受支持的平台标识。 */
|
|
10
|
-
export var isPlatformType = function isPlatformType(value) {
|
|
11
|
-
return typeof value === 'string' && platformTypes.includes(value);
|
|
12
|
-
};
|