user-capture-screen.uvue 1.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
<template>
  <!-- #ifdef APP -->
  <scroll-view style="flex:1">
  <!-- #endif -->
    <page-head :title="title"></page-head>
    <view class="uni-common-mt uni-padding-wrap">
      <view class="uni-btn-v">
        <button type="primary" @tap="onUserCaptureScreen">开启截屏监听</button>
      </view>
      <view class="uni-btn-v">
        <button type="primary" @tap="offUserCaptureScreen">关闭截屏监听</button>
      </view>
    </view>
  <!-- #ifdef APP -->
  </scroll-view>
  <!-- #endif -->
</template>

<script>
  import { OnUserCaptureScreenCallbackResult } from "@/uni_modules/uni-usercapturescreen";
  export default {
    data() {
      return {
        title: 'userCaptureScreen',
        callback: null as ((res : OnUserCaptureScreenCallbackResult) => void) | null
      }
    },
    methods: {
      onUserCaptureScreen() {
        this.callback = (res : OnUserCaptureScreenCallbackResult) => {
          console.log('截屏文件路径(仅Android平台支持)', res.path);
          uni.showToast({
            title: '捕获截屏事件'
          });
        };
        uni.onUserCaptureScreen(this.callback!);
        uni.showToast({
          title: '截屏监听已开启'
        });
      },
      offUserCaptureScreen() {
        uni.offUserCaptureScreen(this.callback);
        uni.showToast({
          title: '截屏监听已关闭'
        });
      }
    },
    onUnload() {
      uni.offUserCaptureScreen(this.callback);
    }
  }
</script>

<style>

</style>