mingdeng-universal-utils 0.0.9 → 0.1.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mingdeng-universal-utils",
3
- "version": "0.0.9",
3
+ "version": "0.1.3",
4
4
  "description": "JavaScript通用utils",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -261,7 +261,9 @@ const openWindow = (url, title, w, h) => {
261
261
  */
262
262
  const formatDate = (oldDate, fmt = 'yyyy-MM-dd hh:mm:ss') => {
263
263
  let date = new Date()
264
- if (typeof oldDate === 'string' || typeof oldDate === 'number') {
264
+ if (typeof oldDate === 'string') {
265
+ date = new Date(oldDate.replace(/\-/g, '/'))
266
+ } else if (typeof oldDate === 'number') {
265
267
  date = new Date(oldDate)
266
268
  } else {
267
269
  date = oldDate
@@ -303,19 +305,24 @@ export const BrowserInfo = {
303
305
 
304
306
  /**
305
307
  * 函数防抖
306
- * @param {Function} func 函数
307
- * @param {Number} wait 等待时间
308
+ * @param {Function} callback 函数
309
+ * @param {Number} timeout 等待时间
310
+ * @param {Boolean} immediate 决定触发时机
308
311
  * @return {Function}
309
312
  */
310
- const debounce = (func, wait) => {
311
- let timeout;
313
+ const debounce = (callback, timeout, immediate) => {
314
+ let timer;
312
315
  return function () {
313
- let context = this;
314
- let args = arguments;
315
- if (timeout) clearTimeout(timeout);
316
- timeout = setTimeout(() => {
317
- func.apply(context, args)
318
- }, wait);
316
+ const context = this; // 持有执行上下文
317
+ const args = arguments; // 记录传参
318
+ const later = function () {
319
+ timer = null; // 贤者时间过了,重振旗鼓,重置为初始状态
320
+ if (!immediate) callback.apply(context, args); // 设置为尾部调用才延时触发
321
+ }
322
+ const callNow = immediate && !timer; // 如果确认允许首部调用,且首次调用,那么本次立即触发
323
+ clearTimeout(timer); // 杀掉上次的计时器,重新计时
324
+ timer = setTimeout(later, timeout); // 重启一个计时器,过了贤者时间之后才触发
325
+ callNow && callback.apply(context, args); // 设置为首部调用立即触发
319
326
  }
320
327
  }
321
328
 
@@ -0,0 +1,70 @@
1
+
2
+ /**
3
+ * 上传图片
4
+ * @param {Number} count 单次可选数量
5
+ * @param {Array<String>} sizeType original 原图,compressed 压缩图,默认二者都有
6
+ * @param {Object} [config]
7
+ * @param {string} [config.uploadUrl] 上传地址
8
+ * @param {string} [config.token] token
9
+ */
10
+ const uploadPics = (count = 1, sizeType, config) => {
11
+ let requestPicUrlList = []
12
+ return new Promise((resolve, reject) => {
13
+ uni.chooseImage({
14
+ count,
15
+ sizeType,
16
+ success: chooseImageRes => {
17
+ uni.showLoading({
18
+ title: '上传中...'
19
+ })
20
+ const tempFilePaths = chooseImageRes.tempFilePaths;
21
+ tempFilePaths.map(item => {
22
+ uploadFile(item, config).then(url => {
23
+ requestPicUrlList.push(url)
24
+ if (tempFilePaths.length === requestPicUrlList.length) {
25
+ uni.hideLoading()
26
+ resolve(requestPicUrlList)
27
+ }
28
+ }).catch(() => {
29
+ reject()
30
+ });
31
+ });
32
+ }, fail: () => {
33
+ reject()
34
+ }
35
+ });
36
+ })
37
+ }
38
+
39
+ const uploadFile = (filePath, config) => {
40
+ return new Promise((resolve, reject) => {
41
+ const { uploadUrl, token } = config
42
+ uni.uploadFile({
43
+ url: uploadUrl, //仅为示例,非真实的接口地址
44
+ filePath,
45
+ header: {
46
+ token
47
+ },
48
+ name: 'file',
49
+ success: uploadFileRes => {
50
+ console.log("🚀 ~ file: index.js ~ line 50 ~ returnnewPromise ~ uploadFileRes", uploadFileRes)
51
+ if (uploadFileRes.statusCode === 200) {
52
+ resolve(JSON.parse(uploadFileRes.data).data)
53
+ } else {
54
+ uni.hideLoading()
55
+ uni.showToast({ title: 'statusCode ' + uploadFileRes.statusCode, icon: 'none' })
56
+ reject()
57
+ }
58
+ },
59
+ fail: err => {
60
+ uni.showToast({
61
+ title: err.data.message || err,
62
+ icon: 'none'
63
+ });
64
+ reject()
65
+ }
66
+ });
67
+ })
68
+
69
+ }
70
+ export { uploadPics, }