Files
gym-manage/gym-manage-coach/utils/index.js
T

134 lines
3.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 工具函数
*/
/**
* 格式化日期时间
*/
export function formatDateTime(dateStr, showTime = true) {
if (!dateStr) return '--'
const d = new Date(dateStr)
const pad = (n) => String(n).padStart(2, '0')
const date = `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}`
if (!showTime) return date
return `${date} ${pad(d.getHours())}:${pad(d.getMinutes())}`
}
/**
* 格式化日期
*/
export function formatDate(dateStr) {
return formatDateTime(dateStr, false)
}
/**
* 格式化时间
*/
export function formatTime(dateStr) {
if (!dateStr) return '--'
const d = new Date(dateStr)
const pad = (n) => String(n).padStart(2, '0')
return `${pad(d.getHours())}:${pad(d.getMinutes())}`
}
/**
* 格式化时间(短格式:MM/DD HH:mm
*/
export function formatTimeShort(dateStr) {
if (!dateStr) return '--'
const d = new Date(dateStr)
const pad = (n) => String(n).padStart(2, '0')
return `${d.getMonth() + 1}/${d.getDate()} ${pad(d.getHours())}:${pad(d.getMinutes())}`
}
/**
* 课程状态(由后端 GET /api/groupCourse/statuses 统一提供,避免前后端各自硬编码)
*/
let _statusMap = null
let _statusClassMap = null
const DEFAULT_STATUS_MAP = { '0': '正常', '1': '已取消', '2': '已结束', '3': '进行中', '4': '超时' }
const DEFAULT_STATUS_CLASS = { '0': 'green', '1': 'orange', '2': 'gray', '3': 'blue', '4': 'red' }
/**
* 从后端加载课程状态映射,返回 Promise<boolean>
*/
export async function loadCourseStatuses() {
try {
const res = await uni.request({
url: `${getBaseUrl()}/api/groupCourse/statuses`,
method: 'GET'
})
if (res.statusCode === 200 && res.data?.success && Array.isArray(res.data.data)) {
const map = {}
const cls = {}
res.data.data.forEach(item => {
map[String(item.dbValue)] = item.label
cls[String(item.dbValue)] = item.cssClass
})
_statusMap = map
_statusClassMap = cls
return true
}
} catch (e) {
console.warn('加载课程状态映射失败,使用默认映射:', e)
}
return false
}
function getBaseUrl() {
// #ifdef H5
return '/gym-api'
// #endif
// #ifndef H5
return 'http://localhost:8084'
// #endif
}
function getStatusMap() {
return _statusMap || DEFAULT_STATUS_MAP
}
function getStatusClassMap() {
return _statusClassMap || DEFAULT_STATUS_CLASS
}
export function getCourseStatusLabel(status, deletedAt) {
if (deletedAt) return '已删除'
return getStatusMap()[String(status)] || '未知'
}
export function getCourseStatusClass(status, deletedAt) {
if (deletedAt) return 'tag-gray'
return 'tag-' + (getStatusClassMap()[String(status)] || 'gray')
}
/**
* 判断课程是否有实际时间记录
*/
export function hasActualTime(course) {
return !!(course && (course.actualStartTime || course.actualEndTime))
}
/**
* Toast 提示
*/
export function showToast(title, icon = 'none') {
uni.showToast({ title, icon, duration: 2000 })
}
/**
* 确认弹窗
*/
export function showConfirm(content, title = '提示') {
return new Promise((resolve) => {
uni.showModal({
title,
content,
success: (res) => {
resolve(res.confirm)
}
})
})
}