226 lines
8.5 KiB
JavaScript
226 lines
8.5 KiB
JavaScript
import { url } from '../request';
|
||
|
||
Page({
|
||
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;
|
||
console.log(userInfo.id);
|
||
if (userInfo && userInfo.cookie) {
|
||
my.request({
|
||
url: url + '/api/collect/list',
|
||
method: 'POST',
|
||
data: {
|
||
id:userInfo.id
|
||
},
|
||
headers: {
|
||
'content-type': 'application/json',
|
||
'Cookie': userInfo.cookie,
|
||
},
|
||
dataType: 'json',
|
||
success: (res) => {
|
||
console.log(res);
|
||
if(res.data.code===0){
|
||
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);
|
||
}else if(res.data.code===40100){
|
||
my.alert({
|
||
content: '登录信息已过期,请重新登录'
|
||
});
|
||
my.navigateTo({
|
||
url:'/pages/denglu/denglu'
|
||
})
|
||
}
|
||
|
||
},
|
||
});
|
||
} 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, // 返回请求结果的数据
|
||
});
|
||
}
|
||
else {
|
||
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: {
|
||
id:userInfo.id
|
||
},
|
||
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: '请求失败,请稍后重试' });
|
||
},
|
||
});
|
||
}
|
||
else{
|
||
my.alert({content:'您未登录,请先登录'})
|
||
my.navigateTo({
|
||
url:'/pages/denglu/denglu'
|
||
})
|
||
}
|
||
},
|
||
});
|
||
},
|
||
tiaozhuan(item){
|
||
const id = item.target.dataset.num
|
||
console.log('传递的数据:', id);
|
||
const ID = id.data.id
|
||
console.log(ID);
|
||
const userId = id.data.userId
|
||
const address = id.data.address
|
||
const businessName = id.data.businessName
|
||
const businessAvatar = id.data.businessAvatar
|
||
const endBusiness = id.data.endBusiness
|
||
const startBusiness = id.data.startBusiness
|
||
const storeStatus = id.data.storeStatus
|
||
const businessPhone =id.data.businessPhone
|
||
const level = id.data.level
|
||
console.log('Address being passed: ', level,ID,address,businessName,businessAvatar,endBusiness,startBusiness,businessPhone);
|
||
|
||
my.navigateTo({
|
||
url: `/pages/dianpuzhuye/dianpuzhuye?userId=${userId}
|
||
&&address=${address}&&businessName=${businessName}
|
||
&&businessAvatar=${businessAvatar}&&startBusiness=${startBusiness}
|
||
&&endBusiness=${endBusiness}&&storeStatus=${storeStatus}
|
||
&&id=${ID}&&businessPhone=${businessPhone}&&level=${level}`,
|
||
});
|
||
}
|
||
});
|