|
|
|
|
// pages/create-supply/index.js
|
|
|
|
|
// 引入API工具
|
|
|
|
|
|
|
|
|
|
//创建新货源页面
|
|
|
|
|
const API = require('../../utils/api.js');
|
|
|
|
|
|
|
|
|
|
Page({
|
|
|
|
|
/**
|
|
|
|
|
* 页面的初始数据
|
|
|
|
|
*/
|
|
|
|
|
data: {
|
|
|
|
|
variety: '', // 品种
|
|
|
|
|
price: '',
|
|
|
|
|
quantity: '',
|
|
|
|
|
grossWeight: '',
|
|
|
|
|
yolk: '', // 蛋黄
|
|
|
|
|
specification: '',
|
|
|
|
|
images: [] // 图片数组
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 生命周期函数--监听页面加载
|
|
|
|
|
*/
|
|
|
|
|
onLoad(options) {
|
|
|
|
|
// 页面加载时的初始化
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 返回上一页
|
|
|
|
|
*/
|
|
|
|
|
onBackTap: function () {
|
|
|
|
|
wx.navigateBack({
|
|
|
|
|
delta: 1
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 品种输入处理
|
|
|
|
|
*/
|
|
|
|
|
onVarietyInput(e) {
|
|
|
|
|
this.setData({
|
|
|
|
|
variety: e.detail.value
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 蛋黄输入处理
|
|
|
|
|
*/
|
|
|
|
|
onYolkInput(e) {
|
|
|
|
|
this.setData({
|
|
|
|
|
yolk: e.detail.value
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 价格输入处理
|
|
|
|
|
*/
|
|
|
|
|
onPriceInput(e) {
|
|
|
|
|
this.setData({
|
|
|
|
|
price: e.detail.value
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 数量输入处理
|
|
|
|
|
*/
|
|
|
|
|
onQuantityInput(e) {
|
|
|
|
|
const value = parseFloat(e.detail.value);
|
|
|
|
|
this.setData({
|
|
|
|
|
quantity: isNaN(value) ? '' : value
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 毛重输入处理
|
|
|
|
|
*/
|
|
|
|
|
onGrossWeightInput(e) {
|
|
|
|
|
this.setData({
|
|
|
|
|
grossWeight: e.detail.value
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 规格输入处理
|
|
|
|
|
*/
|
|
|
|
|
onSpecificationInput(e) {
|
|
|
|
|
this.setData({
|
|
|
|
|
specification: e.detail.value
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 表单验证
|
|
|
|
|
*/
|
|
|
|
|
validateForm() {
|
|
|
|
|
const { variety, price, quantity } = this.data;
|
|
|
|
|
|
|
|
|
|
if (!variety || !variety.trim()) {
|
|
|
|
|
wx.showToast({ title: '请输入品种', icon: 'none' });
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!price || price.trim() === '') {
|
|
|
|
|
wx.showToast({ title: '请输入有效价格', icon: 'none' });
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (quantity === '' || quantity === undefined || quantity === null || quantity <= 0) {
|
|
|
|
|
wx.showToast({ title: '请输入有效数量', icon: 'none' });
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 创建货源按钮点击事件
|
|
|
|
|
*/
|
|
|
|
|
onCreateTap() {
|
|
|
|
|
if (!this.validateForm()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const { variety, price, quantity, grossWeight, yolk, specification, images } = this.data;
|
|
|
|
|
|
|
|
|
|
// 构建商品数据
|
|
|
|
|
const productData = {
|
|
|
|
|
productName: variety.trim(),
|
|
|
|
|
price: price.toString(),
|
|
|
|
|
quantity: quantity.toString(),
|
|
|
|
|
grossWeight: grossWeight || '',
|
|
|
|
|
yolk: yolk || '',
|
|
|
|
|
specification: specification || '',
|
|
|
|
|
images: images,
|
|
|
|
|
imageUrls: images
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
console.log('创建货源数据:', productData);
|
|
|
|
|
|
|
|
|
|
// 显示加载提示
|
|
|
|
|
wx.showLoading({ title: '正在创建货源...' });
|
|
|
|
|
|
|
|
|
|
// 使用API发布商品
|
|
|
|
|
API.publishProduct(productData)
|
|
|
|
|
.then(res => {
|
|
|
|
|
wx.hideLoading();
|
|
|
|
|
console.log('创建货源成功:', res);
|
|
|
|
|
|
|
|
|
|
// 创建货源成功后,将用户类型设置为seller
|
|
|
|
|
API.updateUserType('seller');
|
|
|
|
|
|
|
|
|
|
// 保存到本地存储
|
|
|
|
|
this.saveToLocalStorage(productData, res);
|
|
|
|
|
|
|
|
|
|
// 显示成功弹窗
|
|
|
|
|
wx.showModal({
|
|
|
|
|
title: '创建成功',
|
|
|
|
|
content: '新货源已成功创建!',
|
|
|
|
|
showCancel: false,
|
|
|
|
|
confirmText: '确定',
|
|
|
|
|
success: function() {
|
|
|
|
|
// 返回到上一页
|
|
|
|
|
wx.navigateBack();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
})
|
|
|
|
|
.catch(err => {
|
|
|
|
|
wx.hideLoading();
|
|
|
|
|
console.error('创建货源失败:', err);
|
|
|
|
|
wx.showToast({
|
|
|
|
|
title: err.message || '创建失败,请重试',
|
|
|
|
|
icon: 'none'
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 保存到本地存储
|
|
|
|
|
*/
|
|
|
|
|
saveToLocalStorage(formData, apiResponse) {
|
|
|
|
|
// 从本地存储获取userId
|
|
|
|
|
const userId = wx.getStorageSync('userId') || 'anonymous';
|
|
|
|
|
|
|
|
|
|
// 获取卖家信息
|
|
|
|
|
const users = wx.getStorageSync('users') || {};
|
|
|
|
|
const sellerName = users[userId] && users[userId].info && users[userId].info.nickName
|
|
|
|
|
? users[userId].info.nickName
|
|
|
|
|
: '未知卖家';
|
|
|
|
|
|
|
|
|
|
// 获取当前已有的货源列表
|
|
|
|
|
const supplies = wx.getStorageSync('supplies') || [];
|
|
|
|
|
const newId = supplies.length > 0 ? Math.max(...supplies.map(s => s.id)) + 1 : 1;
|
|
|
|
|
const serverProductId = apiResponse.product && apiResponse.product.productId
|
|
|
|
|
? apiResponse.product.productId
|
|
|
|
|
: '';
|
|
|
|
|
|
|
|
|
|
// 创建新的货源记录
|
|
|
|
|
const newSupply = {
|
|
|
|
|
id: newId,
|
|
|
|
|
productId: serverProductId,
|
|
|
|
|
serverProductId: serverProductId,
|
|
|
|
|
name: formData.productName,
|
|
|
|
|
productName: formData.productName,
|
|
|
|
|
price: formData.price,
|
|
|
|
|
minOrder: formData.quantity,
|
|
|
|
|
yolk: formData.yolk,
|
|
|
|
|
spec: formData.specification,
|
|
|
|
|
grossWeight: formData.grossWeight || '',
|
|
|
|
|
seller: sellerName,
|
|
|
|
|
status: apiResponse.product && apiResponse.product.status
|
|
|
|
|
? apiResponse.product.status
|
|
|
|
|
: 'pending_review',
|
|
|
|
|
imageUrls: formData.imageUrls || [],
|
|
|
|
|
reservedCount: 0,
|
|
|
|
|
isReserved: false
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// 保存到supplies本地存储
|
|
|
|
|
supplies.push(newSupply);
|
|
|
|
|
wx.setStorageSync('supplies', supplies);
|
|
|
|
|
|
|
|
|
|
// 同时保存到goods本地存储,供买家查看
|
|
|
|
|
const goods = wx.getStorageSync('goods') || [];
|
|
|
|
|
const newGoodForBuyer = {
|
|
|
|
|
id: String(newId),
|
|
|
|
|
productId: String(serverProductId),
|
|
|
|
|
name: formData.productName,
|
|
|
|
|
productName: formData.productName,
|
|
|
|
|
price: formData.price,
|
|
|
|
|
minOrder: formData.quantity,
|
|
|
|
|
yolk: formData.yolk,
|
|
|
|
|
spec: formData.specification,
|
|
|
|
|
grossWeight: formData.grossWeight || '',
|
|
|
|
|
displayGrossWeight: formData.grossWeight || '',
|
|
|
|
|
seller: sellerName,
|
|
|
|
|
status: apiResponse.product && apiResponse.product.status
|
|
|
|
|
? apiResponse.product.status
|
|
|
|
|
: 'pending_review',
|
|
|
|
|
imageUrls: formData.imageUrls || [],
|
|
|
|
|
reservedCount: 0,
|
|
|
|
|
isReserved: false
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
goods.push(newGoodForBuyer);
|
|
|
|
|
wx.setStorageSync('goods', goods);
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 选择图片
|
|
|
|
|
*/
|
|
|
|
|
chooseImage() {
|
|
|
|
|
const that = this;
|
|
|
|
|
wx.chooseMedia({
|
|
|
|
|
count: 5 - that.data.images.length,
|
|
|
|
|
mediaType: ['image'],
|
|
|
|
|
sourceType: ['album', 'camera'],
|
|
|
|
|
success: function (res) {
|
|
|
|
|
console.log('选择图片成功:', res);
|
|
|
|
|
const tempFiles = res.tempFiles.map(file => file.tempFilePath);
|
|
|
|
|
that.setData({
|
|
|
|
|
images: [...that.data.images, ...tempFiles]
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
fail: function (err) {
|
|
|
|
|
console.error('选择图片失败:', err);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 删除图片
|
|
|
|
|
*/
|
|
|
|
|
deleteImage(e) {
|
|
|
|
|
const index = e.currentTarget.dataset.index;
|
|
|
|
|
const images = this.data.images;
|
|
|
|
|
images.splice(index, 1);
|
|
|
|
|
this.setData({
|
|
|
|
|
images: images
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
})
|