wxapp/pages/add/add.js
2025-04-21 22:59:44 +08:00

86 lines
1.5 KiB
JavaScript

const app = getApp()
Page({
data: {
name: '',
description: '',
price: 0,
imageUrl: ''
},
onNameInput: function(e) {
this.setData({
name: e.detail.value
})
},
onDescInput: function(e) {
this.setData({
description: e.detail.value
})
},
onPriceInput: function(e) {
this.setData({
price: e.detail.value
})
},
chooseImage: function() {
wx.chooseImage({
count: 1,
sizeType: ['compressed'],
sourceType: ['album', 'camera'],
success: (res) => {
this.setData({
imageUrl: res.tempFilePaths[0]
})
}
})
},
submit: function() {
if (!this.data.name) {
wx.showToast({
title: '请输入物品名称',
icon: 'none'
})
return
}
const newItem = {
id: Date.now().toString(),
name: this.data.name,
description: this.data.description,
imageUrl: this.data.imageUrl,
createTime: new Date().toISOString()
}
// 更新全局数据
app.globalData.items.unshift(newItem)
wx.setStorageSync('items', app.globalData.items)
wx.showToast({
title: '添加成功',
icon: 'success',
duration: 2000,
success: () => {
setTimeout(() => {
wx.navigateBack()
}, 2000)
}
})
},
goBack: function () {
wx.navigateBack({
delta: 1,
success: function(res) {
},
fail: function(err) {
console.log(err)
}
})
}
})