实现是否收藏按钮

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 格式,可选)
// export function collectMemorialFormData(data) {
// return request({

View File

@ -124,7 +124,9 @@
<script>
import { CommonEnum } from '@/enum/common.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 StatusDisplay from '../../components/status-display/status-display.vue'
import EnshrinedList from './compositons/enshrinedList.vue'
@ -134,6 +136,8 @@
export default {
components: {
BaseBackground,
CustomNavbar,
BottomButton,
SearchBox,
StatusDisplay,
@ -191,6 +195,8 @@
await this.getMemorialDetail()
//
await this.getDeceasedList()
//
await this.checkCollectionStatus()
} catch (error) {
console.error('页面初始化失败:', error)
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() {
if (!this.selectedUnitId) {
uni.showToast({
@ -272,18 +304,10 @@
return
}
//
if (this.isCollected) {
uni.showToast({
title: '已经收藏过了',
icon: 'none',
})
return
}
try {
const loadingText = this.isCollected ? '取消收藏中...' : '收藏中...'
uni.showLoading({
title: '收藏中...',
title: loadingText,
})
const data = {
@ -293,18 +317,20 @@
const response = await collectMemorial(data)
if (response.code === 200) {
this.isCollected = true
//
this.isCollected = !this.isCollected
const successText = this.isCollected ? '收藏成功' : '取消收藏成功'
uni.showToast({
title: '收藏成功',
title: successText,
icon: 'success',
})
} else {
throw new Error(response.msg || '收藏失败')
throw new Error(response.msg || '操作失败')
}
} catch (error) {
console.error('收藏失败:', error)
console.error('收藏操作失败:', error)
uni.showToast({
title: error.message || '收藏失败',
title: error.message || '操作失败',
icon: 'none',
})
} finally {
@ -381,6 +407,8 @@
//
await this.getMemorialDetail()
await this.getDeceasedList()
//
await this.checkCollectionStatus()
//
uni.stopPullDownRefresh()
uni.showToast({