2 Star 37 Fork 13

HarmonyOS_Samples/Accountkit-Samplecode-Clientdemo-ArkTS

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

Account Kit

介绍

本示例展示了使用 Account Kit 提供的登录、授权头像昵称、快速验证手机号、收货地址、发票抬头、未成年人模式的能力。

本示例模拟了在应用里,调用一键登录 Button 组件拉起符合华为规范的登录页面;调用登录接口实现静默登录;调用获取头像昵称接口获取头像昵称;调用快速验证手机号接口拉起手机号快速验证页面;调用收货地址接口获取收货地址;调用发票抬头接口获取发票抬头;调用未成年人模式接口开启未成年人模式;订阅未成年人模式公共事件;调用未成年人模式验密接口验证未成年人模式密码。

需要使用 Account Kit 接口 @kit.AccountKit

效果预览

登录/注册页面 一键登录组件登录页面 一键登录组件弹窗页面
avatar avatar avatar
应用其他登录页 用户登录后首页 用户个人信息页面
avatar avatar avatar
头像昵称授权页 获取快速验证手机号页面 获取收货地址页面
avatar avatar avatar
获取发票抬头页面 开启未成年人模式页面 关闭未成年人模式页面
avatar avatar avatar

本示例的配置与使用

在DevEco中配置本示例的步骤如下

  1. 创建项目应用
  2. 华为开发者联盟为应用配置权限(权限名:phone、quickLoginMobilePhone),具体操作可参考Account Kit开发指南
  3. 应用调用收货地址API前,需完成对应的权限申请,当前可通过accountkit@huawei.com进行邮件申请。请提供如下信息进行申请,我们会在5个工作日内回复申请结果,请您留意邮箱信息。
    邮箱主题:【获取收货地址】权限申请  
    邮件正文:***  
    企业名称:***    
    应用名称:***    
    应用包名:com.\*\*\*.\*\*\*  
    APP ID: 1\*\*\*\*12  
    Client ID:1\*\*\*\*14  
    使用场景:***
    
  4. 打开本示例,使用AppGallery Connect配置的应用包名替换app.json5文件中的bundleName属性值。
  5. 使用AppGallery Connect配置的应用Client ID替换module.json5文件中的client_id属性值。
  6. 生成SHA256应用签名证书指纹并添加到AppGallery Connect对应的应用配置中,请参考应用开发准备中的配置签名信息添加公钥指纹章节。

