ai-c/pages/interest-partner/interest-partner.js
2026-02-02 18:21:32 +08:00

282 lines
7.8 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// pages/interest-partner/interest-partner.js - 兴趣搭子页面Figma设计
const api = require('../../utils/api')
const config = require('../../config/index')
const app = getApp()
Page({
data: {
statusBarHeight: 44,
navBarHeight: 44,
totalNavHeight: 88,
// 二维码弹窗
showQrcodeModal: false,
selectedPartner: null, // 当前选中的搭子对象
// 兴趣搭子列表
partnerList: [],
loading: false,
// 固定的6个分类与Figma设计一致
fixedCategories: [
{ name: '美食聚餐', icon: 'food-icon', desc: '同城美食 共享美味' },
{ name: '旅游出行', icon: 'travel-icon', desc: '结伴出游 共度美好' },
{ name: '唱歌观影', icon: 'entertainment-icon', desc: '老歌金曲 经典影视' },
{ name: '舞蹈走秀', icon: 'dance-icon', desc: '展现风采 舞动人生' },
{ name: '书画摄影', icon: 'art-icon', desc: '陶冶情操 记录生活之美' },
{ name: '运动康养', icon: 'sports-icon', desc: '多种运动 身心健康' }
]
},
onLoad() {
// 计算导航栏高度
const systemInfo = wx.getSystemInfoSync()
const statusBarHeight = systemInfo.statusBarHeight || 44
const menuButton = wx.getMenuButtonBoundingClientRect()
const navBarHeight = menuButton.height + (menuButton.top - statusBarHeight) * 2
const totalNavHeight = statusBarHeight + navBarHeight
this.setData({
statusBarHeight,
navBarHeight,
totalNavHeight
})
// 加载兴趣搭子列表
this.loadPartnerList()
},
/**
* 页面显示时刷新数据
*/
onShow() {
// 每次显示页面时重新加载数据,确保数据是最新的
this.loadPartnerList()
},
/**
* 下拉刷新
*/
onPullDownRefresh() {
console.log('下拉刷新兴趣搭子列表')
this.loadPartnerList().then(() => {
wx.stopPullDownRefresh()
wx.showToast({
title: '刷新成功',
icon: 'success'
})
}).catch(() => {
wx.stopPullDownRefresh()
})
},
/**
* 加载兴趣搭子列表
*/
async loadPartnerList() {
if (this.data.loading) return
this.setData({ loading: true })
try {
// 添加时间戳参数,防止缓存
const timestamp = Date.now()
const res = await api.interest.getList({ _t: timestamp })
console.log('[兴趣搭子] API原始响应:', JSON.stringify(res).substring(0, 500))
// 线上API返回格式{ success: true, data: [...] } 或 { success: true, data: { list: [...] } }
if (res.success && res.data) {
// 兼容两种返回格式
let partnerList = Array.isArray(res.data) ? res.data : (res.data.list || [])
console.log('[兴趣搭子] 解析后的列表数量:', partnerList.length)
if (partnerList.length > 0) {
console.log('[兴趣搭子] 第一条数据示例:', JSON.stringify(partnerList[0]))
}
// 处理图片URL - 根据API文档icon和qr_code已经是完整的API路径
partnerList = partnerList.map(item => {
// 处理icon字段
let iconUrl = '/images/icon-interest-default.png' // 默认图标
if (item.icon && item.icon.trim()) {
if (item.icon.startsWith('http://') || item.icon.startsWith('https://')) {
// 已经是完整URL
iconUrl = item.icon
} else if (item.icon.startsWith('/')) {
// 相对路径,需要拼接域名
iconUrl = `https://ai-c.maimanji.com${item.icon}`
} else {
// 不以/开头,添加/再拼接
iconUrl = `https://ai-c.maimanji.com/${item.icon}`
}
}
// 处理qr_code字段
let qrCodeUrl = ''
if (item.qr_code && item.qr_code.trim()) {
if (item.qr_code.startsWith('http://') || item.qr_code.startsWith('https://')) {
qrCodeUrl = item.qr_code
} else if (item.qr_code.startsWith('/')) {
qrCodeUrl = `https://ai-c.maimanji.com${item.qr_code}`
} else {
qrCodeUrl = `https://ai-c.maimanji.com/${item.qr_code}`
}
}
console.log(`[兴趣搭子] ${item.name} - 原始icon: ${item.icon}, 处理后: ${iconUrl}`)
return {
...item,
icon: iconUrl,
qr_code: qrCodeUrl
}
})
console.log(`[${new Date().toLocaleTimeString()}] 兴趣搭子列表加载成功,共 ${partnerList.length} 条数据`)
this.setData({
partnerList,
loading: false
})
// 返回Promise以支持下拉刷新
return Promise.resolve()
} else {
console.warn('兴趣搭子列表返回格式异常:', res)
this.setData({ loading: false })
wx.showToast({
title: '加载失败',
icon: 'none'
})
return Promise.reject()
}
} catch (err) {
console.error('加载兴趣搭子列表失败:', err)
this.setData({ loading: false })
wx.showToast({
title: '网络错误',
icon: 'none'
})
return Promise.reject(err)
}
},
/**
* 返回上一页
*/
onBack() {
wx.navigateBack()
},
/**
* 点击兴趣卡片
*/
onInterestTap(e) {
const { partner } = e.currentTarget.dataset
if (!partner) {
wx.showToast({
title: '数据加载中',
icon: 'none'
})
return
}
// 检查是否有二维码
if (!partner.qr_code || !partner.qr_code.trim()) {
wx.showToast({
title: '二维码暂未配置',
icon: 'none'
})
return
}
// 二维码URL已在loadPartnerList中处理过直接使用
this.setData({
showQrcodeModal: true,
selectedPartner: partner
})
},
/**
* 关闭二维码弹窗
*/
onCloseQrcodeModal() {
this.setData({
showQrcodeModal: false,
selectedPartner: null
})
},
/**
* 保存二维码
*/
onSaveQrcode() {
const { selectedPartner } = this.data
if (!selectedPartner || !selectedPartner.qr_code) {
wx.showToast({
title: '二维码加载中',
icon: 'none'
})
return
}
wx.showLoading({ title: '保存中...' })
// 下载图片
wx.downloadFile({
url: selectedPartner.qr_code,
success: (res) => {
if (res.statusCode === 200) {
// 保存到相册
wx.saveImageToPhotosAlbum({
filePath: res.tempFilePath,
success: () => {
wx.hideLoading()
wx.showToast({
title: '已保存到相册',
icon: 'success'
})
this.onCloseQrcodeModal()
},
fail: (err) => {
wx.hideLoading()
if (err.errMsg.includes('auth deny')) {
wx.showModal({
title: '需要授权',
content: '请允许访问相册以保存二维码',
confirmText: '去设置',
success: (modalRes) => {
if (modalRes.confirm) {
wx.openSetting()
}
}
})
} else {
wx.showToast({
title: '保存失败',
icon: 'none'
})
}
}
})
} else {
wx.hideLoading()
wx.showToast({
title: '下载失败',
icon: 'none'
})
}
},
fail: () => {
wx.hideLoading()
wx.showToast({
title: '下载失败',
icon: 'none'
})
}
})
}
})