使用第三方时间格式化工具

This commit is contained in:
WindowBird 2025-09-19 10:17:06 +08:00
parent 6f5521dfff
commit 3f62ac47b4
2 changed files with 614 additions and 598 deletions

View File

@ -16,10 +16,16 @@
class="table-row"
@click="handleItemClick(item)"
>
<view class="table-cell worshiper-col">{{ item.worshiperName || '未知' }}</view>
<view class="table-cell time-col">{{ formatDate(item.startDate) }}</view>
<view class="table-cell worshiper-col">{{
item.worshiperName || "未知"
}}</view>
<view class="table-cell time-col">{{
formatDate(item.startDate)
}}</view>
<view class="table-cell time-col">{{ formatDate(item.endDate) }}</view>
<view class="table-cell type-col">{{ item.thaliName || '未知类型' }}</view>
<view class="table-cell type-col">{{
item.thaliName || "未知类型"
}}</view>
</view>
</view>
@ -42,10 +48,10 @@
</template>
<script>
import { getEnshrinedList } from '@/api/memorial/memorial.js'
import { getEnshrinedList } from "@/api/memorial/memorial.js";
export default {
name: 'EnshrinedList',
name: "EnshrinedList",
props: {
memorialId: {
type: [String, Number],
@ -53,7 +59,7 @@
},
searchKeyword: {
type: String,
default: '',
default: "",
},
},
data() {
@ -64,7 +70,7 @@
pageSize: 10,
total: 0,
hasMore: true,
}
};
},
watch: {
// searchKeyword
@ -77,135 +83,137 @@
// }
},
mounted() {
this.loadData()
this.loadData();
},
methods: {
//
async loadData() {
if (this.loading) return
if (this.loading) return;
this.loading = true
this.loading = true;
try {
const params = {
memorialId: this.memorialId,
pageNum: this.pageNum,
pageSize: this.pageSize,
}
};
//
// if (this.searchKeyword) {
// params.keyword = this.searchKeyword
// }
const response = await getEnshrinedList(params)
console.log('API 响应数据:', response) //
const response = await getEnshrinedList(params);
console.log("API 响应数据:", response); //
// API: { code: 200, msg: "", data: [...] }
let total = 0
let rows = []
let total = 0;
let rows = [];
if (response && response.code === 200) {
// API
rows = response.rows || []
total = rows.length // 使
console.log('API 返回数据:', rows)
rows = response.rows || [];
total = rows.length; // 使
console.log("API 返回数据:", rows);
} else {
console.log('API 响应异常:', response)
console.log("API 响应异常:", response);
// API使
rows = this.getMockData()
total = rows.length
rows = this.getMockData();
total = rows.length;
}
this.total = total
this.total = total;
if (this.pageNum === 1) {
this.enshrinedList = rows || []
this.enshrinedList = rows || [];
} else {
this.enshrinedList = [...this.enshrinedList, ...(rows || [])]
this.enshrinedList = [...this.enshrinedList, ...(rows || [])];
}
this.hasMore = this.enshrinedList.length < total
this.hasMore = this.enshrinedList.length < total;
} catch (error) {
console.error('加载供奉列表失败:', error)
console.error("加载供奉列表失败:", error);
// 使
console.log('API 调用失败,使用模拟数据')
const mockData = this.getMockData()
console.log("API 调用失败,使用模拟数据");
const mockData = this.getMockData();
if (this.pageNum === 1) {
this.enshrinedList = mockData
this.enshrinedList = mockData;
} else {
this.enshrinedList = [...this.enshrinedList, ...mockData]
this.enshrinedList = [...this.enshrinedList, ...mockData];
}
this.total = mockData.length
this.hasMore = false
this.total = mockData.length;
this.hasMore = false;
uni.showToast({
title: '网络异常,请稍后重试',
icon: 'none',
})
title: "网络异常,请稍后重试",
icon: "none",
});
} finally {
this.loading = false
this.loading = false;
}
},
//
getMockData() {
return []
return [];
},
//
resetList() {
this.enshrinedList = []
this.pageNum = 1
this.total = 0
this.hasMore = true
this.enshrinedList = [];
this.pageNum = 1;
this.total = 0;
this.hasMore = true;
},
//
loadMore() {
if (this.hasMore && !this.loading) {
this.pageNum++
this.loadData()
this.pageNum++;
this.loadData();
}
},
//
formatDate(dateStr) {
if (!dateStr) return '未知'
return this.$u.date(dateStr, "yyyy-mm-dd");
try {
// YYYY-MM-DD
if (/^\d{4}-\d{2}-\d{2}$/.test(dateStr)) {
return dateStr
}
const date = new Date(dateStr)
if (isNaN(date.getTime())) {
return dateStr
}
const year = date.getFullYear()
const month = String(date.getMonth() + 1).padStart(2, '0')
const day = String(date.getDate()).padStart(2, '0')
return `${year}-${month}-${day}`
} catch (error) {
console.warn('日期格式化失败:', dateStr, error)
return dateStr
}
// if (!dateStr) return ''
//
// try {
// // YYYY-MM-DD
// if (/^\d{4}-\d{2}-\d{2}$/.test(dateStr)) {
// return dateStr
// }
//
// const date = new Date(dateStr)
// if (isNaN(date.getTime())) {
// return dateStr
// }
//
// const year = date.getFullYear()
// const month = String(date.getMonth() + 1).padStart(2, '0')
// const day = String(date.getDate()).padStart(2, '0')
//
// return `${year}-${month}-${day}`
// } catch (error) {
// console.warn(':', dateStr, error)
// return dateStr
// }
},
//
handleItemClick(item) {
this.$emit('item-click', item)
this.$emit("item-click", item);
},
//
refresh() {
this.resetList()
this.loadData()
this.resetList();
this.loadData();
},
},
}
};
</script>
<style lang="scss" scoped>

