实现是否收藏按钮
This commit is contained in:
parent
a92033fcb0
commit
5349c734ca
|
|
@ -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({
|
||||
|
|
|
|||
|
|
@ -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({
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user