统一接口返回格式

This commit is contained in:
wangxiaoxian 2026-04-22 14:51:18 +08:00
parent b5c52d3e03
commit 47d263d914
13 changed files with 79 additions and 75 deletions

View File

@ -51,12 +51,14 @@ export async function request(url, options = {}) {
headers
});
// 解析响应数据
const data = await response.json();
// 处理 401 未授权
if (response.status === 401) {
const errorData = await response.json();
// 检查是否是认证失败(如用户名密码错误)
if (errorData.code === 401 && errorData.message) {
throw new Error(errorData.message);
if (data.code === 401 && data.message) {
throw new Error(data.message);
}
// 其他 401 情况(如 token 过期)
clearAuth()
@ -67,11 +69,17 @@ export async function request(url, options = {}) {
throw new Error('登录已过期,请重新登录')
}
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
// 根据后端统一响应格式处理
// 成功响应: { code: 200, message: 'xxx', data: xxx }
if (data.code === 200) {
return data;
}
// 业务错误(如参数错误、权限不足等)
// 后端格式: { code: 400/403/404/500, message: 'xxx', data: null }
const errorMessage = data.message || '操作失败';
throw new Error(errorMessage);
return await response.json();
} catch (error) {
console.error('API request error:', error);
throw error;

View File

@ -251,10 +251,10 @@ export default {
handleUserCommand(command) {
switch (command) {
case 'profile':
this.$router.push('/user/profile')
this.$router.push('/profile')
break
case 'password':
this.$router.push('/user/profile?tab=password')
this.$router.push('/profile?tab=password')
break
case 'logout':
this.logout()
@ -303,10 +303,10 @@ export default {
handleNotificationCommand(command) {
switch (command) {
case 'expired':
this.$router.push('/rental?subStatus=expired')
this.$router.push('/room/status?searchRentalStatus=expired')
break
case 'soonExpire':
this.$router.push('/rental?subStatus=soon_expire')
this.$router.push('/room/status?searchRentalStatus=soon_expire')
break
}
},

View File

@ -134,10 +134,13 @@ export default {
//
const apartmentsResponse = await apartmentApi.getAll(params)
this.apartments = apartmentsResponse.data
this.total = apartmentsResponse.total
// : { data: { list: [], total: n, page: n, pageSize: n } }
this.apartments = apartmentsResponse.data.list
this.total = apartmentsResponse.data.total
} catch (error) {
this.$message.error('加载数据失败')
console.error('加载数据失败:', error)
this.$message.error('加载数据失败: ' + (error.message || '未知错误'))
} finally {
this.isLoading = false
}

View File

@ -65,7 +65,7 @@
<el-option
v-for="rental in roomRentals"
:key="rental.id"
:label="rental.tenantName + ' (' + rental.startDate + ' 至 ' + rental.endDate + ')'"
:label="rental.Renter.name + ' (' + rental.startDate + ' 至 ' + rental.endDate + ')'"
:value="rental.id"
></el-option>
</el-select>
@ -176,7 +176,7 @@ export default {
this.roomRentals = []
if (roomId) {
try {
const response = await rentalApi.getAll({ roomId, status: 'active' })
const response = await rentalApi.list({ roomId, status: 'active' })
this.roomRentals = response.data || []
} catch (error) {
console.error('加载租约列表失败:', error)

View File

@ -248,8 +248,10 @@ export default {
}
const response = await billApi.getAll(params)
this.bills = response.data || []
this.total = response.total || 0
// : { data: { list: [], total: n, page: n, pageSize: n } }
this.bills = response.data.list
this.total = response.data.total
} catch (error) {
this.$message.error('加载数据失败')
console.error(error)

View File

@ -264,10 +264,10 @@ export default {
}
const response = await meterReadingApi.getAll(params)
if (response.data) {
this.meterReadings = response.data
this.total = response.total
}
// : { data: { list: [], total: n, page: n, pageSize: n } }
this.meterReadings = response.data.list
this.total = response.data.total
} catch (error) {
this.$message.error('加载数据失败')
} finally {

View File

@ -279,7 +279,7 @@
<el-button type="primary" size="mini" icon="el-icon-view" @click="handleViewRental(scope.row)">
详情
</el-button>
<el-button type="warning" size="mini" icon="el-icon-edit" @click="handleEditRental(scope.row)" v-if="scope.row.status === 'active' && hasPermission('rental:update')">
<el-button type="warning" size="mini" icon="el-icon-edit" @click="handleEditRental(scope.row)" v-if="scope.row.status === 'active' && hasPermission('rental:edit')">
编辑
</el-button>
<el-button type="danger" size="mini" icon="el-icon-delete" @click="handleDeleteRental(scope.row)" v-if="hasPermission('rental:delete')">
@ -305,7 +305,7 @@
<el-tab-pane label="抄表记录" name="meter">
<div class="tab-header">
<span class="tab-title">水电气抄表记录</span>
<el-button type="primary" size="small" icon="el-icon-plus" @click="handleAddMeterReading" v-if="hasPermission('meter:add') && room.status === 'rented'">
<el-button type="primary" size="small" icon="el-icon-plus" @click="handleAddMeterReading" v-if="hasPermission('meter_reading:add') && room.status === 'rented'">
新增抄表
</el-button>
</div>
@ -348,10 +348,10 @@
</el-table-column>
<el-table-column label="操作" min-width="150" fixed="right">
<template slot-scope="scope">
<el-button type="primary" size="mini" @click="handleEditMeterReading(scope.row)" v-if="hasPermission('meter:update')">
<el-button type="primary" size="mini" @click="handleEditMeterReading(scope.row)" v-if="hasPermission('meter_reading:edit')">
编辑
</el-button>
<el-button type="danger" size="mini" @click="handleDeleteMeterReading(scope.row)" v-if="hasPermission('meter:delete')">
<el-button type="danger" size="mini" @click="handleDeleteMeterReading(scope.row)" v-if="hasPermission('meter_reading:delete')">
删除
</el-button>
</template>
@ -412,10 +412,10 @@
<el-table-column prop="remark" label="备注" min-width="150" show-overflow-tooltip></el-table-column>
<el-table-column label="操作" min-width="200" fixed="right">
<template slot-scope="scope">
<el-button type="success" size="mini" @click="handleReceivePayment(scope.row)" v-if="scope.row.status !== 'paid' && hasPermission('bill:update')">
<el-button type="success" size="mini" @click="handleReceivePayment(scope.row)" v-if="scope.row.status !== 'paid' && hasPermission('bill:edit')">
收款
</el-button>
<el-button type="primary" size="mini" @click="handleEditBill(scope.row)" v-if="hasPermission('bill:update')">
<el-button type="primary" size="mini" @click="handleEditBill(scope.row)" v-if="hasPermission('bill:edit')">
编辑
</el-button>
<el-button type="danger" size="mini" @click="handleDeleteBill(scope.row)" v-if="hasPermission('bill:delete')">
@ -1107,7 +1107,8 @@ export default {
const roomId = this.$route.params.id
//
const billRes = await billApi.getAll({ roomId, page: 1, pageSize: 9999 })
const bills = billRes.data || []
const pageData = billRes.data || {}
const bills = pageData.list || []
this.stats.totalRent = bills
.filter(b => b.category === 'rent')
@ -1136,8 +1137,9 @@ export default {
page: this.currentPage,
pageSize: this.pageSize
})
this.rentalHistory = res.data || []
this.total = res.total || 0
const pageData = res.data || {}
this.rentalHistory = pageData.list || []
this.total = pageData.total || 0
//
this.currentRental = this.rentalHistory.find(r => r.status === 'active')
@ -1159,8 +1161,9 @@ export default {
page: this.meterCurrentPage,
pageSize: this.meterPageSize
})
this.meterReadings = res.data || []
this.meterTotal = res.total || 0
const pageData = res.data || {}
this.meterReadings = pageData.list || []
this.meterTotal = pageData.total || 0
} catch (error) {
this.$message.error('加载抄表记录失败')
console.error(error)
@ -1179,8 +1182,9 @@ export default {
page: this.billCurrentPage,
pageSize: this.billPageSize
})
this.bills = res.data || []
this.billTotal = res.total || 0
const pageData = res.data || {}
this.bills = pageData.list || []
this.billTotal = pageData.total || 0
//
this.unpaidBills = this.bills.filter(b => b.status !== 'paid')

View File

@ -513,16 +513,16 @@ export default {
async loadApartments() {
try {
const res = await apartmentApi.getAll()
const res = await apartmentApi.list()
this.apartments = res.data || res || []
} catch (error) {
console.error('加载公寓列表失败:', error)
}
},
async loadRooms() {
try {
const res = await roomApi.getAll({ pageSize: 9999 })
const res = await roomApi.list()
this.rooms = res.data || res || []
} catch (error) {
console.error('加载房间列表失败:', error)
@ -543,9 +543,10 @@ export default {
}
const res = await rentalApi.getAll(params)
// { data, total, page, pageSize }
this.rentalList = res.data || []
this.total = res.total || 0
// { data: { list: [], total: n, page: n, pageSize: n } }
const pageData = res.data || {}
this.rentalList = pageData.list || []
this.total = pageData.total || 0
} catch (error) {
this.$message.error('加载数据失败')
console.error(error)

View File

@ -309,8 +309,10 @@ export default {
}
const response = await rentalApi.getAll(params)
this.rentalList = response.data || response
this.total = response.total || 0
// : { data: { list: [], total: n, page: n, pageSize: n } }
this.rentalList = response.data.list
this.total = response.data.total
} catch (error) {
this.$message.error('加载数据失败')
} finally {

View File

@ -318,8 +318,10 @@ export default {
}
const response = await waterBillApi.getAll(params)
this.waterBillList = response.data || response
this.total = response.total || 0
// : { data: { list: [], total: n, page: n, pageSize: n } }
this.waterBillList = response.data.list
this.total = response.data.total
} catch (error) {
this.$message.error('加载数据失败')
} finally {

View File

@ -157,13 +157,10 @@ export default {
}
const response = await renterApi.getAll(params)
if (response.data && response.data.list) {
this.renters = response.data.list
this.total = response.data.pagination.total
} else {
this.renters = response.data || []
this.total = response.total || 0
}
// : { data: { list: [], total: n, page: n, pageSize: n } }
this.renters = response.data.list
this.total = response.data.total
} catch (error) {
this.$message.error('加载数据失败')
} finally {

View File

@ -48,7 +48,7 @@
style="width: 100%"
v-loading="isLoading"
>
<el-table-column prop="apartmentName" label="公寓" min-width="120"></el-table-column>
<el-table-column prop="Apartment.name" label="公寓" min-width="120"></el-table-column>
<el-table-column prop="roomNumber" label="房间号" width="100"></el-table-column>
<el-table-column prop="floor" label="楼层" width="80"></el-table-column>
<el-table-column prop="roomType" label="户型" width="120"></el-table-column>
@ -82,7 +82,7 @@
<div v-for="item in rooms" :key="item.id" class="mobile-card">
<div class="mobile-card-header">
<div class="mobile-card-title">
<span>{{ item.apartmentName }}</span>
<span>{{ item.Apartment && item.Apartment.name }}</span>
<span class="room-number">{{ item.roomNumber }}</span>
</div>
<div class="mobile-card-tags">
@ -206,8 +206,8 @@ export default {
this.isLoading = true
try {
const apartmentsResponse = await apartmentApi.getAll()
this.apartments = apartmentsResponse.data || apartmentsResponse
const apartmentsResponse = await apartmentApi.list()
this.apartments = apartmentsResponse.data
const params = {
apartmentId: this.searchForm.apartmentId,
@ -219,25 +219,10 @@ export default {
}
const roomsResponse = await roomApi.getAll(params)
if (roomsResponse.data) {
this.rooms = roomsResponse.data.map(room => {
const apartment = this.apartments.find(a => a.id == room.apartmentId)
return {
...room,
apartmentName: apartment ? apartment.name : ''
}
})
this.total = roomsResponse.total
} else {
this.rooms = roomsResponse.map(room => {
const apartment = this.apartments.find(a => a.id == room.apartmentId)
return {
...room,
apartmentName: apartment ? apartment.name : ''
}
})
this.total = roomsResponse.length
}
// : { data: { list: [], total: n, page: n, pageSize: n } }
this.rooms = roomsResponse.data.list
this.total = roomsResponse.data.total
} catch (error) {
this.$message.error('加载数据失败')
} finally {

View File

@ -521,7 +521,7 @@ export default {
transition: all 0.3s;
position: relative;
overflow: hidden;
min-height: 100px;
min-height: 120px;
}
.room-card:hover {