xctc-utils 1.1.7 → 1.2.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/README.md +1 -0
- package/dist/crypto/index.js +10 -30
- package/dist/time/index.d.ts +1 -1
- package/dist/time/index.js +2 -0
- package/dist/weixin/index.js +31 -14
- package/package.json +11 -2
package/README.md
CHANGED
package/dist/crypto/index.js
CHANGED
|
@@ -1,36 +1,16 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var
|
|
3
|
-
|
|
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;
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
24
4
|
};
|
|
25
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
6
|
exports.decrypt = exports.encrypt = void 0;
|
|
27
|
-
var
|
|
7
|
+
var crypto_js_1 = __importDefault(require("crypto-js"));
|
|
28
8
|
var utils_1 = require("../utils");
|
|
29
9
|
function formatVal(val) {
|
|
30
10
|
var key = "5uMz4R8r0926DkC8";
|
|
31
11
|
if (val)
|
|
32
12
|
key = val;
|
|
33
|
-
return
|
|
13
|
+
return crypto_js_1.default.enc.Utf8.parse(key);
|
|
34
14
|
}
|
|
35
15
|
// 加密
|
|
36
16
|
function encrypt(word, key, iv) {
|
|
@@ -39,10 +19,10 @@ function encrypt(word, key, iv) {
|
|
|
39
19
|
}
|
|
40
20
|
// key //十六位十六进制数作为密钥
|
|
41
21
|
// iv 十六位十六进制数作为密钥偏移量
|
|
42
|
-
var src =
|
|
22
|
+
var src = crypto_js_1.default.enc.Utf8.parse(word);
|
|
43
23
|
key = formatVal(key);
|
|
44
24
|
iv = formatVal(iv);
|
|
45
|
-
var encrypted =
|
|
25
|
+
var encrypted = crypto_js_1.default.AES.encrypt(src, key, { iv: iv, mode: crypto_js_1.default.mode.CBC, padding: crypto_js_1.default.pad.Pkcs7 });
|
|
46
26
|
return encrypted.ciphertext.toString().toUpperCase();
|
|
47
27
|
}
|
|
48
28
|
exports.encrypt = encrypt;
|
|
@@ -51,10 +31,10 @@ exports.encrypt = encrypt;
|
|
|
51
31
|
function decrypt(word, key, iv) {
|
|
52
32
|
key = formatVal(key);
|
|
53
33
|
iv = formatVal(iv);
|
|
54
|
-
var encryptedHexStr =
|
|
55
|
-
var src =
|
|
56
|
-
var decrypt =
|
|
57
|
-
var decryptedStr = decrypt.toString(
|
|
34
|
+
var encryptedHexStr = crypto_js_1.default.enc.Hex.parse(word);
|
|
35
|
+
var src = crypto_js_1.default.enc.Base64.stringify(encryptedHexStr);
|
|
36
|
+
var decrypt = crypto_js_1.default.AES.decrypt(src, key, { iv: iv, mode: crypto_js_1.default.mode.CBC, padding: crypto_js_1.default.pad.Pkcs7 });
|
|
37
|
+
var decryptedStr = decrypt.toString(crypto_js_1.default.enc.Utf8);
|
|
58
38
|
var data = decryptedStr.toString();
|
|
59
39
|
if ((0, utils_1.isJson)(data)) {
|
|
60
40
|
return JSON.parse(data);
|
package/dist/time/index.d.ts
CHANGED
|
@@ -11,4 +11,4 @@ export declare function formatTimeStamp(num?: number, format?: string): string;
|
|
|
11
11
|
* @param str 字符串格式必须为: 2017/03/03 12:23:55 格式,避免兼容性,不能传 “ - ” 分割的时间字符串 2017-03-03
|
|
12
12
|
* @returns
|
|
13
13
|
*/
|
|
14
|
-
export declare function formatStrTime(str: string): number;
|
|
14
|
+
export declare function formatStrTime(str: string): number | undefined;
|
package/dist/time/index.js
CHANGED
package/dist/weixin/index.js
CHANGED
|
@@ -13,15 +13,19 @@ var defaultIv = "5uMz4Rsd0926DkC8";
|
|
|
13
13
|
var weixinConfig = {};
|
|
14
14
|
function weixinUrlCode(config) {
|
|
15
15
|
weixinConfig = config || {};
|
|
16
|
+
(0, index_1.useSessionStorage)("weixinConfig", weixinConfig);
|
|
16
17
|
var cryptoiv = weixinConfig.cryptoiv, cryptokey = weixinConfig.cryptokey;
|
|
17
18
|
var search = loc.search; // ?code=0916SzFa1KH9TA0Ke7Ha1AQx6446SzFr&state=123
|
|
18
19
|
// 获取地址栏返回 code 参数
|
|
19
20
|
var codeKey = weixinConfig['codeKey'];
|
|
21
|
+
if (!codeKey)
|
|
22
|
+
codeKey = "weixinCode";
|
|
20
23
|
var localCode = "";
|
|
21
24
|
if (codeKey)
|
|
22
25
|
localCode = (0, index_1.getSessionStorage)(codeKey);
|
|
23
26
|
if (!localCode && search && search.indexOf("code=") != -1) {
|
|
24
27
|
// localCode 不存在, 微信链接中存在 code 用户初次登录系统,
|
|
28
|
+
// 用户从微信授权页面跳转回来,地址中携带code参数
|
|
25
29
|
var urlArr = formatSearch(search, "code=");
|
|
26
30
|
var code = "";
|
|
27
31
|
for (var k = 0; k < urlArr.length; k++) {
|
|
@@ -32,7 +36,9 @@ function weixinUrlCode(config) {
|
|
|
32
36
|
(0, index_1.useSessionStorage)(codeKey, code);
|
|
33
37
|
}
|
|
34
38
|
}
|
|
35
|
-
|
|
39
|
+
if (code) {
|
|
40
|
+
weixinLogin(code);
|
|
41
|
+
}
|
|
36
42
|
}
|
|
37
43
|
else if (localCode) {
|
|
38
44
|
// localCode 存在,用户已执行微信跳转链接,执行刷新操作
|
|
@@ -55,7 +61,7 @@ function weixinUrlCode(config) {
|
|
|
55
61
|
}
|
|
56
62
|
}
|
|
57
63
|
}
|
|
58
|
-
//
|
|
64
|
+
// 将 state进行本地存储,存储完成后跳转到微信授权页面
|
|
59
65
|
weixinCode(); // 微信授权页面获取CODE
|
|
60
66
|
}
|
|
61
67
|
}
|
|
@@ -72,7 +78,7 @@ function cryptoConfig(key, iv) {
|
|
|
72
78
|
}
|
|
73
79
|
// 执行微信登录
|
|
74
80
|
function weixinLogin(code) {
|
|
75
|
-
var
|
|
81
|
+
var appId = weixinConfig.appId, http = weixinConfig.http, stateKey = weixinConfig.stateKey;
|
|
76
82
|
if (!appId)
|
|
77
83
|
return;
|
|
78
84
|
var obj = {
|
|
@@ -86,7 +92,13 @@ function weixinLogin(code) {
|
|
|
86
92
|
// 未登录则先执行登录
|
|
87
93
|
return weixinCode();
|
|
88
94
|
}
|
|
89
|
-
var data = res.data;
|
|
95
|
+
var data = res === null || res === void 0 ? void 0 : res.data;
|
|
96
|
+
if (data instanceof Object) {
|
|
97
|
+
(0, index_1.useSessionStorage)("weixinLoginData", JSON.stringify(data));
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
(0, index_1.useSessionStorage)("weixinLoginStatus", "\u5FAE\u4FE1\u767B\u5F55\u5931\u8D25\uFF1A".concat(res.code, "\uFF1B").concat(res.msg));
|
|
101
|
+
}
|
|
90
102
|
for (var key in data) {
|
|
91
103
|
var item = data[key];
|
|
92
104
|
if (item instanceof Object) {
|
|
@@ -95,8 +107,10 @@ function weixinLogin(code) {
|
|
|
95
107
|
(0, index_1.useSessionStorage)(key, item);
|
|
96
108
|
}
|
|
97
109
|
var url = loc.origin;
|
|
110
|
+
var hashVal = loc.hash;
|
|
98
111
|
var state = (0, index_1.getSessionStorage)(stateKey);
|
|
99
112
|
var str = "";
|
|
113
|
+
(0, index_1.useSessionStorage)("getWeixinSessionState", state);
|
|
100
114
|
if (state) {
|
|
101
115
|
var stateObj = (0, index_1.getSessionStorage)("".concat(stateKey, "Obj"));
|
|
102
116
|
for (var key in stateObj) {
|
|
@@ -120,6 +134,7 @@ function weixinLogin(code) {
|
|
|
120
134
|
loc.replace(url);
|
|
121
135
|
}
|
|
122
136
|
else {
|
|
137
|
+
url = "".concat(url, "/").concat(hashVal);
|
|
123
138
|
loc.replace(url);
|
|
124
139
|
}
|
|
125
140
|
});
|
|
@@ -167,6 +182,11 @@ function weixinCode() {
|
|
|
167
182
|
console.log("非微信内核浏览器执行其他业务");
|
|
168
183
|
}
|
|
169
184
|
}
|
|
185
|
+
var defaultJsApiList = [
|
|
186
|
+
'updateAppMessageShareData', 'updateTimelineShareData', "onMenuShareWeibo", "onMenuShareQZone", "startRecord", "stopRecord", "closeWindow", "scanQRCode", "chooseWXPay", "chooseCard", "openCard",
|
|
187
|
+
"onVoiceRecordEnd", "playVoice", "pauseVoice", "stopVoice", "onVoicePlayEnd", "uploadVoice", "downloadVoice", "chooseImage", "previewImage", "uploadImage", "openProductSpecificView", "addCard",
|
|
188
|
+
"downloadImage", "translateVoice", "getNetworkType", "openLocation", "getLocation", "hideOptionMenu", "showOptionMenu", "hideMenuItems", "showMenuItems", "hideAllNonBaseMenuItem", "showAllNonBaseMenuItem",
|
|
189
|
+
];
|
|
170
190
|
function weixinShareConfig(config) {
|
|
171
191
|
var _a;
|
|
172
192
|
if (!(config === null || config === void 0 ? void 0 : config.http))
|
|
@@ -177,11 +197,12 @@ function weixinShareConfig(config) {
|
|
|
177
197
|
appId: config.appId,
|
|
178
198
|
};
|
|
179
199
|
var jsApiList = defaultJsApiList;
|
|
180
|
-
if ((_a = config.jsApiList) === null || _a === void 0 ? void 0 : _a.length)
|
|
200
|
+
if ((_a = config === null || config === void 0 ? void 0 : config.jsApiList) === null || _a === void 0 ? void 0 : _a.length)
|
|
181
201
|
jsApiList = config.jsApiList;
|
|
182
202
|
config.http(param).then(function (res) {
|
|
203
|
+
var _a;
|
|
183
204
|
if (res.code == 0) {
|
|
184
|
-
var data = res.data;
|
|
205
|
+
var data = (_a = res === null || res === void 0 ? void 0 : res.data) !== null && _a !== void 0 ? _a : {};
|
|
185
206
|
var wx = weixinInit();
|
|
186
207
|
wx.config({
|
|
187
208
|
debug: false,
|
|
@@ -193,21 +214,17 @@ function weixinShareConfig(config) {
|
|
|
193
214
|
});
|
|
194
215
|
wx.ready(function (res) {
|
|
195
216
|
console.log("加载微信分享配置完成:", res);
|
|
217
|
+
(0, index_1.useSessionStorage)("weixinShareConfigSuccess", "\u52A0\u8F7D\u5FAE\u4FE1\u5206\u4EAB\u914D\u7F6E\u5B8C\u6210\uFF1A".concat(res));
|
|
196
218
|
if ((config === null || config === void 0 ? void 0 : config.cb) && typeof config.cb === "function")
|
|
197
219
|
config.cb();
|
|
198
220
|
});
|
|
199
221
|
wx.error(function (err) {
|
|
200
|
-
|
|
222
|
+
(0, index_1.useSessionStorage)("weixinShareConfigError", "\u52A0\u8F7D\u5FAE\u4FE1\u5206\u4EAB\u914D\u7F6E\u9519\u8BEF\uFF1A".concat(err));
|
|
201
223
|
});
|
|
202
224
|
}
|
|
203
225
|
});
|
|
204
226
|
}
|
|
205
227
|
exports.weixinShareConfig = weixinShareConfig;
|
|
206
|
-
var defaultJsApiList = [
|
|
207
|
-
'updateAppMessageShareData', 'updateTimelineShareData', "onMenuShareWeibo", "onMenuShareQZone", "startRecord", "stopRecord", "closeWindow", "scanQRCode", "chooseWXPay", "chooseCard", "openCard",
|
|
208
|
-
"onVoiceRecordEnd", "playVoice", "pauseVoice", "stopVoice", "onVoicePlayEnd", "uploadVoice", "downloadVoice", "chooseImage", "previewImage", "uploadImage", "openProductSpecificView", "addCard",
|
|
209
|
-
"downloadImage", "translateVoice", "getNetworkType", "openLocation", "getLocation", "hideOptionMenu", "showOptionMenu", "hideMenuItems", "showMenuItems", "hideAllNonBaseMenuItem", "showAllNonBaseMenuItem",
|
|
210
|
-
];
|
|
211
228
|
function weixinShareInit(options) {
|
|
212
229
|
var title = "微信分享";
|
|
213
230
|
if (options === null || options === void 0 ? void 0 : options.title)
|
|
@@ -230,9 +247,9 @@ function weixinShareInit(options) {
|
|
|
230
247
|
}
|
|
231
248
|
}
|
|
232
249
|
var wx = weixinInit();
|
|
233
|
-
|
|
234
|
-
(0, index_1.useSessionStorage)("shareUrl", link);
|
|
250
|
+
(0, index_1.useSessionStorage)("weixinShareUrl", link);
|
|
235
251
|
wx.ready(function () {
|
|
252
|
+
(0, index_1.useSessionStorage)("weixinShareInit", "success");
|
|
236
253
|
//需在用户可能点击分享按钮前就先调用
|
|
237
254
|
wx.updateAppMessageShareData({
|
|
238
255
|
title: title,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "xctc-utils",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
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",
|
|
@@ -12,6 +12,7 @@
|
|
|
12
12
|
"author": "",
|
|
13
13
|
"license": "ISC",
|
|
14
14
|
"devDependencies": {
|
|
15
|
+
"@types/crypto-js": "^4.1.1",
|
|
15
16
|
"@types/node": "^18.11.19",
|
|
16
17
|
"ts-node": "^10.9.1",
|
|
17
18
|
"typescript": "^4.9.5"
|
|
@@ -24,5 +25,13 @@
|
|
|
24
25
|
"type": "git",
|
|
25
26
|
"url": "git@gitee.com:npm-management/xctc-utils.git"
|
|
26
27
|
},
|
|
27
|
-
"keywords": [
|
|
28
|
+
"keywords": [
|
|
29
|
+
"localStorage",
|
|
30
|
+
"sessionStorage",
|
|
31
|
+
"crypto-js",
|
|
32
|
+
"微信授权登录",
|
|
33
|
+
"weixin-js-sdk",
|
|
34
|
+
"微信H5",
|
|
35
|
+
"时间戳时间字符串互转"
|
|
36
|
+
]
|
|
28
37
|
}
|