You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

237 lines
6.2 KiB

const app = getApp();
Page({
data: {
search_height:120,
scroll_height:0,
result:null,
result_num:0,
selected_item:null,
title:"搜索食材",
amount:0,
unit:null,
inputValue:null,
types:['蔬菜','豆/豆制品','肉禽蛋','水产海鲜','水果','乳品烘焙','粮油米面','调味品','酒水饮料','熟食卤味','速食冻品'],
selected_type:"蔬菜",
pre_results:null
},
get_pre_types(){
var that = this
wx.cloud.callContainer({
"config": {
"env": "prod-2g058voqbea31ecb"
},
"path": "fridge/search/",
"header": {
"X-WX-SERVICE": "django-5198",
'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
},
"method": "GET",
data:{content:encodeURI("蔬菜")},
success(res){
console.log(res)
let arr = app.parse_json(res)
that.setData({
pre_results:arr
})
},
})
},
onLoad(){
this.get_pre_types()
},
bindKeyInput: function (e){
var that = this
if(e.detail.value){
wx.cloud.callContainer({
"config": {
"env": "prod-2g058voqbea31ecb"
},
"path": "fridge/search/",
"header": {
"X-WX-SERVICE": "django-5198",
'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
},
"method": "GET",
data:{content:encodeURI(e.detail.value)},
success(res){
console.log(res)
let arr = app.parse_json(res)
if(arr.length<=12){
that.setData({
result:arr,
search_height:Number(arr.length*100+120),
scroll_height:Number(arr.length*100)
})
}
else{
that.setData({
result:arr,
search_height:Number(1260),
scroll_height:Number(1120)
})
}
},
})
}
else{
console.log('sds')
this.setData({
result:null,
search_height:120,
scroll_height:0,
selected_item:null
})
}
},
onTapSpecificItem(e){
var that = this
this.setData({
selected_item:e.currentTarget.dataset.product,
title:"确认食材"
})
wx.showModal({
title: '输入'+e.currentTarget.dataset.product.name+"的数量",
showCancel : true,
confirmText:"确定",
cancelText:"取消",
editable:true,
placeholderText:"如:3",
success:(res)=>{
if(res.confirm){
if(res.content){
that.setData({
amount: res.content
})
wx.showModal({
title: '输入'+e.currentTarget.dataset.product.name+"的单位",
showCancel : true,
confirmText:"确定",
cancelText:"取消",
editable:true,
placeholderText:"如:个",
success:(res)=>{
if(res.confirm){
if(res.content){
that.setData({
unit: res.content
})
wx.showModal({
title: "提示",
content:"确定添加 "+that.data.amount+that.data.unit+e.currentTarget.dataset.product.name+" 至你的冰箱吗?",
showCancel : true,
confirmText:"确定",
cancelText:"取消",
success:(res)=>{
if(res.confirm){
that.sendNewFood()
}
}
})
}
else{
wx.showModal({
title: '错误',
content:"请输入单位!",
showCancel : false,
confirmText:"确定",
})
}
}
}
})
}
else{
wx.showModal({
title: '错误',
content:"请输入数量!",
showCancel : false,
confirmText:"确定",
})
}
}
}
})
},
onTapSpecificType(e){
var that = this
this.setData({
selected_type:e.currentTarget.dataset.product,
})
var that = this
wx.cloud.callContainer({
"config": {
"env": "prod-2g058voqbea31ecb"
},
"path": "fridge/search/",
"header": {
"X-WX-SERVICE": "django-5198",
'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
},
"method": "GET",
data:{content:encodeURI(e.currentTarget.dataset.product)},
success(res){
console.log(res)
let arr = app.parse_json(res)
that.setData({
pre_results:arr
})
},
})
},
bindAmountInput:function(e){
this.setData({
amount:e.detail.value
})
},
bindUnitInput:function(e){
this.setData({
unit:e.detail.value
})
},
sendNewFood(){
var that = this
wx.cloud.callContainer({
"config": {
"env": "prod-2g058voqbea31ecb"
},
"path": "fridge/new-food/",
"header": {
"X-WX-SERVICE": "django-5198",
'content-type': 'application/x-www-form-urlencoded'
},
"method": "POST",
data: {
name:that.data.selected_item.name,
kind:that.data.selected_item.kind,
amount:that.data.amount,
unit:that.data.unit},
success(res){
wx.showToast({
title: '添加成功',
duration:1500,
mask:true,
icon:'success'
})
that.setData({
search_height:120,
scroll_height:0,
result:null,
result_num:0,
selected_item:null,
title:"搜索食材",
amount:0,
unit:null,
inputValue:null
})
}
})
},
back(){
wx.navigateBack({
delta: 0,
})
}
})