123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- export const getLocale = function() {
- const language = uni.getStorageSync('language')
- return language || 'zh-cn'
- }
- export const scrollToElement = (selector) => {
-
- const query = uni.createSelectorQuery().in(this);
-
- query.select('#targetElement').boundingClientRect(data => {
- if (data) {
-
- uni.pageScrollTo({
- scrollTop: data.top,
- duration: 300
- });
- }
- }).exec();
- }
- export const returnTimeFormat = function (startTime, endTime, lang = 'zh-cn') {
-
- const start_date = new Date(startTime);
- const end_date = new Date(endTime);
-
- const isSameDay = start_date.toDateString() === end_date.toDateString();
- const isSameMonth = start_date.getFullYear() === end_date.getFullYear() && start_date.getMonth() === end_date.getMonth();
- const isSameYear = start_date.getFullYear() === end_date.getFullYear();
-
- function padZero(num) {
- return String(num).padStart(2, '0');
- }
-
- function formatDate(date, lang) {
- const year = date.getFullYear();
- const month = padZero(date.getMonth() + 1);
- const day = padZero(date.getDate());
- const hours = padZero(date.getHours());
- const minutes = padZero(date.getMinutes());
- if (lang === 'zh-cn' || lang === 'ja-jp') {
- return `${year}年${month}月${day}日 ${hours}:${minutes}`;
- } else if (lang === 'en-us' || lang === 'en') {
- const options = { year: 'numeric', month: 'long', day: 'numeric', hour: 'numeric', minute: 'numeric' };
- return date.toLocaleDateString('en-US', options);
- }
- }
-
- function formatTime(date) {
- const hours = padZero(date.getHours());
- const minutes = padZero(date.getMinutes());
- return `${hours}:${minutes}`;
- }
- let month = padZero(end_date.getMonth() + 1);
- let day = padZero(end_date.getDate());
-
- if (lang === 'zh-cn' || lang === 'ja-jp') {
- if (isSameDay) {
- return `${formatDate(start_date, 'zh-cn')} - ${formatTime(end_date)}`;
- } else if (isSameMonth) {
- return `${formatDate(start_date, 'zh-cn')} - ${month}月${day}日 ${formatTime(end_date)}`;
- } else if (isSameYear) {
- return `${formatDate(start_date, 'zh-cn')} - ${month}月${day}日 ${formatTime(end_date)}`;
- } else {
- return `${formatDate(start_date, 'zh-cn')} - ${formatDate(end_date, 'zh-cn')}`;
- }
- } else if (lang === 'en-us' || lang === 'en') {
- if (isSameDay) {
- return `${formatDate(start_date, 'en-us')} - ${formatTime(end_date)}`;
- } else if (isSameMonth) {
- const endFormatted = formatDate(end_date, 'en-us').split(', ');
- return `${formatDate(start_date, 'en-us')} - ${endFormatted[1]}`;
- } else if (isSameYear) {
- const endFormatted = formatDate(end_date, 'en-us').split(', ');
- return `${formatDate(start_date, 'en-us')} - ${endFormatted[1]}`;
- } else {
- return `${formatDate(start_date, 'en-us')} - ${formatDate(end_date, 'en-us')}`;
- }
- }
- }
- export function returnEscapeHtml(unsafe) {
- return unsafe
- .replace(/&/g, "&")
- .replace(/</g, "<")
- .replace(/>/g, ">")
- .replace(/"/g, """)
- .replace(/'/g, "'");
- }
- export function convertRichTextToWXML(html) {
-
- const htmlTagRegex = /<\s*(\w+)(?:\s+(?:[\w:-]+)(?:\s*=\s*(?:"[^"]*"|'[^']*'|[\w-]+)|[\w-]*))?\s*(?:\/>|>([\s\S]*?)<\/\1>)/g;
-
- function convertTag(tag, content) {
-
- if (tag === 'p') {
- return `<view>${content}</view>`;
- }
-
- return `<${tag}>${content}</${tag}>`;
- }
-
- return html.replace(htmlTagRegex, (match, p1, p2) => {
- return convertTag(p1.toLowerCase(), p2 ? p2 : '');
- });
- }
|