当前位置:网站首页>云函数实现模糊搜索功能
云函数实现模糊搜索功能
2022-06-23 03:50:00 【JSONP$】
云函数实现模糊搜索功能
核心代码
// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init({
env: cloud.DYNAMIC_CURRENT_ENV
})
const db = cloud.database()
// 云函数入口函数
exports.main = async (event, context) => {
return db.collection('zhusir_video').where({
name:db.RegExp({
regexp:event.name,
options:'i'
})
}).get()
}
先看一下html部分
<!-- 搜索框 -->
<view class="flex">
<van-search shape="round" value="{
{keywords}}" clearable="{
{false}}" placeholder="{
{value}}" custom-class="search" input-class="input" show-action action-text="取消" bind:search="onSearch" bind:change="inputChange" bind:cancel="onCancel" bind:focus="onFocus" bind:blur="onBlur" />
</view>
再看一下js部分
// pages/search/index.js
Page({
/** * 页面的初始数据 */
data: {
searchHistory: [],
movies: [],
value: '请输入片名',
show: false,
keywords: ''
},
/** * 生命周期函数--监听页面加载 */
onLoad(options) {
this.setData({
searchHistory: wx.getStorageSync('searchHistory') || []
})
},
/** * 生命周期函数--监听页面初次渲染完成 */
onReady() {
},
goDetail(e) {
wx.navigateTo({
url: '../detail/detail?id=' + e.currentTarget.dataset.id,
})
},
onFocus() {
this.setData({
show: true
})
},
// onBlur() {
// this.setData({
// show: false
// })
// },
clearHistory() {
this.setData({
searchHistory: []
})
wx.removeStorageSync('searchHistory')
},
inputChange(e) {
let value = e.detail
if (!value) {
this.setData({
movies: []
})
return false
}
this.loadData(value)
},
tapSearch(e) {
const query = e.currentTarget.dataset.name
this.setData({
movies: []
})
this.searchMovie(query)
},
loadData(value) {
wx.cloud.callFunction({
name: 'searchMovie',
data: {
name: value
}
})
.then(res => {
this.setData({
movies: res.result.data
})
})
},
searchMovie(query) {
let history = this.data.searchHistory
this.setData({
value: query,
keywords: '',
show:false
})
if (query.length > 0 && query.trim().length != 0) {
if (!history.some(item => item === query)) {
let hist = [query].concat(history).slice(0, 10)
this.setData({
searchHistory: hist
})
wx.setStorageSync('searchHistory', hist)
}
this.loadData(query)
}
},
onSearch(e) {
const query = e.detail
this.setData({
movies: [],
value: query
})
if (!query) {
wx.showToast({
title: '请输入搜索内容',
icon: 'none'
})
this.setData({
value: '请输入片名'
})
return false
}
this.searchMovie(query)
},
onCancel() {
if (this.data.show) {
this.setData({
show: false
})
return false
}
if (this.data.movies) {
this.setData({
movies: [],
value:'请输入片名'
})
return false
}
wx.switchTab({
url: '../home/index',
})
},
/** * 生命周期函数--监听页面显示 */
onShow() {
},
/** * 生命周期函数--监听页面隐藏 */
onHide() {
},
/** * 生命周期函数--监听页面卸载 */
onUnload() {
},
/** * 页面相关事件处理函数--监听用户下拉动作 */
onPullDownRefresh() {
},
/** * 页面上拉触底事件的处理函数 */
onReachBottom() {
},
/** * 用户点击右上角分享 */
onShareAppMessage() {
}
})
边栏推荐
猜你喜欢

语料库数据处理个案实例(分词和分句、词频统计、排序)

深度学习 简介

Deploying Apache pulsar on kubesphere

LabVIEW displays both hexadecimal and normal characters in the same table

Background ribbon animation plug-in ribbon js

win10下安装、运行MongoDB
![[advanced binary tree] AVLTree - balanced binary search tree](/img/a5/aef68dd489ef5545e5b11ee2d3facc.png)
[advanced binary tree] AVLTree - balanced binary search tree

Getting started with tensorflow
![3D数学基础[十六] 匀加速直线运动的公式](/img/51/5b05694bbd0f4fd01dd26cf55b22c7.png)
3D数学基础[十六] 匀加速直线运动的公式

Software development in 2022: five realities CIOs should know
随机推荐
【多模态】UNIMO
Bug STM32 advanced timer (haha, to tell you the truth, the hardware timer can't reflect its strength. In fact, I want to send the kernel timer. Just think about it. Take your time)
制造型企业开发的SRM供应商管理系统特点是什么
OpenJudge NOI 1.13 51:古代密码
It supports running in kubernetes, adds multiple connectors, and seatunnel version 2.1.2 is officially released!
PTA:6-30 时间相加
Flutter series: wrap in flutter
【二叉樹進階】AVLTree - 平衡二叉搜索樹
32单片机一个变量多个.c里使用
396. 矿场搭建
Permission Operation in dynamics 365 plug-in
OpenJudge NOI 1.13 49:计算对数
Mobile terminal city list sorting JS plug-in vertitylist js
Basic skills of x64dbg
Prince language under insect date category
Pta:6-33 student ranking table (destructor)
Banner 标语 旗帜
VGG 中草药识别
Getting started with tensorflow
How MySQL deletes a row of data in a table