refactor travel

This commit is contained in:
Timi
2025-12-13 18:44:37 +08:00
parent 880e702288
commit 69659a1746
37 changed files with 4154 additions and 400 deletions

View File

@ -0,0 +1,17 @@
{
"component": true,
"usingComponents": {
"t-cell": "tdesign-miniprogram/cell/cell",
"t-icon": "tdesign-miniprogram/icon/icon",
"t-image": "tdesign-miniprogram/image/image",
"t-input": "tdesign-miniprogram/input/input",
"t-button": "tdesign-miniprogram/button/button",
"t-dialog": "tdesign-miniprogram/dialog/dialog",
"t-navbar": "tdesign-miniprogram/navbar/navbar",
"t-loading": "tdesign-miniprogram/loading/loading",
"t-stepper": "tdesign-miniprogram/stepper/stepper",
"t-textarea": "tdesign-miniprogram/textarea/textarea",
"t-cell-group": "tdesign-miniprogram/cell-group/cell-group"
},
"styleIsolation": "shared"
}

View File

@ -0,0 +1,92 @@
// pages/main/travel-editor/index.less
.container {
width: 100vw;
min-height: 100vh;
background: var(--theme-bg-secondary);
.content {
padding-bottom: 64rpx;
.loading {
display: flex;
padding: 128rpx 0;
align-items: center;
flex-direction: column;
.loading-text {
color: var(--theme-text-secondary);
margin-top: 24rpx;
font-size: 28rpx;
}
}
.section {
margin-top: 48rpx;
.picker .slot {
gap: 16rpx;
display: flex;
align-items: center;
}
.days-stepper {
display: flex;
align-items: center;
}
}
.submit-section {
gap: 24rpx;
display: flex;
padding: 24rpx 16rpx 48rpx 16rpx;
margin-top: 64rpx;
flex-direction: column;
&.horizontal {
flex-direction: row;
.save-btn {
flex: 2;
}
.delete-btn {
flex: 1;
}
}
}
}
}
.delete-dialog-content {
gap: 32rpx;
display: flex;
flex-direction: column;
.delete-warning {
gap: 16rpx;
display: flex;
padding: 24rpx;
align-items: center;
border-radius: 12rpx;
flex-direction: column;
background: #FFF4F4;
.warning-text {
color: #E34D59;
font-size: 28rpx;
text-align: center;
}
}
.delete-confirm {
gap: 16rpx;
display: flex;
flex-direction: column;
.confirm-label {
color: var(--theme-text-primary);
font-size: 28rpx;
}
}
}

View File

