xctc-utils 1.6.1 → 1.6.3
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 +54 -24
- package/app.ts +0 -1
- package/dist/index.js +2 -2
- package/dist/params/index.d.ts +16 -0
- package/dist/params/index.js +89 -0
- package/dist/storage/index.d.ts +4 -4
- package/dist/storage/index.js +6 -6
- package/dist/utils.d.ts +1 -0
- package/dist/utils.js +9 -1
- package/dist/weixin/index.d.ts +6 -6
- package/dist/weixin/index.js +101 -87
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -10,30 +10,43 @@
|
|
|
10
10
|
存储: useUtils.useLocalStorage(key,value)
|
|
11
11
|
取值: useUtils.getLocalStorage(key)
|
|
12
12
|
```
|
|
13
|
-
|
|
14
13
|
#### sessionStorage使用,存取值时数据已经过处理
|
|
15
14
|
```
|
|
16
|
-
|
|
17
15
|
存储: useUtils.useSessionStorage(key,value)
|
|
18
16
|
取值: useUtils.getSessionStorage(key)
|
|
19
17
|
|
|
20
18
|
```
|
|
21
|
-
|
|
22
19
|
#### 删除缓存
|
|
23
20
|
```
|
|
24
21
|
key:string 需要删除的缓存键
|
|
25
|
-
|
|
26
|
-
如果
|
|
27
|
-
删除临时缓存: useUtils.removeSessionStorage(key,
|
|
28
|
-
删除永久缓存: useUtils.removeLocalStorage(key,
|
|
29
|
-
|
|
22
|
+
all:boolean 为true时删除调用方法的所有缓存,否则只删除当前传入的 key 值
|
|
23
|
+
如果 all 为 true 时,方法会优先执行缓存的clear函数, key 传任意值,
|
|
24
|
+
删除临时缓存: useUtils.removeSessionStorage(key,all)
|
|
25
|
+
删除永久缓存: useUtils.removeLocalStorage(key,all)
|
|
30
26
|
```
|
|
31
27
|
|
|
32
|
-
###
|
|
33
|
-
####
|
|
28
|
+
### 地址栏参数处理
|
|
29
|
+
#### 设置参数
|
|
34
30
|
```
|
|
35
|
-
|
|
31
|
+
interface SetOptions {
|
|
32
|
+
url:string, // 完整的 url 地址
|
|
33
|
+
data:string, // 具体需要拼接在地址参数
|
|
34
|
+
keyword:string, // 地址栏参数 key 值
|
|
35
|
+
key:string, // 参数加密 key
|
|
36
|
+
iv:string // 参数加密 iv
|
|
37
|
+
}
|
|
38
|
+
useUtils.params.set( options:SetOptions )
|
|
39
|
+
```
|
|
40
|
+
#### 获取参数
|
|
41
|
+
```
|
|
42
|
+
/**
|
|
43
|
+
* @param keys 需要截取地址栏参数键数组值,如: ['code','state']
|
|
44
|
+
* @param cache 是否需要缓存地址栏数据: 不传或传空,则不做处理,session 临时缓存 local永久缓存
|
|
45
|
+
* @param cacheKey 缓存数据的键 默认值 urlParamsData
|
|
46
|
+
*/
|
|
47
|
+
useUtils.params.get( keys:string[],cache?:string,cacheKey?:string )
|
|
36
48
|
```
|
|
49
|
+
|
|
37
50
|
### AES 加密、解密处理函数集合
|
|
38
51
|
#### AES 加密、解密,同一个数据的加密和解密传入的key 和 iv保持一致。
|
|
39
52
|
```
|
|
@@ -41,9 +54,10 @@ work: 需要加密的对象,如传入的是对象,该方法默认进行JSO
|
|
|
41
54
|
key:16位或者32位字符串作为密钥
|
|
42
55
|
iv:16位或者32位字符串作为密钥偏移量
|
|
43
56
|
data: encrypt方法加密后返回的数据
|
|
44
|
-
加密:useUtils.encrypt( work:any , key:string , iv:string )
|
|
45
|
-
解密:useUtils.decrypt( data:string , key:string , iv:string )
|
|
57
|
+
加密:useUtils.crypto.encrypt( work:any , key:string , iv:string )
|
|
58
|
+
解密:useUtils.crypto.decrypt( data:string , key:string , iv:string )
|
|
46
59
|
```
|
|
60
|
+
|
|
47
61
|
### 数据类型判断集合
|
|
48
62
|
#### 数据类型判断
|
|
49
63
|
```
|
|
@@ -60,16 +74,16 @@ useUtils.type.isWindow(val) // 是否在浏览器环境下运行
|
|
|
60
74
|
```
|
|
61
75
|
useUtils.isJson(data) // 判断传入数据是否是 JSON对象字符串,如果是,返回序列化后的JSON对象,否则返回false
|
|
62
76
|
```
|
|
77
|
+
|
|
63
78
|
### 环境判断
|
|
64
|
-
####
|
|
65
|
-
设备环境:
|
|
79
|
+
#### 获取当前设备环境:设备环境
|
|
66
80
|
```
|
|
67
|
-
|
|
68
81
|
当前使用设备类型: useUtils.deviceEnvironment() // android ios ,未识别到返回 other
|
|
69
82
|
是否在微信浏览器环境中: useUtils.weixinBrowser() // true false
|
|
70
83
|
是否是手持设备: useUtils.isMobile() // true 移动设备 false PC设备
|
|
71
84
|
|
|
72
85
|
```
|
|
86
|
+
|
|
73
87
|
### 微信授权等方法集合
|
|
74
88
|
#### 微信授权登录
|
|
75
89
|
```
|
|
@@ -83,7 +97,7 @@ interface configOption {
|
|
|
83
97
|
cryptokey?:string, // 将地址栏携带参数加密key, 必须与 weixinShareInit 方法中的 key 参数一致
|
|
84
98
|
}
|
|
85
99
|
进入系统时,默认调用该方法,自动执行微信跳转授权,回调地址中code处理、state处理,
|
|
86
|
-
useUtils.
|
|
100
|
+
useUtils.weixin.getUrlCode(config)
|
|
87
101
|
|
|
88
102
|
```
|
|
89
103
|
#### 微信config接口权限注入
|
|
@@ -95,12 +109,11 @@ interface ShareConfig{
|
|
|
95
109
|
jsApiList?:string[],
|
|
96
110
|
}
|
|
97
111
|
|
|
98
|
-
useUtils.
|
|
112
|
+
useUtils.weixin.configReady(config:ShareConfig)
|
|
99
113
|
```
|
|
100
|
-
|
|
101
114
|
#### 微信分享接口加载
|
|
102
115
|
```
|
|
103
|
-
interface
|
|
116
|
+
interface ShareOptions{
|
|
104
117
|
title?:string, // 分享标题 默认值 微信分享
|
|
105
118
|
desc?:string, // 分享描述 默认值 微信分享描述
|
|
106
119
|
link: '', // 分享链接,该链接域名或路径必须与当前页面对应的公众号 JS 安全域名一致,默认取当前访问地址
|
|
@@ -109,13 +122,25 @@ interface Share{
|
|
|
109
122
|
iv?:string, // 分享链接中对 state 数据加密的iv 必须与 weixinUrlCode 方法中的 cryptoiv 参数一致
|
|
110
123
|
key?:string, // 分享链接中对 state 数据加密的 key 必须与 weixinUrlCode 方法中的 cryptokey 参数一致
|
|
111
124
|
}
|
|
112
|
-
useUtils.
|
|
125
|
+
useUtils.weixin.shareReady(config:ShareOptions)
|
|
126
|
+
```
|
|
127
|
+
#### 唤起微信支付
|
|
128
|
+
```
|
|
129
|
+
interface ConfigPay{
|
|
130
|
+
timeStamp:string, // 支付签名时间戳
|
|
131
|
+
nonceStr:string, // 支付签名随机串,不长于 32 位
|
|
132
|
+
package:string, // 统一支付接口返回的prepay_id参数值
|
|
133
|
+
signType:string, // 签名方式,默认为'SHA1',使用新版支付需传入'MD5'
|
|
134
|
+
paySign:string,// 支付签名
|
|
135
|
+
cb?:any,//回调
|
|
136
|
+
}
|
|
137
|
+
useUtils.weixin.onlinePay(config:ConfigPay)
|
|
113
138
|
```
|
|
114
139
|
|
|
115
140
|
### 时间相关方法
|
|
116
141
|
#### 获取当前时间的时间戳
|
|
117
142
|
```
|
|
118
|
-
useUtils.getTime() // 返回秒
|
|
143
|
+
useUtils.date.getTime() // 返回秒
|
|
119
144
|
```
|
|
120
145
|
#### 时间戳转任意格式时间字符串
|
|
121
146
|
```
|
|
@@ -125,7 +150,7 @@ useUtils.getTime() // 返回秒
|
|
|
125
150
|
* @param format 解析后展示时间字符串格式,默认 "YYYY-MM-DD HH:mm:ss" 格式, 可传格式如: YYYY年MM月DD日 HH时 YYYY/MM/DD HH
|
|
126
151
|
* @returns
|
|
127
152
|
*/
|
|
128
|
-
useUtils.formatTimeStamp(num,format) // 返回 format 格式时间戳
|
|
153
|
+
useUtils.date.formatTimeStamp(num,format) // 返回 format 格式时间戳
|
|
129
154
|
|
|
130
155
|
```
|
|
131
156
|
#### 时间字符串转时间戳
|
|
@@ -135,7 +160,7 @@ useUtils.formatTimeStamp(num,format) // 返回 format 格式时间戳
|
|
|
135
160
|
* @param str 字符串格式必须为: 2017/03/03 12:23:55 格式,避免兼容性,不能传 “-” 分割的时间字符串 2017-03-03
|
|
136
161
|
* @returns
|
|
137
162
|
*/
|
|
138
|
-
useUtils.formatStrTime(str) // 返回时间戳 秒
|
|
163
|
+
useUtils.date.formatStrTime(str) // 返回时间戳 秒
|
|
139
164
|
|
|
140
165
|
```
|
|
141
166
|
|
|
@@ -176,3 +201,8 @@ useUtils.getIframeContentHeight("ueditor")
|
|
|
176
201
|
useUtils.findIdcardAge(idcard)
|
|
177
202
|
```
|
|
178
203
|
|
|
204
|
+
### 剪贴板操作集合
|
|
205
|
+
#### 将数据复制到剪贴板函数
|
|
206
|
+
```
|
|
207
|
+
useUtils.handleCopyValue(val)
|
|
208
|
+
```
|
package/app.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -39,9 +39,9 @@ var utils = __importStar(require("./utils"));
|
|
|
39
39
|
var crypto = __importStar(require("./crypto"));
|
|
40
40
|
var weixin = __importStar(require("./weixin"));
|
|
41
41
|
var time = __importStar(require("./time"));
|
|
42
|
-
var mobile = __importStar(require("./mobile"));
|
|
43
42
|
var format = __importStar(require("./format"));
|
|
44
43
|
var isType = __importStar(require("./is"));
|
|
45
44
|
var modal = __importStar(require("./modal"));
|
|
46
|
-
var
|
|
45
|
+
var params = __importStar(require("./params"));
|
|
46
|
+
var obj = __assign(__assign(__assign({}, storage), utils), { weixin: __assign({}, weixin), crypto: __assign({}, crypto), date: __assign({}, time), format: __assign({}, format), type: __assign({}, isType), modal: __assign({}, modal), params: __assign({}, params) });
|
|
47
47
|
exports.default = obj;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
interface SetOptions {
|
|
2
|
+
url: string;
|
|
3
|
+
data: string;
|
|
4
|
+
keyword: string;
|
|
5
|
+
key: string;
|
|
6
|
+
iv: string;
|
|
7
|
+
}
|
|
8
|
+
export declare const set: (options: SetOptions) => string | undefined;
|
|
9
|
+
/**
|
|
10
|
+
*
|
|
11
|
+
* @param keys 需要截取地址栏参数键数组值
|
|
12
|
+
* @param cache 是否需要缓存地址栏数据: 不传或传空,则不做处理,session 临时缓存 local永久缓存
|
|
13
|
+
* @param cacheKey 缓存数据的键 默认值 urlParamsData
|
|
14
|
+
*/
|
|
15
|
+
export declare const get: (keys: string[], cache?: string, cacheKey?: string) => void;
|
|
16
|
+
export {};
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.get = exports.set = void 0;
|
|
4
|
+
var crypto_1 = require("../crypto");
|
|
5
|
+
var storage_1 = require("../storage");
|
|
6
|
+
var defaultKey = "5uMz4R8rop26DkC8";
|
|
7
|
+
var defaultIv = "5uMz4Rsd0926DkC8";
|
|
8
|
+
var is_1 = require("../is");
|
|
9
|
+
var win = window;
|
|
10
|
+
var loc = win.location;
|
|
11
|
+
var set = function (options) {
|
|
12
|
+
var _a = options.url, url = _a === void 0 ? "" : _a, _b = options.data, data = _b === void 0 ? "" : _b, _c = options.keyword, keyword = _c === void 0 ? "state" : _c, _d = options.key, key = _d === void 0 ? defaultKey : _d, _e = options.iv, iv = _e === void 0 ? defaultIv : _e;
|
|
13
|
+
if (!data || !url)
|
|
14
|
+
return;
|
|
15
|
+
if (url.length < 10 || !url.includes("http") || !url.includes(".")) {
|
|
16
|
+
console.error("params.set函数必须传入合法的url地址,如:http://dousougou.com");
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
if (!(0, is_1.isString)(data) || !data.includes("=")) {
|
|
20
|
+
console.error("params.set函数data参数必须传入合法的字符串,如id=1&age=22");
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
var val = (0, crypto_1.encrypt)(data, key, iv);
|
|
24
|
+
var urlData = "".concat(url, "?").concat(keyword, "=").concat(val);
|
|
25
|
+
return urlData;
|
|
26
|
+
};
|
|
27
|
+
exports.set = set;
|
|
28
|
+
/**
|
|
29
|
+
*
|
|
30
|
+
* @param keys 需要截取地址栏参数键数组值
|
|
31
|
+
* @param cache 是否需要缓存地址栏数据: 不传或传空,则不做处理,session 临时缓存 local永久缓存
|
|
32
|
+
* @param cacheKey 缓存数据的键 默认值 urlParamsData
|
|
33
|
+
*/
|
|
34
|
+
var get = function (keys, cache, cacheKey) {
|
|
35
|
+
var win = window;
|
|
36
|
+
var loc = win.location;
|
|
37
|
+
var search = loc.search;
|
|
38
|
+
if (search.startsWith("?")) {
|
|
39
|
+
search = search.substring(1);
|
|
40
|
+
}
|
|
41
|
+
var urlArr = search.split("&");
|
|
42
|
+
var paramsData = {};
|
|
43
|
+
for (var k = 0; k < keys.length; k++) {
|
|
44
|
+
var keyItem = keys[k];
|
|
45
|
+
if (keyItem.includes("=")) {
|
|
46
|
+
console.error("传入的参数不能含有=字符,合法格式:['code','state']");
|
|
47
|
+
break;
|
|
48
|
+
}
|
|
49
|
+
var urlItem = "";
|
|
50
|
+
for (var i = 0; k < urlArr.length; i++) {
|
|
51
|
+
if (urlItem === null || urlItem === void 0 ? void 0 : urlItem.includes(keyItem)) {
|
|
52
|
+
urlItem = "";
|
|
53
|
+
break;
|
|
54
|
+
}
|
|
55
|
+
urlItem = urlArr[i];
|
|
56
|
+
if (urlItem === null || urlItem === void 0 ? void 0 : urlItem.includes(keyItem)) {
|
|
57
|
+
var dataStr = urlItem.substring(urlItem.indexOf(keyItem) + keyItem.length + 1);
|
|
58
|
+
var decryptStr = (0, crypto_1.decrypt)(dataStr);
|
|
59
|
+
paramsData[keyItem] = {
|
|
60
|
+
data: getParamsObj(decryptStr),
|
|
61
|
+
str: dataStr
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
if (!cacheKey)
|
|
67
|
+
cacheKey = "urlParamsData";
|
|
68
|
+
if (cache == "session") {
|
|
69
|
+
(0, storage_1.useSessionStorage)(cacheKey, paramsData);
|
|
70
|
+
}
|
|
71
|
+
if (cache == "local") {
|
|
72
|
+
(0, storage_1.useLocalStorage)(cacheKey, paramsData);
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
exports.get = get;
|
|
76
|
+
var getParamsObj = function (objStr) {
|
|
77
|
+
var objData = {};
|
|
78
|
+
if (objStr === null || objStr === void 0 ? void 0 : objStr.includes("=")) {
|
|
79
|
+
var objArr = objStr.split("&");
|
|
80
|
+
for (var k = 0; k < objArr.length; k++) {
|
|
81
|
+
var item = objArr[k];
|
|
82
|
+
if (item.includes("=")) {
|
|
83
|
+
var indexVal = item.indexOf("=");
|
|
84
|
+
objData[item.substring(0, indexVal)] = item.substring(indexVal + 1);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return objData;
|
|
89
|
+
};
|
package/dist/storage/index.d.ts
CHANGED
|
@@ -17,14 +17,14 @@ export declare function getSessionStorage(key: string): any;
|
|
|
17
17
|
/**
|
|
18
18
|
*
|
|
19
19
|
* @param key 需要删除的key值
|
|
20
|
-
* @param
|
|
20
|
+
* @param all 是否删除所有缓存
|
|
21
21
|
* @returns
|
|
22
22
|
*/
|
|
23
|
-
export declare function removeSessionStorage(key?: string,
|
|
23
|
+
export declare function removeSessionStorage(key?: string, all?: boolean): void;
|
|
24
24
|
/**
|
|
25
25
|
*
|
|
26
26
|
* @param key 需要删除的key值
|
|
27
|
-
* @param
|
|
27
|
+
* @param all 是否删除所有缓存
|
|
28
28
|
* @returns
|
|
29
29
|
*/
|
|
30
|
-
export declare function removeLocalStorage(key?: string,
|
|
30
|
+
export declare function removeLocalStorage(key?: string, all?: boolean): void;
|
package/dist/storage/index.js
CHANGED
|
@@ -78,11 +78,11 @@ function getStorage(key, obj) {
|
|
|
78
78
|
/**
|
|
79
79
|
*
|
|
80
80
|
* @param key 需要删除的key值
|
|
81
|
-
* @param
|
|
81
|
+
* @param all 是否删除所有缓存
|
|
82
82
|
* @returns
|
|
83
83
|
*/
|
|
84
|
-
function removeSessionStorage(key,
|
|
85
|
-
if ((0, is_1.isBoolean)(
|
|
84
|
+
function removeSessionStorage(key, all) {
|
|
85
|
+
if ((0, is_1.isBoolean)(all) && all) {
|
|
86
86
|
wss.clear();
|
|
87
87
|
return;
|
|
88
88
|
}
|
|
@@ -94,11 +94,11 @@ exports.removeSessionStorage = removeSessionStorage;
|
|
|
94
94
|
/**
|
|
95
95
|
*
|
|
96
96
|
* @param key 需要删除的key值
|
|
97
|
-
* @param
|
|
97
|
+
* @param all 是否删除所有缓存
|
|
98
98
|
* @returns
|
|
99
99
|
*/
|
|
100
|
-
function removeLocalStorage(key,
|
|
101
|
-
if ((0, is_1.isBoolean)(
|
|
100
|
+
function removeLocalStorage(key, all) {
|
|
101
|
+
if ((0, is_1.isBoolean)(all) && all) {
|
|
102
102
|
wls.clear();
|
|
103
103
|
return;
|
|
104
104
|
}
|
package/dist/utils.d.ts
CHANGED
package/dist/utils.js
CHANGED
|
@@ -1,6 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.handleCopyValue = exports.weixinBrowser = exports.deviceEnvironment = exports.getIframeContentHeight = exports.checkIframeContentHeight = exports.$id = exports.isJson = void 0;
|
|
3
|
+
exports.handleCopyValue = exports.weixinBrowser = exports.deviceEnvironment = exports.getIframeContentHeight = exports.checkIframeContentHeight = exports.$id = exports.isJson = exports.isMobile = void 0;
|
|
4
|
+
var win = window;
|
|
5
|
+
// 是否是手机端环境
|
|
6
|
+
function isMobile() {
|
|
7
|
+
var info = win.navigator.userAgent;
|
|
8
|
+
var isPhone = /mobile/i.test(info);
|
|
9
|
+
return isPhone;
|
|
10
|
+
}
|
|
11
|
+
exports.isMobile = isMobile;
|
|
4
12
|
/**
|
|
5
13
|
* 判断当前传入的字符串是否为 json格式 字符串
|
|
6
14
|
* @param str
|
package/dist/weixin/index.d.ts
CHANGED
|
@@ -9,9 +9,9 @@ interface configOption {
|
|
|
9
9
|
isPassLogin?: boolean;
|
|
10
10
|
redirectUri?: string;
|
|
11
11
|
}
|
|
12
|
-
export declare function
|
|
13
|
-
export declare function
|
|
14
|
-
export declare function
|
|
12
|
+
export declare function getUrlCode(config: configOption): void;
|
|
13
|
+
export declare function envSDK(): any;
|
|
14
|
+
export declare function getCode(appId?: string, scope?: string): void;
|
|
15
15
|
interface ShareConfig {
|
|
16
16
|
http?: any;
|
|
17
17
|
cb?: any;
|
|
@@ -20,7 +20,7 @@ interface ShareConfig {
|
|
|
20
20
|
openTagList?: string[];
|
|
21
21
|
url?: string;
|
|
22
22
|
}
|
|
23
|
-
export declare function
|
|
23
|
+
export declare function configReady(config: ShareConfig): void;
|
|
24
24
|
interface ShareOptions {
|
|
25
25
|
title?: string;
|
|
26
26
|
desc?: string;
|
|
@@ -31,7 +31,7 @@ interface ShareOptions {
|
|
|
31
31
|
key?: string;
|
|
32
32
|
shareUrl?: string;
|
|
33
33
|
}
|
|
34
|
-
export declare function
|
|
34
|
+
export declare function shareReady(options: ShareOptions): void;
|
|
35
35
|
/**
|
|
36
36
|
*
|
|
37
37
|
* @param options
|
|
@@ -45,5 +45,5 @@ interface ConfigPay {
|
|
|
45
45
|
paySign: string;
|
|
46
46
|
cb?: any;
|
|
47
47
|
}
|
|
48
|
-
export declare function
|
|
48
|
+
export declare function onlinePay(config: ConfigPay): void;
|
|
49
49
|
export {};
|
package/dist/weixin/index.js
CHANGED
|
@@ -1,37 +1,59 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
2
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
26
|
+
exports.onlinePay = exports.shareReady = exports.configReady = exports.getCode = exports.envSDK = exports.getUrlCode = void 0;
|
|
4
27
|
// import * as weixin from 'weixin-js-sdk' // 引入微信SDK
|
|
5
28
|
var weixin = require("weixin-js-sdk");
|
|
6
29
|
var utils_1 = require("../utils");
|
|
7
30
|
var crypto_1 = require("../crypto");
|
|
31
|
+
var params = __importStar(require("../params"));
|
|
8
32
|
var index_1 = require("../storage/index");
|
|
9
33
|
var w = window;
|
|
10
34
|
var loc = w.location;
|
|
11
35
|
var defaultKey = "5uMz4R8rop26DkC8";
|
|
12
36
|
var defaultIv = "5uMz4Rsd0926DkC8";
|
|
13
37
|
var weixinConfig = {};
|
|
14
|
-
function
|
|
38
|
+
function getUrlCode(config) {
|
|
39
|
+
var _a;
|
|
15
40
|
config = config || {};
|
|
16
|
-
var appId = config
|
|
41
|
+
var appId = (config === null || config === void 0 ? void 0 : config.appId) || "";
|
|
17
42
|
if (!appId)
|
|
18
43
|
return;
|
|
19
44
|
weixinConfig = config;
|
|
20
|
-
var scope = config
|
|
21
|
-
var codeKey = config
|
|
22
|
-
var stateKey = config
|
|
45
|
+
var scope = (config === null || config === void 0 ? void 0 : config.scope) || "snsapi_userinfo";
|
|
46
|
+
var codeKey = (config === null || config === void 0 ? void 0 : config.codeKey) || "weixinAuthCode";
|
|
47
|
+
var stateKey = (config === null || config === void 0 ? void 0 : config.stateKey) || "weixinAuthState";
|
|
23
48
|
weixinConfig['scope'] = scope;
|
|
24
49
|
weixinConfig['codeKey'] = codeKey;
|
|
25
50
|
weixinConfig['stateKey'] = stateKey;
|
|
26
51
|
(0, index_1.useSessionStorage)("weixinConfig", weixinConfig);
|
|
27
|
-
var
|
|
52
|
+
var _b = weixinConfig.cryptoiv, cryptoiv = _b === void 0 ? "" : _b, _c = weixinConfig.cryptokey, cryptokey = _c === void 0 ? "" : _c;
|
|
28
53
|
var search = loc.search; // ?code=0916SzFa1KH9TA0Ke7Ha1AQx6446SzFr&state=123
|
|
29
|
-
|
|
30
|
-
// 获取地址栏返回 code
|
|
31
|
-
var localCode = "";
|
|
32
|
-
if (codeKey) {
|
|
33
|
-
localCode = (0, index_1.getSessionStorage)(codeKey);
|
|
34
|
-
}
|
|
54
|
+
initSdk(); // 加载微信sdk环境
|
|
55
|
+
// 获取地址栏返回 code 参数值
|
|
56
|
+
var localCode = (0, index_1.getSessionStorage)("urlParamsData") || "";
|
|
35
57
|
// 默认第一次通过链接进入系统,没有code和state
|
|
36
58
|
var searchIndex = -1;
|
|
37
59
|
var stateIndex = -1;
|
|
@@ -39,63 +61,36 @@ function weixinUrlCode(config) {
|
|
|
39
61
|
searchIndex = search.indexOf("code=");
|
|
40
62
|
stateIndex = search.indexOf("state=");
|
|
41
63
|
}
|
|
64
|
+
// 如果本地已经存储 微信授权返回的 code 值,证明用户在执行刷新操作,不再执行微信授权逻辑
|
|
42
65
|
if (localCode && stateIndex == -1 && searchIndex == -1) {
|
|
43
66
|
return;
|
|
44
67
|
}
|
|
45
|
-
// 如果链接地址中带有code
|
|
68
|
+
// 如果链接地址中带有code,证明执行微信授权登录,并返回授权所需的 code 参数
|
|
46
69
|
if (searchIndex >= 0) {
|
|
47
70
|
if (localCode) {
|
|
48
71
|
// localCode 存在,用户已执行微信跳转链接,用户可能执行当前网页的刷新操作
|
|
49
72
|
return;
|
|
50
73
|
}
|
|
51
74
|
else {
|
|
52
|
-
//
|
|
53
|
-
var
|
|
54
|
-
var
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
if (item.indexOf("code=") != -1) {
|
|
58
|
-
code = item.substr(5);
|
|
59
|
-
if (codeKey)
|
|
60
|
-
(0, index_1.useSessionStorage)(codeKey, code);
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
if (code) {
|
|
64
|
-
weixinLogin(code);
|
|
75
|
+
// 当前系统本地没有存储 code,流程为用户只需微信授权登录后返回系统,此时通过 CODE 执行微信登录
|
|
76
|
+
var urlParamsData = (0, index_1.getSessionStorage)("urlParamsData") || "";
|
|
77
|
+
var codeData = ((_a = urlParamsData === null || urlParamsData === void 0 ? void 0 : urlParamsData.code) === null || _a === void 0 ? void 0 : _a.str) || ""; // 第一次进入系统为空
|
|
78
|
+
if (codeData) {
|
|
79
|
+
login(codeData);
|
|
65
80
|
}
|
|
66
81
|
}
|
|
67
82
|
return;
|
|
68
83
|
}
|
|
69
|
-
// 用户通过分享链接进入系统,此时含有
|
|
84
|
+
// 用户通过分享链接进入系统,此时含有 state 参数 , 无code参数
|
|
70
85
|
if (stateIndex >= 0) {
|
|
71
|
-
var hrefState = "";
|
|
72
|
-
var urlArr = formatSearch(search, "state=");
|
|
73
|
-
for (var k = 0; k < urlArr.length; k++) {
|
|
74
|
-
var item = urlArr[k];
|
|
75
|
-
if (item.indexOf("state=") != -1) {
|
|
76
|
-
hrefState = item.substr(6);
|
|
77
|
-
var stateKey_1 = weixinConfig['stateKey'];
|
|
78
|
-
if (!stateKey_1) {
|
|
79
|
-
stateKey_1 = "weixinState";
|
|
80
|
-
weixinConfig['stateKey'] = stateKey_1;
|
|
81
|
-
}
|
|
82
|
-
(0, index_1.useSessionStorage)(stateKey_1, hrefState);
|
|
83
|
-
var cryptoObj = cryptoConfig(cryptokey, cryptoiv);
|
|
84
|
-
var stateObj = (0, crypto_1.decrypt)(hrefState, cryptoObj.key, cryptoObj.iv);
|
|
85
|
-
if (stateObj instanceof Object)
|
|
86
|
-
stateObj = JSON.stringify(stateObj);
|
|
87
|
-
(0, index_1.useSessionStorage)("".concat(stateKey_1, "Obj"), stateObj);
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
86
|
// 将 state进行本地存储,存储完成后跳转到微信授权页面
|
|
91
|
-
|
|
92
|
-
return;
|
|
87
|
+
return getCode(); // 微信授权页面获取CODE
|
|
93
88
|
}
|
|
94
89
|
if (!localCode && stateIndex == -1 && searchIndex == -1) {
|
|
95
|
-
return
|
|
90
|
+
return getCode();
|
|
96
91
|
}
|
|
97
92
|
}
|
|
98
|
-
exports.
|
|
93
|
+
exports.getUrlCode = getUrlCode;
|
|
99
94
|
function cryptoConfig(key, iv) {
|
|
100
95
|
if (!key)
|
|
101
96
|
key = defaultKey;
|
|
@@ -107,26 +102,41 @@ function cryptoConfig(key, iv) {
|
|
|
107
102
|
};
|
|
108
103
|
}
|
|
109
104
|
// 执行微信登录
|
|
110
|
-
function
|
|
105
|
+
function login(code) {
|
|
111
106
|
var appId = weixinConfig.appId, http = weixinConfig.http, stateKey = weixinConfig.stateKey, redirectUri = weixinConfig.redirectUri;
|
|
112
|
-
if (!appId)
|
|
107
|
+
if (!appId) {
|
|
108
|
+
console.error("未正常传入微信公众号appId");
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
if (!code) {
|
|
112
|
+
console.error("未获取到微信授权的code参数");
|
|
113
113
|
return;
|
|
114
|
+
}
|
|
114
115
|
var obj = {
|
|
115
116
|
"app_id": appId,
|
|
116
117
|
"js_code": code
|
|
117
118
|
};
|
|
118
|
-
if (!http)
|
|
119
|
+
if (!http) {
|
|
120
|
+
console.error("未正常传入微信授权登录http函数");
|
|
119
121
|
return;
|
|
120
|
-
|
|
122
|
+
}
|
|
123
|
+
(0, index_1.useSessionStorage)("loginParams", JSON.stringify(obj));
|
|
121
124
|
// 调试字段 isPassLogin ,为true 时不继续执行微信登录,前端可拿到 code 给后端测试
|
|
122
125
|
// if(weixinConfig['isPassLogin']) return
|
|
123
126
|
weixinConfig.http(obj).then(function (res) {
|
|
124
|
-
|
|
127
|
+
var _a;
|
|
128
|
+
(0, index_1.useSessionStorage)("loginRes", res);
|
|
125
129
|
if (res.code == 40163) {
|
|
126
130
|
// 未登录则先执行登录
|
|
127
|
-
return
|
|
131
|
+
return getCode();
|
|
132
|
+
}
|
|
133
|
+
var data = {};
|
|
134
|
+
if (res === null || res === void 0 ? void 0 : res.hasOwnProperty("data")) {
|
|
135
|
+
data = res.data;
|
|
136
|
+
}
|
|
137
|
+
if (res === null || res === void 0 ? void 0 : res.hasOwnProperty("Data")) {
|
|
138
|
+
data = res.Data;
|
|
128
139
|
}
|
|
129
|
-
var data = res['data'] || {};
|
|
130
140
|
for (var key in data) {
|
|
131
141
|
var item = data[key];
|
|
132
142
|
if (item instanceof Object) {
|
|
@@ -140,24 +150,24 @@ function weixinLogin(code) {
|
|
|
140
150
|
url = encodeURIComponent(redirectUri);
|
|
141
151
|
}
|
|
142
152
|
var hashVal = loc.hash;
|
|
143
|
-
var
|
|
153
|
+
var urlParamsData = (0, index_1.getSessionStorage)("urlParamsData") || "";
|
|
154
|
+
var stateData = ((_a = urlParamsData === null || urlParamsData === void 0 ? void 0 : urlParamsData.state) === null || _a === void 0 ? void 0 : _a.data) || ""; // 默认有值,链接携带
|
|
144
155
|
var str = "";
|
|
145
|
-
if (
|
|
146
|
-
var
|
|
147
|
-
for (var key in stateObj) {
|
|
156
|
+
if (stateData) {
|
|
157
|
+
for (var key in stateData) {
|
|
148
158
|
if (key == "path") {
|
|
149
|
-
if (
|
|
150
|
-
url = "".concat(url, "/#").concat(
|
|
159
|
+
if (stateData[key][0] == "/") {
|
|
160
|
+
url = "".concat(url, "/#").concat(stateData.path);
|
|
151
161
|
}
|
|
152
162
|
else {
|
|
153
|
-
url = "".concat(url, "/#/").concat(
|
|
163
|
+
url = "".concat(url, "/#/").concat(stateData.path);
|
|
154
164
|
}
|
|
155
165
|
}
|
|
156
166
|
else {
|
|
157
|
-
str += "".concat(key, "=").concat(
|
|
167
|
+
str += "".concat(key, "=").concat(stateData[key], "&");
|
|
158
168
|
}
|
|
159
169
|
}
|
|
160
|
-
if (
|
|
170
|
+
if (stateData === null || stateData === void 0 ? void 0 : stateData.path) {
|
|
161
171
|
if (str) {
|
|
162
172
|
url = "".concat(url, "?").concat(str);
|
|
163
173
|
}
|
|
@@ -179,7 +189,7 @@ function weixinLogin(code) {
|
|
|
179
189
|
}
|
|
180
190
|
}
|
|
181
191
|
}).catch(function (err) {
|
|
182
|
-
(0, index_1.useSessionStorage)("
|
|
192
|
+
(0, index_1.useSessionStorage)("loginError", err);
|
|
183
193
|
});
|
|
184
194
|
}
|
|
185
195
|
/**
|
|
@@ -193,11 +203,14 @@ function formatSearch(search, keys) {
|
|
|
193
203
|
return false;
|
|
194
204
|
var index = search.indexOf(keys);
|
|
195
205
|
search = search.substr(index);
|
|
206
|
+
if (search.startsWith("?")) {
|
|
207
|
+
search = search.substring(1);
|
|
208
|
+
}
|
|
196
209
|
var urlArr = search.split("&");
|
|
197
210
|
return urlArr;
|
|
198
211
|
}
|
|
199
212
|
// 加载当前微信sdk环境
|
|
200
|
-
function
|
|
213
|
+
function initSdk() {
|
|
201
214
|
var wx = null;
|
|
202
215
|
if (w.wx && w.wx.ready) {
|
|
203
216
|
wx = w.wx;
|
|
@@ -209,13 +222,14 @@ function weixinInit() {
|
|
|
209
222
|
return wx;
|
|
210
223
|
}
|
|
211
224
|
// 返回微信环境 sdk
|
|
212
|
-
function
|
|
213
|
-
var env =
|
|
225
|
+
function envSDK() {
|
|
226
|
+
var env = initSdk() || {};
|
|
214
227
|
return env;
|
|
215
228
|
}
|
|
216
|
-
exports.
|
|
229
|
+
exports.envSDK = envSDK;
|
|
217
230
|
// 前往微信授权登录页面
|
|
218
|
-
function
|
|
231
|
+
function getCode(appId, scope) {
|
|
232
|
+
params.get(['code', 'state'], "session");
|
|
219
233
|
if ((0, utils_1.weixinBrowser)()) {
|
|
220
234
|
var id = "";
|
|
221
235
|
if (weixinConfig['appId']) {
|
|
@@ -225,7 +239,7 @@ function weixinCode(appId, scope) {
|
|
|
225
239
|
id = appId;
|
|
226
240
|
}
|
|
227
241
|
if (!id) {
|
|
228
|
-
console.log("调用跳转微信授权方法时
|
|
242
|
+
console.log("调用跳转微信授权方法时getCode,请传入参数:appId");
|
|
229
243
|
return;
|
|
230
244
|
}
|
|
231
245
|
var scopeVal = "snsapi_userinfo";
|
|
@@ -249,7 +263,7 @@ function weixinCode(appId, scope) {
|
|
|
249
263
|
console.log("非微信内核浏览器执行其他业务");
|
|
250
264
|
}
|
|
251
265
|
}
|
|
252
|
-
exports.
|
|
266
|
+
exports.getCode = getCode;
|
|
253
267
|
var defaultJsApiList = [
|
|
254
268
|
'updateAppMessageShareData', 'updateTimelineShareData', "onMenuShareWeibo", "onMenuShareQZone", "startRecord", "stopRecord", "closeWindow", "scanQRCode", "chooseWXPay", "chooseCard", "openCard",
|
|
255
269
|
"onVoiceRecordEnd", "playVoice", "pauseVoice", "stopVoice", "onVoicePlayEnd", "uploadVoice", "downloadVoice", "chooseImage", "previewImage", "uploadImage", "openProductSpecificView", "addCard",
|
|
@@ -259,7 +273,7 @@ var defaultOpenTagList = [
|
|
|
259
273
|
"wx-open-subscribe", "wx-open-launch-app", "wx-open-launch-weapp",
|
|
260
274
|
"wx-open-audio"
|
|
261
275
|
];
|
|
262
|
-
function
|
|
276
|
+
function configReady(config) {
|
|
263
277
|
if (!config['http'])
|
|
264
278
|
return;
|
|
265
279
|
var url = loc.origin;
|
|
@@ -281,7 +295,7 @@ function weixinShareConfig(config) {
|
|
|
281
295
|
config.http(param).then(function (res) {
|
|
282
296
|
if (res.code == 0) {
|
|
283
297
|
var data = res['data'] || {};
|
|
284
|
-
var wx =
|
|
298
|
+
var wx = initSdk();
|
|
285
299
|
wx.config({
|
|
286
300
|
debug: false,
|
|
287
301
|
appId: data.appId,
|
|
@@ -293,18 +307,18 @@ function weixinShareConfig(config) {
|
|
|
293
307
|
});
|
|
294
308
|
wx.ready(function (res) {
|
|
295
309
|
console.log("加载微信分享配置完成:", res);
|
|
296
|
-
(0, index_1.useSessionStorage)("
|
|
310
|
+
(0, index_1.useSessionStorage)("configReadySuccess", "\u52A0\u8F7D\u5FAE\u4FE1\u5206\u4EAB\u914D\u7F6E\u5B8C\u6210\uFF1A".concat(res));
|
|
297
311
|
if (config['cb'] && typeof config.cb === "function")
|
|
298
312
|
config.cb();
|
|
299
313
|
});
|
|
300
314
|
wx.error(function (err) {
|
|
301
|
-
(0, index_1.useSessionStorage)("
|
|
315
|
+
(0, index_1.useSessionStorage)("configReadyError", "\u52A0\u8F7D\u5FAE\u4FE1\u5206\u4EAB\u914D\u7F6E\u9519\u8BEF\uFF1A".concat(err));
|
|
302
316
|
});
|
|
303
317
|
}
|
|
304
318
|
});
|
|
305
319
|
}
|
|
306
|
-
exports.
|
|
307
|
-
function
|
|
320
|
+
exports.configReady = configReady;
|
|
321
|
+
function shareReady(options) {
|
|
308
322
|
var title = "微信分享标题";
|
|
309
323
|
if (options['title'])
|
|
310
324
|
title = options.title;
|
|
@@ -330,10 +344,10 @@ function weixinShareInit(options) {
|
|
|
330
344
|
}
|
|
331
345
|
if (options['shareUrl'])
|
|
332
346
|
link = options['shareUrl'];
|
|
333
|
-
var wx =
|
|
347
|
+
var wx = initSdk();
|
|
334
348
|
(0, index_1.useSessionStorage)("weixinShareUrl", link);
|
|
335
349
|
wx.ready(function () {
|
|
336
|
-
(0, index_1.useSessionStorage)("
|
|
350
|
+
(0, index_1.useSessionStorage)("shareReady", "success");
|
|
337
351
|
//需在用户可能点击分享按钮前就先调用
|
|
338
352
|
wx.updateAppMessageShareData({
|
|
339
353
|
title: title,
|
|
@@ -365,9 +379,9 @@ function weixinShareInit(options) {
|
|
|
365
379
|
console.log("配置进入错误:", res);
|
|
366
380
|
});
|
|
367
381
|
}
|
|
368
|
-
exports.
|
|
369
|
-
function
|
|
370
|
-
var wx =
|
|
382
|
+
exports.shareReady = shareReady;
|
|
383
|
+
function onlinePay(config) {
|
|
384
|
+
var wx = initSdk();
|
|
371
385
|
// let { timeStamp,signType,prepay_id,paySign,package,nonceStr,appId } = params
|
|
372
386
|
var cb = "";
|
|
373
387
|
if (config['cb'] && typeof config.cb === "function")
|
|
@@ -396,4 +410,4 @@ function weixinPay(config) {
|
|
|
396
410
|
}
|
|
397
411
|
});
|
|
398
412
|
}
|
|
399
|
-
exports.
|
|
413
|
+
exports.onlinePay = onlinePay;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "xctc-utils",
|
|
3
|
-
"version": "1.6.
|
|
3
|
+
"version": "1.6.3",
|
|
4
4
|
"description": "localStorage存储\r ```\r sessionStorage存储\r ```\r crypto-js加密、解密\r ```\r 微信授权登录、微信分享\r ```\r 设备环境获取\r ```\r 是否是微信浏览器\r ```\r 时间戳转时间,字符串转时间戳",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|