成都捷德LIEBE餐盘机可提供"一人一盘“精准统计就餐人数解决方案,订餐验证自动出餐盘.餐盘机可提供一个 JSON 数据库,数据库中的每条记录都是一个 JSON 格式的对象。一个数据库可以有多个集合(相当于关系型数据中的表),集合可看做一个 JSON 数组,数组中的每个对象就是一条记录,记录的格式是 JSON 对象。下面是历史记录的处理方法,菜品收藏与其类似。
-
<view class='search'>
-
<image src='/images/search.png'></image>
-
<input placeholder='今天吃什么' focus bindinput="bindKeyInput" bindconfirm='goSearch' ></input>
-
<text bindtap='goSearch'>搜索</text>
-
</view>
-
// pages/search/search.js
-
Page({
-
data: {
-
inputValue: '',
-
openid: '',
-
showHistory: true,
-
historyList: []
-
},
-
// 进入搜索结果页 -> list
-
goSearch() {
-
let content = this.data.inputValue
-
if (!content) {
-
return
-
}
-
this.onHistory(content)
-
wx.navigateTo({
-
url: `/pages/list/list?content=${content}`,
-
})
-
},
-
// 添加历史记录
-
onHistory (content) {
-
const db = wx.cloud.database() // 获取数据库引用
-
let that = this
-
// 查看是否有历史记录
-
db.collection('food').where({
-
_openid: this.data.openid,
-
_id: 'history' + this.data.openid
-
}).get({
-
success: res => {
-
console.log('数据库查询成功: ', res)
-
if (!res.data.length) {
-
console.log(' 历史记录为空')
-
let historyArray = []
-
historyArray.unshift(content)
-
db.collection('food').add({
-
data: {
-
_id: 'history' + that.data.openid,
-
description: 'history',
-
historyList: historyArray
-
}
-
}).then(res => {
-
console.log(res)
-
})
-
} else {
-
console.log('已有历史记录')
-
let historyArray = res.data[0].historyList
-
historyArray.unshift(content)
-
console.log([...new Set(historyArray)])
-
db.collection('food').doc('history' + that.data.openid).update({
-
data: {
-
historyList: [...new Set(historyArray)]
-
}
-
}).then((res) => {
-
console.log(res)
-
})
-
}
-
},
-
fail: err => {
-
wx.showToast({
-
icon: 'none',
-
title: '查询记录失败'
-
})
-
console.error('数据库查询失败:', err)
-
}
-
})
-
},
-
// 读取历史记录
-
getHistory() {
-
let that = this
-
const db = wx.cloud.database()
-
db.collection('food').doc('history' + that.data.openid).get({
-
success(res) {
-
console.log(res.data)
-
that.setData({
-
historyList: res.data.historyList
-
})
-
}
-
})
-
},
-
// 清空历史记录
-
bindClearHistory() {
-
const db = wx.cloud.database()
-
db.collection('food').doc('history' + this.data.openid).update({
-
data: {
-
historyList: []
-
}
-
}).then((res) => {
-
console.log(res)
-
wx.showToast({
-
icon: '删除',
-
title: '清空历史',
-
})
-
})
-
this.setData({
-
historyList: []
-
})
-
}
-
})