|
- /**
- * @description:使用JSON进行深度拷贝
- */
- export function deepCopy(data) {
- const copyData = JSON.stringify(data)
- return JSON.parse(copyData)
- }
-
- /**
- * @description 将bytes转化
- * @param {number} bytes
- * @return string
- */
- export function bytesToSize(bytes) {
- if (bytes === 0) return '0 B';
- var k = 1000, // or 1024
- sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'],
- i = Math.floor(Math.log(bytes) / Math.log(k));
-
- return (bytes / Math.pow(k, i)).toPrecision(3) + ' ' + sizes[i];
- }
-
- /**
- * @description 秒转化时分秒
- * @param {number} seconds
- * @return string
- */
- export function formatSeconds(value) {
- var theTime = parseInt(value);// 秒
- var theTime1 = 0;// 分
- var theTime2 = 0;// 小时
- if (theTime > 60) {
- theTime1 = parseInt(theTime / 60);
- theTime = parseInt(theTime % 60);
- if (theTime1 > 60) {
- theTime2 = parseInt(theTime1 / 60);
- theTime1 = parseInt(theTime1 % 60);
- }
- }
- var result = "" + parseInt(theTime) + " 秒";
- if (theTime1 > 0) {
- result = "" + parseInt(theTime1) + " 分" + result;
- }
- if (theTime2 > 0) {
- result = "" + parseInt(theTime2) + " 小时" + result;
- }
- return result;
- }
-
- /**
- * @description:滚动到对应id的位置
- * @param {type} id
- */
- export function scrollToID(id) {
- document.querySelector("#" + id).scrollIntoView({
- behavior: "smooth",
- block: "start",
- inline: "nearest",
- });
- }
-
- /**
- * @description 根据时间戳获取时间
- * @param {*} timestamp 必传,number类型,时间戳数据(10位及以下,10位至13位)
- * @param {*} format 选传,string类型,提供以下时间格式:YYYY-MM-DD hh:mm:ss、YYYY/MM/DD hh:mm:ss、YYYY.MM.DD hh:mm:ss、YYYY MM DD hh:mm:ss、YYYY年MM月DD日 hh:mm:ss、YYYY-MM-DD、YYYY/MM/DD、YYYY.MM.DD、YYYY MM DD、YYYY年MM月DD日;若不传,则默认为:YYYY-MM-DD
- * @returns 根据要求的时间格式
- * @version V 1.0, Created by YWQ, 2022.10.20
- */
- export function timestampToTime(timestamp, format) {
- //时间戳为10位需*1000,时间戳为13位不需乘1000
- const length = timestamp && timestamp.length ? timestamp.length : ''
- if (!length) return ""
- let date
- if (length <= 10) {
- date = new Date(timestamp * 1000)
- } else {
- date = new Date(timestamp)
- }
- let Y = String(date.getFullYear())
- let M = String(date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1)
- let D = String(date.getDate() + 1 < 10 ? '0' + (date.getDate()) : date.getDate())
- let h = String(date.getHours() + 1 < 10 ? '0' + (date.getHours()) : date.getHours())
- let m = String(date.getMinutes() + 1 < 10 ? '0' + (date.getMinutes()) : date.getMinutes())
- let s = String(date.getSeconds() + 1 < 10 ? '0' + (date.getSeconds()) : date.getSeconds())
- // return Y + M + D + h + m + s
- if (format == "YYYY-MM-DD hh:mm:ss") {
- return Y + "-" + M + "-" + D + " " + h + ":" + m + ":" + s
- } else if (format == "YYYY/MM/DD hh:mm:ss") {
- return Y + "/" + M + "/" + D + " " + h + ":" + m + ":" + s
- } else if (format == "YYYY.MM.DD hh:mm:ss") {
- return Y + "." + M + "." + D + " " + h + ":" + m + ":" + s
- } else if (format == "YYYY MM DD hh:mm:ss") {
- return Y + " " + M + " " + D + " " + h + ":" + m + ":" + s
- } else if (format == "YYYY年MM月DD日 hh:mm:ss") {
- return Y + "年" + M + "月" + D + "日" + " " + h + ":" + m + ":" + s
- } else if (format == "YYYY-MM-DD") {
- return Y + "-" + M + "-" + D
- } else if (format == "YYYY/MM/DD") {
- return Y + "/" + M + "/" + D
- } else if (format == "YYYY.MM.DD") {
- return Y + "." + M + "." + D
- } else if (format == "YYYY MM DD") {
- return Y + " " + M + " " + D
- } else if (format == "YYYY年MM月DD日") {
- return Y + "年" + M + "月" + D + "日"
- } else {
- return Y + "-" + M + "-" + D
- }
- }
-
- // /**
- // * @description:获取设备类型
- // * @returns PC端为null,移动端为设备类型(Array)
- // */
- // export function getDeviceType() {
- // return navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i)
- // }
-
- /**
- * @description:根据经纬度计算距离
- * @param {*} locationInfo (lat1, lng1, lat2, lng2)
- * @return: distanceObj: { distance , distance_str }
- */
- export function getDistances(lat1, lng1, lat2, lng2) {
-
- function rad(d) {
- return d * Math.PI / 180.0;
- }
- var radLat1 = rad(lat1);
- var radLat2 = rad(lat2);
- var a = radLat1 - radLat2;
- var b = rad(lng1) - rad(lng2);
- var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) +
- Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
- s = s * 6378.137;
- s = Math.round(s * 10000) / 10000;
-
- var distance = s;
- var distance_str = "";
-
- if (parseInt(distance) >= 1) {
- distance_str = distance.toFixed(2) + "km";
- } else {
- distance_str = (distance * 1000).toFixed(2) + "m";
- }
-
- let objData = {
- distance: distance,
- distance_str: distance_str
- }
- return objData
- }
-
- /**
- * @description:文本打字机
- * @param {*} data (text,target,duration)
- */
- export function typeWriter(text, target, duration) {
- let i = 0;
- let timer = setInterval(() => {
- target += text[i];
- if (i + 1 == text.length) {
- clearInterval(timer);
- } else {
- i++;
- }
- }, duration || 100);
- }
|