1.更新工具类
Showing
1 changed file
with
164 additions
and
5 deletions
... | @@ -8,6 +8,31 @@ function checkMobile(str) { | ... | @@ -8,6 +8,31 @@ function checkMobile(str) { |
8 | } | 8 | } |
9 | 9 | ||
10 | 10 | ||
11 | /** | ||
12 | * 链接参数转换为obj | ||
13 | * 入参 完整链接 | ||
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 | } | ||
30 | |||
31 | /** | ||
32 | * 格式化日期常规日期 | ||
33 | * 格式yyyy-MM-dd hh:mm:ss | ||
34 | * @param {*} date | ||
35 | */ | ||
11 | function formatTime(date) { | 36 | function formatTime(date) { |
12 | var year = date.getFullYear() | 37 | var year = date.getFullYear() |
13 | var month = date.getMonth() + 1 | 38 | var month = date.getMonth() + 1 |
... | @@ -17,18 +42,18 @@ function formatTime(date) { | ... | @@ -17,18 +42,18 @@ function formatTime(date) { |
17 | var minute = date.getMinutes() | 42 | var minute = date.getMinutes() |
18 | var second = date.getSeconds() | 43 | var second = date.getSeconds() |
19 | 44 | ||
20 | |||
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 |
... | @@ -52,8 +77,142 @@ function getLastScreenHeight(useHeight = 0, unit = 'rpx') { | ... | @@ -52,8 +77,142 @@ function getLastScreenHeight(useHeight = 0, unit = 'rpx') { |
52 | return lastHeight; | 77 | return lastHeight; |
53 | } | 78 | } |
54 | 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 | } | ||
205 | } | ||
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 | } | ... | ... |
-
Please register or sign in to post a comment