zhl-methods 1.0.2 → 1.0.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.
- package/README.md +65 -0
- package/js/index.js +205 -0
- package/package.json +5 -2
- package/vue3/index.js +7 -0
- package/wx/index.js +34 -2
package/README.md
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# zhl-methods
|
|
2
|
+
|
|
3
|
+
JS 微信小程序 工具类
|
|
4
|
+
|
|
5
|
+
#### 安装
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
npm i zhl-methods
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
#### 引入使用
|
|
12
|
+
|
|
13
|
+
```
|
|
14
|
+
import { isPhone } from 'zhl-methods/js'
|
|
15
|
+
isPhone(123)
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
#### JS
|
|
19
|
+
|
|
20
|
+
##### 手机号校验
|
|
21
|
+
|
|
22
|
+
- @example isPhone('15093676561') => true
|
|
23
|
+
- @param {value} 要校验的数据
|
|
24
|
+
- @returns {Boolean} true false 是 否
|
|
25
|
+
|
|
26
|
+
##### 对象转 url 参数
|
|
27
|
+
|
|
28
|
+
- @example toUrlQuery({a:1}) => '?a=1'
|
|
29
|
+
- @param {value} 要转换的对象
|
|
30
|
+
- @returns {String} '?a=1'
|
|
31
|
+
|
|
32
|
+
##### 对象转 url 参数
|
|
33
|
+
|
|
34
|
+
- @example numberToPrice(1234.12) => '1,234.12'
|
|
35
|
+
- @param {number} 要转换的数字
|
|
36
|
+
- @returns {String} '1,234.12'
|
|
37
|
+
|
|
38
|
+
##### 保留两位小数
|
|
39
|
+
|
|
40
|
+
- @example setTwoDecimal(3.144) => '3.14'
|
|
41
|
+
- @param {number} 要保留两位小数的数字
|
|
42
|
+
- @param {isAdjust} 是否四舍五入
|
|
43
|
+
- @returns {String} '3.14'
|
|
44
|
+
|
|
45
|
+
#### 微信小程序
|
|
46
|
+
|
|
47
|
+
##### 获取导航栏高度
|
|
48
|
+
|
|
49
|
+
- @returns {number}
|
|
50
|
+
|
|
51
|
+
##### 弹窗二次确认
|
|
52
|
+
|
|
53
|
+
- @param {Function} callback
|
|
54
|
+
|
|
55
|
+
##### 获取当前环境
|
|
56
|
+
|
|
57
|
+
- @returns {string} release 线上 trial 测试 develop 开发
|
|
58
|
+
|
|
59
|
+
##### 获取 appId
|
|
60
|
+
|
|
61
|
+
- @returns {string}
|
|
62
|
+
|
|
63
|
+
##### 获取底部安全距离
|
|
64
|
+
|
|
65
|
+
- @returns {string}
|
package/js/index.js
CHANGED
|
@@ -13,3 +13,208 @@ export const isPhone = (value) => {
|
|
|
13
13
|
}
|
|
14
14
|
return false;
|
|
15
15
|
};
|
|
16
|
+
/**
|
|
17
|
+
* 身份证号校验
|
|
18
|
+
* @example isIDCode('410781199909090000') => true
|
|
19
|
+
* @param value 要校验的数据
|
|
20
|
+
* @returns true false 是 否
|
|
21
|
+
*/
|
|
22
|
+
export const isIDCode = (value) => {
|
|
23
|
+
if (typeof value == "string") {
|
|
24
|
+
return /^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/.test(
|
|
25
|
+
value
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
return false;
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* 对象转url参数
|
|
32
|
+
* @example toUrlQuery({a:1}) => '?a=1'
|
|
33
|
+
* @param {value} 要转换的对象
|
|
34
|
+
* @returns {String} '?a=1'
|
|
35
|
+
*/
|
|
36
|
+
export const toUrlQuery = (query) => {
|
|
37
|
+
try {
|
|
38
|
+
let str = "?";
|
|
39
|
+
Object.keys(query).forEach((key) => {
|
|
40
|
+
str += `${key}=${query[key] ? query[key] : ""}&`;
|
|
41
|
+
});
|
|
42
|
+
return str.slice(0, -1);
|
|
43
|
+
} catch (error) {
|
|
44
|
+
console.log(error, "toUrlQuery");
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
* 将传入的数据转换成千位符
|
|
49
|
+
* @example numberToPrice(1234.12) => '1,234.12'
|
|
50
|
+
* @param {number} 要转换的数字
|
|
51
|
+
* @returns {String} '1,234.12'
|
|
52
|
+
*/
|
|
53
|
+
export const numberToPrice = (number) => {
|
|
54
|
+
try {
|
|
55
|
+
return number.toLocaleString("zh", {
|
|
56
|
+
minimumFractionDigits: 2,
|
|
57
|
+
maximumFractionDigits: 2,
|
|
58
|
+
style: "decimal",
|
|
59
|
+
});
|
|
60
|
+
} catch (error) {
|
|
61
|
+
console.log(error, "numberToPrice");
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
/**
|
|
65
|
+
* 千位符格式转换数字
|
|
66
|
+
* @example moneyToNumber('123,456.789') => 123456.789
|
|
67
|
+
* @author zhl
|
|
68
|
+
* @date 2023/11/23
|
|
69
|
+
* @param data 要转换的数据
|
|
70
|
+
* @returns 转换完成的数据
|
|
71
|
+
*/
|
|
72
|
+
export const priceToNumber = (data) => {
|
|
73
|
+
return Number(data.replace(/,/gi, ""));
|
|
74
|
+
};
|
|
75
|
+
/**
|
|
76
|
+
* 保留两位小数
|
|
77
|
+
* @example setTwoDecimal(3.144) => '3.14'
|
|
78
|
+
* @param {number} 要保留两位小数的数字
|
|
79
|
+
* @param {isAdjust} 是否四舍五入
|
|
80
|
+
* @returns {String} '3.14'
|
|
81
|
+
*/
|
|
82
|
+
export const setTwoDecimal = (number, isAdjust) => {
|
|
83
|
+
try {
|
|
84
|
+
if (isAdjust) {
|
|
85
|
+
return Math.round(number * Math.pow(10, 2)) / Math.pow(10, 2);
|
|
86
|
+
}
|
|
87
|
+
return (Math.floor(number * 100) / 100).toFixed(2);
|
|
88
|
+
} catch (error) {
|
|
89
|
+
console.log(error, "setTwoDecimal");
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
/**
|
|
93
|
+
* 生成随机数
|
|
94
|
+
* @example getRandomNumber(1,100) => 98
|
|
95
|
+
* @param {min} 最小值
|
|
96
|
+
* @param {max} 最大值
|
|
97
|
+
* @returns {Number} 98
|
|
98
|
+
*/
|
|
99
|
+
export const getRandomNumber = (min, max) => {
|
|
100
|
+
return Math.floor(Math.random() * (max - min + 1)) + min;
|
|
101
|
+
};
|
|
102
|
+
/**
|
|
103
|
+
* 检测数据格式
|
|
104
|
+
* @example isType({},object) => true
|
|
105
|
+
* @example isType({}) => object
|
|
106
|
+
* @author zhl
|
|
107
|
+
* @param data 要检测的数据
|
|
108
|
+
* @param type 类型 非必填
|
|
109
|
+
* @returns 不传type 返回数据的类型
|
|
110
|
+
* 传type 返回 true false
|
|
111
|
+
*/
|
|
112
|
+
export const isType = (data, type) => {
|
|
113
|
+
if (type) {
|
|
114
|
+
return Object.prototype.toString.call(data) === type;
|
|
115
|
+
}
|
|
116
|
+
return Object.prototype.toString.call(data);
|
|
117
|
+
};
|
|
118
|
+
/**
|
|
119
|
+
* 时间戳转换成时间
|
|
120
|
+
* @example toTypeDate('1701139100',true) => 2023-11-28 10:38:20
|
|
121
|
+
* @author zhl
|
|
122
|
+
* @param date 要转换的数据 Date格式
|
|
123
|
+
* @param isSeconds 是否显示秒
|
|
124
|
+
* @returns 转换完成的数据
|
|
125
|
+
*/
|
|
126
|
+
export const toTypeDate = (date, isSeconds) => {
|
|
127
|
+
if (date && typeof date == "number") {
|
|
128
|
+
String(date).length == 10
|
|
129
|
+
? (date = new Date(date * 1000))
|
|
130
|
+
: (date = new Date(date));
|
|
131
|
+
} else {
|
|
132
|
+
date = new Date();
|
|
133
|
+
}
|
|
134
|
+
const year = date.getFullYear();
|
|
135
|
+
let month = date.getMonth() + 1;
|
|
136
|
+
let ri = date.getDate();
|
|
137
|
+
let hours = date.getHours();
|
|
138
|
+
let minutes = date.getMinutes();
|
|
139
|
+
let seconds = date.getSeconds();
|
|
140
|
+
if (month < 10) month = "0" + month;
|
|
141
|
+
if (ri < 10) ri = "0" + ri;
|
|
142
|
+
if (hours < 10) hours = "0" + hours;
|
|
143
|
+
if (minutes < 10) minutes = "0" + minutes;
|
|
144
|
+
if (seconds < 10) seconds = "0" + seconds;
|
|
145
|
+
|
|
146
|
+
return (
|
|
147
|
+
year +
|
|
148
|
+
"-" +
|
|
149
|
+
month +
|
|
150
|
+
"-" +
|
|
151
|
+
ri +
|
|
152
|
+
" " +
|
|
153
|
+
hours +
|
|
154
|
+
":" +
|
|
155
|
+
minutes +
|
|
156
|
+
(isSeconds ? ":" + seconds : "")
|
|
157
|
+
);
|
|
158
|
+
};
|
|
159
|
+
/**
|
|
160
|
+
* 转换成整数
|
|
161
|
+
* @example toInt(123.123) => 123
|
|
162
|
+
* @param value 要转成整数的数据 字符串 数字
|
|
163
|
+
* @returns true false 是 否
|
|
164
|
+
*/
|
|
165
|
+
export const toInt = (value) => {
|
|
166
|
+
if (typeof value == "number") {
|
|
167
|
+
return ~~value;
|
|
168
|
+
}
|
|
169
|
+
if (typeof value == "string") {
|
|
170
|
+
return parseInt(value, 10);
|
|
171
|
+
}
|
|
172
|
+
return 0;
|
|
173
|
+
};
|
|
174
|
+
/**
|
|
175
|
+
* 复制
|
|
176
|
+
* @example copyText(123) => Promise
|
|
177
|
+
* @param content 要复制的数据
|
|
178
|
+
* @returns Promise 复制成功 失败
|
|
179
|
+
*/
|
|
180
|
+
export const copyText = (content) => {
|
|
181
|
+
return new Promise((resolve, reject) => {
|
|
182
|
+
try {
|
|
183
|
+
// 创建输入框元素
|
|
184
|
+
const input = document.createElement("input"); //不会保留文本格式
|
|
185
|
+
//如果要保留文本格式,比如保留换行符,或者多行文本,可以使用 textarea 标签,再配和模板字符串 ` `
|
|
186
|
+
//const input = document.createElement('textarea')
|
|
187
|
+
// 将想要复制的值
|
|
188
|
+
input.value = content;
|
|
189
|
+
// 页面底部追加输入框
|
|
190
|
+
document.body.appendChild(input);
|
|
191
|
+
// 选中输入框
|
|
192
|
+
input.select();
|
|
193
|
+
// 执行浏览器复制命令
|
|
194
|
+
document.execCommand("Copy");
|
|
195
|
+
// 弹出复制成功信息
|
|
196
|
+
//this.$message.success('复制成功');
|
|
197
|
+
// 复制后移除输入框
|
|
198
|
+
input.remove();
|
|
199
|
+
resolve(true);
|
|
200
|
+
} catch (err) {
|
|
201
|
+
reject(err);
|
|
202
|
+
}
|
|
203
|
+
});
|
|
204
|
+
};
|
|
205
|
+
/**
|
|
206
|
+
* 下载文件
|
|
207
|
+
* @example downloadFile('www.baidu.com','文件名称')
|
|
208
|
+
* @param url 要下载的文件 地址
|
|
209
|
+
* @param name 要下载的文件名称
|
|
210
|
+
* @returns 没有返回值
|
|
211
|
+
*/
|
|
212
|
+
export const downloadFile = (url, name) => {
|
|
213
|
+
let a = document.createElement("a");
|
|
214
|
+
a.href = url;
|
|
215
|
+
a.download = name;
|
|
216
|
+
a.style.display = "none";
|
|
217
|
+
document.body.appendChild(a);
|
|
218
|
+
a.click();
|
|
219
|
+
a.remove();
|
|
220
|
+
};
|
package/package.json
CHANGED
package/vue3/index.js
ADDED
package/wx/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* 获取导航栏高度
|
|
3
3
|
* @returns {number}
|
|
4
4
|
*/
|
|
5
5
|
export const getNavBarHeight = () => {
|
|
@@ -26,6 +26,24 @@ export const requestApi = (callback) => {
|
|
|
26
26
|
},
|
|
27
27
|
});
|
|
28
28
|
};
|
|
29
|
+
/**
|
|
30
|
+
* 弹窗二次确认
|
|
31
|
+
* @param {Object} query title 标题 默认值 确认该操作吗? content 内容 可以不填
|
|
32
|
+
* @param {Function} callback
|
|
33
|
+
*/
|
|
34
|
+
export const showModal = (query = {}, callback) => {
|
|
35
|
+
wx.showModal({
|
|
36
|
+
title: query.title ? query.title : "确认该操作吗?",
|
|
37
|
+
content: query.content ? query.content : "",
|
|
38
|
+
confirmText: "确认",
|
|
39
|
+
cancelText: "取消",
|
|
40
|
+
async success(res) {
|
|
41
|
+
if (res.confirm) {
|
|
42
|
+
callback();
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
});
|
|
46
|
+
};
|
|
29
47
|
/**
|
|
30
48
|
* 获取当前环境
|
|
31
49
|
* @returns {string} release线上 trial测试 develop开发
|
|
@@ -46,6 +64,20 @@ export const getAppId = () => {
|
|
|
46
64
|
*/
|
|
47
65
|
export const getSafeBottom = () => {
|
|
48
66
|
let winInfo = wx.getWindowInfo();
|
|
49
|
-
console.log(winInfo)
|
|
50
67
|
return winInfo.safeArea.bottom - winInfo.safeArea.height;
|
|
51
68
|
};
|
|
69
|
+
/**
|
|
70
|
+
* 复制
|
|
71
|
+
* @param {string|number} 要复制的数据
|
|
72
|
+
*/
|
|
73
|
+
export const copyText = (val) => {
|
|
74
|
+
wx.setClipboardData({
|
|
75
|
+
data: val,
|
|
76
|
+
success: () => {
|
|
77
|
+
wx.showToast({ title: "复制成功", icon: "none" });
|
|
78
|
+
},
|
|
79
|
+
fail: (res) => {
|
|
80
|
+
wx.showToast({ title: "复制失败", icon: "none" });
|
|
81
|
+
},
|
|
82
|
+
});
|
|
83
|
+
};
|