rentease-app/pages/room-add/room-add.vue

415 lines
12 KiB
Vue
Raw Permalink Normal View History

2026-04-20 06:23:11 +00:00
<template>
<view class="room-add-page">
<view class="custom-nav safe-area-top">
<view class="nav-content">
<view class="nav-btn" @click="goBack">
<uni-icons type="left" size="22" color="#1E293B"></uni-icons>
</view>
<text class="nav-title">{{isEdit ? '编辑房间' : '添加房间'}}</text>
<view class="nav-btn" @click="saveRoom">
<text class="save-text">保存</text>
</view>
</view>
</view>
<scroll-view scroll-y class="form-content">
<view class="form-card">
<view class="form-item">
<text class="form-label">所属公寓 <text class="required">*</text></text>
2026-04-22 06:47:04 +00:00
<view class="form-control">
<picker mode="selector" :range="apartmentOptions" :value="selectedApartmentIndex" @change="onApartmentChange">
<view class="picker-value" :class="{ placeholder: !form.apartmentId }">
<text>{{selectedApartmentLabel}}</text>
<uni-icons type="arrowright" size="16" color="#94A3B8"></uni-icons>
</view>
</picker>
</view>
2026-04-20 06:23:11 +00:00
</view>
<view class="form-item">
<text class="form-label">房间号 <text class="required">*</text></text>
2026-04-22 06:47:04 +00:00
<view class="form-control">
<input
type="text"
v-model="form.roomNumber"
placeholder="请输入房间号101"
class="form-input"
maxlength="10"
/>
</view>
2026-04-20 06:23:11 +00:00
</view>
<view class="form-item">
2026-04-22 06:47:04 +00:00
<text class="form-label">楼层</text>
<view class="form-control">
<input
type="number"
v-model="form.floor"
placeholder="请输入楼层"
class="form-input"
/>
</view>
2026-04-20 06:23:11 +00:00
</view>
<view class="form-item">
2026-04-22 06:47:04 +00:00
<text class="form-label">户型</text>
<view class="form-control">
<input
type="text"
v-model="form.roomType"
placeholder="请输入户型,如一室一厅"
class="form-input"
/>
</view>
</view>
<view class="form-item">
<text class="form-label">面积 ()</text>
<view class="form-control">
<input
type="digit"
v-model="form.area"
placeholder="请输入面积"
class="form-input"
/>
</view>
2026-04-20 06:23:11 +00:00
</view>
<view class="form-item">
<text class="form-label">月租金 ()</text>
2026-04-22 06:47:04 +00:00
<view class="form-control">
<input
type="digit"
v-model="form.monthlyPrice"
placeholder="请输入月租金"
class="form-input"
/>
</view>
</view>
<view class="form-item">
<text class="form-label">年租金 ()</text>
<view class="form-control">
<input
type="digit"
v-model="form.yearlyPrice"
placeholder="请输入年租金"
class="form-input"
/>
</view>
</view>
<view class="form-item">
<text class="form-label">押金 ()</text>
<view class="form-control">
<input
type="digit"
v-model="form.deposit"
placeholder="请输入押金"
class="form-input"
/>
</view>
2026-04-20 06:23:11 +00:00
</view>
<view class="form-item">
2026-04-22 06:47:04 +00:00
<text class="form-label">排序</text>
<view class="form-control">
<input
type="number"
v-model="form.sortOrder"
placeholder="数字越小越靠前"
class="form-input"
/>
</view>
</view>
<view class="form-item">
<text class="form-label">主状态</text>
<view class="form-control">
<picker mode="selector" :range="statusOptions" :value="statusIndex" @change="onStatusChange">
<view class="picker-value" :class="{ placeholder: !form.status }">
<text>{{getStatusLabel(form.status)}}</text>
<uni-icons type="arrowright" size="16" color="#94A3B8"></uni-icons>
</view>
</picker>
</view>
</view>
<view class="form-item" v-if="form.status === 'rented'">
<text class="form-label">租房标识</text>
<view class="form-control">
<picker mode="selector" :range="rentalStatusOptions" :value="rentalStatusIndex" @change="onRentalStatusChange">
<view class="picker-value" :class="{ placeholder: !form.rentalStatus }">
<text>{{getRentalStatusLabel(form.rentalStatus)}}</text>
<uni-icons type="arrowright" size="16" color="#94A3B8"></uni-icons>
</view>
</picker>
</view>
2026-04-20 06:23:11 +00:00
</view>
</view>
<button class="btn-submit" :loading="submitLoading" @click="saveRoom">{{isEdit ? '保存修改' : '确认添加'}}</button>
<view class="safe-area-bottom" style="height: 40rpx;"></view>
</scroll-view>
</view>
</template>
<script>
import roomApi from '@/api/room.js'
import apartmentApi from '@/api/apartment.js'
2026-04-22 06:47:04 +00:00
2026-04-20 06:23:11 +00:00
export default {
data() {
return {
isEdit: false,
roomId: null,
submitLoading: false,
apartments: [],
2026-04-22 06:47:04 +00:00
statusOptions: ['空房', '预定', '在租'],
statusValues: ['empty', 'reserved', 'rented'],
rentalStatusOptions: ['正常', '即将到期', '已到期'],
rentalStatusValues: ['normal', 'soon_expire', 'expired'],
2026-04-20 06:23:11 +00:00
form: {
apartmentId: '',
roomNumber: '',
2026-04-22 06:47:04 +00:00
floor: '',
roomType: '',
2026-04-20 06:23:11 +00:00
area: '',
2026-04-22 06:47:04 +00:00
monthlyPrice: '',
yearlyPrice: '',
deposit: '',
sortOrder: 0,
status: 'empty',
rentalStatus: 'normal'
2026-04-20 06:23:11 +00:00
}
}
},
computed: {
apartmentOptions() {
return this.apartments.map(a => a.name)
},
selectedApartmentIndex() {
const index = this.apartments.findIndex(a => a.id === this.form.apartmentId)
return index >= 0 ? index : -1
},
selectedApartmentLabel() {
const apartment = this.apartments.find(a => a.id === this.form.apartmentId)
return apartment ? apartment.name : '请选择公寓'
},
2026-04-22 06:47:04 +00:00
statusIndex() {
return this.statusValues.indexOf(this.form.status)
},
rentalStatusIndex() {
return this.rentalStatusValues.indexOf(this.form.rentalStatus)
2026-04-20 06:23:11 +00:00
}
},
onLoad(options) {
this.loadApartments()
if (options.id) {
this.isEdit = true
this.roomId = options.id
this.loadRoomDetail(options.id)
} else if (options.apartmentId) {
this.form.apartmentId = parseInt(options.apartmentId)
}
},
methods: {
async loadApartments() {
try {
2026-04-22 06:47:04 +00:00
const res = await apartmentApi.list()
2026-04-20 06:23:11 +00:00
if (res.data) {
this.apartments = res.data
}
} catch (error) {
console.error('加载公寓列表失败:', error)
}
},
async loadRoomDetail(id) {
try {
const res = await roomApi.getDetail(id)
if (res.data) {
const data = res.data
this.form = {
apartmentId: data.apartmentId,
roomNumber: data.roomNumber,
2026-04-22 06:47:04 +00:00
floor: data.floor ? String(data.floor) : '',
roomType: data.roomType || '',
2026-04-20 06:23:11 +00:00
area: data.area ? String(data.area) : '',
2026-04-22 06:47:04 +00:00
monthlyPrice: data.monthlyPrice ? String(data.monthlyPrice) : '',
yearlyPrice: data.yearlyPrice ? String(data.yearlyPrice) : '',
deposit: data.deposit ? String(data.deposit) : '',
sortOrder: data.sortOrder !== undefined ? data.sortOrder : 0,
status: data.status || 'empty',
rentalStatus: data.rentalStatus || 'normal'
2026-04-20 06:23:11 +00:00
}
}
} catch (error) {
console.error('加载房间详情失败:', error)
uni.showToast({ title: '加载失败', icon: 'none' })
}
},
onApartmentChange(e) {
this.form.apartmentId = this.apartments[e.detail.value].id
},
2026-04-22 06:47:04 +00:00
onStatusChange(e) {
this.form.status = this.statusValues[e.detail.value]
},
onRentalStatusChange(e) {
this.form.rentalStatus = this.rentalStatusValues[e.detail.value]
},
getStatusLabel(value) {
const index = this.statusValues.indexOf(value)
return index >= 0 ? this.statusOptions[index] : '请选择状态'
},
getRentalStatusLabel(value) {
const index = this.rentalStatusValues.indexOf(value)
return index >= 0 ? this.rentalStatusOptions[index] : '请选择租房标识'
2026-04-20 06:23:11 +00:00
},
async saveRoom() {
if (!this.form.apartmentId) {
uni.showToast({ title: '请选择所属公寓', icon: 'none' })
return
}
if (!this.form.roomNumber.trim()) {
uni.showToast({ title: '请输入房间号', icon: 'none' })
return
}
this.submitLoading = true
try {
const data = {
apartmentId: this.form.apartmentId,
roomNumber: this.form.roomNumber.trim(),
2026-04-22 06:47:04 +00:00
floor: this.form.floor ? parseInt(this.form.floor) : null,
roomType: this.form.roomType.trim() || null,
2026-04-20 06:23:11 +00:00
area: this.form.area ? parseFloat(this.form.area) : null,
2026-04-22 06:47:04 +00:00
monthlyPrice: this.form.monthlyPrice ? parseFloat(this.form.monthlyPrice) : null,
yearlyPrice: this.form.yearlyPrice ? parseFloat(this.form.yearlyPrice) : null,
deposit: this.form.deposit ? parseFloat(this.form.deposit) : null,
sortOrder: this.form.sortOrder !== '' ? parseInt(this.form.sortOrder) : 0,
status: this.form.status,
rentalStatus: this.form.rentalStatus
2026-04-20 06:23:11 +00:00
}
if (this.isEdit) {
await roomApi.update(this.roomId, data)
uni.showToast({ title: '修改成功', icon: 'success' })
} else {
await roomApi.create(data)
uni.showToast({ title: '添加成功', icon: 'success' })
}
setTimeout(() => uni.navigateBack(), 1500)
} catch (error) {
console.error('保存失败:', error)
uni.showToast({ title: this.isEdit ? '修改失败' : '添加失败', icon: 'none' })
} finally {
this.submitLoading = false
}
},
goBack() {
uni.navigateBack()
}
}
}
</script>
<style scoped>
.room-add-page {
min-height: 100vh;
background: #F8FAFC;
display: flex;
flex-direction: column;
}
.custom-nav {
background: #FFFFFF;
border-bottom: 2rpx solid #F1F5F9;
}
.nav-content {
display: flex;
align-items: center;
justify-content: space-between;
padding: 20rpx 32rpx;
}
.nav-btn {
width: 72rpx;
height: 72rpx;
display: flex;
align-items: center;
justify-content: center;
border-radius: 50%;
}
.nav-title {
font-size: 34rpx;
font-weight: 600;
color: #1E293B;
}
.save-text {
font-size: 28rpx;
color: #2563EB;
font-weight: 600;
}
.form-content {
flex: 1;
padding: 24rpx 32rpx;
}
.form-card {
background: #FFFFFF;
border-radius: 20rpx;
padding: 0 24rpx;
margin-bottom: 40rpx;
}
.form-item {
2026-04-22 06:47:04 +00:00
display: flex;
align-items: center;
2026-04-20 06:23:11 +00:00
padding: 24rpx 0;
border-bottom: 2rpx solid #F1F5F9;
}
.form-item:last-child {
border-bottom: none;
}
.form-label {
2026-04-22 06:47:04 +00:00
width: 180rpx;
font-size: 28rpx;
2026-04-20 06:23:11 +00:00
color: #64748B;
2026-04-22 06:47:04 +00:00
flex-shrink: 0;
}
.form-control {
flex: 1;
2026-04-20 06:23:11 +00:00
}
.required {
color: #EF4444;
}
.picker-value {
display: flex;
align-items: center;
justify-content: space-between;
2026-04-22 06:47:04 +00:00
font-size: 28rpx;
2026-04-20 06:23:11 +00:00
color: #1E293B;
}
.picker-value.placeholder {
color: #94A3B8;
}
.form-input {
width: 100%;
2026-04-22 06:47:04 +00:00
height: 48rpx;
font-size: 28rpx;
2026-04-20 06:23:11 +00:00
color: #1E293B;
2026-04-22 06:47:04 +00:00
text-align: right;
2026-04-20 06:23:11 +00:00
}
2026-04-22 06:47:04 +00:00
.form-input::placeholder {
color: #94A3B8;
2026-04-20 06:23:11 +00:00
}
.btn-submit {
background: linear-gradient(135deg, #2563EB 0%, #1D4ED8 100%);
color: #FFFFFF;
font-size: 30rpx;
font-weight: 600;
2026-04-22 06:47:04 +00:00
height: 88rpx;
line-height: 88rpx;
2026-04-20 06:23:11 +00:00
border-radius: 16rpx;
border: none;
2026-04-22 06:47:04 +00:00
width: 100%;
2026-04-20 06:23:11 +00:00
}
</style>