本示例使用说明

  1. 运行本示例,依次点击“登录/注册”和“华为账号一键登录”按钮进行登录,如果系统账号未登录,会拉起应用其他登录方式。
  2. 如系统已登录注册地为中国境内(不包含中国香港、中国澳门、中国台湾)且已绑定手机号的华为账号,应用在申请完“华为账号一键登录”权限后,点击“登录/注册”按钮,会拉起嵌有“华为账号一键登录”按钮的登录页面,否则拉起其他登录方式,其他登录方式仅为演示,实际并未登录成功,通过其他登录方式登录进入的个人信息页同一键登录后的个人信息页功能保持一致。
  3. 登录成功后,在应用首页点击“亲爱的用户”会进入个人信息页面,如果头像昵称未授权,点击亲爱的用户或默认头像会拉起头像昵称授权页面,授权成功后会在个人信息页展示华为账号头像昵称。个人信息页面的头像、昵称仅用于展示,不可点击修改。
  4. 申请 phone 权限后,在个人信息页点击手机号,会拉起手机号验证及授权页面,授权成功后展示匿名手机号或 180******00(本示例只获取 code,获取 code 后需要将 code 传给应用服务器,调用华为账号服务器接口换取 Access Token,然后再获取用户手机号),如未申请权限则点击无效。
  5. 申请获取收货地址 API 权限后,在个人信息页点击收货地址拉起选择收货地址页面,用户选择对应的地址后展示收货地址信息。
  6. 在个人信息页点击发票抬头拉起选择发票抬头页面,用户选择对应的抬头后展示发票抬头信息。
  7. 当前应用启动后会订阅未成年人模式开启/关闭事件:
    • 当订阅到系统未成年人模式开启时,会再判断应用内未成年人模式单独关闭标记(userTurnOffFlag)是否为true(true表示用户主动关闭应用的未成年人模式),如果应用内单独关闭标记为true,则关闭应用内未成年人模式,如果应用内单独关闭标记为false(false表示用户未主动关闭应用的未成年人模式),则应用内未成年人模式开启,并获取适龄内容年龄段。
    • 当订阅到系统未成年人模式关闭时,会关闭应用内未成年人模式,并设置应用内未成年人模式单独关闭标记为false。
  8. 应用启动时,会调用 getMinorsProtectionInfoSync 接口,查询系统未成年人模式的开启状态,当查询到系统未成年人模式已关闭时,设置应用内未成年人模式单独关闭标记为false。
  9. 当应用内未成年人模式未开启时,点击未成年人模式开关,如当前系统未成年人模式状态为已开启,则应用直接开启自身未成年人模式,展示当前从系统获取的年龄段信息;如当前系统未成年人模式状态为未开启,则引导用户开启系统未成年人模式。
  10. 当应用内未成年人模式开启时:
    • 可以点击可用时长限制开关,系统会校验健康使用设备密码,校验通过后,才可修改可用时长限制开关的状态。
    • 可以点击未成年人模式开关,系统会校验健康使用设备密码,校验通过后,应用的未成年人模式成功关闭,设置应用内未成年人模式单独关闭标记为true。
  11. 在个人信息页,点击使用华为账号静默登录开关,会开启或关闭静默登录功能,开启静默登录后,不管用户是否退出登录应用,下次重新打开应用都会通过华为账号静默登录应用。
  12. 在个人信息页面,点击退出登录按钮后,修改用户登录状态为未登录并返回首页。
  13. 当未成年人模式开启时,当前设备的开发者调试模式会被禁用,开发者可以进入设置-系统-开发者选项,点击USB调试开关,会校验健康使用设备密码,校验成功后可解除开发者调试模式限制。

工程目录

├─entry/src/main/ets            // 代码区
│ ├─common                      // 公用代码
│ │ └─AvoidRepeatClick.ets      // 防止重复点击工具类
│ │ └─CommonEventUtil.ets       // 公共类,包含监听未成年人状态变化的事件
│ │ └─ErrorCodeEntity.ets       // 登录过程中可能遇到的错误码信息页面
│ │ └─UserInfo.ets              // 用户信息数据结构
│ │ └─Utils.ets                 // 公用的一些业务方法
│ ├─components                  // 存放组件文件目录
│ │ └─Address.ets               // 获取收货地址组件
│ │ └─AgreementDialog.ets       // 隐私协议弹窗组件
│ │ └─AvatarAndNickname.ets     // 获取头像、昵称组件
│ │ └─InvoiceTitle.ets          // 获取发票抬头组件
│ │ └─MinorsProtection.ets      // 开启、关闭未成年人模式组件
│ │ └─Phone.ets                 // 手机号授权组件
│ │ └─SilentlyLoginSwitch.ets   // 开启、关闭静默登录组件
│ │ └─UserAgreement.ets         // 用户协议组件
│ ├─entryability
│ │ └─EntryAbility.ets          // 程序入口类
│ ├─pages                       // 存放页面文件目录
│ │ └─HomePage.ets              // 主页面,作为Navigation跳转的容器
│ │ └─OtherLoginPage.ets        // 应用自定义的其他登录方式
│ │ └─PersonalInfoPage.ets      // 个人信息页,包含头像、昵称、默认手机号展示,同时包含快速验证手机号授权、获取收货地址、设置未成年人模式、获取发票抬头、使用华为账号静默登录场景
│ │ └─PrepareLoginPage.ets      // 首页内容,包括获取匿名手机号场景
│ │ └─ProtocolWebView.ets       // 华为账号用户认证协议页面
│ │ └─QuickLoginPage.ets        // 华为账号一键登录组件,包含组件调用
└──entry/src/main/resources     // 资源文件目录

