Browse Source

移除API方法中的自动登录逻辑,改为用户主动选择登录

pull/1/head
徐飞洋 3 months ago
parent
commit
9368a88fe7
  1. 37
      app.js
  2. 4
      app.json
  3. 1
      custom-tab-bar/index.js
  4. 40
      pages/buyer/index.js
  5. 35
      pages/favorites/index.js
  6. 2
      pages/profile/index.js
  7. 125
      utils/api.js

37
app.js

@ -1,4 +1,41 @@
App({
// 全局事件总线功能
eventBus: {
listeners: {},
// 监听事件
on: function (event, callback) {
if (!this.listeners[event]) {
this.listeners[event] = [];
}
this.listeners[event].push(callback);
},
// 触发事件
emit: function (event, data) {
if (this.listeners[event]) {
this.listeners[event].forEach(callback => {
try {
callback(data);
} catch (error) {
console.error('事件处理错误:', error);
}
});
}
},
// 移除事件监听
off: function (event, callback) {
if (this.listeners[event]) {
if (callback) {
this.listeners[event] = this.listeners[event].filter(cb => cb !== callback);
} else {
delete this.listeners[event];
}
}
}
},
onLaunch: function () {
// 初始化应用
console.log('App Launch')

4
app.json

@ -73,6 +73,10 @@
"pagePath": "pages/seller/index",
"text": "货源"
},
{
"pagePath": "pages/favorites/index",
"text": "收藏"
},
{
"pagePath": "pages/profile/index",
"text": "我的"

1
custom-tab-bar/index.js

@ -94,6 +94,7 @@ Component({
'pages/index/index',
'pages/buyer/index',
'pages/seller/index',
'pages/favorites/index',
'pages/profile/index'
];

40
pages/buyer/index.js

@ -123,6 +123,14 @@ Page({
this.fallbackToLocalStorageWithPagination();
});
});
// 添加收藏状态变化事件监听
const app = getApp();
this.favoriteChangedHandler = (data) => {
console.log('收到收藏状态变化通知:', data);
this.updateGoodsFavoriteStatus(data.productId, data.isFavorite);
};
app.eventBus.on('favoriteChanged', this.favoriteChangedHandler);
},
// 点击"我想要"按钮
@ -1069,6 +1077,7 @@ Page({
String(id) === String(item.id) ||
String(id) === String(item.productId)
),
isFavorite: false, // 初始化收藏状态为false
reservedCount: finalReservationCount,
currentImageIndex: item.currentImageIndex || 0,
createdAt: item.createdAt || item.created_at || item.createTime || null
@ -2013,6 +2022,13 @@ Page({
// 更新商品的收藏状态
this.updateGoodsFavoriteStatus(productId, true);
// 触发全局事件,通知其他页面收藏状态已更改
const app = getApp();
app.eventBus.emit('favoriteChanged', {
productId: productId,
isFavorite: true
});
// 显示成功提示
wx.showToast({
title: '收藏成功',
@ -2066,6 +2082,13 @@ Page({
// 更新商品的收藏状态
this.updateGoodsFavoriteStatus(productId, false);
// 触发全局事件,通知其他页面收藏状态已更改
const app = getApp();
app.eventBus.emit('favoriteChanged', {
productId: productId,
isFavorite: false
});
// 显示成功提示
wx.showToast({
title: '取消收藏成功',
@ -2143,9 +2166,13 @@ Page({
.then(res => {
console.log('loadUserFavorites - 获取收藏列表成功:', res);
if (res.success && res.favorites) {
// 修复:根据API实际返回格式处理数据
const favorites = res.data && res.data.favorites ? res.data.favorites : [];
console.log('loadUserFavorites - 处理后的收藏列表:', favorites);
if (favorites.length > 0) {
// 提取收藏的商品ID列表
const favoriteProductIds = res.favorites.map(item => String(item.productId || item.id));
const favoriteProductIds = favorites.map(item => String(item.productId || item.id));
console.log('loadUserFavorites - 收藏的商品ID列表:', favoriteProductIds);
// 更新商品的收藏状态
@ -2167,5 +2194,14 @@ Page({
console.error('loadUserFavorites - 获取收藏列表失败:', err);
// 错误处理,不影响页面显示
});
},
// 页面卸载时移除事件监听
onUnload: function () {
const app = getApp();
if (this.favoriteChangedHandler) {
app.eventBus.off('favoriteChanged', this.favoriteChangedHandler);
console.log('移除收藏状态变化事件监听');
}
}
});

35
pages/favorites/index.js

@ -33,6 +33,15 @@ Page({
*/
onLoad(options) {
this.loadFavorites();
// 添加收藏状态变化事件监听
const app = getApp();
this.favoriteChangedHandler = (data) => {
console.log('收藏页面收到收藏状态变化通知:', data);
// 重新加载收藏列表以更新状态
this.loadFavorites();
};
app.eventBus.on('favoriteChanged', this.favoriteChangedHandler);
},
/**
@ -48,6 +57,18 @@ Page({
onShow() {
// 每次显示页面时重新加载收藏列表
this.loadFavorites();
// 更新自定义tabBar状态
if (typeof this.getTabBar === 'function' && this.getTabBar()) {
this.getTabBar().setData({
selected: 3
});
}
// 更新全局tab状态
const app = getApp();
if (app.updateCurrentTab) {
app.updateCurrentTab('favorites');
}
},
/**
@ -61,7 +82,12 @@ Page({
* 生命周期函数--监听页面卸载
*/
onUnload() {
// 移除事件监听
const app = getApp();
if (this.favoriteChangedHandler) {
app.eventBus.off('favoriteChanged', this.favoriteChangedHandler);
console.log('收藏页面移除收藏状态变化事件监听');
}
},
/**
@ -161,6 +187,13 @@ Page({
title: '取消收藏成功',
icon: 'success'
});
// 触发全局事件,通知其他页面收藏状态已更改
const app = getApp();
app.eventBus.emit('favoriteChanged', {
productId: productId,
isFavorite: false
});
}).catch(err => {
console.error('取消收藏失败:', err);
wx.showToast({

2
pages/profile/index.js

@ -16,7 +16,7 @@ Page({
// 更新自定义tabBar状态
if (typeof this.getTabBar === 'function' && this.getTabBar()) {
this.getTabBar().setData({
selected: 3
selected: 4
});
}
// 更新全局tab状态

125
utils/api.js

@ -2041,40 +2041,16 @@ module.exports = {
}
}
// 如果没有openid,需要先登录
// 如果没有openid,直接返回未登录错误
if (!openid) {
return this.login().then(loginRes => {
// 重新尝试添加收藏
return this.addFavorite(productId);
}).catch(loginErr => {
reject(new Error('用户未登录,无法添加收藏'));
});
return;
}
// 如果没有手机号,需要先获取
// 如果没有手机号,直接返回错误
if (!userPhone) {
// 尝试获取用户信息
return this.getUserInfo(openid).then(userInfoRes => {
let phoneNumber = null;
if (userInfoRes.data && userInfoRes.data.phoneNumber) {
phoneNumber = userInfoRes.data.phoneNumber;
}
if (!phoneNumber) {
reject(new Error('无法获取用户手机号,无法添加收藏'));
} else {
// 保存手机号到本地
if (userId) {
users[userId] = users[userId] || {};
users[userId].phoneNumber = phoneNumber;
wx.setStorageSync('users', users);
}
// 重新尝试添加收藏
return this.addFavorite(productId);
}
}).catch(err => {
reject(new Error('无法获取用户信息,无法添加收藏'));
});
return;
}
// 构建收藏数据
@ -2119,39 +2095,16 @@ module.exports = {
}
}
// 如果没有openid,直接返回未登录错误
if (!openid) {
return this.login().then(loginRes => {
// 重新尝试取消收藏
return this.cancelFavorite(productId);
}).catch(loginErr => {
reject(new Error('用户未登录,无法取消收藏'));
});
return;
}
// 如果没有手机号,需要先获取
// 如果没有手机号,直接返回错误
if (!userPhone) {
// 尝试获取用户信息
return this.getUserInfo(openid).then(userInfoRes => {
let phoneNumber = null;
if (userInfoRes.data && userInfoRes.data.phoneNumber) {
phoneNumber = userInfoRes.data.phoneNumber;
}
if (!phoneNumber) {
reject(new Error('无法获取用户手机号,无法取消收藏'));
} else {
// 保存手机号到本地
if (userId) {
users[userId] = users[userId] || {};
users[userId].phoneNumber = phoneNumber;
wx.setStorageSync('users', users);
}
// 重新尝试取消收藏
return this.cancelFavorite(productId);
}
}).catch(err => {
reject(new Error('无法获取用户信息,无法取消收藏'));
});
return;
}
const cancelData = {
@ -2193,39 +2146,16 @@ module.exports = {
}
}
// 如果没有openid,直接返回未登录错误
if (!openid) {
return this.login().then(loginRes => {
// 重新尝试获取收藏列表
return this.getFavorites();
}).catch(loginErr => {
reject(new Error('用户未登录,无法获取收藏列表'));
});
return;
}
// 如果没有手机号,需要先获取
// 如果没有手机号,直接返回错误
if (!userPhone) {
// 尝试获取用户信息
return this.getUserInfo(openid).then(userInfoRes => {
let phoneNumber = null;
if (userInfoRes.data && userInfoRes.data.phoneNumber) {
phoneNumber = userInfoRes.data.phoneNumber;
}
if (!phoneNumber) {
reject(new Error('无法获取用户手机号,无法获取收藏列表'));
} else {
// 保存手机号到本地
if (userId) {
users[userId] = users[userId] || {};
users[userId].phoneNumber = phoneNumber;
wx.setStorageSync('users', users);
}
// 重新尝试获取收藏列表
return this.getFavorites();
}
}).catch(err => {
reject(new Error('无法获取用户信息,无法获取收藏列表'));
});
return;
}
const requestData = {
@ -2892,39 +2822,16 @@ module.exports = {
}
}
// 如果没有openid,直接返回未登录错误
if (!openid) {
return this.login().then(loginRes => {
// 重新尝试取消收藏
return this.cancelFavorite(productId);
}).catch(loginErr => {
reject(new Error('用户未登录,无法取消收藏'));
});
return;
}
// 如果没有手机号,需要先获取
// 如果没有手机号,直接返回错误
if (!userPhone) {
// 尝试获取用户信息
return this.getUserInfo(openid).then(userInfoRes => {
let phoneNumber = null;
if (userInfoRes.data && userInfoRes.data.phoneNumber) {
phoneNumber = userInfoRes.data.phoneNumber;
}
if (!phoneNumber) {
reject(new Error('无法获取用户手机号,无法取消收藏'));
} else {
// 保存手机号到本地
if (userId) {
users[userId] = users[userId] || {};
users[userId].phoneNumber = phoneNumber;
wx.setStorageSync('users', users);
}
// 重新尝试取消收藏
return this.cancelFavorite(productId);
}
}).catch(err => {
reject(new Error('无法获取用户信息,无法取消收藏'));
});
return;
}
const requestData = {

Loading…
Cancel
Save