zhl-methods 1.0.2 → 1.0.3
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 +62 -0
- package/package.json +1 -1
- 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,65 @@ export const isPhone = (value) => {
|
|
|
13
13
|
}
|
|
14
14
|
return false;
|
|
15
15
|
};
|
|
16
|
+
/**
|
|
17
|
+
* 对象转url参数
|
|
18
|
+
* @example toUrlQuery({a:1}) => '?a=1'
|
|
19
|
+
* @param {value} 要转换的对象
|
|
20
|
+
* @returns {String} '?a=1'
|
|
21
|
+
*/
|
|
22
|
+
export const toUrlQuery = (query) => {
|
|
23
|
+
try {
|
|
24
|
+
let str = "?";
|
|
25
|
+
Object.keys(query).forEach((key) => {
|
|
26
|
+
str += `${key}=${query[key] ? query[key] : ""}&`;
|
|
27
|
+
});
|
|
28
|
+
return str.slice(0, -1);
|
|
29
|
+
} catch (error) {
|
|
30
|
+
console.log(error, "toUrlQuery");
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
/**
|
|
34
|
+
* 对象转url参数
|
|
35
|
+
* @example numberToPrice(1234.12) => '1,234.12'
|
|
36
|
+
* @param {number} 要转换的数字
|
|
37
|
+
* @returns {String} '1,234.12'
|
|
38
|
+
*/
|
|
39
|
+
export const numberToPrice = (number) => {
|
|
40
|
+
try {
|
|
41
|
+
return number.toLocaleString("zh", {
|
|
42
|
+
minimumFractionDigits: 2,
|
|
43
|
+
maximumFractionDigits: 2,
|
|
44
|
+
style: "decimal",
|
|
45
|
+
});
|
|
46
|
+
} catch (error) {
|
|
47
|
+
console.log(error, "numberToPrice");
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* 保留两位小数
|
|
52
|
+
* @example setTwoDecimal(3.144) => '3.14'
|
|
53
|
+
* @param {number} 要保留两位小数的数字
|
|
54
|
+
* @param {isAdjust} 是否四舍五入
|
|
55
|
+
* @returns {String} '3.14'
|
|
56
|
+
*/
|
|
57
|
+
export const setTwoDecimal = (number, isAdjust) => {
|
|
58
|
+
try {
|
|
59
|
+
if (isAdjust) {
|
|
60
|
+
return Math.round(number * Math.pow(10, 2)) / Math.pow(10, 2);
|
|
61
|
+
}
|
|
62
|
+
return (Math.floor(number * 100) / 100).toFixed(2);
|
|
63
|
+
} catch (error) {
|
|
64
|
+
console.log(error, "setTwoDecimal");
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
/**
|
|
68
|
+
* 生成随机数
|
|
69
|
+
* @example getRandomNumber(1,100) => 98
|
|
70
|
+
* @param {min} 最小值
|
|
71
|
+
* @param {max} 最大值
|
|
72
|
+
* @returns {Number} 98
|
|
73
|
+
*/
|
|
74
|
+
|
|
75
|
+
export const getRandomNumber = (min, max) => {
|
|
76
|
+
return Math.floor(Math.random() * (max - min + 1)) + min;
|
|
77
|
+
};
|
package/package.json
CHANGED
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
|
+
};
|