具体实现

调用一键登录组件拉起符合华为规范的登录页面,参考QuickLoginPage.ets:

  • 使用 loginComponentManager.PrivacyText 创建 QuickLoginPage 组件隐私文本对象 privacyText
  • 使用 loginComponentManager.LoginWithHuaweiIDButtonController 构造 QuickLoginPage 组件的控制器对象 controller
  • 创建一键登录组件对象将 privacyText、controller 等作为参数传入,拉起登录界面
  • 使用 setAgreementStatus 方法设置用户是否同意协议,通过该状态判断是否需要拉起协议弹窗
  • 使用 continueLogin 方法设置同意协议与登录一同完成,同意协议后直接触发登录的异步回调,用户无需再次点击登录按钮
  • 通过 LoginWithHuaweiIDButton 组件内设置 param 中的 extraStyle 属性开启一键登录按钮的点击加载态

调用获取头像昵称授权接口拉起授权页面、调用快速验证手机号接口完成手机号快速验证与授权、调用收货地址接口获取收货地址信息、调用发票抬头接口获取发票抬头信息,参考 PersonalInfoPage.ets:

  • 使用 authentication.HuaweiIDProvider().createAuthorizationWithHuaweiIDRequest() 创建授权请求,完成头像昵称参数设置
  • 使用 authentication.AuthenticationController(getContext(this)) 创建 controller
  • 调用 controller.executeRequest() 执行请求
  • 使用 authentication.HuaweiIDProvider().createAuthorizationWithHuaweiIDRequest() 创建授权请求,完成快速验证手机号参数设置
  • 使用 authentication.AuthenticationController(getContext(this)) 创建 controller
  • 调用 controller.executeRequest() 执行请求
  • 使用 shippingAddress.chooseAddress 获取 AddressInfo 对象
  • 从 AddressInfo 对象中获取收货人姓名、电话、地址信息
  • 使用 invoiceAssistant.selectInvoiceTitle 获取 InvoiceTitle 对象
  • 从 InvoiceTitle 对象中获取发票抬头名称、税号等信息

调用未成年人模式相关接口设置未成年人模式,参考 MinorsProtection.ets:

  • 使用 minorsProtection.leadToTurnOnMinorsMode 引导用户开启未成年人模式,应用内提供入口,帮助用户快速开启未成年人模式
  • 使用 minorsProtection.leadToTurnOffMinorsMode 引导用户关闭未成年人模式,应用内提供入口,帮助用户快速关闭未成年人模式
  • 使用 minorsProtection.verifyMinorsProtectionCredential 校验用户开启未成年人模式的密码,防止未成年人更改系统未成年人模式设置

使用 HomePage 作为 Navigation 容器进行页面承载,参考 HomePage.ets:

  • 使用 new authentication.HuaweiIDProvider().createLoginWithHuaweiIDRequest() 创建静默登录请求,配合 PersistentStorage 模拟静默登录

HomePage 作为应用首页调用未成年人模式接口,参考 HomePage.ets:

  • 使用 PersistentStorage和AppStorage 存储和获取到当前应用是否开启未成年人模式的状态
  • 调用 minorsProtection.getMinorsProtectionInfoSync() 函数,从 minorsProtection.MinorsProtectionInfo 对象中获取未成年人模式的开启状态,同时查询应用内未成年人模式单独关闭标记:
    • 当查询到系统的未成年人模式为开启,应用内未成年人模式单独关闭标记为已开启时,表明用户单独关闭过该应用的未成年人模式,则应用不应该开启未成年人模式。
    • 当查询到系统的未成年人模式为开启,应用内未成年人模式单独关闭标记为已关闭时,应用应正常开启未成年人模式。
    • 当查询到系统的未成年人模式为关闭时,应用也应关闭未成年人模式,同时清除应用内未成年人模式单独关闭标记。

