2024-12-10 10:35:22 +00:00
|
|
|
|
import { url } from '../request';
|
|
|
|
|
|
2024-11-10 07:01:22 +00:00
|
|
|
|
Page({
|
2024-12-10 10:35:22 +00:00
|
|
|
|
data: {
|
|
|
|
|
rightBtns: [
|
|
|
|
|
{
|
|
|
|
|
text: '取消收藏',
|
|
|
|
|
bgColor: '#CCCCCC',
|
|
|
|
|
color: '#fff',
|
|
|
|
|
width: 200,
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
soucang: [], // 收藏的列表
|
|
|
|
|
collectedData: [], // 存储每个 id 请求的数据
|
|
|
|
|
swipeIndex: -1,
|
|
|
|
|
shuzhi: '', // 当前点击的收藏 id
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
onButtonTap(event) {
|
|
|
|
|
const storeId = event.currentTarget.dataset.id;
|
|
|
|
|
console.log('点击了店铺ID:', storeId);
|
|
|
|
|
this.setData({
|
|
|
|
|
shuzhi: storeId
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
onShow() {
|
|
|
|
|
my.getStorage({
|
|
|
|
|
key: 'userInfo',
|
|
|
|
|
success: (res) => {
|
|
|
|
|
const userInfo = res.data;
|
|
|
|
|
if (userInfo && userInfo.cookie) {
|
|
|
|
|
my.request({
|
|
|
|
|
url: url + '/api/collect/list',
|
|
|
|
|
method: 'POST',
|
|
|
|
|
data: {},
|
|
|
|
|
headers: {
|
|
|
|
|
'content-type': 'application/json',
|
|
|
|
|
'Cookie': userInfo.cookie,
|
|
|
|
|
},
|
|
|
|
|
dataType: 'json',
|
|
|
|
|
success: (res) => {
|
|
|
|
|
console.log(res);
|
2024-12-17 11:46:10 +00:00
|
|
|
|
if(res.data.code===0){
|
|
|
|
|
const soucang = res.data.data || [];
|
2024-12-10 10:35:22 +00:00
|
|
|
|
this.setData({ soucang });
|
|
|
|
|
// 从返回的 data 数组中提取 id 并存储到 collectedData 中
|
|
|
|
|
const collectedIds = soucang.map(item => item.id); // 提取 id
|
|
|
|
|
this.setData({
|
2024-12-17 11:46:10 +00:00
|
|
|
|
collectedData: collectedIds // 存储id
|
2024-12-10 10:35:22 +00:00
|
|
|
|
});
|
|
|
|
|
this.fetchCollectedData(soucang);
|
2024-12-17 11:46:10 +00:00
|
|
|
|
}else if(res.data.code===40100){
|
|
|
|
|
my.alert({
|
|
|
|
|
content: '登录信息已过期,请重新登录'
|
|
|
|
|
});
|
|
|
|
|
my.navigateTo({
|
|
|
|
|
url:'/pages/denglu/denglu'
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-10 10:35:22 +00:00
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
my.alert({
|
|
|
|
|
content: '您未登录,请先登录。',
|
|
|
|
|
success: () => {
|
|
|
|
|
my.navigateTo({
|
|
|
|
|
url: '/pages/denglu/denglu',
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// 获取每个 id 的数据
|
|
|
|
|
fetchCollectedData(soucang) {
|
|
|
|
|
const requests = soucang.map(item => {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
console.log("发送 id:", item.businessId);
|
|
|
|
|
my.request({
|
|
|
|
|
url: url + '/api/business/userGetById', // 正确的接口
|
|
|
|
|
method: 'GET',
|
|
|
|
|
data: {
|
|
|
|
|
id: item.businessId, // 传递 id 作为参数
|
|
|
|
|
},
|
|
|
|
|
headers: {
|
|
|
|
|
'content-type': 'application/json',
|
|
|
|
|
},
|
|
|
|
|
success: (res) => {
|
|
|
|
|
console.log(res, 'id:', item.businessId);
|
|
|
|
|
if (res.data) {
|
|
|
|
|
// 将 businessId 和返回的数据一起存储
|
|
|
|
|
resolve({
|
|
|
|
|
businessId: item.id, // 添加 businessId
|
|
|
|
|
data: res.data.data, // 返回请求结果的数据
|
|
|
|
|
});
|
2024-12-17 11:46:10 +00:00
|
|
|
|
}
|
|
|
|
|
else {
|
2024-12-10 10:35:22 +00:00
|
|
|
|
reject('请求数据为空');
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
fail: (error) => {
|
|
|
|
|
reject('请求失败: ' + error);
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 等待所有请求完成
|
|
|
|
|
Promise.all(requests)
|
|
|
|
|
.then(results => {
|
|
|
|
|
console.log('所有请求完成', results);
|
|
|
|
|
// 更新 collectedData,保存 businessId 和对应的数据
|
|
|
|
|
this.setData({
|
|
|
|
|
collectedData: results, // 存储 {businessId, data} 结构的数据
|
|
|
|
|
});
|
|
|
|
|
})
|
|
|
|
|
.catch(error => {
|
|
|
|
|
console.error('请求失败:', error);
|
|
|
|
|
my.alert({ content: '部分数据请求失败,请稍后重试' });
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
quxiao() {
|
|
|
|
|
const number = this.data.shuzhi;
|
|
|
|
|
console.log('取消收藏 id:', number);
|
|
|
|
|
my.request({
|
|
|
|
|
url: url + '/api/collect/delete', // 正确的接口
|
|
|
|
|
method: 'POST',
|
|
|
|
|
data: {
|
|
|
|
|
id: number // 传递 id 作为参数
|
|
|
|
|
},
|
|
|
|
|
headers: {
|
|
|
|
|
'content-type': 'application/json',
|
|
|
|
|
},
|
|
|
|
|
success: (res) => {
|
|
|
|
|
if (number) {
|
|
|
|
|
my.alert({ content: '取消收藏成功。' });
|
|
|
|
|
this.updateCartList();
|
|
|
|
|
} else {
|
|
|
|
|
my.alert({ content: '请先点击一下要取消收藏的店铺' });
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
fail: (error) => {
|
|
|
|
|
my.alert({ content: '请求失败,请稍后重试' });
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
updateCartList() {
|
|
|
|
|
my.getStorage({
|
|
|
|
|
key: 'userInfo',
|
|
|
|
|
success: (res) => {
|
|
|
|
|
const userInfo = res.data;
|
|
|
|
|
if (userInfo && userInfo.cookie) {
|
|
|
|
|
my.request({
|
|
|
|
|
url: url + '/api/collect/list',
|
|
|
|
|
method: 'POST',
|
|
|
|
|
data: {},
|
|
|
|
|
headers: {
|
|
|
|
|
'content-type': 'application/json',
|
|
|
|
|
'Cookie': userInfo.cookie,
|
|
|
|
|
},
|
|
|
|
|
dataType: 'json',
|
|
|
|
|
success: (res) => {
|
|
|
|
|
console.log(res);
|
|
|
|
|
const soucang = res.data.data || [];
|
|
|
|
|
this.setData({ soucang });
|
|
|
|
|
|
|
|
|
|
// 从返回的 data 数组中提取 id 并存储到 collectedData 中
|
|
|
|
|
const collectedIds = soucang.map(item => item.id); // 提取 id
|
|
|
|
|
this.setData({
|
|
|
|
|
collectedData: collectedIds // 存储 id
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 你可以在这里继续调用其他函数,进行数据处理或页面渲染
|
|
|
|
|
this.fetchCollectedData(soucang);
|
|
|
|
|
},
|
|
|
|
|
fail: (error) => {
|
|
|
|
|
console.error('请求失败: ', JSON.stringify(error));
|
|
|
|
|
my.alert({ content: '请求失败,请稍后重试' });
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
},
|
2024-11-10 07:01:22 +00:00
|
|
|
|
});
|