xctc-utils 1.1.3 → 1.1.5
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/weixin/index.js +27 -20
- package/package.json +1 -1
package/dist/weixin/index.js
CHANGED
|
@@ -5,17 +5,21 @@ exports.weixinPay = exports.weixinShareInit = exports.weixinShareConfig = export
|
|
|
5
5
|
var weixin = require("weixin-js-sdk");
|
|
6
6
|
var utils_1 = require("../utils");
|
|
7
7
|
var crypto_1 = require("../crypto");
|
|
8
|
+
var index_1 = require("../storage/index");
|
|
8
9
|
var w = window;
|
|
9
10
|
var loc = w.location;
|
|
11
|
+
var defaultKey = "5uMz4R8rop26DkC8";
|
|
12
|
+
var defaultIv = "5uMz4Rsd0926DkC8";
|
|
10
13
|
var weixinConfig = {};
|
|
11
14
|
function weixinUrlCode(config) {
|
|
12
|
-
weixinConfig = config;
|
|
15
|
+
weixinConfig = config || {};
|
|
16
|
+
var cryptoiv = weixinConfig.cryptoiv, cryptokey = weixinConfig.cryptokey;
|
|
13
17
|
var search = loc.search; // ?code=0916SzFa1KH9TA0Ke7Ha1AQx6446SzFr&state=123
|
|
14
18
|
// 获取地址栏返回 code 参数
|
|
15
19
|
var codeKey = weixinConfig['codeKey'];
|
|
16
20
|
var localCode = "";
|
|
17
21
|
if (codeKey)
|
|
18
|
-
localCode =
|
|
22
|
+
localCode = (0, index_1.getSessionStorage)(codeKey);
|
|
19
23
|
if (!localCode && search && search.indexOf("code=") != -1) {
|
|
20
24
|
// localCode 不存在, 微信链接中存在 code 用户初次登录系统,
|
|
21
25
|
var urlArr = formatSearch(search, "code=");
|
|
@@ -25,7 +29,7 @@ function weixinUrlCode(config) {
|
|
|
25
29
|
if (item.indexOf("code=") != -1) {
|
|
26
30
|
code = item.substr(5);
|
|
27
31
|
if (codeKey)
|
|
28
|
-
|
|
32
|
+
(0, index_1.useSessionStorage)(codeKey, code);
|
|
29
33
|
}
|
|
30
34
|
}
|
|
31
35
|
weixinLogin(code);
|
|
@@ -43,8 +47,11 @@ function weixinUrlCode(config) {
|
|
|
43
47
|
if (item.indexOf("state=") != -1) {
|
|
44
48
|
hrefState = item.substr(6);
|
|
45
49
|
var stateKey = weixinConfig['stateKey'] || "weixinState";
|
|
46
|
-
|
|
47
|
-
|
|
50
|
+
(0, index_1.useSessionStorage)(stateKey, hrefState);
|
|
51
|
+
var stateObj = (0, crypto_1.decrypt)(hrefState, cryptoConfig(cryptokey).key, cryptoConfig(cryptoiv).iv);
|
|
52
|
+
if (stateObj instanceof Object)
|
|
53
|
+
stateObj = JSON.stringify(stateObj);
|
|
54
|
+
(0, index_1.useSessionStorage)("".concat(stateKey, "Obj"), stateObj);
|
|
48
55
|
}
|
|
49
56
|
}
|
|
50
57
|
}
|
|
@@ -53,12 +60,11 @@ function weixinUrlCode(config) {
|
|
|
53
60
|
}
|
|
54
61
|
}
|
|
55
62
|
exports.weixinUrlCode = weixinUrlCode;
|
|
56
|
-
function cryptoConfig() {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
key = weixinConfig.cryptokey;
|
|
63
|
+
function cryptoConfig(key, iv) {
|
|
64
|
+
if (!key)
|
|
65
|
+
key = defaultKey;
|
|
66
|
+
if (!iv)
|
|
67
|
+
key = defaultIv;
|
|
62
68
|
return {
|
|
63
69
|
key: key,
|
|
64
70
|
iv: iv,
|
|
@@ -66,13 +72,14 @@ function cryptoConfig() {
|
|
|
66
72
|
}
|
|
67
73
|
// 执行微信登录
|
|
68
74
|
function weixinLogin(code) {
|
|
69
|
-
|
|
75
|
+
var cryptoiv = weixinConfig.cryptoiv, cryptokey = weixinConfig.cryptokey, appId = weixinConfig.appId, http = weixinConfig.http, stateKey = weixinConfig.stateKey;
|
|
76
|
+
if (!appId)
|
|
70
77
|
return;
|
|
71
78
|
var obj = {
|
|
72
|
-
"app_id":
|
|
79
|
+
"app_id": appId,
|
|
73
80
|
"js_code": code
|
|
74
81
|
};
|
|
75
|
-
if (!
|
|
82
|
+
if (!http)
|
|
76
83
|
return;
|
|
77
84
|
weixinConfig.http(obj).then(function (res) {
|
|
78
85
|
if (res.code == 40163) {
|
|
@@ -85,13 +92,13 @@ function weixinLogin(code) {
|
|
|
85
92
|
if (item instanceof Object) {
|
|
86
93
|
item = JSON.stringify(item);
|
|
87
94
|
}
|
|
88
|
-
|
|
95
|
+
(0, index_1.useSessionStorage)(key, item);
|
|
89
96
|
}
|
|
90
97
|
var url = loc.origin;
|
|
91
|
-
var state =
|
|
98
|
+
var state = (0, index_1.getSessionStorage)(stateKey);
|
|
92
99
|
var str = "";
|
|
93
100
|
if (state) {
|
|
94
|
-
var stateObj = (0,
|
|
101
|
+
var stateObj = (0, index_1.getSessionStorage)("".concat(stateKey, "Obj"));
|
|
95
102
|
for (var key in stateObj) {
|
|
96
103
|
if (key == "path") {
|
|
97
104
|
if (stateObj[key][0] == "/") {
|
|
@@ -208,7 +215,7 @@ function weixinShareInit(options) {
|
|
|
208
215
|
var desc = "微信分享描述";
|
|
209
216
|
if (options === null || options === void 0 ? void 0 : options.desc)
|
|
210
217
|
desc = options.desc;
|
|
211
|
-
var link = loc.
|
|
218
|
+
var link = loc.origin;
|
|
212
219
|
if (options === null || options === void 0 ? void 0 : options.link)
|
|
213
220
|
link = options.link;
|
|
214
221
|
var imgUrl = "";
|
|
@@ -218,13 +225,13 @@ function weixinShareInit(options) {
|
|
|
218
225
|
var state = "";
|
|
219
226
|
if (options === null || options === void 0 ? void 0 : options.data) {
|
|
220
227
|
var data = options.data;
|
|
221
|
-
state = (0, crypto_1.encrypt)(data, options['key'], options['iv']);
|
|
228
|
+
state = (0, crypto_1.encrypt)(data, cryptoConfig(options['key']).key, cryptoConfig(options['iv']).iv);
|
|
222
229
|
link = "".concat(link, "?state=").concat(state);
|
|
223
230
|
}
|
|
224
231
|
}
|
|
225
232
|
var wx = weixinInit();
|
|
226
233
|
// console.log("shareUrl===",link)
|
|
227
|
-
|
|
234
|
+
(0, index_1.useSessionStorage)("shareUrl", link);
|
|
228
235
|
wx.ready(function () {
|
|
229
236
|
//需在用户可能点击分享按钮前就先调用
|
|
230
237
|
wx.updateAppMessageShareData({
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "xctc-utils",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.5",
|
|
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",
|