xctc-utils 1.5.3 → 1.5.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
@@ -1,29 +1,34 @@
1
1
  ### 项目中常用的方法集合
2
2
  #### email: dybself@163.com
3
3
  #### 安装 npm i xctc-utils
4
- #### 项目中引入 import utils from "xctc-utils"
4
+ #### 项目中引入 import useUtils from "xctc-utils"
5
5
 
6
6
 
7
7
  #### LocalStorage使用,存取值时数据已经过处理
8
8
  ```
9
- 存储: utils.useLocalStorage(key,value)
10
- 取值: utils.getLocalStorage(key)
9
+ 存储: useUtils.useLocalStorage(key,value)
10
+ 取值: useUtils.getLocalStorage(key)
11
11
  ```
12
12
 
13
13
  #### sessionStorage使用,存取值时数据已经过处理
14
14
  ```
15
15
 
16
- 存储: utils.useSessionStorage(key,value)
17
- 取值: utils.getSessionStorage(key)
16
+ 存储: useUtils.useSessionStorage(key,value)
17
+ 取值: useUtils.getSessionStorage(key)
18
18
 
19
+ ```
20
+
21
+ #### 将数据复制到剪贴板函数
22
+ ```
23
+ useUtils.handleCopyValue(val)
19
24
  ```
20
25
  #### 删除缓存
21
26
  ```
22
27
  key:string 需要删除的缓存键
23
28
  isAll:boolean 为true时删除调用方法的所有缓存,否则只删除当前传入的 key 值
24
29
  如果 isAll 为 true 时,方法会优先执行缓存的clear函数, key 传任意值,
25
- 删除临时缓存: utils.removeSessionStorage(key,isAll)
26
- 删除永久缓存: utils.removeLocalStorage(key,isAll)
30
+ 删除临时缓存: useUtils.removeSessionStorage(key,isAll)
31
+ 删除永久缓存: useUtils.removeLocalStorage(key,isAll)
27
32
 
28
33
  ```
29
34
  #### AES 加密、解密,同一个数据的加密和解密传入的key 和 iv保持一致。
@@ -32,27 +37,27 @@ work: 需要加密的对象,如传入的是对象,该方法默认进行JSO
32
37
  key:16位或者32位字符串作为密钥
33
38
  iv:16位或者32位字符串作为密钥偏移量
34
39
  data: encrypt方法加密后返回的数据
35
- 加密:utils.encrypt( work:any , key:string , iv:string )
36
- 解密:utils.decrypt( data:string , key:string , iv:string )
40
+ 加密:useUtils.encrypt( work:any , key:string , iv:string )
41
+ 解密:useUtils.decrypt( data:string , key:string , iv:string )
37
42
  ```
38
43
  #### 类型判断
39
44
  ```
40
- utils.type.isFunction(val) // 是否是函数
41
- utils.type.isObject(val) // 是否是对象 null 类型为对象,该方法已对null进行过滤
42
- utils.type.isDate(val) // 是否是时间对象
43
- utils.type.isNumber(val) // 是否是number类型
44
- utils.type.isString(val) // 是否是字符串类型
45
- utils.type.isBoolean(val) // 是否是boolean类型
46
- utils.type.isWindow(val) // 是否在浏览器环境下运行
45
+ useUtils.type.isFunction(val) // 是否是函数
46
+ useUtils.type.isObject(val) // 是否是对象 null 类型为对象,该方法已对null进行过滤
47
+ useUtils.type.isDate(val) // 是否是时间对象
48
+ useUtils.type.isNumber(val) // 是否是number类型
49
+ useUtils.type.isString(val) // 是否是字符串类型
50
+ useUtils.type.isBoolean(val) // 是否是boolean类型
51
+ useUtils.type.isWindow(val) // 是否在浏览器环境下运行
47
52
 
48
53
  ```
49
54
  #### 获取当前设备环境
50
55
  设备环境:
51
56
  ```
52
57
 
53
- 当前使用设备类型: utils.deviceEnvironment() // android ios
54
- 是否在微信浏览器环境中: utils.weixinBrowser() // true false
55
- 是否是手持设备: utils.isMobile() // true 移动设备 false PC设备
58
+ 当前使用设备类型: useUtils.deviceEnvironment() // android ios
59
+ 是否在微信浏览器环境中: useUtils.weixinBrowser() // true false
60
+ 是否是手持设备: useUtils.isMobile() // true 移动设备 false PC设备
56
61
 
