隐私协议静态共享库

隐私协议对话框静态共享库模块是使用Static Library静态共享库模块实现隐私协议对话框和隐私协议显示,对话框使用自定义对话框实现,隐私协议显示在一个Webview组件页面上,支持本地html文件和http或https返回html文件。

一、下载安装

ohpm install @nutpi/privacy_dialog

OpenHarmony ohpm 环境配置等更多内容,请参考如何安装 OpenHarmony ohpm 包

二、使用

1.在EntryAbility.ts文件里创建首选项数据库

import { PreferencesUtil } from '@nutpi/privacy_dialog'
let preferencesUtil = new PreferencesUtil();

onCreate(want, launchParam) {
    // 创建首选项数据库
    preferencesUtil.createPrivacyPreferences(this.context);
    // 设置隐私协议默认不同意
    preferencesUtil.saveDefaultPrivacy(false);
  }

2.在Index.ets页面,调用对话框

import { CustomDialogPrivacy,PreferencesUtil,RouterParams } from '@nutpi/privacy_dialog'
let preferencesUtil = new PreferencesUtil();

import('@nutpi/privacy_dialog/src/main/ets/components/PrivacyPage');  // 引入静态共享包中的命名路由页面

  // 开始显示隐私协议对话框
  /**
   * 如果localHtml参数为true,urlPage参数为空,显示默认隐私协议
   * 如果localHtml参数为true,urlPage参数不为空,显示urlPage参数本地html文件
   * 如果localHtml参数为false,urlPage参数为空,显示默认隐私协议
   * 如果localHtml参数为false,urlPage参数不为空,显示urlPage参数http或https返回html文件
   */
  // PrivacyPage为三方库里的routeName
  // RouterParams参数1为localHtml=true时,使用本地rawfile目录下html文件,为false时,使用远程http隐私协议
  // RouterParams参数2根据参数1而定,为true时,写本地html文件名如:privacy.html,为false时,如:http://xxxx/zh-cn_userAgreement.html
  openPrivacyPage() {
    try {
      router.pushNamedRoute({
        name: 'PrivacyPage',
        params: new RouterParams(true, 'privacy.html')
      }).then(() => {
        console.info("xx push page success");
      }).catch((err: BusinessError) => {
        console.error(`xx pushUrl failed, code: ${err.code}, msg: ${err.message}`);
      })
    } catch (err) {
      let message = (err as BusinessError).message
      let code = (err as BusinessError).code
      console.error(`xx pushNamedRoute failed, code is ${code}, message is ${message}`);
    }
  }
  
  onConfirm() {
    // 此处可以写点击同意后跳转到主界面或其它逻辑
  }
  
  privacyDialogController: CustomDialogController = new CustomDialogController({
    builder: CustomDialogPrivacy({
      openPrivacy: this.openPrivacyPage,
      onConfirm: this.onConfirm
    }),
    autoCancel: false,
    alignment: DialogAlignment.Center,
    customStyle: true
  })
  
  onPageShow() {
    console.info('xx onPageShow 显示隐私协议')
    preferencesUtil.getChangePrivacy().then((value) => {
      console.info(`xx onPageShow 获取隐私协议状态:${value}`)
      if (!value) {
        this.privacyDialogController.open()
      }
    })
  }
  onPageHide() {
    console.info(`xx Index -> onPageHide Close Start`)
    this.privacyDialogController.close()
    console.info(`xx Index -> onPageHide Close End`)
  }
  aboutToDisappear() {
    console.info(`xx Index -> aboutToDisappear`)
    this.privacyDialogController.close()
  }
  // 结束显示隐私协议对话框

创建对话框时,可选参数以下:

  // 隐私协议标题
  private title?: string = '协议和隐私政策提示'
  // 前辍隐私协议信息
  private prefixMessage?: string = '感谢您选择xxx元服务!我们非常重视您的个人信息和隐私保护。为更好地保障您的个人权益,在使用产品前,请您认真阅读'
  // 后辍隐私协议信息
  private suffixMessage?: string = '的全部内容。'
  // 隐私协议信息,点击可以跳转
  private privacyMessage?: string = '《协议与隐私政策》'

3.点击设备桌面图标运行查看效果

输入图片说明


输入图片说明

三、开源协议

本项目基于 Apache ,请自由地享受和参与开源。欢迎大家与坚果派建立联系。

Logo

讨论HarmonyOS开发技术,专注于API与组件、DevEco Studio、测试、元服务和应用上架分发等。

更多推荐