5f588c45 by simon

1.更新工具类

1 parent a09fad57
1 // 手机正则 1 // 手机正则
2 const REGEXPS = { 2 const REGEXPS = {
3 "mobile": /^1\d{10}$/ 3 "mobile": /^1\d{10}$/
4 } 4 }
5 // 验证手机 5 // 验证手机
6 function checkMobile(str) { 6 function checkMobile(str) {
7 return REGEXPS.mobile.test(str); 7 return REGEXPS.mobile.test(str);
8 } 8 }
9 9
10 10
11 function formatTime(date) { 11 /**
12 var year = date.getFullYear() 12 * 链接参数转换为obj
13 var month = date.getMonth() + 1 13 * 入参 完整链接
14 var day = date.getDate() 14 * @param {*} url
15 */
16 function param2Obj(url) {
17 const search = url.split('?')[1]
18 if (!search) {
19 return {}
20 }
21 return JSON.parse(
22 '{"' +
23 decodeURIComponent(search)
24 .replace(/"/g, '\\"')
25 .replace(/&/g, '","')
26 .replace(/=/g, '":"') +
27 '"}'
28 )
29 }
15 30
16 var hour = date.getHours() 31 /**
17 var minute = date.getMinutes() 32 * 格式化日期常规日期
18 var second = date.getSeconds() 33 * 格式yyyy-MM-dd hh:mm:ss
34 * @param {*} date
35 */
36 function formatTime(date) {
37 var year = date.getFullYear()
38 var month = date.getMonth() + 1
39 var day = date.getDate()
19 40
41 var hour = date.getHours()
42 var minute = date.getMinutes()
43 var second = date.getSeconds()
20 44
21 return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':') 45 return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
22 } 46 }
23 47
48 /**
49 * 格式化数字,不足一位补充0
50 * @param {*} n
51 */
24 function formatNumber(n) { 52 function formatNumber(n) {
25 n = n.toString() 53 n = n.toString()
26 return n[1] ? n : '0' + n 54 return n[1] ? n : '0' + n
27 } 55 }
28 56
29
30
31
32 /** 57 /**
33 * 获取屏幕剩余高度 58 * 获取屏幕剩余高度
34 * useHeight 单位是rpx 59 * useHeight 单位是rpx
35 * 默认返回单位是rpx 可通过unit参数改为 px 60 * 默认返回单位是rpx 可通过unit参数改为 px
36 */ 61 */
37 function getLastScreenHeight(useHeight = 0, unit = 'rpx') { 62 function getLastScreenHeight(useHeight = 0, unit = 'rpx') {
38 let sysInfo = wx.getSystemInfoSync(); 63 let sysInfo = wx.getSystemInfoSync();
39 let clientHeight = sysInfo.windowHeight; 64 let clientHeight = sysInfo.windowHeight;
40 // 获取可使用窗口高度 65 // 获取可使用窗口高度
41 let clientWidth = sysInfo.windowWidth; 66 let clientWidth = sysInfo.windowWidth;
42 // 算出比例 67 // 算出比例
43 let ratio = 750 / clientWidth; 68 let ratio = 750 / clientWidth;
44 // 算出屏幕高度(单位rpx) 69 // 算出屏幕高度(单位rpx)
45 let height = clientHeight * ratio; 70 let height = clientHeight * ratio;
46 // 计算剩余高度 71 // 计算剩余高度
47 let lastHeight = height - useHeight; 72 let lastHeight = height - useHeight;
48 // 可转换成px 73 // 可转换成px
49 if (unit == 'px') { 74 if (unit == 'px') {
50 lastHeight = lastHeight / 750 * clientWidth 75 lastHeight = lastHeight / 750 * clientWidth
51 } 76 }
52 return lastHeight; 77 return lastHeight;
78 }
79
80
81 /**
82 * px转rpx
83 * @param {*} value
84 */
85 function pxToRpx(value) {
86 let sysInfo = wx.getSystemInfoSync();
87 let clientWidth = sysInfo.windowWidth;
88 let result = value / 750 * clientWidth
89 return result;
90 }
91
92
93 // 格式化星期几
94 function formatWeek(week) {
95 let result = "";
96 switch (week) {
97 case 1:
98 result = "一";
99 break;
100 case 2:
101 result = "二";
102 break;
103 case 3:
104 result = "三";
105 break;
106 case 4:
107 result = "四";
108 break;
109 case 5:
110 result = "五";
111 break;
112 case 6:
113 result = "六";
114 break;
115 case 0:
116 result = "日";
117 break;
118
119 default:
120 break;
121 }
122 return result;
123 }
124
125 /**
126 * 获取点击传值
127 * @param {*} evt
128 * @param {*} key
129 */
130 function getBindtapData(evt, key = "data") {
131 let keyStr = key || "data";
132 return evt.currentTarget.dataset[keyStr];
133 }
134
135 /**
136 * 获取小程序码
137 * path = "/pages/index/index?pa=1"
138 * @param {*} path
139 */
140 function wxacodeGet(path) {
141 return " https://api.k.wxpai.cn/bizproxy/mzcfsapi/qrcode/create?path=" + encodeURIComponent(path);
142 }
143
144
145 /**
146 * @desc 函数防抖
147 * @param func 函数
148 * @param wait 延迟执行毫秒数
149 * @param immediate true 表立即执行,false 表非立即执行
150 */
151 function debounce(func, wait, immediate) {
152 let timeout;
153
154 return function () {
155 let context = this;
156 let args = arguments;
157
158 if (timeout) clearTimeout(timeout);
159 if (immediate) {
160 var callNow = !timeout;
161 timeout = setTimeout(() => {
162 timeout = null;
163 }, wait)
164 if (callNow) func.apply(context, args)
165 } else {
166 timeout = setTimeout(function () {
167 func.apply(context, args)
168 }, wait);
169 }
170 }
171 }
172
173 /**
174 * @desc 函数节流
175 * @param func 函数
176 * @param wait 延迟执行毫秒数
177 * @param type 1 表时间戳版,2 表定时器版
178 * 时间戳版的函数触发是在时间段内开始的时候,而定时器版的函数触发是在时间段内结束的时候。
179 */
180 function throttle(func, wait, type) {
181 if (type === 1) {
182 var previous = 0;
183 } else if (type === 2) {
184 var timeout;
185 }
186 return function () {
187 let context = this;
188 let args = arguments;
189 if (type === 1) {
190 let now = Date.now();
191
192 if (now - previous > wait) {
193 func.apply(context, args);
194 previous = now;
195 }
196 } else if (type === 2) {
197 if (!timeout) {
198 timeout = setTimeout(() => {
199 timeout = null;
200 func.apply(context, args)
201 }, wait)
202 }
203 }
204 }
53 } 205 }
54 206
55 module.exports = { 207 module.exports = {
56 formatTime: formatTime, 208 formatTime: formatTime,
57 checkMobile: checkMobile, 209 checkMobile: checkMobile,
58 getLastScreenHeight: getLastScreenHeight 210 getLastScreenHeight: getLastScreenHeight,
211 debounce: debounce,
212 throttle: throttle,
213 param2Obj: param2Obj,
214 pxToRpx: pxToRpx,
215 formatWeek: formatWeek,
216 getBindtapData: getBindtapData,
217 wxacodeGet: wxacodeGet,
59 } 218 }
......