云函数:exportsmain = async event context = const db = clouddatabase;const yddata = dbcollectionyddata;const pageSize = 1000; 定义每页显示的数据数量const startIndex = 0; 定义起始索引值let query = yddatawhere;if eventmail
云函数代码:
// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init()
// 云函数入口函数
exports.main = async (event, context) => {
const db = cloud.database();
const yddata = db.collection('yddata');
const pageSize = 1000; // 定义每页显示的数据数量
const startIndex = 0; // 定义起始索引值
let query = yddata.where({});
if (event.mailNumber) {
query = query.where({ mailNumber: event.mailNumber });
}
if(event.startDate && event.endDate) {
const startDate1 = event.startDate.split('-');
const endDate1 = event.endDate.split('-');
const startDatestr = `${startDate1[0]}.${parseInt(startDate1[1])}.${parseInt(startDate1[2])} 0:0:0`;
const endDatestr = `${endDate1[0]}.${parseInt(endDate1[1])}.${parseInt(endDate1[2])} 0:0:0`;
query = query.where({
date: db.command.gte(startDatestr).and(db.command.lte(endDatestr))
});
}
const searchData = await query.orderBy('date', 'desc').skip(startIndex).limit(pageSize).get().then(res => {
if (res.data.length > 0) {
return {
searchData: res.data
};
} else {
return {
error: '未找到相关数据'
};
}
}).catch(err => {
console.error(err);
return {
error: err
};
});
return searchData;
}
js代码:
// 页面上的search函数
search: async function () {
wx.cloud.callFunction({
name: 'searchData',
data: {
mailNumber: this.data.mailNumber,
startDate: this.data.startDate,
endDate: this.data.endDate
},
success: res => {
const searchData = res.result;
if (searchData.error) {
wx.showToast({
title: '未找到相关数据',
icon: 'none'
});
} else {
this.setData({
searchData: searchData.searchData
});
}
},
fail: err => {
console.error(err);
}
});
},
json文件:
{
"pages": [
"pages/index/index"
],
"cloud": {
"functions": [
{
"name": "searchData",
"config": {
"timeout": 300
}
}
]
}
}
wxml代码:
<!-- 页面上的搜索按钮 -->
<button bindtap="search">搜索</button>
<!-- 数据展示 -->
<view wx:for="{{searchData}}" wx:key="{{index}}">
<view>{{item.mailNumber}}</view>
<view>{{item.date}}</view>
<!-- 缩略图 -->
<image src="{{item.thumbnail}}" mode="aspectFit" bindtap="viewImage"></image>
</view>
<!-- 图片预览 -->
<view wx:if="{{showPreview}}">
<image src="{{previewImage}}" bindtap="hidePreview"></image>
</view>
wxss代码:
/** 可根据需要进行样式设置 **/
以上代码实现了根据输入的邮寄编号、起始日期和结束日期进行云数据库的查询,并将查询结果展示在页面上。每条数据包括邮寄编号、日期和缩略图,可以点击缩略图进行预览

原文地址: http://www.cveoy.top/t/topic/ibzJ 著作权归作者所有。请勿转载和采集!