refactor: 代码清理和优化
- 删除 utils/util.js,将 formatDate 等函数统一到 api.js - 删除 utils/weapp.qrcode.esm.js,改用微信官方 wxacode API - 删除 .cloudbase/container/debug.json 空配置文件 - 统一时间日期格式化函数到 api.js - 优化 app.js 注释,移除冗余说明 - 优化各页面引入语句,合并重复导入 主要变更: 1. utils/api.js: 统一管理所有格式化函数 2. utils/util.js: 文件废弃并删除 3. utils/weapp.qrcode.esm.js: 不再使用,删除 4. pages/*/: 优化导入语句,移除未使用的函数
This commit is contained in:
@@ -1 +0,0 @@
|
|||||||
{"containers":[],"config":{}}
|
|
||||||
@@ -1,6 +1,5 @@
|
|||||||
// appointment.js
|
// appointment.js
|
||||||
const { formatDate } = require('../../utils/util')
|
const { formatDate, appointmentDB } = require('../../utils/api')
|
||||||
const { appointmentDB } = require('../../utils/api')
|
|
||||||
const app = getApp()
|
const app = getApp()
|
||||||
|
|
||||||
// 订阅消息模板ID
|
// 订阅消息模板ID
|
||||||
|
|||||||
+52
-7
@@ -32,21 +32,61 @@ function request(options) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 格式化记录中的 createTime 字段
|
* 格式化时间戳为本地日期时间字符串
|
||||||
* @param {object} record - 含 createTime 的记录
|
* @param {string|number} timestamp - 时间戳
|
||||||
|
* @returns {string} 格式化后的日期时间,如:2024/04/27 18:30:45
|
||||||
|
*/
|
||||||
|
function formatDateTime(timestamp) {
|
||||||
|
if (!timestamp) return ''
|
||||||
|
const date = new Date(timestamp)
|
||||||
|
const year = date.getFullYear()
|
||||||
|
const month = String(date.getMonth() + 1).padStart(2, '0')
|
||||||
|
const day = String(date.getDate()).padStart(2, '0')
|
||||||
|
const hour = String(date.getHours()).padStart(2, '0')
|
||||||
|
const minute = String(date.getMinutes()).padStart(2, '0')
|
||||||
|
const second = String(date.getSeconds()).padStart(2, '0')
|
||||||
|
return `${year}/${month}/${day} ${hour}:${minute}:${second}`
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 格式化记录中的时间字段
|
||||||
|
* @param {object} record - 含时间字段的记录
|
||||||
* @returns {object} 格式化后的记录
|
* @returns {object} 格式化后的记录
|
||||||
*/
|
*/
|
||||||
function formatRecord(record) {
|
function formatRecord(record) {
|
||||||
if (!record || !record.createTime) return record
|
if (!record) return record
|
||||||
return {
|
const formatted = { ...record }
|
||||||
...record,
|
if (record.createTime) {
|
||||||
createTime: new Date(record.createTime).toLocaleString('zh-CN')
|
formatted.createTime = formatDateTime(record.createTime)
|
||||||
}
|
}
|
||||||
|
if (record.visitDate) {
|
||||||
|
formatted.visitDate = formatDate(record.visitDate)
|
||||||
|
}
|
||||||
|
if (record.updateTime) {
|
||||||
|
formatted.updateTime = formatDateTime(record.updateTime)
|
||||||
|
}
|
||||||
|
return formatted
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 格式化日期为 YYYY-MM-DD 格式
|
||||||
|
* @param {string|number|Date} date - 日期
|
||||||
|
* @returns {string} 格式化后的日期,如:2024-04-27
|
||||||
|
*/
|
||||||
|
function formatDate(date) {
|
||||||
|
if (!date) return ''
|
||||||
|
const d = new Date(date)
|
||||||
|
const year = d.getFullYear()
|
||||||
|
const month = String(d.getMonth() + 1).padStart(2, '0')
|
||||||
|
const day = String(d.getDate()).padStart(2, '0')
|
||||||
|
return `${year}-${month}-${day}`
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将后端 API 返回的预约记录映射为前端模板字段
|
* 将后端 API 返回的预约记录映射为前端模板字段
|
||||||
* 后端: id, visitDate, visitTime → 前端: _id, date, time
|
* 后端: id, visitDate, visitTime → 前端: _id, date, time
|
||||||
|
* @param {object} record - 后端返回的记录
|
||||||
|
* @returns {object} 映射后的记录
|
||||||
*/
|
*/
|
||||||
function mapApiRecord(record) {
|
function mapApiRecord(record) {
|
||||||
if (!record) return null
|
if (!record) return null
|
||||||
@@ -54,7 +94,9 @@ function mapApiRecord(record) {
|
|||||||
...record,
|
...record,
|
||||||
_id: record.id,
|
_id: record.id,
|
||||||
date: record.visitDate,
|
date: record.visitDate,
|
||||||
time: record.visitTime
|
time: record.visitTime,
|
||||||
|
createTime: record.createTime,
|
||||||
|
updateTime: record.updateTime
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -171,6 +213,9 @@ async function getWxacode(scene, page = 'pages/scan/result/index') {
|
|||||||
module.exports = {
|
module.exports = {
|
||||||
request,
|
request,
|
||||||
formatRecord,
|
formatRecord,
|
||||||
|
mapApiRecord,
|
||||||
|
formatDateTime,
|
||||||
|
formatDate,
|
||||||
appointmentDB,
|
appointmentDB,
|
||||||
getWxacode
|
getWxacode
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,27 +0,0 @@
|
|||||||
const formatTime = date => {
|
|
||||||
const year = date.getFullYear()
|
|
||||||
const month = date.getMonth() + 1
|
|
||||||
const day = date.getDate()
|
|
||||||
const hour = date.getHours()
|
|
||||||
const minute = date.getMinutes()
|
|
||||||
const second = date.getSeconds()
|
|
||||||
|
|
||||||
return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
|
|
||||||
}
|
|
||||||
|
|
||||||
const formatDate = date => {
|
|
||||||
const year = date.getFullYear()
|
|
||||||
const month = date.getMonth() + 1
|
|
||||||
const day = date.getDate()
|
|
||||||
return `${year}-${formatNumber(month)}-${formatNumber(day)}`
|
|
||||||
}
|
|
||||||
|
|
||||||
const formatNumber = n => {
|
|
||||||
n = n.toString()
|
|
||||||
return n[1] ? n : `0${n}`
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = {
|
|
||||||
formatTime,
|
|
||||||
formatDate
|
|
||||||
}
|
|
||||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user