You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
yudao/front-end/mall4m/utils/util.js

63 lines
4.0 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

// 这个函数用于将给定的日期对象格式化为特定的字符串格式,格式为 "年/月/日 时:分:秒"
const formatTime = date => {
// 获取日期对象中的年份信息
const year = date.getFullYear();
// 获取日期对象中的月份信息需要注意的是JavaScript中月份是从0开始计数的所以这里要加1得到实际的月份值
const month = date.getMonth() + 1;
// 获取日期对象中的日信息
const day = date.getDate();
// 获取日期对象中的小时信息
const hour = date.getHours();
// 获取日期对象中的分钟信息
const minute = date.getMinutes();
// 获取日期对象中的秒信息
const second = date.getSeconds();
// 先将年、月、日组成的数组中的每个元素通过formatNumber函数进行格式化处理然后使用"/"将它们连接起来;
// 再将小时、分钟、秒组成的数组中的每个元素通过formatNumber函数进行格式化处理然后使用":"将它们连接起来;
// 最后将这两部分用空格连接起来,形成最终的格式化后的时间字符串并返回
return [year, month, day].map(formatNumber).join('/') +'' + [hour, minute, second].map(formatNumber).join(':');
}
// 这个函数用于将数字格式化为固定长度的字符串格式,如果数字是个位数,则在前面添加"0"
const formatNumber = n => {
// 将传入的参数转换为字符串类型
n = n.toString();
// 如果字符串长度大于1说明不是个位数直接返回原字符串否则在字符串前面添加"0"后返回
return n[1]? n : '0' + n;
}
// 这个函数用于对HTML内容字符串进行样式相关的格式化处理主要是调整图片和表格单元格的样式
const formatHtml = content => {
// 查找HTML内容中所有的<img>标签并给它们添加特定的内联样式设置宽度为100%、高度自适应、外边距为0以及以flex布局显示
content = content.replace(/\<img/gi, '<img style="width:100%!important;height:auto!important;margin:0;display:flex;" ');
// 查找HTML内容中所有的<td>标签,并给它们添加特定的内联样式,设置单元格间距、内边距、边框等样式属性,使其更符合特定的布局需求
content = content.replace(/\<td/gi, '<td cellspacing="0" cellpadding="0" border="0" style="display:block;vertical-align:top;margin: 0px; padding: 0px; border: 0px;outline-width:0px;" ');
// 将HTML内容中所有的width=属性替换为sss=,这里可能是为了暂时去除原有的宽度设置,后续再统一处理或者有其他特殊用途
content = content.replace(/width=/gi, 'sss=');
// 将HTML内容中所有的height=属性替换为sss=作用类似于对width属性的处理可能是为了后续统一管理高度相关样式
content = content.replace(/height=/gi, 'sss=');
// 查找HTML内容中所有的结束标签 />并给对应的标签添加特定的内联样式设置最大宽度为100%、高度自适应、外边距为0以及以块级元素显示然后替换原内容中的结束标签部分
content = content.replace(/ \/\>/gi, ' style="max-width:100%!important;height:auto!important;margin:0;display:block;" \/\>');
// 返回处理后的HTML内容字符串
return content;
}
// 这个函数的作用是移除购物车Tabbar上显示的数字可能是未读消息数量等提示信息通过调用微信小程序的API来实现
// 这里假设使用的是微信小程序的开发环境wx是小程序的全局对象removeTabBarBadge是其提供的用于移除Tabbar角标数字提示的方法
/**
* 移除购物车Tabbar的数字
*/
const removeTabBadge = () => {
wx.removeTabBarBadge({
// 指定要移除角标的Tabbar选项卡的索引这里的2表示购物车对应的选项卡索引具体索引值可能根据小程序的实际布局而定
index: 2
})
}
// 将上述定义的几个函数作为模块的属性进行导出,方便其他模块引入并使用这些功能函数
module.exports = {
formatTime: formatTime,
formatHtml: formatHtml,
removeTabBadge: removeTabBadge
}