uniapp 控制只出现一次

255 阅读1分钟
// 判断弹窗是否已出现过
getHintModal(index) {
    const showPopup = uni.getStorageSync('show_hint_modal') || {}
    if (!showPopup['show' + index]) {
        Http.sendRequest({
            url: 'xxx',
            data: {
                type: index
            },
            success: res => {
                // res.data.is_read:1未出现
                if (200 === res.code && 1 === res.data.is_read) {
                    let info = uni.getStorageSync('show_hint_modal') || {}
                    info['show' + index] = true
                    uni.setStorageSync('show_hint_modal', info)
                }
            }
        });
    }
},
// 设置弹窗已出现
readHintModal(index) {
    const showPopup = uni.getStorageSync('show_hint_modal') || {}
    if (!showPopup['show' + index]) {
        Http.sendRequest({
            url: 'xxx',
            data: {
                type: index
            },
            success: res => {
                if (200 === res.code) {
                    let info = uni.getStorageSync('show_hint_modal') || {}
                    info['show' + index] = true
                    uni.setStorageSync('show_hint_modal', info)
                }
            }
        });
    }
}

使用:

 init() {
    this.getHintModal(1, () => {
        // 打开弹窗
    })
},
closeRulePopup() {
    // 关闭弹窗时候设为已读
    this.readHintModal(1)
},