View File

@ -16,10 +16,16 @@
class="table-row"
@click="handleItemClick(item)"
>
<view class="table-cell worshiper-col">{{ item.worshiperName || '未知' }}</view>
<view class="table-cell time-col">{{ formatDate(item.startDate) }}</view>
<view class="table-cell worshiper-col">{{
item.worshiperName || "未知"
}}</view>
<view class="table-cell time-col">{{
formatDate(item.startDate)
}}</view>
<view class="table-cell time-col">{{ formatDate(item.endDate) }}</view>
<view class="table-cell type-col">{{ item.thaliName || '未知类型' }}</view>
<view class="table-cell type-col">{{
item.thaliName || "未知类型"
}}</view>
</view>
</view>
@ -42,10 +48,10 @@
</template>
<script>
import { getEnshrinedList } from '@/api/memorial/index.js'
import { getEnshrinedList } from "@/api/memorial/index.js";
export default {
name: 'EnshrinedList',
name: "EnshrinedList",
props: {
memorialId: {
type: [String, Number],
@ -53,7 +59,7 @@
},
searchKeyword: {
type: String,
default: '',
default: "",
},
},
data() {
@ -64,7 +70,7 @@
pageSize: 10,
total: 0,
hasMore: true,
}
};
},
watch: {
// searchKeyword
@ -77,135 +83,137 @@
// }
},
mounted() {
this.loadData()
this.loadData();
},
methods: {
//
async loadData() {
if (this.loading) return
if (this.loading) return;
this.loading = true
this.loading = true;
try {
const params = {
memorialId: this.memorialId,
pageNum: this.pageNum,
pageSize: this.pageSize,
}
};
//
// if (this.searchKeyword) {
// params.keyword = this.searchKeyword
// }
const response = await getEnshrinedList(params)
console.log('API 响应数据:', response) //
const response = await getEnshrinedList(params);
console.log("API 响应数据:", response); //
// API: { code: 200, msg: "", data: [...] }
let total = 0
let rows = []
let total = 0;
let rows = [];
if (response && response.code === 200) {
// API
rows = response.data || []
total = rows.length // 使
console.log('API 返回数据:', rows)
rows = response.data || [];
total = rows.length; // 使
console.log("API 返回数据:", rows);
} else {
console.log('API 响应异常:', response)
console.log("API 响应异常:", response);
// API使
rows = this.getMockData()
total = rows.length
rows = this.getMockData();
total = rows.length;
}
this.total = total
this.total = total;
if (this.pageNum === 1) {
this.enshrinedList = rows || []
this.enshrinedList = rows || [];
} else {
this.enshrinedList = [...this.enshrinedList, ...(rows || [])]
this.enshrinedList = [...this.enshrinedList, ...(rows || [])];
}
this.hasMore = this.enshrinedList.length < total
this.hasMore = this.enshrinedList.length < total;
} catch (error) {
console.error('加载供奉列表失败:', error)
console.error("加载供奉列表失败:", error);
// 使
console.log('API 调用失败,使用模拟数据')
const mockData = this.getMockData()
console.log("API 调用失败,使用模拟数据");
const mockData = this.getMockData();
if (this.pageNum === 1) {
this.enshrinedList = mockData
this.enshrinedList = mockData;
} else {
this.enshrinedList = [...this.enshrinedList, ...mockData]
this.enshrinedList = [...this.enshrinedList, ...mockData];
}
this.total = mockData.length
this.hasMore = false
this.total = mockData.length;
this.hasMore = false;
uni.showToast({
title: '网络异常,请稍后重试',
icon: 'none',
})
title: "网络异常,请稍后重试",
icon: "none",
});
} finally {
this.loading = false
this.loading = false;
}
},
//
getMockData() {
return []
return [];
},
//
resetList() {
this.enshrinedList = []
this.pageNum = 1
this.total = 0
this.hasMore = true
this.enshrinedList = [];
this.pageNum = 1;
this.total = 0;
this.hasMore = true;
},
//
loadMore() {
if (this.hasMore && !this.loading) {
this.pageNum++
this.loadData()
this.pageNum++;
this.loadData();
}
},
//
formatDate(dateStr) {
if (!dateStr) return '未知'
return this.$u.date(dateStr, "yyyy-mm-dd");
try {
// YYYY-MM-DD
if (/^\d{4}-\d{2}-\d{2}$/.test(dateStr)) {
return dateStr
}
// if (!dateStr) return ''
const date = new Date(dateStr)
if (isNaN(date.getTime())) {
return dateStr
}
const year = date.getFullYear()
const month = String(date.getMonth() + 1).padStart(2, '0')
const day = String(date.getDate()).padStart(2, '0')
return `${year}-${month}-${day}`
} catch (error) {
console.warn('日期格式化失败:', dateStr, error)
return dateStr
}
// try {
// // YYYY-MM-DD
// if (/^\d{4}-\d{2}-\d{2}$/.test(dateStr)) {
// return dateStr
// }
//
// const date = new Date(dateStr)
// if (isNaN(date.getTime())) {
// return dateStr
// }
//
// const year = date.getFullYear()
// const month = String(date.getMonth() + 1).padStart(2, '0')
// const day = String(date.getDate()).padStart(2, '0')
//
// return `${year}-${month}-${day}`
// } catch (error) {
// console.warn(':', dateStr, error)
// return dateStr
// }
},
//
handleItemClick(item) {
this.$emit('item-click', item)
this.$emit("item-click", item);
},
//
refresh() {
this.resetList()
this.loadData()
this.resetList();
this.loadData();
},
},
}
};
</script>
<style lang="scss" scoped>