xctc-utils 1.3.1 → 1.3.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.
@@ -12,30 +12,38 @@ var defaultKey = "5uMz4R8rop26DkC8";
12
12
  var defaultIv = "5uMz4Rsd0926DkC8";
13
13
  var weixinConfig = {};
14
14
  function weixinUrlCode(config) {
15
- weixinConfig = config || {};
15
+ var _a, _b, _c, _d;
16
+ config = config !== null && config !== void 0 ? config : {};
17
+ var appId = (_a = config === null || config === void 0 ? void 0 : config.appId) !== null && _a !== void 0 ? _a : "";
18
+ if (!appId)
19
+ return;
20
+ weixinConfig = config;
21
+ var scope = (_b = config === null || config === void 0 ? void 0 : config.scope) !== null && _b !== void 0 ? _b : "snsapi_userinfo";
22
+ var codeKey = (_c = config === null || config === void 0 ? void 0 : config.codeKey) !== null && _c !== void 0 ? _c : "weixinCode";
23
+ var stateKey = (_d = config === null || config === void 0 ? void 0 : config.stateKey) !== null && _d !== void 0 ? _d : "weixinState";
24
+ weixinConfig['scope'] = scope;
25
+ weixinConfig['codeKey'] = codeKey;
26
+ weixinConfig['stateKey'] = stateKey;
16
27
  (0, index_1.useSessionStorage)("weixinConfig", weixinConfig);
17
- var cryptoiv = weixinConfig.cryptoiv, cryptokey = weixinConfig.cryptokey;
28
+ var _e = weixinConfig.cryptoiv, cryptoiv = _e === void 0 ? "" : _e, _f = weixinConfig.cryptokey, cryptokey = _f === void 0 ? "" : _f;
18
29
  var search = loc.search; // ?code=0916SzFa1KH9TA0Ke7Ha1AQx6446SzFr&state=123
19
30
  // 获取地址栏返回 code 参数
20
- var codeKey = weixinConfig['codeKey'];
21
- if (!codeKey) {
22
- codeKey = "weixinCode";
23
- weixinConfig['codeKey'] = codeKey;
24
- }
25
31
  var localCode = "";
26
- if (codeKey)
32
+ if (codeKey) {
27
33
  localCode = (0, index_1.getSessionStorage)(codeKey);
34
+ }
28
35
  // 默认第一次通过链接进入系统,没有code和state
29
36
  var searchIndex = -1;
30
37
  var stateIndex = -1;
31
38
  if (search) {
32
39
  searchIndex = search.indexOf("code=");
33
40
  stateIndex = search.indexOf("state=");
34
- searchIndex = parseInt(searchIndex);
35
- stateIndex = parseInt(stateIndex);
36
41
  }
37
- // 如果链接地址中带有code和state
38
- if (searchIndex >= 0 && stateIndex >= 0) {
42
+ if (localCode && stateIndex == -1 && searchIndex == -1) {
43
+ return;
44
+ }
45
+ // 如果链接地址中带有code
46
+ if (searchIndex >= 0) {
39
47
  if (localCode) {
40
48
  // localCode 存在,用户已执行微信跳转链接,用户可能执行当前网页的刷新操作
41
49
  return;
@@ -59,33 +67,31 @@ function weixinUrlCode(config) {
59
67
  return;
60
68
  }
61
69
  // 用户通过分享链接进入系统,此时含有State参数,无code参数
62
- if (stateIndex >= 0 && !(searchIndex >= 0)) {
70
+ if (stateIndex >= 0) {
63
71
  var hrefState = "";
64
72
  var urlArr = formatSearch(search, "state=");
65
73
  for (var k = 0; k < urlArr.length; k++) {
66
74
  var item = urlArr[k];
67
75
  if (item.indexOf("state=") != -1) {
68
76
  hrefState = item.substr(6);
69
- var stateKey = weixinConfig['stateKey'];
70
- if (!stateKey) {
71
- stateKey = "weixinState";
72
- weixinConfig['stateKey'] = stateKey;
77
+ var stateKey_1 = weixinConfig['stateKey'];
78
+ if (!stateKey_1) {
79
+ stateKey_1 = "weixinState";
80
+ weixinConfig['stateKey'] = stateKey_1;
73
81
  }
74
- (0, index_1.useSessionStorage)(stateKey, hrefState);
82
+ (0, index_1.useSessionStorage)(stateKey_1, hrefState);
75
83
  var cryptoObj = cryptoConfig(cryptokey, cryptoiv);
76
84
  var stateObj = (0, crypto_1.decrypt)(hrefState, cryptoObj.key, cryptoObj.iv);
77
85
  if (stateObj instanceof Object)
78
86
  stateObj = JSON.stringify(stateObj);
79
- (0, index_1.useSessionStorage)("".concat(stateKey, "Obj"), stateObj);
87
+ (0, index_1.useSessionStorage)("".concat(stateKey_1, "Obj"), stateObj);
80
88
  }
81
89
  }
82
90
  // 将 state进行本地存储,存储完成后跳转到微信授权页面
83
91
  weixinCode(); // 微信授权页面获取CODE
84
92
  return;
85
93
  }
86
- if (localCode)
87
- return;
88
- if (!localCode) {
94
+ if (!localCode && stateIndex == -1 && searchIndex == -1) {
89
95
  return weixinCode();
90
96
  }
91
97
  }
@@ -130,15 +136,10 @@ function weixinLogin(code) {
130
136
  }
131
137
  var url = loc.origin;
132
138
  var hashVal = loc.hash;
133
- (0, index_1.useSessionStorage)("stateKey===", stateKey);
134
- var state = (0, index_1.getSessionStorage)(stateKey) || "";
139
+ var state = (0, index_1.getSessionStorage)(stateKey);
135
140
  var str = "";
136
- (0, index_1.useSessionStorage)("getstatedata===", state);
137
141
  if (state) {
138
- (0, index_1.useSessionStorage)("stateKey===", "".concat(stateKey, "Obj"));
139
142
  var stateObj = (0, index_1.getSessionStorage)("".concat(stateKey, "Obj"));
140
- (0, index_1.useSessionStorage)("getstateObj===", stateObj);
141
- (0, index_1.useSessionStorage)("url11111===", url);
142
143
  for (var key in stateObj) {
143
144
  if (key == "path") {
144
145
  if (stateObj[key][0] == "/") {
@@ -147,7 +148,6 @@ function weixinLogin(code) {
147
148
  else {
148
149
  url = "".concat(url, "/#/").concat(stateObj.path);
149
150
  }
150
- (0, index_1.useSessionStorage)("url22222===", url);
151
151
  }
152
152
  else {
153
153
  str += "".concat(key, "=").concat(stateObj[key], "&");
@@ -158,7 +158,6 @@ function weixinLogin(code) {
158
158
  url = "".concat(url, "?").concat(str);
159
159
  }
160
160
  }
161
- (0, index_1.useSessionStorage)("url333333===", url);
162
161
  loc.replace(url);
163
162
  }
164
163
  else {
@@ -193,16 +192,11 @@ function weixinInit() {
193
192
  return wx;
194
193
  }
195
194
  function weixinCode() {
195
+ var _a;
196
196
  if ((0, utils_1.weixinBrowser)()) {
197
197
  if (!weixinConfig['appId'])
198
198
  return;
199
- var scope = "snsapi_userinfo";
200
- if (weixinConfig['scope']) {
201
- scope = weixinConfig['scope'];
202
- }
203
- else {
204
- weixinConfig['scope'] = scope;
205
- }
199
+ var scope = (_a = weixinConfig === null || weixinConfig === void 0 ? void 0 : weixinConfig.scope) !== null && _a !== void 0 ? _a : "snsapi_userinfo";
206
200
  var base = "https://open.weixin.qq.com/connect/oauth2/authorize?";
207
201
  var uri = encodeURIComponent(loc.href.split("?")[0]);
208
202
  var config = "appid=".concat(weixinConfig.appId, "&redirect_uri=").concat(uri, "&response_type=code&scope=").concat(scope, "&state=state#wechat_redirect");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xctc-utils",
3
- "version": "1.3.1",
3
+ "version": "1.3.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",