|
|
|
|
@ -358,218 +358,83 @@ Page({
|
|
|
|
|
tradeMethodIndex
|
|
|
|
|
} = this.data;
|
|
|
|
|
|
|
|
|
|
// 获取当前用户信息并关联T_user表
|
|
|
|
|
wx.cloud.callFunction({
|
|
|
|
|
name: 'quickstartFunctions',
|
|
|
|
|
data: {
|
|
|
|
|
type: 'getOpenId'
|
|
|
|
|
},
|
|
|
|
|
success: (userRes) => {
|
|
|
|
|
const userInfo = userRes.result || {};
|
|
|
|
|
const openid = userInfo.openid || '';
|
|
|
|
|
|
|
|
|
|
console.log('获取到用户openid:', openid);
|
|
|
|
|
|
|
|
|
|
// 通过云函数查询T_user表,获取用户详细信息并建立关联
|
|
|
|
|
wx.cloud.callFunction({
|
|
|
|
|
name: 'quickstartFunctions',
|
|
|
|
|
data: {
|
|
|
|
|
type: 'getUserByOpenId',
|
|
|
|
|
openid: openid
|
|
|
|
|
},
|
|
|
|
|
success: (userQueryRes) => {
|
|
|
|
|
let sellerUserId = null;
|
|
|
|
|
let sellerUserInfo = null;
|
|
|
|
|
|
|
|
|
|
if (userQueryRes.result && userQueryRes.result.success && userQueryRes.result.data) {
|
|
|
|
|
// 找到用户记录,建立关联
|
|
|
|
|
const userData = userQueryRes.result.data;
|
|
|
|
|
sellerUserId = userData.userId;
|
|
|
|
|
sellerUserInfo = {
|
|
|
|
|
userId: userData.userId,
|
|
|
|
|
sno: userData.sno || '',
|
|
|
|
|
sname: userData.sname || '',
|
|
|
|
|
phone: userData.phone || '',
|
|
|
|
|
avatar: userData.avatar || ''
|
|
|
|
|
};
|
|
|
|
|
console.log('找到用户记录,建立关联:', sellerUserInfo);
|
|
|
|
|
} else {
|
|
|
|
|
console.warn('未找到用户记录,仅保存openid');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 构建商品数据
|
|
|
|
|
const productData = {
|
|
|
|
|
// 基本信息
|
|
|
|
|
productName: productName,
|
|
|
|
|
productCategory: productCategory,
|
|
|
|
|
productDescription: productDescription,
|
|
|
|
|
productImage: imageFileID,
|
|
|
|
|
|
|
|
|
|
// 价格信息
|
|
|
|
|
originalPrice: parseFloat(originalPrice) || 0,
|
|
|
|
|
suggestedPrice: parseFloat(suggestedPrice) || 0,
|
|
|
|
|
salePrice: parseFloat(salePrice) || 0,
|
|
|
|
|
priceRange: priceRange || '',
|
|
|
|
|
|
|
|
|
|
// 商品状况
|
|
|
|
|
conditionLevel: conditionLevel || '',
|
|
|
|
|
aiScore: aiScore || '',
|
|
|
|
|
analysisReport: analysisReport || '',
|
|
|
|
|
|
|
|
|
|
// 发布信息
|
|
|
|
|
contactInfo: contactInfo,
|
|
|
|
|
transactionMethod: transactionMethods[tradeMethodIndex] || '面交',
|
|
|
|
|
|
|
|
|
|
// 状态信息
|
|
|
|
|
status: '在售', // 商品状态:在售、已售、下架
|
|
|
|
|
viewCount: 0, // 浏览数
|
|
|
|
|
likeCount: 0, // 点赞数
|
|
|
|
|
|
|
|
|
|
// 时间信息
|
|
|
|
|
createTime: new Date(),
|
|
|
|
|
updateTime: new Date(),
|
|
|
|
|
|
|
|
|
|
// 用户关联信息(建立与T_user的关联)
|
|
|
|
|
sellerOpenId: openid, // 微信openid
|
|
|
|
|
sellerUserId: sellerUserId, // 关联到T_user表的_id(主要关联字段)
|
|
|
|
|
sellerAppId: userInfo.appid || '',
|
|
|
|
|
|
|
|
|
|
// 用户快照信息(可选,用于快速显示,避免频繁查询)
|
|
|
|
|
sellerInfo: sellerUserInfo ? {
|
|
|
|
|
sno: sellerUserInfo.sno,
|
|
|
|
|
sname: sellerUserInfo.sname,
|
|
|
|
|
phone: sellerUserInfo.phone,
|
|
|
|
|
avatar: sellerUserInfo.avatar
|
|
|
|
|
} : null
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
console.log('准备保存商品数据(含用户关联):', productData);
|
|
|
|
|
|
|
|
|
|
// 保存到数据库
|
|
|
|
|
db.collection('T_product').add({
|
|
|
|
|
data: productData,
|
|
|
|
|
success: (res) => {
|
|
|
|
|
console.log('商品发布成功:', res);
|
|
|
|
|
wx.hideLoading();
|
|
|
|
|
|
|
|
|
|
this.setData({
|
|
|
|
|
isPublishing: false
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 显示发布成功提示
|
|
|
|
|
wx.showToast({
|
|
|
|
|
title: '发布成功',
|
|
|
|
|
icon: 'success',
|
|
|
|
|
duration: 2000
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 延迟跳转到主页面
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
wx.switchTab({
|
|
|
|
|
url: '/pages/main/main'
|
|
|
|
|
});
|
|
|
|
|
}, 1500);
|
|
|
|
|
},
|
|
|
|
|
fail: (err) => {
|
|
|
|
|
console.error('商品保存失败:', err);
|
|
|
|
|
wx.hideLoading();
|
|
|
|
|
|
|
|
|
|
this.setData({
|
|
|
|
|
isPublishing: false
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
wx.showModal({
|
|
|
|
|
title: '发布失败',
|
|
|
|
|
content: '商品保存失败,请重试。错误信息:' + (err.errMsg || '未知错误'),
|
|
|
|
|
showCancel: false,
|
|
|
|
|
confirmText: '知道了'
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
fail: (err) => {
|
|
|
|
|
console.error('查询用户信息失败:', err);
|
|
|
|
|
// 即使查询失败,也尝试保存商品(仅保存openid)
|
|
|
|
|
const productData = {
|
|
|
|
|
// 基本信息
|
|
|
|
|
productName: productName,
|
|
|
|
|
productCategory: productCategory,
|
|
|
|
|
productDescription: productDescription,
|
|
|
|
|
productImage: imageFileID,
|
|
|
|
|
|
|
|
|
|
// 价格信息
|
|
|
|
|
originalPrice: parseFloat(originalPrice) || 0,
|
|
|
|
|
suggestedPrice: parseFloat(suggestedPrice) || 0,
|
|
|
|
|
salePrice: parseFloat(salePrice) || 0,
|
|
|
|
|
priceRange: priceRange || '',
|
|
|
|
|
|
|
|
|
|
// 商品状况
|
|
|
|
|
conditionLevel: conditionLevel || '',
|
|
|
|
|
aiScore: aiScore || '',
|
|
|
|
|
analysisReport: analysisReport || '',
|
|
|
|
|
|
|
|
|
|
// 发布信息
|
|
|
|
|
contactInfo: contactInfo,
|
|
|
|
|
transactionMethod: transactionMethods[tradeMethodIndex] || '面交',
|
|
|
|
|
|
|
|
|
|
// 状态信息
|
|
|
|
|
status: '在售',
|
|
|
|
|
viewCount: 0,
|
|
|
|
|
likeCount: 0,
|
|
|
|
|
|
|
|
|
|
// 时间信息
|
|
|
|
|
createTime: new Date(),
|
|
|
|
|
updateTime: new Date(),
|
|
|
|
|
|
|
|
|
|
// 用户信息(仅保存openid)
|
|
|
|
|
sellerOpenId: openid,
|
|
|
|
|
sellerAppId: userInfo.appid || '',
|
|
|
|
|
sellerUserId: null // 关联失败,留空
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
db.collection('T_product').add({
|
|
|
|
|
data: productData,
|
|
|
|
|
success: (res) => {
|
|
|
|
|
console.log('商品发布成功(用户关联失败):', res);
|
|
|
|
|
wx.hideLoading();
|
|
|
|
|
this.setData({ isPublishing: false });
|
|
|
|
|
wx.showToast({
|
|
|
|
|
title: '发布成功',
|
|
|
|
|
icon: 'success',
|
|
|
|
|
duration: 2000
|
|
|
|
|
});
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
wx.switchTab({ url: '/pages/main/main' });
|
|
|
|
|
}, 1500);
|
|
|
|
|
},
|
|
|
|
|
fail: (saveErr) => {
|
|
|
|
|
console.error('商品保存失败:', saveErr);
|
|
|
|
|
wx.hideLoading();
|
|
|
|
|
this.setData({ isPublishing: false });
|
|
|
|
|
wx.showModal({
|
|
|
|
|
title: '发布失败',
|
|
|
|
|
content: '商品保存失败,请重试',
|
|
|
|
|
showCancel: false,
|
|
|
|
|
confirmText: '知道了'
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
// 构建商品数据
|
|
|
|
|
const productData = {
|
|
|
|
|
// 基本信息
|
|
|
|
|
productName: productName,
|
|
|
|
|
productCategory: productCategory,
|
|
|
|
|
productDescription: productDescription,
|
|
|
|
|
productImage: imageFileID,
|
|
|
|
|
|
|
|
|
|
// 价格信息
|
|
|
|
|
originalPrice: parseFloat(originalPrice) || 0,
|
|
|
|
|
suggestedPrice: parseFloat(suggestedPrice) || 0,
|
|
|
|
|
salePrice: parseFloat(salePrice) || 0,
|
|
|
|
|
priceRange: priceRange || '',
|
|
|
|
|
|
|
|
|
|
// 商品状况
|
|
|
|
|
conditionLevel: conditionLevel || '',
|
|
|
|
|
aiScore: aiScore || '',
|
|
|
|
|
analysisReport: analysisReport || '',
|
|
|
|
|
|
|
|
|
|
// 发布信息
|
|
|
|
|
contactInfo: contactInfo,
|
|
|
|
|
transactionMethod: transactionMethods[tradeMethodIndex] || '面交',
|
|
|
|
|
|
|
|
|
|
// 状态信息
|
|
|
|
|
status: '在售',
|
|
|
|
|
viewCount: 0,
|
|
|
|
|
likeCount: 0,
|
|
|
|
|
|
|
|
|
|
// 时间信息
|
|
|
|
|
createTime: new Date(),
|
|
|
|
|
updateTime: new Date(),
|
|
|
|
|
|
|
|
|
|
// 用户信息(暂时简化)
|
|
|
|
|
sellerOpenId: 'test_openid', // 暂时使用测试数据
|
|
|
|
|
sellerAppId: 'test_appid'
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
console.log('准备保存商品数据:', productData);
|
|
|
|
|
|
|
|
|
|
// 保存到数据库
|
|
|
|
|
db.collection('T_product').add({
|
|
|
|
|
data: productData,
|
|
|
|
|
success: (res) => {
|
|
|
|
|
console.log('商品发布成功:', res);
|
|
|
|
|
wx.hideLoading();
|
|
|
|
|
|
|
|
|
|
this.setData({
|
|
|
|
|
isPublishing: false
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// 显示发布成功提示
|
|
|
|
|
wx.showToast({
|
|
|
|
|
title: '发布成功',
|
|
|
|
|
icon: 'success',
|
|
|
|
|
duration: 2000
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 延迟跳转到主页面
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
wx.switchTab({
|
|
|
|
|
url: '/pages/main/main'
|
|
|
|
|
});
|
|
|
|
|
}, 1500);
|
|
|
|
|
},
|
|
|
|
|
fail: (err) => {
|
|
|
|
|
console.error('获取用户信息失败:', err);
|
|
|
|
|
console.error('商品保存失败:', err);
|
|
|
|
|
wx.hideLoading();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.setData({
|
|
|
|
|
isPublishing: false
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
wx.showToast({
|
|
|
|
|
title: '获取用户信息失败',
|
|
|
|
|
icon: 'none',
|
|
|
|
|
duration: 2000
|
|
|
|
|
wx.showModal({
|
|
|
|
|
title: '发布失败',
|
|
|
|
|
content: '商品保存失败,请重试。错误信息:' + (err.errMsg || '未知错误'),
|
|
|
|
|
showCancel: false,
|
|
|
|
|
confirmText: '知道了'
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|