当前位置:网站首页>Cloud function realizes fuzzy search function

Cloud function realizes fuzzy search function

2022-06-23 04:55:00 JSONP$

Cloud function realizes fuzzy search function

Core code

//  Cloud function entry file 
const cloud = require('wx-server-sdk')

cloud.init({
    
    env: cloud.DYNAMIC_CURRENT_ENV
})

const db = cloud.database()

//  Cloud function entry function 
exports.main = async (event, context) => {
    
    return db.collection('zhusir_video').where({
    
        name:db.RegExp({
    
            regexp:event.name,
            options:'i'
        })
    }).get()
}

Have a look first html part

 <!--  Search box  -->
    <view class="flex">
        <van-search shape="round" value="{
    {keywords}}" clearable="{
    {false}}" placeholder="{
    {value}}" custom-class="search" input-class="input" show-action  action-text=" Cancel " bind:search="onSearch" bind:change="inputChange" bind:cancel="onCancel" bind:focus="onFocus" bind:blur="onBlur" />
    </view>

Look again. js part

// pages/search/index.js
Page({
    

    /** *  Initial data of the page  */
    data: {
    
        searchHistory: [],
        movies: [],
        value: ' Please enter the title ',
        show: false,
        keywords: ''
    },

    /** *  Life cycle function -- Monitor page loading  */
    onLoad(options) {
    
        this.setData({
    
            searchHistory: wx.getStorageSync('searchHistory') || []
        })
    },

    /** *  Life cycle function -- Listening page first rendering completed  */
    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: ' Please enter the search content ',
                icon: 'none'
            })
            this.setData({
    
                value: ' Please enter the title '
            })
            return false
        }
        this.searchMovie(query)
    },
    onCancel() {
    
        if (this.data.show) {
    
            this.setData({
    
                show: false
            })
            return false
        }
        if (this.data.movies) {
    
            this.setData({
    
                movies: [],
                value:' Please enter the title '
            })
            return false
        }
        wx.switchTab({
    
            url: '../home/index',
        })
    },
    /** *  Life cycle function -- Monitor page display  */
    onShow() {
    

    },

    /** *  Life cycle function -- Monitor page hidden  */
    onHide() {
    

    },

    /** *  Life cycle function -- Monitor page uninstall  */
    onUnload() {
    

    },

    /** *  Page related event handler -- Monitor user pull-down action  */
    onPullDownRefresh() {
    

    },

    /** *  Handling function of page pull bottom event  */
    onReachBottom() {
    

    },

    /** *  Users click the upper right corner to share  */
    onShareAppMessage() {
    

    }
})
原网站

版权声明
本文为[JSONP$]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206222351400073.html