实现是否收藏按钮

This commit is contained in:
WindowBird 2025-08-14 15:17:29 +08:00
parent a92033fcb0
commit 5349c734ca
2 changed files with 56 additions and 17 deletions

View File

@ -40,6 +40,17 @@ export function collectMemorial(data) {
}) })
} }
// 检查是否已收藏
export function checkIsCollected(memorialId) {
return request({
url: '/app/collect/isCollect',
method: 'get',
params: {
memorialId: memorialId,
},
})
}
// // 收藏牌位 (使用 form-data 格式,可选) // // 收藏牌位 (使用 form-data 格式,可选)
// export function collectMemorialFormData(data) { // export function collectMemorialFormData(data) {
// return request({ // return request({

View File

@ -124,7 +124,9 @@
<script> <script>
import { CommonEnum } from '@/enum/common.js' import { CommonEnum } from '@/enum/common.js'
import { getDeceasedList, getMemorialDetail } from '@/api/memorial/index.js' import { getDeceasedList, getMemorialDetail } from '@/api/memorial/index.js'
import { getEnshrinedList, collectMemorial } from '@/api/memorial/memorial.js' import { getEnshrinedList, collectMemorial, checkIsCollected } from '@/api/memorial/memorial.js'
import BaseBackground from '../../components/base-background/base-background.vue'
import CustomNavbar from '../../components/custom-navbar/custom-navbar.vue'
import SearchBox from '../../components/search-box/search-box.vue' import SearchBox from '../../components/search-box/search-box.vue'
import StatusDisplay from '../../components/status-display/status-display.vue' import StatusDisplay from '../../components/status-display/status-display.vue'
import EnshrinedList from './compositons/enshrinedList.vue' import EnshrinedList from './compositons/enshrinedList.vue'
@ -134,6 +136,8 @@
export default { export default {
components: { components: {
BaseBackground,
CustomNavbar,
BottomButton, BottomButton,
SearchBox, SearchBox,
StatusDisplay, StatusDisplay,
@ -191,6 +195,8 @@
await this.getMemorialDetail() await this.getMemorialDetail()
// //
await this.getDeceasedList() await this.getDeceasedList()
//
await this.checkCollectionStatus()
} catch (error) { } catch (error) {
console.error('页面初始化失败:', error) console.error('页面初始化失败:', error)
uni.showToast({ uni.showToast({
@ -262,7 +268,33 @@
} }
}, },
// //
async checkCollectionStatus() {
if (!this.selectedUnitId) {
console.warn('未获取到牌位ID无法检查收藏状态')
return
}
try {
const response = await checkIsCollected(this.selectedUnitId)
console.log('收藏状态检查响应:', response)
if (response.code === 200) {
this.isCollected = response.data === true
console.log('当前收藏状态:', this.isCollected)
} else {
console.error('检查收藏状态失败:', response.msg)
//
this.isCollected = false
}
} catch (error) {
console.error('检查收藏状态异常:', error)
//
this.isCollected = false
}
},
// /
async handleCollect() { async handleCollect() {
if (!this.selectedUnitId) { if (!this.selectedUnitId) {
uni.showToast({ uni.showToast({
@ -272,18 +304,10 @@
return return
} }
//
if (this.isCollected) {
uni.showToast({
title: '已经收藏过了',
icon: 'none',
})
return
}
try { try {
const loadingText = this.isCollected ? '取消收藏中...' : '收藏中...'
uni.showLoading({ uni.showLoading({
title: '收藏中...', title: loadingText,
}) })
const data = { const data = {
@ -293,18 +317,20 @@
const response = await collectMemorial(data) const response = await collectMemorial(data)
if (response.code === 200) { if (response.code === 200) {
this.isCollected = true //
this.isCollected = !this.isCollected
const successText = this.isCollected ? '收藏成功' : '取消收藏成功'
uni.showToast({ uni.showToast({
title: '收藏成功', title: successText,
icon: 'success', icon: 'success',
}) })
} else { } else {
throw new Error(response.msg || '收藏失败') throw new Error(response.msg || '操作失败')
} }
} catch (error) { } catch (error) {
console.error('收藏失败:', error) console.error('收藏操作失败:', error)
uni.showToast({ uni.showToast({
title: error.message || '收藏失败', title: error.message || '操作失败',
icon: 'none', icon: 'none',
}) })
} finally { } finally {
@ -381,6 +407,8 @@
// //
await this.getMemorialDetail() await this.getMemorialDetail()
await this.getDeceasedList() await this.getDeceasedList()
//
await this.checkCollectionStatus()
// //
uni.stopPullDownRefresh() uni.stopPullDownRefresh()
uni.showToast({ uni.showToast({