|
|
@ -317,11 +317,29 @@ module.exports = { |
|
|
// 添加商品到购物车 - 增强版本,即使本地找不到商品也尝试直接请求服务器
|
|
|
// 添加商品到购物车 - 增强版本,即使本地找不到商品也尝试直接请求服务器
|
|
|
addToCart: function (goodsItem) { |
|
|
addToCart: function (goodsItem) { |
|
|
return new Promise((resolve, reject) => { |
|
|
return new Promise((resolve, reject) => { |
|
|
var openid = wx.getStorageSync('openid'); |
|
|
// 获取用户信息,包含手机号
|
|
|
console.log('API.addToCart - openid:', openid, 'goodsItem:', goodsItem); |
|
|
const users = wx.getStorageSync('users') || {}; |
|
|
|
|
|
const userId = wx.getStorageSync('userId'); |
|
|
|
|
|
let userPhone = null; |
|
|
|
|
|
|
|
|
|
|
|
// 尝试从users中获取手机号
|
|
|
|
|
|
if (userId && users[userId] && users[userId].phoneNumber) { |
|
|
|
|
|
userPhone = users[userId].phoneNumber; |
|
|
|
|
|
} else { |
|
|
|
|
|
// 尝试从全局用户信息获取
|
|
|
|
|
|
const userInfo = wx.getStorageSync('userInfo'); |
|
|
|
|
|
if (userInfo && userInfo.phoneNumber) { |
|
|
|
|
|
userPhone = userInfo.phoneNumber; |
|
|
|
|
|
} else { |
|
|
|
|
|
// 尝试从直接存储的phoneNumber获取
|
|
|
|
|
|
userPhone = wx.getStorageSync('phoneNumber'); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
console.log('API.addToCart - userPhone:', userPhone, 'goodsItem:', goodsItem); |
|
|
|
|
|
|
|
|
// 1. 验证用户登录状态
|
|
|
// 1. 验证用户登录状态
|
|
|
if (!openid) { |
|
|
if (!userPhone) { |
|
|
return reject(new Error('用户未登录')); |
|
|
return reject(new Error('用户未登录')); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -358,7 +376,7 @@ module.exports = { |
|
|
console.log(`尝试添加到购物车,第${attempts}/${maxAttempts}次尝试`); |
|
|
console.log(`尝试添加到购物车,第${attempts}/${maxAttempts}次尝试`); |
|
|
|
|
|
|
|
|
// 总是先尝试直接向服务器发送请求(这是修复新创建货源的关键)
|
|
|
// 总是先尝试直接向服务器发送请求(这是修复新创建货源的关键)
|
|
|
this.sendAddToCartRequest(openid, basicProduct).then(resolve).catch(err => { |
|
|
this.sendAddToCartRequest(userPhone, basicProduct).then(resolve).catch(err => { |
|
|
console.error('添加到购物车请求失败:', err.message, '尝试次数:', attempts); |
|
|
console.error('添加到购物车请求失败:', err.message, '尝试次数:', attempts); |
|
|
|
|
|
|
|
|
// 检查是否为外键约束错误或者服务器找不到商品的情况
|
|
|
// 检查是否为外键约束错误或者服务器找不到商品的情况
|
|
|
@ -386,13 +404,13 @@ module.exports = { |
|
|
|
|
|
|
|
|
console.log('使用的商品信息:', updatedProduct); |
|
|
console.log('使用的商品信息:', updatedProduct); |
|
|
// 直接再次尝试,不再经过复杂的判断
|
|
|
// 直接再次尝试,不再经过复杂的判断
|
|
|
this.sendAddToCartRequest(openid, updatedProduct).then(resolve).catch(reject); |
|
|
this.sendAddToCartRequest(userPhone, updatedProduct).then(resolve).catch(reject); |
|
|
}).catch(innerErr => { |
|
|
}).catch(innerErr => { |
|
|
console.error('刷新商品列表失败:', innerErr); |
|
|
console.error('刷新商品列表失败:', innerErr); |
|
|
// 刷新失败也尝试再次发送请求,不轻易放弃
|
|
|
// 刷新失败也尝试再次发送请求,不轻易放弃
|
|
|
if (attempts < maxAttempts) { |
|
|
if (attempts < maxAttempts) { |
|
|
console.log('刷新失败,但仍尝试再次发送请求'); |
|
|
console.log('刷新失败,但仍尝试再次发送请求'); |
|
|
this.sendAddToCartRequest(openid, basicProduct).then(resolve).catch(reject); |
|
|
this.sendAddToCartRequest(userPhone, basicProduct).then(resolve).catch(reject); |
|
|
} else { |
|
|
} else { |
|
|
reject(new Error('商品信息已更新,请稍后重试')); |
|
|
reject(new Error('商品信息已更新,请稍后重试')); |
|
|
} |
|
|
} |
|
|
@ -441,12 +459,12 @@ module.exports = { |
|
|
}, |
|
|
}, |
|
|
|
|
|
|
|
|
// 发送添加到购物车请求的辅助方法 - 完全符合服务器格式版
|
|
|
// 发送添加到购物车请求的辅助方法 - 完全符合服务器格式版
|
|
|
sendAddToCartRequest: function (openid, product) { |
|
|
sendAddToCartRequest: function (userPhone, product) { |
|
|
return new Promise((resolve, reject) => { |
|
|
return new Promise((resolve, reject) => { |
|
|
// 重要:直接使用传入的openid参数,不再本地重新获取
|
|
|
// 重要:直接使用传入的userPhone参数
|
|
|
console.log('构建的product对象:', product); |
|
|
console.log('构建的product对象:', product); |
|
|
console.log('发送添加到购物车请求,productId:', product.productId, '类型:', typeof product.productId); |
|
|
console.log('发送添加到购物车请求,productId:', product.productId, '类型:', typeof product.productId); |
|
|
console.log('用户openid:', openid); |
|
|
console.log('用户手机号:', userPhone); |
|
|
console.log('请求URL:', '/api/cart/add'); |
|
|
console.log('请求URL:', '/api/cart/add'); |
|
|
|
|
|
|
|
|
// 前置验证:确保productId存在且类型正确
|
|
|
// 前置验证:确保productId存在且类型正确
|
|
|
@ -483,10 +501,10 @@ module.exports = { |
|
|
name: product.productName || '未命名商品' |
|
|
name: product.productName || '未命名商品' |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
// 根据服务器端代码分析,服务器期望的格式就是 { openid, product: {...} }
|
|
|
// 根据服务器端代码分析,服务器期望的格式是 { user_phone, product: {...} }
|
|
|
// 这是最简单直接的格式,服务器会自动从product对象中提取数据
|
|
|
// 这是最简单直接的格式,服务器会自动从product对象中提取数据
|
|
|
const requestData = { |
|
|
const requestData = { |
|
|
openid: openid, |
|
|
user_phone: userPhone, |
|
|
product: safeProduct |
|
|
product: safeProduct |
|
|
}; |
|
|
}; |
|
|
console.log('最终发送的请求数据完整结构:', requestData); |
|
|
console.log('最终发送的请求数据完整结构:', requestData); |
|
|
@ -1567,9 +1585,14 @@ module.exports = { |
|
|
}); |
|
|
}); |
|
|
}, |
|
|
}, |
|
|
|
|
|
|
|
|
// 微信登录函数 - 增强版,支持手机号一键登录
|
|
|
// 微信登录函数 - 增强版,强制要求手机号授权登录
|
|
|
login: function (encryptedData = null, iv = null) { |
|
|
login: function (encryptedData = null, iv = null) { |
|
|
return new Promise((resolve, reject) => { |
|
|
return new Promise((resolve, reject) => { |
|
|
|
|
|
// 强制要求手机号授权
|
|
|
|
|
|
if (!encryptedData || !iv) { |
|
|
|
|
|
return reject(new Error('登录必须进行手机号授权')); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// 1. 调用微信登录接口获取code
|
|
|
// 1. 调用微信登录接口获取code
|
|
|
wx.login({ |
|
|
wx.login({ |
|
|
success: loginRes => { |
|
|
success: loginRes => { |
|
|
@ -1818,11 +1841,8 @@ module.exports = { |
|
|
}); |
|
|
}); |
|
|
}).catch(phoneErr => { |
|
|
}).catch(phoneErr => { |
|
|
console.error('手机号上传失败:', phoneErr); |
|
|
console.error('手机号上传失败:', phoneErr); |
|
|
// 手机号上传失败不影响登录,仍然返回登录成功
|
|
|
// 手机号上传失败导致登录失败
|
|
|
resolve({ |
|
|
reject(new Error('手机号授权失败: ' + (phoneErr.message || '未知错误'))); |
|
|
success: true, |
|
|
|
|
|
data: { openid, userId, sessionKey, phoneError: phoneErr } |
|
|
|
|
|
}); |
|
|
|
|
|
}); |
|
|
}); |
|
|
} else { |
|
|
} else { |
|
|
// 没有手机号信息,直接返回登录成功
|
|
|
// 没有手机号信息,直接返回登录成功
|
|
|
@ -1974,11 +1994,8 @@ module.exports = { |
|
|
const sessionKey = wx.getStorageSync('sessionKey'); |
|
|
const sessionKey = wx.getStorageSync('sessionKey'); |
|
|
|
|
|
|
|
|
if (!openid) { |
|
|
if (!openid) { |
|
|
// 如果没有openid,先执行登录
|
|
|
// 如果没有openid,返回错误,不自动登录
|
|
|
return this.login().then(loginRes => { |
|
|
return Promise.reject(new Error('用户未登录')); |
|
|
// 重新尝试上传
|
|
|
|
|
|
return tryUpload(); |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 检查是否包含openid,如果没有则添加
|
|
|
// 检查是否包含openid,如果没有则添加
|
|
|
@ -2023,7 +2040,6 @@ module.exports = { |
|
|
addFavorite: function (productId) { |
|
|
addFavorite: function (productId) { |
|
|
console.log('API.addFavorite - productId:', productId); |
|
|
console.log('API.addFavorite - productId:', productId); |
|
|
return new Promise((resolve, reject) => { |
|
|
return new Promise((resolve, reject) => { |
|
|
const openid = wx.getStorageSync('openid'); |
|
|
|
|
|
const userId = wx.getStorageSync('userId'); |
|
|
const userId = wx.getStorageSync('userId'); |
|
|
|
|
|
|
|
|
// 获取用户信息,包含手机号
|
|
|
// 获取用户信息,包含手机号
|
|
|
@ -2038,18 +2054,15 @@ module.exports = { |
|
|
const userInfo = wx.getStorageSync('userInfo'); |
|
|
const userInfo = wx.getStorageSync('userInfo'); |
|
|
if (userInfo && userInfo.phoneNumber) { |
|
|
if (userInfo && userInfo.phoneNumber) { |
|
|
userPhone = userInfo.phoneNumber; |
|
|
userPhone = userInfo.phoneNumber; |
|
|
|
|
|
} else { |
|
|
|
|
|
// 尝试从直接存储的phoneNumber获取
|
|
|
|
|
|
userPhone = wx.getStorageSync('phoneNumber'); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 如果没有openid,直接返回未登录错误
|
|
|
|
|
|
if (!openid) { |
|
|
|
|
|
reject(new Error('用户未登录,无法添加收藏')); |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 如果没有手机号,直接返回错误
|
|
|
// 如果没有手机号,直接返回错误
|
|
|
if (!userPhone) { |
|
|
if (!userPhone) { |
|
|
reject(new Error('无法获取用户手机号,无法添加收藏')); |
|
|
reject(new Error('请先登录并绑定手机号')); |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -2077,8 +2090,6 @@ module.exports = { |
|
|
cancelFavorite: function (productId) { |
|
|
cancelFavorite: function (productId) { |
|
|
console.log('API.cancelFavorite - productId:', productId); |
|
|
console.log('API.cancelFavorite - productId:', productId); |
|
|
return new Promise((resolve, reject) => { |
|
|
return new Promise((resolve, reject) => { |
|
|
const openid = wx.getStorageSync('openid'); |
|
|
|
|
|
|
|
|
|
|
|
// 获取用户信息,包含手机号
|
|
|
// 获取用户信息,包含手机号
|
|
|
const users = wx.getStorageSync('users') || {}; |
|
|
const users = wx.getStorageSync('users') || {}; |
|
|
const userId = wx.getStorageSync('userId'); |
|
|
const userId = wx.getStorageSync('userId'); |
|
|
@ -2092,18 +2103,15 @@ module.exports = { |
|
|
const userInfo = wx.getStorageSync('userInfo'); |
|
|
const userInfo = wx.getStorageSync('userInfo'); |
|
|
if (userInfo && userInfo.phoneNumber) { |
|
|
if (userInfo && userInfo.phoneNumber) { |
|
|
userPhone = userInfo.phoneNumber; |
|
|
userPhone = userInfo.phoneNumber; |
|
|
|
|
|
} else { |
|
|
|
|
|
// 尝试从直接存储的phoneNumber获取
|
|
|
|
|
|
userPhone = wx.getStorageSync('phoneNumber'); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 如果没有openid,直接返回未登录错误
|
|
|
|
|
|
if (!openid) { |
|
|
|
|
|
reject(new Error('用户未登录,无法取消收藏')); |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 如果没有手机号,直接返回错误
|
|
|
// 如果没有手机号,直接返回错误
|
|
|
if (!userPhone) { |
|
|
if (!userPhone) { |
|
|
reject(new Error('无法获取用户手机号,无法取消收藏')); |
|
|
reject(new Error('请先登录并绑定手机号')); |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -2125,38 +2133,16 @@ module.exports = { |
|
|
}, |
|
|
}, |
|
|
|
|
|
|
|
|
// 获取用户收藏列表
|
|
|
// 获取用户收藏列表
|
|
|
getFavorites: function () { |
|
|
getFavorites: function (user_phone) { |
|
|
console.log('API.getFavorites'); |
|
|
console.log('API.getFavorites - user_phone:', user_phone); |
|
|
return new Promise((resolve, reject) => { |
|
|
return new Promise((resolve, reject) => { |
|
|
const openid = wx.getStorageSync('openid'); |
|
|
// 必须传入user_phone参数且不能为空
|
|
|
|
|
|
if (!user_phone) { |
|
|
// 获取用户信息,包含手机号
|
|
|
reject(new Error('参数错误:必须传入有效的手机号')); |
|
|
const users = wx.getStorageSync('users') || {}; |
|
|
|
|
|
const userId = wx.getStorageSync('userId'); |
|
|
|
|
|
let userPhone = null; |
|
|
|
|
|
|
|
|
|
|
|
// 尝试从users中获取手机号
|
|
|
|
|
|
if (userId && users[userId] && users[userId].phoneNumber) { |
|
|
|
|
|
userPhone = users[userId].phoneNumber; |
|
|
|
|
|
} else { |
|
|
|
|
|
// 尝试从全局用户信息获取
|
|
|
|
|
|
const userInfo = wx.getStorageSync('userInfo'); |
|
|
|
|
|
if (userInfo && userInfo.phoneNumber) { |
|
|
|
|
|
userPhone = userInfo.phoneNumber; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 如果没有openid,直接返回未登录错误
|
|
|
|
|
|
if (!openid) { |
|
|
|
|
|
reject(new Error('用户未登录,无法获取收藏列表')); |
|
|
|
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 如果没有手机号,直接返回错误
|
|
|
const userPhone = user_phone; |
|
|
if (!userPhone) { |
|
|
|
|
|
reject(new Error('无法获取用户手机号,无法获取收藏列表')); |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const requestData = { |
|
|
const requestData = { |
|
|
user_phone: userPhone |
|
|
user_phone: userPhone |
|
|
@ -2164,7 +2150,8 @@ module.exports = { |
|
|
|
|
|
|
|
|
console.log('获取收藏列表请求数据:', requestData); |
|
|
console.log('获取收藏列表请求数据:', requestData); |
|
|
|
|
|
|
|
|
request('/api/favorites/list', 'GET', requestData).then(res => { |
|
|
// 将GET请求改为POST请求,因为GET请求通常不处理请求体
|
|
|
|
|
|
request('/api/favorites/list', 'POST', requestData).then(res => { |
|
|
console.log('获取收藏列表成功:', res); |
|
|
console.log('获取收藏列表成功:', res); |
|
|
resolve(res); |
|
|
resolve(res); |
|
|
}).catch(error => { |
|
|
}).catch(error => { |
|
|
@ -2333,13 +2320,21 @@ module.exports = { |
|
|
// 验证用户登录状态
|
|
|
// 验证用户登录状态
|
|
|
validateUserLogin: function () { |
|
|
validateUserLogin: function () { |
|
|
const openid = wx.getStorageSync('openid'); |
|
|
const openid = wx.getStorageSync('openid'); |
|
|
console.log('API.validateUserLogin - openid:', openid); |
|
|
const phoneNumber = wx.getStorageSync('phoneNumber') || ''; |
|
|
|
|
|
console.log('API.validateUserLogin - openid:', openid, 'phoneNumber:', phoneNumber); |
|
|
|
|
|
|
|
|
|
|
|
// 验证登录状态和手机号
|
|
|
if (!openid) { |
|
|
if (!openid) { |
|
|
return Promise.reject(new Error('用户未登录')); |
|
|
return Promise.reject(new Error('用户未登录')); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (!phoneNumber) { |
|
|
|
|
|
return Promise.reject(new Error('用户未完成手机号授权')); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
return request('/api/user/validate', 'POST', { |
|
|
return request('/api/user/validate', 'POST', { |
|
|
openid: openid |
|
|
openid: openid, |
|
|
|
|
|
phoneNumber: phoneNumber |
|
|
}); |
|
|
}); |
|
|
}, |
|
|
}, |
|
|
|
|
|
|
|
|
@ -2703,6 +2698,12 @@ module.exports = { |
|
|
updateProductContacts: function () { |
|
|
updateProductContacts: function () { |
|
|
return request('/api/products/update-contacts', 'POST'); |
|
|
return request('/api/products/update-contacts', 'POST'); |
|
|
}, |
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
// 获取产品详情
|
|
|
|
|
|
getProductDetail: function ({ productId }) { |
|
|
|
|
|
console.log('API.getProductDetail - productId:', productId); |
|
|
|
|
|
return request('/api/products/detail', 'POST', { productId: productId }); |
|
|
|
|
|
}, |
|
|
|
|
|
|
|
|
// 预约商品
|
|
|
// 预约商品
|
|
|
reserveProduct: function ({ id }) { |
|
|
reserveProduct: function ({ id }) { |
|
|
@ -2804,8 +2805,6 @@ module.exports = { |
|
|
cancelFavorite: function (productId) { |
|
|
cancelFavorite: function (productId) { |
|
|
console.log('API.cancelFavorite - productId:', productId); |
|
|
console.log('API.cancelFavorite - productId:', productId); |
|
|
return new Promise((resolve, reject) => { |
|
|
return new Promise((resolve, reject) => { |
|
|
const openid = wx.getStorageSync('openid'); |
|
|
|
|
|
|
|
|
|
|
|
// 获取用户信息,包含手机号
|
|
|
// 获取用户信息,包含手机号
|
|
|
const users = wx.getStorageSync('users') || {}; |
|
|
const users = wx.getStorageSync('users') || {}; |
|
|
const userId = wx.getStorageSync('userId'); |
|
|
const userId = wx.getStorageSync('userId'); |
|
|
@ -2819,18 +2818,15 @@ module.exports = { |
|
|
const userInfo = wx.getStorageSync('userInfo'); |
|
|
const userInfo = wx.getStorageSync('userInfo'); |
|
|
if (userInfo && userInfo.phoneNumber) { |
|
|
if (userInfo && userInfo.phoneNumber) { |
|
|
userPhone = userInfo.phoneNumber; |
|
|
userPhone = userInfo.phoneNumber; |
|
|
|
|
|
} else { |
|
|
|
|
|
// 尝试从直接存储的phoneNumber获取
|
|
|
|
|
|
userPhone = wx.getStorageSync('phoneNumber'); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 如果没有openid,直接返回未登录错误
|
|
|
|
|
|
if (!openid) { |
|
|
|
|
|
reject(new Error('用户未登录,无法取消收藏')); |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 如果没有手机号,直接返回错误
|
|
|
// 如果没有手机号,直接返回错误
|
|
|
if (!userPhone) { |
|
|
if (!userPhone) { |
|
|
reject(new Error('无法获取用户手机号,无法取消收藏')); |
|
|
reject(new Error('请先登录并绑定手机号')); |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|