|
|
|
|
// pages/create-supply/index.js
|
|
|
|
|
// 引入API工具
|
|
|
|
|
|
|
|
|
|
//创建新货源页面
|
|
|
|
|
const API = require('../../utils/api.js');
|
|
|
|
|
|
|
|
|
|
Page({
|
|
|
|
|
// 分享给朋友/群聊
|
|
|
|
|
onShareAppMessage() {
|
|
|
|
|
return {
|
|
|
|
|
title: '鸡蛋贸易平台 - 发布货源,轻松销售',
|
|
|
|
|
path: '/pages/create-supply/index',
|
|
|
|
|
imageUrl: '/images/你有好蛋.png'
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// 分享到朋友圈
|
|
|
|
|
onShareTimeline() {
|
|
|
|
|
return {
|
|
|
|
|
title: '鸡蛋贸易平台 - 发布货源,轻松销售',
|
|
|
|
|
query: '',
|
|
|
|
|
imageUrl: '/images/你有好蛋.png'
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 页面的初始数据
|
|
|
|
|
*/
|
|
|
|
|
data: {
|
|
|
|
|
variety: '', // 品种
|
|
|
|
|
price: '',
|
|
|
|
|
quantity: '',
|
|
|
|
|
grossWeight: '',
|
|
|
|
|
yolk: '', // 蛋黄
|
|
|
|
|
specification: '',
|
|
|
|
|
images: [], // 图片数组
|
|
|
|
|
province: '',
|
|
|
|
|
city: '',
|
|
|
|
|
district: ''
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 生命周期函数--监听页面加载
|
|
|
|
|
*/
|
|
|
|
|
onLoad(options) {
|
|
|
|
|
// 页面加载时从本地存储读取地区信息
|
|
|
|
|
const storedProvince = wx.getStorageSync('createSupplyProvince') || '';
|
|
|
|
|
const storedCity = wx.getStorageSync('createSupplyCity') || '';
|
|
|
|
|
const storedDistrict = wx.getStorageSync('createSupplyDistrict') || '';
|
|
|
|
|
|
|
|
|
|
// 即使部分数据存在,也加载已有的数据
|
|
|
|
|
if (storedProvince || storedCity || storedDistrict) {
|
|
|
|
|
this.setData({
|
|
|
|
|
province: storedProvince,
|
|
|
|
|
city: storedCity,
|
|
|
|
|
district: storedDistrict
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 返回按钮点击事件
|
|
|
|
|
*/
|
|
|
|
|
onBackTap: function () {
|
|
|
|
|
wx.navigateBack({
|
|
|
|
|
delta: 1
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 生命周期函数--监听页面卸载
|
|
|
|
|
*/
|
|
|
|
|
onUnload: function () {
|
|
|
|
|
// 页面卸载时不再自动保存地区数据,仅在用户明确点击确定时保存
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 保存地区数据到本地存储
|
|
|
|
|
*/
|
|
|
|
|
saveRegionToStorage: function () {
|
|
|
|
|
const { province, city, district } = this.data;
|
|
|
|
|
wx.setStorageSync('createSupplyProvince', province);
|
|
|
|
|
wx.setStorageSync('createSupplyCity', city);
|
|
|
|
|
wx.setStorageSync('createSupplyDistrict', district);
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 品种输入处理
|
|
|
|
|
*/
|
|
|
|
|
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
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 地区选择处理
|
|
|
|
|
*/
|
|
|
|
|
onRegionChange(e) {
|
|
|
|
|
// 只有当用户点击确定按钮时,code数组才会有值
|
|
|
|
|
if (e.detail.code && e.detail.code.length > 0) {
|
|
|
|
|
const value = e.detail.value;
|
|
|
|
|
const province = value[0] || '';
|
|
|
|
|
const city = value[1] || '';
|
|
|
|
|
const district = value[2] || '';
|
|
|
|
|
|
|
|
|
|
this.setData({
|
|
|
|
|
province: province,
|
|
|
|
|
city: city,
|
|
|
|
|
district: district
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 保存到本地存储,实现记忆功能
|
|
|
|
|
this.saveRegionToStorage();
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 表单验证
|
|
|
|
|
*/
|
|
|
|
|
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,
|
|
|
|
|
province: province || '',
|
|
|
|
|
city: city || '',
|
|
|
|
|
district: district || ''
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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.name || users[userId].info.nickName)
|
|
|
|
|
? (users[userId].info.name || 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 || '',
|
|
|
|
|
province: formData.province || '',
|
|
|
|
|
city: formData.city || '',
|
|
|
|
|
district: formData.district || '',
|
|
|
|
|
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 || '',
|
|
|
|
|
province: formData.province || '',
|
|
|
|
|
city: formData.city || '',
|
|
|
|
|
district: formData.district || '',
|
|
|
|
|
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
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
})
|