在 CommonEventUtil 监听未成年状态变化,参考在CommonEventUtil.ets:

  • 使用 @ohos.commonEventManager 系统能力监听 COMMON_EVENT_MINORSMODE_ON 和 COMMON_EVENT_MINORSMODE_OFF 事件,当应用进程存在时,应用可以实时感知到系统未成年人模式的变化,从而与系统联动改变自己的未成年人模式,并清除应用内未成年人模式单独关闭标记。

在 OtherLoginPage页面中自定义用户的其他登录方式:

  • 比如手机号验证码、密码等

在 ProtocolWebView 页面中使用Web组件显示《华为账号用户认证协议》内容:

  • 在 data.json 中获取网页链接,当前仅支持中英文

在 AgreementDialog 中实现用户隐私协议的弹窗组件

  • 通过自定义 CustomDialogController 控制弹窗的显隐

在 ErrorCodeEntity 中定义通用的实体数据信息

  • 比如通用错误码、通用常量等

在 UserInfo 中定义 UserInfo 的数据结构

在 EntryAbility 中订阅未成年人模式公共事件

在 AvoidRepeatClick 类中实现防止用户短时间内重复点击方法

参考

  1. entry\src\main\ets\pages\QuickLoginPage.ets
  2. entry\src\main\ets\pages\PersonalInfoPage.ets
  3. entry\src\main\ets\common\CommonEventUtil.ets
  4. entry\src\main\ets\pages\HomePage.ets
  5. entry\src\main\ets\pages\OtherLoginPage.ets
  6. entry\src\main\ets\pages\ProtocolWebView.ets
  7. entry\src\main\ets\components\AgreementDialog.ets
  8. entry\src\main\ets\common\ErrorCodeEntity.ets
  9. entry\src\main\ets\common\UserInfo.ets
  10. entry\src\main\ets\entryability\EntryAbility.ets
  11. entry\src\main\ets\common\AvoidRepeatClick.ets
  12. entry\src\main\ets\components\MinorsProtection.ets

相关权限

  1. 本示例需要访问《华为账号用户认证协议》页面,已在 module.json5 文件中添加允许使用 Internet 网络权限"ohos.permission.INTERNET"。
  2. 本示例在跳转网页前需要查询网络连接状态,已在 module.json5 文件中添加允许应用获取数据网络信息权限"ohos.permission.GET_NETWORK_INFO"。

依赖

依赖设备具备 WIFI 能力

约束与限制

  1. 本示例仅支持标准系统上运行,支持设备:华为手机、2in1、平板,支持账号:已绑定手机号的中国境内(不包含中国香港、中国澳门、中国台湾)成人账号。
  2. 本示例不支持大字体模式、隐私空间模式。
  3. HarmonyOS 系统:HarmonyOS Next Beta1 及以上。
  4. DevEco Studio 版本:DevEco Studio Next Beta1 及以上。
  5. HarmonyOS SDK 版本:HarmonyOS Next Beta1 及以上。
Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS

简介

本示例基于HarmonyOS实现了Account Kit(华为账号服务)提供的华为账号一键登录、静默登录、获取头像昵称、获取手机号、获取收货地址、未成年人模式和获取发票抬头等功能。 展开 收起
README
Apache-2.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/harmonyos_samples/accountkit-samplecode-clientdemo-arkts.git
git@gitee.com:harmonyos_samples/accountkit-samplecode-clientdemo-arkts.git
harmonyos_samples
accountkit-samplecode-clientdemo-arkts
Accountkit-Samplecode-Clientdemo-ArkTS
master

搜索帮助