202 lines
4.9 KiB
JavaScript
202 lines
4.9 KiB
JavaScript
![]() |
/**
|
|||
|
* Parse the time to string
|
|||
|
* @param {(Object|string|number)} time
|
|||
|
* @param {string} cFormat
|
|||
|
* @returns {string | null}
|
|||
|
*/
|
|||
|
export function parseTime(time, cFormat) {
|
|||
|
if (arguments.length === 0 || !time) {
|
|||
|
return null
|
|||
|
}
|
|||
|
const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
|
|||
|
let date
|
|||
|
if (typeof time === 'object') {
|
|||
|
date = time
|
|||
|
} else {
|
|||
|
if ((typeof time === 'string')) {
|
|||
|
if ((/^[0-9]+$/.test(time))) {
|
|||
|
time = parseInt(time)
|
|||
|
} else {
|
|||
|
time = time.replace(new RegExp(/-/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()
|
|||
|
}
|
|||
|
// eslint-disable-next-line camelcase
|
|||
|
const time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => {
|
|||
|
const value = formatObj[key]
|
|||
|
// Note: getDay() returns 0 on Sunday
|
|||
|
if (key === 'a') {
|
|||
|
return ['日', '一', '二', '三', '四', '五', '六'][value]
|
|||
|
}
|
|||
|
return value.toString().padStart(2, '0')
|
|||
|
})
|
|||
|
// eslint-disable-next-line camelcase
|
|||
|
return time_str
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* 过滤时间 格式化当天时间 比如把"20:00:00" 格式化成(晚上)8:00
|
|||
|
* @param {string} value 时间格式位"20:00:00"
|
|||
|
*/
|
|||
|
export function formatTime(value) {
|
|||
|
if (!value) return '' // 如果时间为空,则返回空字符串
|
|||
|
|
|||
|
// 将时间字符串转换为 Date 对象
|
|||
|
let time = new Date("2000/01/01 " + value)
|
|||
|
|
|||
|
// 获取小时和分钟
|
|||
|
let hour = time.getHours()
|
|||
|
let minute = time.getMinutes()
|
|||
|
|
|||
|
// 根据小时判断是上午还是下午
|
|||
|
let period = hour >= 12 ? '下午' : '上午'
|
|||
|
|
|||
|
// 格式化小时,如果是下午,则减去12小时
|
|||
|
hour = hour % 12 === 0 ? 12 : hour % 12
|
|||
|
|
|||
|
// 格式化分钟,补零操作
|
|||
|
minute = minute.toString().padStart(2, '0')
|
|||
|
|
|||
|
return `(${period})${hour}:${minute}`
|
|||
|
}
|
|||
|
/**
|
|||
|
* 过滤价格数值 超过小数点两位得 保留两位
|
|||
|
* @param {string} value
|
|||
|
*/
|
|||
|
export function formatPrice(value) {
|
|||
|
if (isNaN(value)) return '' // 如果不是数字,则返回空字符串
|
|||
|
|
|||
|
// 格式化价格为保留两位小数
|
|||
|
let formattedPrice = parseFloat(value).toFixed(2)
|
|||
|
|
|||
|
// 如果价格有两位小数以上,则保留两位小数,否则显示整数或者一位小数
|
|||
|
let decimalCount = (formattedPrice.split('.')[1] || []).length
|
|||
|
if (decimalCount > 2) {
|
|||
|
formattedPrice = parseFloat(formattedPrice).toFixed(2)
|
|||
|
}
|
|||
|
|
|||
|
return formattedPrice
|
|||
|
}
|
|||
|
/**
|
|||
|
* 计算单spu 的单价 ps: spu可能有单sku组成 也可能
|
|||
|
* @param {string} value
|
|||
|
*/
|
|||
|
export function calculateTotal(order) {
|
|||
|
// 检查参数是否有效
|
|||
|
if (!order || typeof order !== 'object' || !order.spu_id || !order.skuG || !order.countG || !order
|
|||
|
.priceG) {
|
|||
|
return ''
|
|||
|
}
|
|||
|
// 计算总价
|
|||
|
let total = 0
|
|||
|
for (let i = 0; i < order.skuG.length; i++) {
|
|||
|
let price = parseFloat(order.priceG[i])
|
|||
|
if (!isNaN(price)) {
|
|||
|
total += price
|
|||
|
}
|
|||
|
}
|
|||
|
// 保留两位小数并返回
|
|||
|
return total.toFixed(2)
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* 计算单购物车列表商品的价格总和
|
|||
|
* @param {string} cartList 购物车列表
|
|||
|
* @returns {string | null} 价格总和
|
|||
|
*/
|
|||
|
export function totalPrice(cartList) {
|
|||
|
let totalPrice = 0
|
|||
|
if (cartList.length !== 0) {
|
|||
|
cartList.forEach(item => {
|
|||
|
for (let i = 0; i < item.priceG.length; i++) {
|
|||
|
totalPrice += item.priceG[i] * item.countG[i]
|
|||
|
}
|
|||
|
})
|
|||
|
}
|
|||
|
return Number(totalPrice) // 将 totalPrice 转换为小数点后两位的格式
|
|||
|
}
|
|||
|
/**
|
|||
|
* 检查用户信息登录 信息
|
|||
|
* @param {obj} store vex $store对象
|
|||
|
*/
|
|||
|
export function checkUserInfo(store) {
|
|||
|
//用户信息 从本地缓存 提取到内存
|
|||
|
if (!store.state.userInfo.token) {
|
|||
|
uni.getStorage({
|
|||
|
key: 'userInfo',
|
|||
|
success: (res) => {
|
|||
|
store.commit('setUserInfo', res.data)
|
|||
|
},
|
|||
|
fail: (err) => {
|
|||
|
wxLogin(store)
|
|||
|
}
|
|||
|
});
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* 微信登录 ps;无感登录
|
|||
|
* @param {obj} store vex $store对象
|
|||
|
*/
|
|||
|
export function wxLogin(store) {
|
|||
|
uni.login({
|
|||
|
provider: 'weixin',
|
|||
|
success: res => {
|
|||
|
if (res.code) {
|
|||
|
uni.$u.http.post('/Api/Login/authLogin', {
|
|||
|
code: res.code
|
|||
|
}, {
|
|||
|
header: {
|
|||
|
'Content-Type': 'application/x-www-form-urlencoded'
|
|||
|
}
|
|||
|
}).then(res => {
|
|||
|
// 登录接口访问成功后
|
|||
|
if (res.data.status === 0) {
|
|||
|
uni.showToast({
|
|||
|
title: res.data.msg,
|
|||
|
icon: 'none',
|
|||
|
duration: 2000
|
|||
|
})
|
|||
|
} else if (res.data.status === 1) {
|
|||
|
//写入 用户信息缓存
|
|||
|
uni.setStorage({
|
|||
|
key: 'userInfo',
|
|||
|
data: res.data.userInfo,
|
|||
|
success: () => {
|
|||
|
//登录成功后 把用户信息提取到内存
|
|||
|
store.commit('setUserInfo', res.data.userInfo)
|
|||
|
}
|
|||
|
})
|
|||
|
}
|
|||
|
})
|
|||
|
} else {
|
|||
|
uni.showToast({
|
|||
|
title: '登录态失败',
|
|||
|
icon: 'none',
|
|||
|
duration: 2000
|
|||
|
})
|
|||
|
}
|
|||
|
},
|
|||
|
fail: err => {
|
|||
|
uni.showToast({
|
|||
|
title: '登录态失败',
|
|||
|
icon: 'none',
|
|||
|
duration: 2000
|
|||
|
})
|
|||
|
}
|
|||
|
})
|
|||
|
}
|