util.js.map 5.3 KB

1
  1. {"version":3,"file":"util.js","sources":["common/util.js"],"sourcesContent":["// utils.js\n\nexport function formatTime(time) {\n\tif (typeof time !== 'number' || time < 0) {\n\t\treturn time.toString();\n\t}\n\n\tconst hour = Math.floor(time / 3600);\n\ttime %= 3600;\n\tconst minute = Math.floor(time / 60);\n\tconst second = time % 60;\n\n\treturn [hour, minute, second].map(n => n.toString().padStart(2, '0')).join(':');\n}\n\nexport function formatLocation(longitude, latitude) {\n\tif (typeof longitude === 'string' && typeof latitude === 'string') {\n\t\tlongitude = parseFloat(longitude);\n\t\tlatitude = parseFloat(latitude);\n\t}\n\n\treturn {\n\t\tlongitude: longitude.toFixed(2).split('.'),\n\t\tlatitude: latitude.toFixed(2).split('.')\n\t};\n}\n\nexport const dateUtils = {\n\tUNITS: {\n\t\t'年': 31557600000,\n\t\t'月': 2629800000,\n\t\t'天': 86400000,\n\t\t'小时': 3600000,\n\t\t'分钟': 60000,\n\t\t'秒': 1000\n\t},\n\thumanize(milliseconds) {\n\t\tfor (const key in this.UNITS) {\n\t\t\tif (milliseconds >= this.UNITS[key]) {\n\t\t\t\treturn Math.floor(milliseconds / this.UNITS[key]) + key + '前';\n\t\t\t}\n\t\t}\n\t\treturn '刚刚';\n\t},\n\tformat(dateStr) {\n\t\tconst date = this.parse(dateStr);\n\t\tconst diff = Date.now() - date.getTime();\n\n\t\tif (diff < this.UNITS['天']) {\n\t\t\treturn this.humanize(diff);\n\t\t}\n\n\t\tconst _format = number => (number < 10 ? `0${number}` : number);\n\n\t\treturn `${date.getFullYear()}/${_format(date.getMonth() + 1)}/${_format(date.getDate())} - ${_format(date.getHours())}:${_format(date.getMinutes())}`;\n\t},\n\tparse(str) { // 将\"yyyy-mm-dd HH:MM:ss\"格式的字符串,转化为一个Date对象\n\t\tconst a = str.split(/[^0-9]/);\n\t\treturn new Date(a[0], a[1] - 1, a[2], a[3], a[4], a[5]);\n\t}\n};\n\nexport function formateDates(datetime, type) {\n\tconst year = datetime.getFullYear();\n\tconst month = String(datetime.getMonth() + 1).padStart(2, '0');\n\tconst date = String(datetime.getDate()).padStart(2, '0');\n\tconst hour = String(datetime.getHours()).padStart(2, '0');\n\tconst minute = String(datetime.getMinutes()).padStart(2, '0');\n\tconst second = String(datetime.getSeconds()).padStart(2, '0');\n\n\tswitch (type) {\n\t\tcase \"Y-M-D h:min:s\":\n\t\t\treturn `${year}-${month}-${date} ${hour}:${minute}:${second}`;\n\t\tcase \"Y-M-D\":\n\t\t\treturn `${year}-${month}-${date}`;\n\t\tcase \"h:min:s\":\n\t\t\treturn `${hour}:${minute}:${second}`;\n\t\tdefault:\n\t\t\treturn '';\n\t}\n}\n\nexport function deteleObject(obj) {\n\tconst uniques = [];\n\tconst stringify = {};\n\tobj.forEach(item => {\n\t\tconst keys = Object.keys(item).sort();\n\t\tconst str = keys.map(key => JSON.stringify([key, item[key]])).join('');\n\t\tif (!stringify[str]) {\n\t\t\tuniques.push(item);\n\t\t\tstringify[str] = true;\n\t\t}\n\t});\n\treturn uniques;\n}\n\nexport function formateDate(datetime, type) {\n\tconst year = datetime.getFullYear();\n\tconst month = String(datetime.getMonth() + 1).padStart(2, '0');\n\tconst date = String(datetime.getDate()).padStart(2, '0');\n\tconst hour = String(datetime.getHours()).padStart(2, '0');\n\tconst minute = String(datetime.getMinutes()).padStart(2, '0');\n\tconst second = String(datetime.getSeconds()).padStart(2, '0');\n\n\tswitch (type) {\n\t\tcase \"Y-M-D h:min:s\":\n\t\t\treturn `${year}-${month}-${date} ${hour}:${minute}:${second}`;\n\t\tcase \"Y-M-D\":\n\t\t\treturn `${year}年${month}月${date}日`;\n\t\tcase \"h:min:s\":\n\t\t\treturn `${hour}:${minute}:${second}`;\n\t\tcase \"h\":\n\t\t\treturn hour;\n\t\tcase \"min\":\n\t\t\treturn minute;\n\t\tdefault:\n\t\t\treturn '';\n\t}\n}\n\nexport function randomNum(minNum, maxNum = null) {\n\tif (maxNum === null) {\n\t\tmaxNum = minNum;\n\t\tminNum = 0;\n\t}\n\treturn Math.floor(Math.random() * (maxNum - minNum + 1)) + minNum;\n}\n\nexport function pointInsideCircle(point, circle, r) {\n\tif (r === 0) return false;\n\tconst dx = circle[0] - point[0];\n\tconst dy = circle[1] - point[1];\n\treturn dx * dx + dy * dy <= r * r;\n}\n\nexport function isSameDay(timeStampA) {\n\tconst dateA = new Date(timeStampA);\n\tconst dateB = new Date();\n\treturn dateA.setHours(0, 0, 0, 0) === dateB.setHours(0, 0, 0, 0);\n}\n\nexport function getCurrentMonthFirst() {\n\tconst date = new Date();\n\tdate.setDate(1);\n\treturn `${date.getFullYear()}年${date.getMonth() + 1}月${date.getDate()}日`;\n}\n\nexport function getCurrentMonthLast() {\n\tconst date = new Date();\n\tdate.setMonth(date.getMonth() + 1, 0);\n\treturn `${date.getFullYear()}年${date.getMonth() + 1}月${date.getDate()}日`;\n}"],"names":[],"mappings":";AAgGO,SAAS,YAAY,UAAU,MAAM;AAC3C,QAAM,OAAO,SAAS;AACtB,QAAM,QAAQ,OAAO,SAAS,SAAQ,IAAK,CAAC,EAAE,SAAS,GAAG,GAAG;AAC7D,QAAM,OAAO,OAAO,SAAS,QAAS,CAAA,EAAE,SAAS,GAAG,GAAG;AACvD,QAAM,OAAO,OAAO,SAAS,SAAU,CAAA,EAAE,SAAS,GAAG,GAAG;AACxD,QAAM,SAAS,OAAO,SAAS,WAAY,CAAA,EAAE,SAAS,GAAG,GAAG;AAC5D,QAAM,SAAS,OAAO,SAAS,WAAY,CAAA,EAAE,SAAS,GAAG,GAAG;AAE5D,UAAQ,MAAI;AAAA,IACX,KAAK;AACJ,aAAO,GAAG,IAAI,IAAI,KAAK,IAAI,IAAI,IAAI,IAAI,IAAI,MAAM,IAAI,MAAM;AAAA,IAC5D,KAAK;AACJ,aAAO,GAAG,IAAI,IAAI,KAAK,IAAI,IAAI;AAAA,IAChC,KAAK;AACJ,aAAO,GAAG,IAAI,IAAI,MAAM,IAAI,MAAM;AAAA,IACnC,KAAK;AACJ,aAAO;AAAA,IACR,KAAK;AACJ,aAAO;AAAA,IACR;AACC,aAAO;AAAA,EACR;AACF;AAiBO,SAAS,UAAU,YAAY;AACrC,QAAM,QAAQ,IAAI,KAAK,UAAU;AACjC,QAAM,QAAQ,oBAAI;AAClB,SAAO,MAAM,SAAS,GAAG,GAAG,GAAG,CAAC,MAAM,MAAM,SAAS,GAAG,GAAG,GAAG,CAAC;AAChE;;;"}