57
62
  ```
58
63
 
@@ -67,9 +72,9 @@ data:encrypt方法加密后返回的数据
67
72
  对word进行CryptoJS.enc.Utf8.parse转义
68
73
  CryptoJS.AES.encrypt加密,加密模式:CBC ; padding: CryptoJS.pad.Pkcs7
69
74
  对数据进行加密
70
- utils.encrypt(word:any,key:string,iv:string)
75
+ useUtils.encrypt(word:any,key:string,iv:string)
71
76
  // 对数据进行解密
72
- utils.decrypt(data:string,key:string,iv:string)
77
+ useUtils.decrypt(data:string,key:string,iv:string)
73
78
 
74
79
  ```
75
80
  #### 微信H5使用方法集合
@@ -85,7 +90,7 @@ interface configOption {
85
90
  cryptokey?:string, // 将地址栏携带参数加密key, 必须与 weixinShareInit 方法中的 key 参数一致
86
91
  }
87
92
  进入系统时,默认调用该方法,自动执行微信跳转授权,回调地址中code处理、state处理,
88
- utils.weixinUrlCode(config)
93
+ useUtils.weixinUrlCode(config)
89
94
 
90
95
  ```
91
96
  #### 微信config接口权限注入
@@ -97,7 +102,7 @@ interface ShareConfig{
97
102
  jsApiList?:string[],
98
103
  }
99
104
 
100
- utils.weixinShareConfig(config:ShareConfig)
105
+ useUtils.weixinShareConfig(config:ShareConfig)
101
106
  ```
102
107
 
103
108
  #### 微信分享接口加载
@@ -111,14 +116,14 @@ interface Share{
111
116
  iv?:string, // 分享链接中对 state 数据加密的iv 必须与 weixinUrlCode 方法中的 cryptoiv 参数一致
112
117
  key?:string, // 分享链接中对 state 数据加密的 key 必须与 weixinUrlCode 方法中的 cryptokey 参数一致
113
118
  }
114
- utils.weixinShareInit(config:Share)
119
+ useUtils.weixinShareInit(config:Share)
115
120
  ```
116
121
 
117
122
  ### 时间相关方法
118
123
  #### 获取当前时间的时间戳
119
124
  ```
120
125
 
121
- utils.getTime() // 返回秒
126
+ useUtils.getTime() // 返回秒
122
127
 
123
128
  ```
124
129
  #### 时间戳转任意格式时间字符串
@@ -129,7 +134,7 @@ utils.getTime() // 返回秒
129
134
  * @param format 解析后展示时间字符串格式,默认 "YYYY-MM-DD HH:mm:ss" 格式, 可传格式如: YYYY年MM月DD日 HH时 YYYY/MM/DD HH
130
135
  * @returns
131
136
  */
132
- utils.formatTimeStamp(num,format) // 返回 format 格式时间戳
137
+ useUtils.formatTimeStamp(num,format) // 返回 format 格式时间戳
133
138
 
134
139
  ```
135
140
  #### 时间字符串转时间戳
@@ -139,6 +144,6 @@ utils.formatTimeStamp(num,format) // 返回 format 格式时间戳
139
144
  * @param str 字符串格式必须为: 2017/03/03 12:23:55 格式,避免兼容性,不能传 “-” 分割的时间字符串 2017-03-03
140
145
  * @returns
141
146
  */
142
- utils.formatStrTime(str) // 返回时间戳 秒
147
+ useUtils.formatStrTime(str) // 返回时间戳 秒
143
148
 
