我们在用 uni-app 开发前端时,有进入某些页面需要屏幕高亮的需求。如进入支付码页面屏幕高亮,退出页面屏幕亮度还原。实现方式如下:

<script>
    export default {
        data() {
            return {
                brightness: null
            }
        },
        mounted() {
        // #ifndef H5
        uni.getScreenBrightness({ // 获取屏幕亮度
            success: res => {
                if (res.value !== 1) {
                    this.brightness = res.value // 屏幕亮度值,范围 0~1,0 最暗,1 最亮
                    uni.setScreenBrightness({ // 设置屏幕亮度
                        value: 1 // 屏幕亮度值,范围 0~1,0 最暗,1 最亮
                    })
                }
            }
        })
        // #endif
    },
    beforeDestroy() {
        // #ifndef H5
        if (this.brightness != null) {
            uni.setScreenBrightness({ // 设置屏幕亮度到进入时的亮度
                value: this.brightness
            })
        }
        // #endif
    }
}
</script>

具体请查看:屏幕相关官方接口文档

注意:如果手机设置了自动亮度,无法还原成随系统亮度,只能还原到未设置自动亮度时的初始屏幕亮度。

如果觉得我的文章对你有用,请点个赞