xctc-utils 1.3.2 → 1.3.4

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,36 +12,37 @@ var defaultKey = "5uMz4R8rop26DkC8";
12
12
  var defaultIv = "5uMz4Rsd0926DkC8";
13
13
  var weixinConfig = {};
14
14
  function weixinUrlCode(config) {
15
- var _a, _b, _c, _d;
16
- var appId = (_a = config === null || config === void 0 ? void 0 : config.appId) !== null && _a !== void 0 ? _a : "";
15
+ config = config || {};
16
+ var appId = config['appId'] || "";
17
17
  if (!appId)
18
18
  return;
19
- weixinConfig = config || {};
20
- var scope = (_b = config === null || config === void 0 ? void 0 : config.scope) !== null && _b !== void 0 ? _b : "snsapi_userinfo";
21
- var codeKey = (_c = config === null || config === void 0 ? void 0 : config.codeKey) !== null && _c !== void 0 ? _c : "weixinCode";
22
- var stateKey = (_d = config === null || config === void 0 ? void 0 : config.stateKey) !== null && _d !== void 0 ? _d : "weixinState";
19
+ weixinConfig = config;
20
+ var scope = config['scope'] || "snsapi_userinfo";
21
+ var codeKey = config['codeKey'] || "weixinCode";
22
+ var stateKey = config['stateKey'] || "weixinState";
23
23
  weixinConfig['scope'] = scope;
24
24
  weixinConfig['codeKey'] = codeKey;
25
25
  weixinConfig['stateKey'] = stateKey;
26
- weixinConfig['scope'] = scope;
27
26
  (0, index_1.useSessionStorage)("weixinConfig", weixinConfig);
28
- var cryptoiv = weixinConfig.cryptoiv, cryptokey = weixinConfig.cryptokey;
27
+ var _a = weixinConfig.cryptoiv, cryptoiv = _a === void 0 ? "" : _a, _b = weixinConfig.cryptokey, cryptokey = _b === void 0 ? "" : _b;
29
28
  var search = loc.search; // ?code=0916SzFa1KH9TA0Ke7Ha1AQx6446SzFr&state=123
30
29
  // 获取地址栏返回 code 参数
31
30
  var localCode = "";
32
- if (codeKey)
31
+ if (codeKey) {
33
32
  localCode = (0, index_1.getSessionStorage)(codeKey);
33
+ }
34
34
  // 默认第一次通过链接进入系统,没有code和state
35
35
  var searchIndex = -1;
36
36
  var stateIndex = -1;
37
37
  if (search) {
38
38
  searchIndex = search.indexOf("code=");
39
39
  stateIndex = search.indexOf("state=");
40
- searchIndex = parseInt(searchIndex);
41
- stateIndex = parseInt(stateIndex);
42
40
  }
43
- // 如果链接地址中带有code和state
44
- if (searchIndex >= 0 && stateIndex >= 0) {
41
+ if (localCode && stateIndex == -1 && searchIndex == -1) {
42
+ return;
43
+ }
44
+ // 如果链接地址中带有code
45
+ if (searchIndex >= 0) {
45
46
  if (localCode) {
46
47
  // localCode 存在,用户已执行微信跳转链接,用户可能执行当前网页的刷新操作
47
48
  return;
@@ -65,7 +66,7 @@ function weixinUrlCode(config) {
65
66
  return;
66
67
  }
67
68
  // 用户通过分享链接进入系统,此时含有State参数,无code参数
68
- if (stateIndex >= 0 && !(searchIndex >= 0)) {
69
+ if (stateIndex >= 0) {
69
70
  var hrefState = "";
70
71
  var urlArr = formatSearch(search, "state=");
71
72
  for (var k = 0; k < urlArr.length; k++) {
@@ -89,9 +90,7 @@ function weixinUrlCode(config) {
89
90
  weixinCode(); // 微信授权页面获取CODE
90
91
  return;
91
92
  }
92
- if (localCode)
93
- return;
94
- if (!localCode) {
93
+ if (!localCode && stateIndex == -1 && searchIndex == -1) {
95
94
  return weixinCode();
96
95
  }
97
96
  }
@@ -119,14 +118,14 @@ function weixinLogin(code) {
119
118
  return;
120
119
  (0, index_1.useSessionStorage)("weixinLoginParams", JSON.stringify(obj));
121
120
  // 调试字段 isPassLogin ,为true 时不继续执行微信登录,前端可拿到 code 给后端测试
122
- if (weixinConfig === null || weixinConfig === void 0 ? void 0 : weixinConfig.isPassLogin)
121
+ if (weixinConfig['isPassLogin'])
123
122
  return;
124
123
  weixinConfig.http(obj).then(function (res) {
125
124
  if (res.code == 40163) {
126
125
  // 未登录则先执行登录
127
126
  return weixinCode();
128
127
  }
129
- var data = res === null || res === void 0 ? void 0 : res.data;
128
+ var data = res['data'] || {};
130
129
  for (var key in data) {
131
130
  var item = data[key];
132
131
  if (item instanceof Object) {
@@ -197,7 +196,7 @@ function weixinCode() {
197
196
  return;
198
197
  var scope = "snsapi_userinfo";
199
198
  if (weixinConfig['scope']) {
200
- scope = weixinConfig['scope'];
199
+ scope = weixinConfig.scope;
201
200
  }
202
201
  var base = "https://open.weixin.qq.com/connect/oauth2/authorize?";
203
202
  var uri = encodeURIComponent(loc.href.split("?")[0]);
@@ -216,8 +215,7 @@ var defaultJsApiList = [
216
215
  "downloadImage", "translateVoice", "getNetworkType", "openLocation", "getLocation", "hideOptionMenu", "showOptionMenu", "hideMenuItems", "showMenuItems", "hideAllNonBaseMenuItem", "showAllNonBaseMenuItem",
217
216
  ];
218
217
  function weixinShareConfig(config) {
219
- var _a;
220
- if (!(config === null || config === void 0 ? void 0 : config.http))
218
+ if (!config['http'])
221
219
  return;
222
220
  var url = loc.href.split("#")[0];
223
221
  var param = {
@@ -225,12 +223,12 @@ function weixinShareConfig(config) {
225
223
  appId: config.appId,
226
224
  };
227
225
  var jsApiList = defaultJsApiList;
228
- if ((_a = config === null || config === void 0 ? void 0 : config.jsApiList) === null || _a === void 0 ? void 0 : _a.length)
226
+ if (config && config.jsApiList && config.jsApiList.length) {
229
227
  jsApiList = config.jsApiList;
228
+ }
230
229
  config.http(param).then(function (res) {
231
- var _a;
232
230
  if (res.code == 0) {
233
- var data = (_a = res === null || res === void 0 ? void 0 : res.data) !== null && _a !== void 0 ? _a : {};
231
+ var data = res['data'] || {};
234
232
  var wx = weixinInit();
235
233
  wx.config({
236
234
  debug: false,
@@ -243,7 +241,7 @@ function weixinShareConfig(config) {
243
241
  wx.ready(function (res) {
244
242
  console.log("加载微信分享配置完成:", res);
245
243
  (0, index_1.useSessionStorage)("weixinShareConfigSuccess", "\u52A0\u8F7D\u5FAE\u4FE1\u5206\u4EAB\u914D\u7F6E\u5B8C\u6210\uFF1A".concat(res));
246
- if ((config === null || config === void 0 ? void 0 : config.cb) && typeof config.cb === "function")
244
+ if (config['cb'] && typeof config.cb === "function")
247
245
  config.cb();
248
246
  });
249
247
  wx.error(function (err) {
@@ -254,23 +252,26 @@ function weixinShareConfig(config) {
254
252
  }
255
253
  exports.weixinShareConfig = weixinShareConfig;
256
254
  function weixinShareInit(options) {
257
- var title = "微信分享";
258
- if (options === null || options === void 0 ? void 0 : options.title)
255
+ var title = "微信分享标题";
256
+ if (options['title'])
259
257
  title = options.title;
260
258
  var desc = "微信分享描述";
261
- if (options === null || options === void 0 ? void 0 : options.desc)
259
+ if (options['desc'])
262
260
  desc = options.desc;
263
261
  var link = loc.origin;
264
- if (options === null || options === void 0 ? void 0 : options.link)
262
+ if (options['link'])
265
263
  link = options.link;
266
264
  var imgUrl = "";
267
- if (options === null || options === void 0 ? void 0 : options.imgUrl)
265
+ if (options['imgUrl'])
268
266
  imgUrl = options.imgUrl;
269
267
  if (options && typeof options == "object") {
270
268
  var state = "";
271
- if (options === null || options === void 0 ? void 0 : options.data) {
269
+ if (options['data']) {
272
270
  var data = options.data;
273
- state = (0, crypto_1.encrypt)(data, cryptoConfig(options['key']).key, cryptoConfig(options['iv']).iv);
271
+ var key = options['key'] || defaultKey;
272
+ var iv = options['iv'] || defaultIv;
273
+ var cryptoObj = cryptoConfig(key, iv);
274
+ state = (0, crypto_1.encrypt)(data, cryptoObj.key, cryptoObj.iv);
274
275
  link = "".concat(link, "?state=").concat(state);
275
276
  }
276
277
  }
@@ -314,7 +315,7 @@ function weixinPay(config) {
314
315
  var wx = weixinInit();
315
316
  // let { timeStamp,signType,prepay_id,paySign,package,nonceStr,appId } = params
316
317
  var cb = "";
317
- if ((config === null || config === void 0 ? void 0 : config.cb) && typeof config.cb === "function")
318
+ if (config['cb'] && typeof config.cb === "function")
318
319
  cb = config.cb;
319
320
  wx.chooseWXPay({
320
321
  timestamp: config.timeStamp,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xctc-utils",
3
- "version": "1.3.2",
3
+ "version": "1.3.4",
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",