import store from '../store'; import { Toast } from 'vant'; import { phoneCheck } from '@/api/index'; // 日期格式化 export function parseTime(time, pattern) { if (time != 'null') { if (arguments.length === 0 || !time) { return null; } const format = pattern || '{y}-{m}-{d} {h}:{i}:{s}'; let date; if (typeof time === 'object') { date = time; } else { if (typeof time === 'string' && /^[0-9]+$/.test(time)) { time = parseInt(time); } else if (typeof time === 'string') { time = time .replace(new RegExp(/-/gm), '/') .replace('T', ' ') .replace(new RegExp(/\.[\d]{3}/gm), ''); } if (typeof time === 'number' && time.toString().length === 10) { time = time * 1000; } date = new Date(time); } const formatObj = { y: date.getFullYear(), m: date.getMonth() + 1, d: date.getDate(), h: date.getHours(), i: date.getMinutes(), s: date.getSeconds(), a: date.getDay(), }; const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => { let value = formatObj[key]; // Note: getDay() returns 0 on Sunday if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value]; } if (result.length > 0 && value < 10) { value = '0' + value; } return value || 0; }); return time_str; } else { return ''; } } // 千分号 // 中文日期格式(年-月-日) export function formatChineseDate(time) { // 统一处理日期格式 const formatDate = (dateObj) => { const year = dateObj.getFullYear(); const month = (dateObj.getMonth() + 1).toString().padStart(2, '0'); const day = dateObj.getDate().toString().padStart(2, '0'); return `${year}年${month}月${day}日`; }; // 处理字符串格式(支持多种分隔符) if (typeof time === 'string') { // 清理日期字符串中的时间部分(如果有) const dateString = time.split(' ')[0]; // 匹配 yyyy-mm-dd 或 yyyy/m/d 等格式 if (/^\d{4}[-\/]\d{1,2}[-\/]\d{1,2}$/.test(dateString)) { const separator = dateString.includes('-') ? '-' : '/'; const [year, month, day] = dateString.split(separator); return formatDate(new Date(year, month - 1, day)); } } // 处理时间戳和Date对象 try { const date = new Date(time); if (!isNaN(date)) { return formatDate(date); } } catch (e) { console.error('Invalid date format:', time); } return '无效日期格式'; } export function Micrometer(num) { if (num != null) { let numt = (num || 0).toString().split('.'); if (numt[1] == undefined) { return numt[0].replace(/(\d)(?=(?:\d{3})+$)/g, '$1,'); } else { return numt[0].replace(/(\d)(?=(?:\d{3})+$)/g, '$1,') + '.' + numt[1]; } } else { return 0.0; } } export function weeklyTimeDivision(time, type) { if (type == 0) { return time.split(' ')[0] + ' '; } else { return time.split(' ')[1]; } } export function parseTimeParagraph(dayTime) { var currentDate = new Date(dayTime); var timesStamp = currentDate.getTime(); var currenDay = currentDate.getDay(); var dates = []; var tabTime = []; for (var i = 0; i < 14; i++) { var dataTime = new Date(timesStamp + 24 * 60 * 60 * 1000 * (i - ((currenDay + 6) % 7))) .toLocaleDateString() .replace(/\//g, '-'); var dataTimeArr = dataTime.split('-'); if (dataTimeArr[1] < 10) { dataTimeArr[1] = '0' + dataTimeArr[1]; } if (dataTimeArr[2] < 10) { dataTimeArr[2] = '0' + dataTimeArr[2]; } dates.push( dataTimeArr.join('-') + ' ' + ['周日', '周一', '周二', '周三', '周四', '周五', '周六'][ new Date(dataTime.toString().replace(/-/g, '/')).getDay() ], ); } tabTime.push(dates[0]); tabTime.push(dates[4]); return dates; } export function weeklay(dataTime) { return ['周日', '周一', '周二', '周三', '周四', '周五', '周六'][new Date(dataTime).getDay()]; } // 回显数据字典 export function selectDictLabel(datas, value) { var actions = []; Object.keys(datas).some((key) => { if (datas[key].dictValue == '' + value) { actions.push(datas[key].dictLabel); return true; } }); return actions.join(''); } export function selectDictLabelu(datas, value) { var actions = []; Object.keys(datas).some((key) => { if (datas[key].dictValue == '' + value) { actions.push(datas[key].text); return true; } }); return actions.join(''); } // 回显数据字典(字符串数组) export function selectDictLabels(datas, value, separator) { var actions = []; var currentSeparator = undefined === separator ? ',' : separator; var temp = value.split(currentSeparator); Object.keys(value.split(currentSeparator)).some((val) => { Object.keys(datas).some((key) => { if (datas[key].dictValue == '' + temp[val]) { actions.push(datas[key].dictLabel + currentSeparator); } }); }); return actions.join('').substring(0, actions.join('').length - 1); } // 字符串格式化(%s ) export function sprintf(str) { var args = arguments, flag = true, i = 1; str = str.replace(/%s/g, function () { var arg = args[i++]; if (typeof arg === 'undefined') { flag = false; return ''; } return arg; }); return flag ? str : ''; } // 转换字符串,undefined,null等转化为"" export function praseStrEmpty(str) { if (!str || str == 'undefined' || str == 'null') { return ''; } return str; } // export function twoPointSum(latA, lonA, latB, lonB) { var coordinate = 6371000; var PI = 3.14159265358979324; var x = Math.cos((latA * PI) / 180) * Math.cos((latB * PI) / 180) * Math.cos(((lonA - lonB) * PI) / 180); var y = Math.sin((latA * PI) / 180) * Math.sin((latB * PI) / 180); var s = x + y; if (s > 1) s = 1; if (s < -1) s = -1; var alphabet = Math.acos(s); var PointSum = alphabet * coordinate; return PointSum; } export function CJ02BD(gcjLat, gcjLon) { var x = gcjLon, y = gcjLat; var x_pi = (3.14159265358979324 * 3000.0) / 180.0; var z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi); var theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi); var bdLon = z * Math.cos(theta) + 0.0065; var bdLat = z * Math.sin(theta) + 0.006; return { lat: bdLat, lon: bdLon, }; } export function gcj02BD(gcjLat, gcjLon) { var x = gcjLon - 0.0065, y = gcjLat - 0.006; var x_pi = (3.14159265358979324 * 3000.0) / 180.0; var z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi); var theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi); var bdLon = z * Math.cos(theta); var bdLat = z * Math.sin(theta); return { lat: bdLat, lon: bdLon }; } /** *门店类型集合,返回对应的类型 *@param {*String} dictValue //类型 * */ export function verifyStoreType(dictValue) { if (!dictValue) return null; let storeData = null; storeData = store.getters.storeType.find((val) => val.dictValue == dictValue); let remarkType = storeData ? JSON.parse(storeData.remark) : null; return remarkType; } export function getMonthCommon() { let timeData = ''; // 获取当前日期 var currentDate = new Date(); // 获取当前月份 var currentMonth = currentDate.getMonth(); var previousMonthDate1 = new Date(); if (currentDate.getDate() == 1) { previousMonthDate1.setMonth(currentMonth - 1); } var previousMonth1 = previousMonthDate1.getMonth(); var previousYear1 = previousMonthDate1.getFullYear(); // 计算前三个月的年份和月份 var previousMonthDate = new Date(); if (currentDate.getDate() == 1) { previousMonthDate.setMonth(currentMonth - 3); } else { previousMonthDate.setMonth(currentMonth - 2); } var previousMonth = previousMonthDate.getMonth(); var previousYear = previousMonthDate.getFullYear(); //前三个月 if (previousYear1 == previousYear) { var formattedPreviousMonth1 = previousYear1 + '-' + (previousMonth1 + 1); // 格式化年份和月份 var formattedPreviousMonth = previousYear + '-' + (previousMonth + 1); timeData = formattedPreviousMonth.split('-')[1] + '-' + formattedPreviousMonth1.split('-')[1] + '月'; } else { var formattedPreviousMonth1 = previousYear1 + '年' + (previousMonth1 + 1) + '月'; // .toString().padStart(2, '0'); // 格式化年份和月份 var formattedPreviousMonth = previousYear + '年' + (previousMonth + 1) + '月'; timeData = formattedPreviousMonth + '-' + formattedPreviousMonth1; } return timeData; } // 手机号校验 export function validatePhone(telephone) { return new Promise((resolve, reject) => { if (telephone == '' || !telephone) { Toast('请输入手机号'); reject(); } var telrg = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/; if (telephone.trim() == '') { reject(); } else if (!telrg.test(telephone)) { Toast('手机号格式错误'); reject(); } else { phoneCheck({ phoneNumber: telephone }).then((res) => { if (res.code == 200) { resolve(); } else { Toast(res.msg); reject(); } }); } }); } // 分享视频链接 export function shareVedioLinks() { window.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=ww5444eb205d75e730&redirect_uri=https%3A%2F%2Fknowledgewiki.nipponpaint.com.cn%2Fapi%2Fauth%2Fwx%2FshareOuth%3Fguid%3Dd4ac9b85658570389cfebe70e4505071%26shareId%3DSHARE21482&response_type=code&scope=snsapi_base&state=ZyKPSJuKA6-ZikabG6WSBqgGRsjsYK8j6P5I2bVdqOs&agentid=1000291#wechat_redirect'; } export function convertToChinese(num) { if (num === 0) return '零'; const digitMap = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九']; const units = ['', '十', '百', '千']; const bigUnits = ['', '万', '亿']; let str = num.toString(); let result = ''; let len = str.length; for (let i = 0; i < len; i++) { let digit = parseInt(str[i]); let pos = len - 1 - i; if (digit !== 0) { result += digitMap[digit] + units[pos % 4]; } else if (result && result[result.length - 1] !== '零') { result += '零'; } if (pos % 4 === 0 && pos > 0) { result += bigUnits[Math.floor(pos / 4)]; } } // 清理多余的零 result = result.replace(/零+/g, '零').replace(/^零|零$/g, ''); // 特殊处理:如果以"一十"开头,去掉"一" if (result.startsWith('一十')) { result = result.substring(1); } return result; }