@ -0,0 +1,292 @@
// pages/main/travel-editor/index.ts
import Time from "../../../utils/Time";
import { TravelApi } from "../../../api/TravelApi";
import { TravelStatus, TransportationType } from "../../../types/Travel";
interface TravelEditorData {
/** 模式create 或 edit */
mode: "create" | "edit";
/** 旅行 ID编辑模式 */
id?: number;
/** 标题 */
title: string;
/** 内容 */
content: string;
/** 出行日期 */
date: string;
/** 出行时间 */
time: string;
/** 天数 */
days: number;
/** 交通类型 */
transportationType: TransportationType;
/** 状态 */
status: TravelStatus;
/** 是否正在加载(编辑模式) */
isLoading: boolean;
/** 是否正在保存 */
isSaving: boolean;
/** 交通类型选项 */
transportationTypes: { label: string; value: TransportationType }[];
/** 交通类型选中索引 */
transportationTypeIndex: number;
/** 状态选项 */
statuses: { label: string; value: TravelStatus }[];
/** 状态选中索引 */
statusIndex: number;
/** 删除对话框可见性 */
deleteDialogVisible: boolean;
/** 删除确认文本 */
deleteConfirmText: string;
}
Page({
data: <TravelEditorData>{
mode: "create",
id: undefined,
title: "",
content: "",
date: "2025-06-28",
time: "16:00",
days: 1,
transportationType: TransportationType.PLANE,
transportationTypeIndex: 0,
status: TravelStatus.PLANNING,
statusIndex: 0,
isLoading: false,
isSaving: false,
transportationTypes: [
{ label: "飞机", value: TransportationType.PLANE },
{ label: "火车", value: TransportationType.TRAIN },
{ label: "汽车", value: TransportationType.CAR },
{ label: "轮船", value: TransportationType.SHIP },
{ label: "自驾", value: TransportationType.SELF_DRIVING },
{ label: "其他", value: TransportationType.OTHER }
],
statuses: [
{ label: "计划中", value: TravelStatus.PLANNING },
{ label: "进行中", value: TravelStatus.ONGOING },
{ label: "已完成", value: TravelStatus.COMPLETED }
],
deleteDialogVisible: false,
deleteConfirmText: ""
},
onLoad(options: any) {
// 判断模式:有 ID 是编辑,无 ID 是创建
const id = options.id ? parseInt(options.id) : undefined;
if (id) {
// 编辑模式
this.setData({
mode: "edit",
id,
isLoading: true
});
this.loadTravelDetail(id);
} else {
// 创建模式
this.setData({
mode: "create",
isLoading: false
});
// 设置当前时间
const unixTime = new Date().getTime();
this.setData({
date: Time.toDate(unixTime),
time: Time.toTime(unixTime)
});
}
},
/** 加载旅行详情(编辑模式) */
async loadTravelDetail(id: number) {
wx.showLoading({ title: "加载中...", mask: true });
try {
const travel = await TravelApi.getDetail(id);
// 格式化数据
let date = "";
let time = "";
if (travel.travelAt) {
date = Time.toDate(travel.travelAt);
time = Time.toTime(travel.travelAt);
}
// 计算交通类型索引
const transportationType = travel.transportationType || TransportationType.PLANE;
const transportationTypeIndex = this.data.transportationTypes.findIndex(
item => item.value === transportationType
);
// 计算状态索引
const status = travel.status || TravelStatus.PLANNING;
const statusIndex = this.data.statuses.findIndex(item => item.value === status);
this.setData({
title: travel.title || "",
content: travel.content || "",
date,
time,
days: travel.days || 1,
transportationType,
transportationTypeIndex: transportationTypeIndex >= 0 ? transportationTypeIndex : 0,
status,
statusIndex: statusIndex >= 0 ? statusIndex : 0,
isLoading: false
});
} catch (error) {
wx.showToast({
title: "加载失败",
icon: "error"
});
setTimeout(() => {
wx.navigateBack();
}, 1500);
} finally {
wx.hideLoading();
}
},
/** 改变交通类型 */
onChangeTransportationType(e: any) {
const index = e.detail.value;
this.setData({
transportationTypeIndex: index,
transportationType: this.data.transportationTypes[index].value
});
},
/** 改变状态 */
onChangeStatus(e: any) {
const index = e.detail.value;
this.setData({
statusIndex: index,
status: this.data.statuses[index].value
});
},
/** 取消 */
cancel() {
if (this.data.mode === "create") {
wx.navigateBack();
} else {
wx.navigateBack();
}
},
/** 提交/保存 */
submit() {
// 验证必填字段
if (!this.data.title.trim()) {
wx.showToast({
title: "请输入标题",
icon: "error"
});
return;
}
if (this.data.mode === "create") {
this.createTravel();
} else {
this.updateTravel();
}
},
/** 创建旅行 */
async createTravel() {
this.setData({ isSaving: true });
try {
await TravelApi.create({
title: this.data.title.trim(),
content: this.data.content.trim(),
travelAt: new Date(`${this.data.date}T${this.data.time}:00`).getTime(),
days: this.data.days,
transportationType: this.data.transportationType,
status: this.data.status
});
wx.showToast({
title: "创建成功",
icon: "success"
});
setTimeout(() => {
wx.navigateBack();
}, 1000);
} catch (error) {
this.setData({ isSaving: false });
}
},
/** 更新旅行 */
async updateTravel() {
this.setData({ isSaving: true });
try {
await TravelApi.update({
id: this.data.id!,
title: this.data.title.trim(),
content: this.data.content.trim(),
travelAt: new Date(`${this.data.date}T${this.data.time}:00`).getTime(),
days: this.data.days,
transportationType: this.data.transportationType,
status: this.data.status
});
wx.showToast({
title: "保存成功",
icon: "success"
});
setTimeout(() => {
wx.navigateBack();
}, 1000);
} catch (error) {
this.setData({ isSaving: false });
}
},
/** 删除旅行 */
deleteTravel() {
this.setData({
deleteDialogVisible: true,
deleteConfirmText: ""
});
},
/** 取消删除 */
cancelDelete() {
this.setData({
deleteDialogVisible: false,
deleteConfirmText: ""
});
},
/** 确认删除 */
confirmDelete() {
const inputText = this.data.deleteConfirmText.trim();
if (inputText !== "确认删除") {
wx.showToast({
title: "输入不匹配",
icon: "error"
});
return;
}
this.setData({
deleteDialogVisible: false
});
this.executeDelete();
},
/** 执行删除 */
async executeDelete() {
if (!this.data.id) return;
wx.showLoading({ title: "删除中...", mask: true });
try {
await TravelApi.delete(this.data.id);
wx.showToast({
title: "删除成功",
icon: "success"
});
setTimeout(() => {
wx.navigateBack({ delta: 2 });
}, 1500);
} catch (error) {
// 错误已由 Network 类处理
} finally {
wx.hideLoading();
}
}
});

