xctc-utils 1.1.6 → 1.1.9

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 CHANGED
@@ -3,6 +3,7 @@
3
3
  #### 安装 npm i xctc-utils
4
4
  #### 项目中引入 import utils from "xctc-utils"
5
5
 
6
+
6
7
  #### LocalStorage使用,存取值时数据已经过处理
7
8
  ```
8
9
 
@@ -25,6 +26,7 @@
25
26
 
26
27
  当前使用设备类型: utils.deviceEnvironment() // android ios
27
28
  是否在微信浏览器环境中: utils.weixinBrowser() // true false
29
+ 是否是手持设备: utils.isMobile() // true 移动设备 false PC设备
28
30
 
29
31
  ```
30
32
 
@@ -1,36 +1,16 @@
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;
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 CryptoJS = __importStar(require("crypto-js"));
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 CryptoJS.enc.Utf8.parse(key);
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 = CryptoJS.enc.Utf8.parse(word);
22
+ var src = crypto_js_1.default.enc.Utf8.parse(word);
43
23
  key = formatVal(key);
44
24
  iv = formatVal(iv);
45
- var encrypted = CryptoJS.AES.encrypt(src, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 });
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 = CryptoJS.enc.Hex.parse(word);
55
- var src = CryptoJS.enc.Base64.stringify(encryptedHexStr);
56
- var decrypt = CryptoJS.AES.decrypt(src, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 });
57
- var decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
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/index.js CHANGED
@@ -39,5 +39,6 @@ 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 obj = __assign(__assign(__assign(__assign(__assign({}, storage), utils), weixin), crypto), time);
42
+ var mobile = __importStar(require("./mobile"));
43
+ var obj = __assign(__assign(__assign(__assign(__assign(__assign({}, storage), utils), weixin), crypto), time), mobile);
43
44
  exports.default = obj;
@@ -0,0 +1 @@
1
+ export declare function isMobile(): boolean;
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isMobile = void 0;
4
+ var w = window;
5
+ function isMobile() {
6
+ var info = w.navigator.userAgent;
7
+ var isPhone = /mobile/i.test(info);
8
+ return isPhone;
9
+ }
10
+ exports.isMobile = isMobile;
@@ -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;
@@ -68,6 +68,8 @@ function isExist(str, key) {
68
68
  * @returns
69
69
  */
70
70
  function formatStrTime(str) {
71
+ if (!str)
72
+ return;
71
73
  var timestamp = new Date(str).getTime();
72
74
  timestamp = timestamp / 1000;
73
75
  return parseInt(timestamp);
@@ -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
- weixinLogin(code);
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
- // 将进入系统所携带的state参数传到微信登录链接,微信跳转登录后原样返回
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 cryptoiv = weixinConfig.cryptoiv, cryptokey = weixinConfig.cryptokey, appId = weixinConfig.appId, http = weixinConfig.http, stateKey = weixinConfig.stateKey;
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,8 @@ 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
+ (0, index_1.useSessionStorage)("weixinLoginstatus", "success");
90
97
  for (var key in data) {
91
98
  var item = data[key];
92
99
  if (item instanceof Object) {
@@ -167,6 +174,11 @@ function weixinCode() {
167
174
  console.log("非微信内核浏览器执行其他业务");
168
175
  }
169
176
  }
177
+ var defaultJsApiList = [
178
+ 'updateAppMessageShareData', 'updateTimelineShareData', "onMenuShareWeibo", "onMenuShareQZone", "startRecord", "stopRecord", "closeWindow", "scanQRCode", "chooseWXPay", "chooseCard", "openCard",
179
+ "onVoiceRecordEnd", "playVoice", "pauseVoice", "stopVoice", "onVoicePlayEnd", "uploadVoice", "downloadVoice", "chooseImage", "previewImage", "uploadImage", "openProductSpecificView", "addCard",
180
+ "downloadImage", "translateVoice", "getNetworkType", "openLocation", "getLocation", "hideOptionMenu", "showOptionMenu", "hideMenuItems", "showMenuItems", "hideAllNonBaseMenuItem", "showAllNonBaseMenuItem",
181
+ ];
170
182
  function weixinShareConfig(config) {
171
183
  var _a;
172
184
  if (!(config === null || config === void 0 ? void 0 : config.http))
@@ -177,11 +189,12 @@ function weixinShareConfig(config) {
177
189
  appId: config.appId,
178
190
  };
179
191
  var jsApiList = defaultJsApiList;
180
- if ((_a = config.jsApiList) === null || _a === void 0 ? void 0 : _a.length)
192
+ if ((_a = config === null || config === void 0 ? void 0 : config.jsApiList) === null || _a === void 0 ? void 0 : _a.length)
181
193
  jsApiList = config.jsApiList;
182
194
  config.http(param).then(function (res) {
195
+ var _a;
183
196
  if (res.code == 0) {
184
- var data = res.data;
197
+ var data = (_a = res === null || res === void 0 ? void 0 : res.data) !== null && _a !== void 0 ? _a : {};
185
198
  var wx = weixinInit();
186
199
  wx.config({
187
200
  debug: false,
@@ -193,21 +206,17 @@ function weixinShareConfig(config) {
193
206
  });
194
207
  wx.ready(function (res) {
195
208
  console.log("加载微信分享配置完成:", res);
209
+ (0, index_1.useSessionStorage)("weixinShareConfigSuccess", "\u52A0\u8F7D\u5FAE\u4FE1\u5206\u4EAB\u914D\u7F6E\u5B8C\u6210\uFF1A".concat(res));
196
210
  if ((config === null || config === void 0 ? void 0 : config.cb) && typeof config.cb === "function")
197
211
  config.cb();
198
212
  });
199
213
  wx.error(function (err) {
200
- console.log("加载微信分享配置错误:", err);
214
+ (0, index_1.useSessionStorage)("weixinShareConfigError", "\u52A0\u8F7D\u5FAE\u4FE1\u5206\u4EAB\u914D\u7F6E\u9519\u8BEF\uFF1A".concat(err));
201
215
  });
202
216
  }
203
217
  });
204
218
  }
205
219
  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
220
  function weixinShareInit(options) {
212
221
  var title = "微信分享";
213
222
  if (options === null || options === void 0 ? void 0 : options.title)
@@ -230,9 +239,9 @@ function weixinShareInit(options) {
230
239
  }
231
240
  }
232
241
  var wx = weixinInit();
233
- // console.log("shareUrl===",link)
234
- (0, index_1.useSessionStorage)("shareUrl", link);
242
+ (0, index_1.useSessionStorage)("weixinShareUrl", link);
235
243
  wx.ready(function () {
244
+ (0, index_1.useSessionStorage)("weixinShareInit", "success");
236
245
  //需在用户可能点击分享按钮前就先调用
237
246
  wx.updateAppMessageShareData({
238
247
  title: title,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xctc-utils",
3
- "version": "1.1.6",
3
+ "version": "1.1.9",
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": [ "localStorage","sessionStorage","crypto-js","微信授权登录","weixin-js-sdk","微信H5","时间戳时间字符串互转" ]
28
+ "keywords": [
29
+ "localStorage",
30
+ "sessionStorage",
31
+ "crypto-js",
32
+ "微信授权登录",
33
+ "weixin-js-sdk",
34
+ "微信H5",
35
+ "时间戳时间字符串互转"
36
+ ]
28
37
  }