xctc-utils 1.3.3 → 1.3.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/README.md CHANGED
@@ -3,13 +3,20 @@
3
3
  #### 安装 npm i xctc-utils
4
4
  #### 项目中引入 import utils from "xctc-utils"
5
5
 
6
-
6
+ #### 类型判断
7
+ ```
8
+ utils.type.isFunction(val) // 是否是函数
9
+ utils.type.isObject(val) // 是否是对象 null 类型为对象,该方法已对null进行过滤
10
+ utils.type.isDate(val) // 是否是时间对象
11
+ utils.type.isNumber(val) // 是否是number类型
12
+ utils.type.isString(val) // 是否是字符串类型
13
+ utils.type.isBoolean(val) // 是否是boolean类型
14
+ utils.type.isWindow(val) // 是否在浏览器环境下运行
15
+ ```
7
16
  #### LocalStorage使用,存取值时数据已经过处理
8
17
  ```
9
-
10
18
  存储: utils.useLocalStorage(key,value)
11
19
  取值: utils.getLocalStorage(key)
12
-
13
20
  ```
14
21
 
15
22
  #### sessionStorage使用,存取值时数据已经过处理
@@ -18,6 +25,15 @@
18
25
  存储: utils.useSessionStorage(key,value)
19
26
  取值: utils.getSessionStorage(key)
20
27
 
28
+ ```
29
+ #### 删除缓存
30
+ ```
31
+ key:string 需要删除的缓存键
32
+ isAll:boolean 为true时删除调用方法的所有缓存,否则只删除当前传入的 key 值
33
+ 如果 isAll 为 true 时,方法会优先执行缓存的clear函数, key 传任意值,
34
+ 删除临时缓存: utils.removeSessionStorage(key,isAll)
35
+ 删除永久缓存: utils.removeLocalStorage(key,isAll)
36
+
21
37
  ```
22
38
 
23
39
  #### 获取当前设备环境
@@ -31,13 +47,15 @@
31
47
  ```
32
48
 
33
49
 