View File

@ -0,0 +1,150 @@
<!--pages/main/travel-editor/index.wxml-->
<t-navbar title="{{mode === 'create' ? '新建旅行' : '编辑旅行'}}">
<text slot="left" bindtap="cancel">取消</text>
</t-navbar>
<scroll-view class="container" type="custom" scroll-y show-scrollbar="{{false}}">
<view class="content">
<view wx:if="{{isLoading}}" class="loading">
<t-loading theme="dots" size="40rpx" />
<text class="loading-text">加载中...</text>
</view>
<block wx:else>
<t-cell-group class="section">
<t-input
class="input"
placeholder="请输入旅行标题"
model:value="{{title}}"
maxlength="50"
borderless
>
<text slot="label">标题</text>
</t-input>
<t-textarea
class="textarea"
placeholder="添加详细说明(选填)"
model:value="{{content}}"
maxlength="500"
indicator
>
<text slot="label">内容</text>
</t-textarea>
</t-cell-group>
<t-cell-group class="section">
<t-cell class="travel-at" title="出行时间">
<view slot="right-icon">
<picker class="picker" mode="date" model:value="{{date}}">
<view slot="content" class="slot">
<t-icon name="calendar" size="20px" class="icon" />
<text>{{date}}</text>
</view>
</picker>
</view>
</t-cell>
<t-cell title="旅行天数" t-class="days-cell">
<view slot="right-icon" class="days-stepper">
<t-stepper
theme="filled"
model:value="{{days}}"
size="medium"
min="{{1}}"
max="{{999}}"
t-class="stepper"
/>
</view>
</t-cell>
<t-cell title="交通方式">
<view slot="right-icon">
<picker
class="picker"
mode="selector"
range="{{transportationTypes}}"
range-key="label"
value="{{transportationTypeIndex}}"
bindchange="onChangeTransportationType"
>
<view class="slot">
<text>{{transportationTypes[transportationTypeIndex].label}}</text>
<t-icon name="chevron-right" size="20px" class="icon" />
</view>
</picker>
</view>
</t-cell>
<t-cell title="状态">
<view slot="right-icon">
<picker
class="picker"
mode="selector"
range="{{statuses}}"
range-key="label"
value="{{statusIndex}}"
bindchange="onChangeStatus"
>
<view class="slot">
<text>{{statuses[statusIndex].label}}</text>
<t-icon name="chevron-right" size="20px" class="icon" />
</view>
</picker>
</view>
</t-cell>
</t-cell-group>
<view wx:if="{{mode === 'create'}}" class="submit-section">
<t-button
theme="primary"
size="large"
block
bind:tap="submit"
disabled="{{isSaving}}"
>
创建旅行
</t-button>
</view>
<view wx:else class="submit-section horizontal">
<t-button
theme="danger"
variant="outline"
size="large"
icon="delete"
t-class="delete-btn"
bind:tap="deleteTravel"
>
删除
</t-button>
<t-button
theme="primary"
size="large"
t-class="save-btn"
bind:tap="submit"
disabled="{{isSaving}}"
>
保存修改
</t-button>
</view>
</block>
</view>
</scroll-view>
<!-- 删除确认对话框 -->
<t-dialog
visible="{{deleteDialogVisible}}"
title="删除旅行计划"
confirm-btn="删除"
cancel-btn="取消"
bind:confirm="confirmDelete"
bind:cancel="cancelDelete"
>
<view class="delete-dialog-content">
<view class="delete-warning">
<t-icon name="error-circle" size="48rpx" color="#E34D59" />
<text class="warning-text">删除后无法恢复,请谨慎操作!</text>
</view>
<view class="delete-confirm">
<text class="confirm-label">请输入"确认删除"以继续:</text>
<t-input
placeholder="请输入确认删除"
model:value="{{deleteConfirmText}}"
borderless="{{false}}"
/>
</view>
</view>
</t-dialog>