144
149
  ```
@@ -8,6 +8,7 @@ function makePhone(tel) {
8
8
  }
9
9
  }
10
10
  exports.makePhone = makePhone;
11
+ // 将手机号后四位转换为***
11
12
  function formatMobile(mobile) {
12
13
  if (!mobile)
13
14
  return;
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.isMobile = void 0;
4
4
  var w = window;
5
+ // 是否是手机端环境
5
6
  function isMobile() {
6
7
  var info = w.navigator.userAgent;
7
8
  var isPhone = /mobile/i.test(info);
@@ -1,6 +1,30 @@
1
+ /**
2
+ *
3
+ * @param key 需要存储的数据key值
4
+ * @param value 需要存储的数据
5
+ * @returns
6
+ */
1
7
  export declare function useLocalStorage(key: string, value: any): false | undefined;
2
8
  export declare function getLocalStorage(key: string): any;
9
+ /**
10
+ *
11
+ * @param key 需要存储的数据key值
12
+ * @param value 需要存储的数据
13
+ * @returns
14
+ */
3
15
  export declare function useSessionStorage(key: string, value: any): false | undefined;
4
16
  export declare function getSessionStorage(key: string): any;
17
+ /**
18
+ *
19
+ * @param key 需要删除的key值
20
+ * @param isAll 是否删除所有缓存
21
+ * @returns
22
+ */
5
23
  export declare function removeSessionStorage(key?: string, isAll?: boolean): void;
24
+ /**
25
+ *
26
+ * @param key 需要删除的key值
27
+ * @param isAll 是否删除所有缓存
28
+ * @returns
29
+ */
6
30
  export declare function removeLocalStorage(key?: string, isAll?: boolean): void;
@@ -5,6 +5,12 @@ var is_1 = require("../is");
5
5
  var wls = window.localStorage;
6
6
  var wss = window.sessionStorage;
7
7
  var utils_1 = require("../utils");
8
+ /**
9
+ *
10
+ * @param key 需要存储的数据key值
11
+ * @param value 需要存储的数据
12
+ * @returns
13
+ */
8
14
  function useLocalStorage(key, value) {
9
15
  if (!key || !value) {
10
16
  console.log("useLocalStorage存储时缺少key:value");
@@ -19,6 +25,12 @@ function getLocalStorage(key) {
19
25
  return getStorage(key, wls);
20
26
  }
21
27
  exports.getLocalStorage = getLocalStorage;
28
+ /**
29
+ *
30
+ * @param key 需要存储的数据key值
31
+ * @param value 需要存储的数据
32
+ * @returns
33
+ */
22
34
  function useSessionStorage(key, value) {
23
35
  if (!key || !value) {
24
36
  console.log("useSessionStorage存储时缺少key:value");
@@ -33,6 +45,12 @@ function getSessionStorage(key) {
33
45
  return getStorage(key, wss);
34
46
  }
35
47
  exports.getSessionStorage = getSessionStorage;
48
+ /**
49
+ *
50
+ * @param key 需要存储的数据key值
51
+ * @param value 需要存储的数据
52
+ * @param obj localStorage或者sessionStorage对象
53
+ */
36
54
  function useStorage(key, value, obj) {
37
55
  if (value instanceof Object) {
38
56
  obj.setItem(key, JSON.stringify(value));
@@ -41,6 +59,12 @@ function useStorage(key, value, obj) {
41
59
  obj.setItem(key, value);
42
60
  }
43
61
  }
62
+ /**
63
+ *
64
+ * @param key 需要获取的数据key值
65
+ * @param obj localStorage或者sessionStorage对象
66
+ * @returns
67
+ */
44
68
  function getStorage(key, obj) {
45
69
  var val = obj.getItem(key);
46
70
  if (val == 'undefined' || val == null) {
@@ -51,6 +75,12 @@ function getStorage(key, obj) {
51
75
  val = data;
52
76
  return val;
53
77
  }
78
+ /**
79
+ *
80
+ * @param key 需要删除的key值
81
+ * @param isAll 是否删除所有缓存
82
+ * @returns
83
+ */
54
84
  function removeSessionStorage(key, isAll) {
55
85
  if ((0, is_1.isBoolean)(isAll) && isAll) {
56
86
  wss.clear();
@@ -61,6 +91,12 @@ function removeSessionStorage(key, isAll) {
61
91
  removeStorage(key, wss);
62
92
  }
63
93
  exports.removeSessionStorage = removeSessionStorage;
94
+ /**
95
+ *
96
+ * @param key 需要删除的key值
97
+ * @param isAll 是否删除所有缓存
98
+ * @returns
99
+ */
64
100
  function removeLocalStorage(key, isAll) {
65
101
  if ((0, is_1.isBoolean)(isAll) && isAll) {
66
102
  wls.clear();
@@ -19,6 +19,7 @@ exports.getTime = getTime;
19
19
  function formatTimeStamp(num, format) {
20
20
  if (!format)
21
21
  format = "YYYY-MM-DD HH:mm:ss";
22
+ num = Number(num); // 先将数据转换为数据类型
22
23
  if (!num)
23
24
  return "";
24
25
  // 传入秒 时间戳
@@ -70,8 +71,13 @@ function isExist(str, key) {
70
71
  function formatStrTime(str) {
71
72
  if (!str)
72
73
  return "";
73
- var timestamp = new Date(str).getTime();
74
- timestamp = timestamp / 1000;
75
- return parseInt(timestamp);
74
+ var strDate = new Date(str);
75
+ var timestamp = "";
76
+ if (strDate) {
77
+ timestamp = strDate.getTime();
78
+ timestamp = timestamp / 1000;
79
+ return parseInt(timestamp);
80
+ }
81
+ return "";
76
82
  }
77
83
  exports.formatStrTime = formatStrTime;
package/dist/utils.d.ts CHANGED
@@ -1,7 +1,26 @@
1
1
  import { UeditorHeightOption } from "./types";
2
+ /**
3
+ * 判断当前传入的字符串是否为 json格式 字符串
4
+ * @param str
5
+ * @returns
6
+ */
2
7
  export declare function isJson(str: string): any;
3
8
  export declare function $id(id: string): false | HTMLElement;
4
9
  export declare function checkIframeContentHeight(option: UeditorHeightOption): string;
5
10
  export declare function getIframeContentHeight(id: string): any;
11
+ /**
12
+ * 判断当前手机端环境
13
+ * @returns
14
+ */
6
15
  export declare function deviceEnvironment(): string;
16
+ /**
17
+ * 判断是否是微信内置浏览器
18
+ * @returns
19
+ */
7
20
  export declare function weixinBrowser(): boolean;
21
+ /**
22
+ * 将传入的数据复制到粘贴板
23
+ * @param text
24
+ * @returns
25
+ */
26
+ export declare function handleCopyValue(text: string): any;
package/dist/utils.js CHANGED
@@ -1,6 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.weixinBrowser = exports.deviceEnvironment = exports.getIframeContentHeight = exports.checkIframeContentHeight = exports.$id = exports.isJson = void 0;
3
+ exports.handleCopyValue = exports.weixinBrowser = exports.deviceEnvironment = exports.getIframeContentHeight = exports.checkIframeContentHeight = exports.$id = exports.isJson = void 0;
4
+ /**
5
+ * 判断当前传入的字符串是否为 json格式 字符串
6
+ * @param str
7
+ * @returns
8
+ */
4
9
  function isJson(str) {
5
10
  if (!str)
6
11
  return false;
@@ -67,6 +72,10 @@ function getIframeContentHeight(id) {
67
72
  return 0;
68
73
  }
69
74
  exports.getIframeContentHeight = getIframeContentHeight;
75
+ /**
76
+ * 判断当前手机端环境
77
+ * @returns
78
+ */
70
79
  function deviceEnvironment() {
71
80
  var u = navigator.userAgent;
72
81
  if (u.indexOf('Android') > -1 || u.indexOf('Adr') > -1)
@@ -76,6 +85,10 @@ function deviceEnvironment() {
76
85
  return "other";
77
86
  }
78
87
  exports.deviceEnvironment = deviceEnvironment;
88
+ /**
89
+ * 判断是否是微信内置浏览器
90
+ * @returns
91
+ */
79
92
  function weixinBrowser() {
80
93
  var ua = navigator.userAgent.toLowerCase();
81
94
  var isWeixin = ua.indexOf("micromessenger") != -1;
@@ -87,3 +100,40 @@ function weixinBrowser() {
87
100
  }
88
101
  }
89
102
  exports.weixinBrowser = weixinBrowser;
103
+ // 复制功能
104
+ /**
105
+ * 将传入的数据复制到粘贴板
106
+ * @param text
107
+ * @returns
108
+ */
109
+ function handleCopyValue(text) {
110
+ if (!text)
111
+ return console.error("请传入需要复制到粘贴板的数据");
112
+ //浏览器禁用了非安全域的 navigator.clipboard 对象
113
+ //在线上环境会报错 TypeError: Cannot read properties of undefined (reading 'writeText')
114
+ var clipboard = (navigator === null || navigator === void 0 ? void 0 : navigator.clipboard) || "";
115
+ if (!clipboard && window.isSecureContext) {
116
+ return clipboard === null || clipboard === void 0 ? void 0 : clipboard.writeText(text);
117
+ }
118
+ else {
119
+ // 判断是否支持拷贝
120
+ if (!document.execCommand('copy'))
121
+ return Promise.reject();
122
+ // 创建标签,并隐藏
123
+ var textArea_1 = document.createElement('textarea');
124
+ textArea_1.style.position = 'fixed';
125
+ textArea_1.style.top = textArea_1.style.left = '-100vh';
126
+ textArea_1.style.opacity = '0';
127
+ textArea_1.value = text;
128
+ document.body.appendChild(textArea_1);
129
+ // 聚焦、复制
130
+ textArea_1.focus();
131
+ textArea_1.select();
132
+ return new Promise(function (resolve, reject) {
133
+ // 不知为何,必须写这个三目,不然copy不上
134
+ document.execCommand('copy') ? resolve() : reject();
135
+ textArea_1.remove();
136
+ });
137
+ }
138
+ }
139
+ exports.handleCopyValue = handleCopyValue;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xctc-utils",
3
- "version": "1.5.3",
3
+ "version": "1.5.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",