flower-mp/pages/garden/edit.js
2025-11-18 17:06:26 +08:00

186 lines
3.8 KiB
JavaScript

// pages/index/edit.js
const config = require("../../config/config")
const { api } = require("../../utils/api")
Page({
/**
* 页面的初始数据
*/
data: {
showDialog:false,
newName:'',
id:'',
info:null
},
/**
* 生命周期函数--监听页面加载
*/
onLoad(options) {
this.setData({id:options.id})
},
fetchInfo(){
api('/plant/detail','GET',{id: this.data.id}).then(res => {
if (res.code === 200){
const tmp = res.data
tmp.farms.suitableFertilizer = tmp.pestsDiseases.split(',')
if (tmp.ossList.length >0 ){
tmp.pic = tmp.ossList[0].url
}
this.setData({info:res.data})
}
})
},
changeDialog(){
const show = this.data.showDialog === false ? true:false
this.setData({showDialog:show})
},
onInput(e){
const value = e.detail.value
this.setData({newName:value})
},
updateFarms(e){
const value = e.detail.value
var data = {id: value.prop,cycleDays: value.value}
api('/plant/updateFarm','POST',data,'json').then(res => {
if (res.code === 200){
wx.showToast({
icon:'success',
title: res.msg,
})
}
})
},
upload(){
const _this = this
wx.chooseMedia({
count: 1,
mediaType: ['image'],
sourceType: ['album', 'camera'],
camera: 'back',
success(res) {
const avatarUrl = res.tempFiles[0].tempFilePath
wx.showLoading({
title: '请稍后...',
})
wx.uploadFile({
filePath: avatarUrl,
name: 'file',
header:{
'Authorization': 'Bearer ' + wx.getStorageSync('token'),
},
url: config.baseUrl + '/oss/upload',
success: res => {
var data = JSON.parse(res.data);
if (data.code === 200) {
const params = {ossIds:[data.data.file.id],plantId: _this.data.info.id}
api('/plant/uploadImg','POST',params,'json').then(res => {
if (res.code === 200){
wx.showToast({
icon:'success',
title: res.msg,
})
_this.fetchInfo()
}else {
wx.showModal({
content: res.msg
})
}
})
}
}
})
}
})
},
update(){
const data = {id: this.data.id}
if (this.data.newName != ''){
data.name = this.data.newName
}
console.log(data);
api('/plant/updatePlant','POST',data,'json').then(res => {
if (res.code === 200){
this.fetchInfo()
} else {
wx.showModal({
content: res.msg
})
}
this.setData({showDialog:false})
})
},
del(){
wx.showModal({
content: '删除改植物后,与之相关的数据和历史记录也将被删除,此删除不能被恢复。',
complete: (res) => {
if (res.confirm) {
const data = {ids: [this.data.id]}
api('/plant/delete','POST',data,'json').then(res => {
if (res.code === 200){
wx.navigateBack()
}
})
}
}
})
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady() {
},
/**
* 生命周期函数--监听页面显示
*/
onShow() {
this.fetchInfo()
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide() {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload() {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh() {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom() {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage() {
}
})