34
- #### 加密解密方法
50
+ #### crypto-js 加密解密方法
35
51
  ```
36
52
  word: 需要加密的数据,数据可是字符串,对象等
37
53
  key: 加密 密钥
38
54
  iv: 密钥偏移量
39
55
  对同一个数据进行加密、解密时,encrypt和decrypt的iv和key保持一致
40
- 对数据进行加密
56
+ 对word进行CryptoJS.enc.Utf8.parse转义
57
+ CryptoJS.AES.encrypt加密,加密模式:CBC ; padding: CryptoJS.pad.Pkcs7
58
+ 对数据进行加密
41
59
  utils.encrypt(word:any,key:any,iv:any)
42
60
  // 对数据进行解密
43
61
  utils.decrypt(word:any,key:any,iv:any)
@@ -0,0 +1,2 @@
1
+ export declare function makePhone(tel: string): void;
2
+ export declare function formatMobile(mobile: string): string | undefined;
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.formatMobile = exports.makePhone = void 0;
4
+ // h5页面拨打电话
5
+ function makePhone(tel) {
6
+ if (tel) {
7
+ window.location.href = "tel://".concat(tel);
8
+ }
9
+ }
10
+ exports.makePhone = makePhone;
11
+ function formatMobile(mobile) {
12
+ if (!mobile)
13
+ return;
14
+ return mobile.replace(/(\d{3})\d{4}(\d{4})/, "$1****$2");
15
+ }
16
+ exports.formatMobile = formatMobile;
package/dist/index.js CHANGED
@@ -40,5 +40,7 @@ var crypto = __importStar(require("./crypto"));
40
40
  var weixin = __importStar(require("./weixin"));
41
41
  var time = __importStar(require("./time"));
42
42
  var mobile = __importStar(require("./mobile"));
43
- var obj = __assign(__assign(__assign(__assign(__assign(__assign({}, storage), utils), weixin), crypto), time), mobile);
43
+ var format = __importStar(require("./format"));
44
+ var is = __importStar(require("./is"));
45
+ var obj = __assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign({}, storage), utils), weixin), crypto), time), mobile), format), { type: __assign({}, is) });
44
46
  exports.default = obj;
@@ -0,0 +1,36 @@
1
+ /**
2
+ * @description: 判断传入的值是否为指定类型
3
+ */
4
+ export declare function is(val: unknown, type: string): boolean;
5
+ /**
6
+ * @description: 是否为函数
7
+ */
8
+ export declare function isFunction<T = Function>(val: unknown): val is T;
9
+ /**
10
+ * @description: 是否为对象
11
+ */
12
+ export declare const isObject: (val: any) => val is Record<any, any>;
13
+ /**
14
+ * @description: 是否为时间对象
15
+ */
16
+ export declare function isDate(val: unknown): val is Date;
17
+ /**
18
+ * @description: 是否为number类型
19
+ */
20
+ export declare function isNumber(val: unknown): val is number;
21
+ /**
22
+ * @description: 是否为字符串
23
+ */
24
+ export declare function isString(val: unknown): val is string;
25
+ /**
26
+ * @description: 是否为boolean类型
27
+ */
28
+ export declare function isBoolean(val: unknown): val is boolean;
29
+ /**
30
+ * @description: 是否为数组
31
+ */
32
+ export declare function isArray(val: any): val is Array<any>;
33
+ /**
34
+ * @description: 是否为浏览器
35
+ */
36
+ export declare const isWindow: (val: any) => val is Window;
@@ -0,0 +1,73 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isWindow = exports.isArray = exports.isBoolean = exports.isString = exports.isNumber = exports.isDate = exports.isObject = exports.isFunction = exports.is = void 0;
4
+ var toString = Object.prototype.toString;
5
+ /**
6
+ * @description: 判断传入的值是否为指定类型
7
+ */
8
+ function is(val, type) {
9
+ if (!type)
10
+ return false;
11
+ return toString.call(val) === "[object ".concat(type, "]");
12
+ }
13
+ exports.is = is;
14
+ /**
15
+ * @description: 是否为函数
16
+ */
17
+ function isFunction(val) {
18
+ if (!val)
19
+ return false;
20
+ return is(val, "Function");
21
+ }
22
+ exports.isFunction = isFunction;
23
+ /**
24
+ * @description: 是否为对象
25
+ */
26
+ var isObject = function (val) {
27
+ return val !== null && is(val, "Object");
28
+ };
29
+ exports.isObject = isObject;
30
+ /**
31
+ * @description: 是否为时间对象
32
+ */
33
+ function isDate(val) {
34
+ if (!val)
35
+ return false;
36
+ return is(val, "Date");
37
+ }
38
+ exports.isDate = isDate;
39
+ /**
40
+ * @description: 是否为number类型
41
+ */
42
+ function isNumber(val) {
43
+ return is(val, "Number");
44
+ }
45
+ exports.isNumber = isNumber;
46
+ /**
47
+ * @description: 是否为字符串
48
+ */
49
+ function isString(val) {
50
+ return is(val, "String");
51
+ }
52
+ exports.isString = isString;
53
+ /**
54
+ * @description: 是否为boolean类型
55
+ */
56
+ function isBoolean(val) {
57
+ return is(val, "Boolean");
58
+ }
59
+ exports.isBoolean = isBoolean;
60
+ /**
61
+ * @description: 是否为数组
62
+ */
63
+ function isArray(val) {
64
+ return val && Array.isArray(val);
65
+ }
66
+ exports.isArray = isArray;
67
+ /**
68
+ * @description: 是否为浏览器
69
+ */
70
+ var isWindow = function (val) {
71
+ return typeof window !== "undefined" && is(val, "Window");
72
+ };
73
+ exports.isWindow = isWindow;
@@ -2,3 +2,5 @@ export declare function useLocalStorage(key: string, value: any): false | undefi
2
2
  export declare function getLocalStorage(key: string): any;
3
3
  export declare function useSessionStorage(key: string, value: any): false | undefined;
4
4
  export declare function getSessionStorage(key: string): any;
5
+ export declare function removeSessionStorage(key?: string, isAll?: boolean): void;
6
+ export declare function removeLocalStorage(key?: string, isAll?: boolean): void;
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getSessionStorage = exports.useSessionStorage = exports.getLocalStorage = exports.useLocalStorage = void 0;
3
+ exports.removeLocalStorage = exports.removeSessionStorage = exports.getSessionStorage = exports.useSessionStorage = exports.getLocalStorage = exports.useLocalStorage = void 0;
4
+ var is_1 = require("../is");
4
5
  var wls = window.localStorage;
5
6
  var wss = window.sessionStorage;
6
7
  var utils_1 = require("../utils");
@@ -50,3 +51,26 @@ function getStorage(key, obj) {
50
51
  val = data;
51
52
  return val;
52
53
  }
54
+ function removeSessionStorage(key, isAll) {
55
+ if ((0, is_1.isBoolean)(isAll) && isAll) {
56
+ wss.clear();
57
+ return;
58
+ }
59
+ if (!key)
60
+ return;
61
+ removeStorage(key, wss);
62
+ }
63
+ exports.removeSessionStorage = removeSessionStorage;
64
+ function removeLocalStorage(key, isAll) {
65
+ if ((0, is_1.isBoolean)(isAll) && isAll) {
66
+ wls.clear();
67
+ return;
68
+ }
69
+ if (!key)
70
+ return;
71
+ removeStorage(key, wls);
72
+ }
73
+ exports.removeLocalStorage = removeLocalStorage;
74
+ function removeStorage(key, obj) {
75
+ obj.removeItem(key);
76
+ }
@@ -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 | undefined;
14
+ export declare function formatStrTime(str: string): any;
@@ -69,7 +69,7 @@ function isExist(str, key) {
69
69
  */
70
70
  function formatStrTime(str) {
71
71
  if (!str)
72
- return;
72
+ return "";
73
73
  var timestamp = new Date(str).getTime();
74
74
  timestamp = timestamp / 1000;
75
75
  return parseInt(timestamp);
@@ -12,20 +12,19 @@ var defaultKey = "5uMz4R8rop26DkC8";
12
12
  var defaultIv = "5uMz4Rsd0926DkC8";
13
13
  var weixinConfig = {};
14
14
  function weixinUrlCode(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 : "";
15
+ config = config || {};
16
+ var appId = config['appId'] || "";
18
17
  if (!appId)
19
18
  return;
20
19
  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";
20
+ var scope = config['scope'] || "snsapi_userinfo";
21
+ var codeKey = config['codeKey'] || "weixinCode";
22
+ var stateKey = config['stateKey'] || "weixinState";
24
23
  weixinConfig['scope'] = scope;
25
24
  weixinConfig['codeKey'] = codeKey;
26
25
  weixinConfig['stateKey'] = stateKey;
27
26
  (0, index_1.useSessionStorage)("weixinConfig", weixinConfig);
28
- var _e = weixinConfig.cryptoiv, cryptoiv = _e === void 0 ? "" : _e, _f = weixinConfig.cryptokey, cryptokey = _f === void 0 ? "" : _f;
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 = "";
@@ -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) {
@@ -192,11 +191,13 @@ function weixinInit() {
192
191
  return wx;
193
192
  }
194
193
  function weixinCode() {
195
- var _a;
196
194
  if ((0, utils_1.weixinBrowser)()) {
197
195
  if (!weixinConfig['appId'])
198
196
  return;
199
- var scope = (_a = weixinConfig === null || weixinConfig === void 0 ? void 0 : weixinConfig.scope) !== null && _a !== void 0 ? _a : "snsapi_userinfo";
197
+ var scope = "snsapi_userinfo";
198
+ if (weixinConfig['scope']) {
199
+ scope = weixinConfig.scope;
200
+ }
200
201
  var base = "https://open.weixin.qq.com/connect/oauth2/authorize?";
201
202
  var uri = encodeURIComponent(loc.href.split("?")[0]);
202
203
  var config = "appid=".concat(weixinConfig.appId, "&redirect_uri=").concat(uri, "&response_type=code&scope=").concat(scope, "&state=state#wechat_redirect");
@@ -214,8 +215,7 @@ var defaultJsApiList = [
214
215
  "downloadImage", "translateVoice", "getNetworkType", "openLocation", "getLocation", "hideOptionMenu", "showOptionMenu", "hideMenuItems", "showMenuItems", "hideAllNonBaseMenuItem", "showAllNonBaseMenuItem",
215
216
  ];
216
217
  function weixinShareConfig(config) {
217
- var _a;
218
- if (!(config === null || config === void 0 ? void 0 : config.http))
218
+ if (!config['http'])
219
219
  return;
220
220
  var url = loc.href.split("#")[0];
221
221
  var param = {
@@ -223,12 +223,12 @@ function weixinShareConfig(config) {
223
223
  appId: config.appId,
224
224
  };
225
225
  var jsApiList = defaultJsApiList;
226
- 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) {
227
227
  jsApiList = config.jsApiList;
228
+ }
228
229
  config.http(param).then(function (res) {
229
- var _a;
230
230
  if (res.code == 0) {
231
- var data = (_a = res === null || res === void 0 ? void 0 : res.data) !== null && _a !== void 0 ? _a : {};
231
+ var data = res['data'] || {};
232
232
  var wx = weixinInit();
233
233
  wx.config({
234
234
  debug: false,
@@ -241,7 +241,7 @@ function weixinShareConfig(config) {
241
241
  wx.ready(function (res) {
242
242
  console.log("加载微信分享配置完成:", res);
243
243
  (0, index_1.useSessionStorage)("weixinShareConfigSuccess", "\u52A0\u8F7D\u5FAE\u4FE1\u5206\u4EAB\u914D\u7F6E\u5B8C\u6210\uFF1A".concat(res));
244
- if ((config === null || config === void 0 ? void 0 : config.cb) && typeof config.cb === "function")
244
+ if (config['cb'] && typeof config.cb === "function")
245
245
  config.cb();
246
246
  });
247
247
  wx.error(function (err) {
@@ -252,23 +252,26 @@ function weixinShareConfig(config) {
252
252
  }
253
253
  exports.weixinShareConfig = weixinShareConfig;
254
254
  function weixinShareInit(options) {
255
- var title = "微信分享";
256
- if (options === null || options === void 0 ? void 0 : options.title)
255
+ var title = "微信分享标题";
256
+ if (options['title'])
257
257
  title = options.title;
258
258
  var desc = "微信分享描述";
259
- if (options === null || options === void 0 ? void 0 : options.desc)
259
+ if (options['desc'])
260
260
  desc = options.desc;
261
261
  var link = loc.origin;
262
- if (options === null || options === void 0 ? void 0 : options.link)
262
+ if (options['link'])
263
263
  link = options.link;
264
264
  var imgUrl = "";
265
- if (options === null || options === void 0 ? void 0 : options.imgUrl)
265
+ if (options['imgUrl'])
266
266
  imgUrl = options.imgUrl;
267
267
  if (options && typeof options == "object") {
268
268
  var state = "";
269
- if (options === null || options === void 0 ? void 0 : options.data) {
269
+ if (options['data']) {
270
270
  var data = options.data;
271
- 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);
272
275
  link = "".concat(link, "?state=").concat(state);
273
276
  }
274
277
  }
@@ -312,7 +315,7 @@ function weixinPay(config) {
312
315
  var wx = weixinInit();
313
316
  // let { timeStamp,signType,prepay_id,paySign,package,nonceStr,appId } = params
314
317
  var cb = "";
315
- if ((config === null || config === void 0 ? void 0 : config.cb) && typeof config.cb === "function")
318
+ if (config['cb'] && typeof config.cb === "function")
316
319
  cb = config.cb;
317
320
  wx.chooseWXPay({
318
321
  timestamp: config.timeStamp,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xctc-utils",
3
- "version": "1.3.3",
3
+ "version": "1.3.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",