privacy.uvue 2.0 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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
<template>
	<view class="uni-padding-wrap">
		<page-head :title="title"></page-head>
    <view class="item-box">
      <text>当前应用隐私状态:</text>
      <text>{{ appPrivacy }}</text>
    </view>
    <view>
      <button class="privacy-button" type="primary" @tap="getPrivacySetting">
        获取隐私协议状态
      </button>
      <button class="privacy-button" type="primary" open-type="agreePrivacyAuthorization">
        同意隐私协议
      </button>
      <button class="privacy-button" type="primary" @tap="resetPrivacyAuthorization">
        重置隐私协议状态
      </button>
    </view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
			  title: '隐私信息授权',
			  appPrivacy: '未获取',
        listenId: 0
			}
		},
    onReady() {
      //添加 隐私协议监听
      const id = uni.onPrivacyAuthorizationChange((res) => {
         this.appPrivacy = res.needAuthorization ? "未同意" : "已同意"
         const privacyState = "监听到隐私协议状态已变更为 " + this.appPrivacy;
         uni.showToast({
           "position": "bottom",
           "title": privacyState
         })
      })
      this.listenId = id;
      uni.showToast({
        "position": "bottom",
        "title": "开启监听隐私协议状态"
      })
    },
    onUnload() {
      //注销监听
      uni.offPrivacyAuthorizationChange(this.listenId)
      this.listenId = 0;
      uni.showToast({
        "position": "bottom",
        "title": "已停止监听隐私协议状态"
      })
    },
		methods: {
			getPrivacySetting() {
			  uni.getPrivacySetting({
			    success: (res) => {
			      this.appPrivacy = res.needAuthorization ? "未同意" : "已同意"
			    }
			  })
			},
      resetPrivacyAuthorization(){
        uni.resetPrivacyAuthorization()
      }
		}
	}
</script>

<style>
.item-box {
    margin-bottom: 15px;
    display: flex;
    flex-direction: row;
    justify-content: space-between;
  }
.privacy-button{
  margin-top: 15px;
  margin-bottom: 15px;
}
</style>