HarmonyOS开发实例:【app帐号管理】
1.OpenHarmony南向开发案例:【智能风扇】2.鸿蒙HarmonyOS开发实例:【简单时钟】3.HarmonyOS开发实例:【状态管理】4.鸿蒙OS开发学习:【尺寸适配实现】5.OpenHarmony鸿蒙开发学习:【Ability的启动模式】6.鸿蒙HarmonyOS开发实战:【分布式音乐播放】7.HarmonyOS开发实例:【分布式数据管理】8.鸿蒙HarmonyOS开发实例:【分布式关系型数据库】9.OpenHarmony开发技术:【国际化】实例10.OpenHarmony开发案例:【分布式计算器】11.HarmonyOS开发实例:【数字管家app】12.HarmonyOS开发:【NFC配置流程】13.HarmonyOS开发:【数字管家app设备接入FA】14.鸿蒙OS开发实例:【HarmonyHttpClient】网络框架15.鸿蒙OS开发实例:【Native C++】16.HarmonyOS开发实例:【菜单app】17.OpenHarmony开发学习:【源码下载和编译】18.HarmonyOS开发实例:【自定义Emitter】19.鸿蒙OS开发学习:【第三方库调用】20.HarmonyOS开发学习:【DevEco Device Tool 安装配置(问题全解)】21.OpenHarmony开发实例:【鸿蒙.bin文件烧录】
22.HarmonyOS开发实例:【app帐号管理】
23.OpenHarmony实例应用:【常用组件和容器低代码】24.OpenHarmony南向嵌入式:【XR806开发板指导文档】25.OpenHarmony南向开发案例:【智能门锁】26.HarmonyOS开发实例:【任务延时调度】27.OpenHarmony开发案例:【自定义通知】28.OpenHarmony开发实例:【仿桌面应用】29.HarmonyOS开发实例:【事件的订阅和发布】30.OpenHarmony开发案例:【电影卡片】31.OpenHarmony南向开发案例:【智能体重秤】32.鸿蒙OS开发指导:【应用包签名工具】33.OpenHarmony开发实例:【分布式游戏鉴权应用】34.OpenHarmony开发实例:【新闻客户端】35.鸿蒙OpenHarmony Native API【Native_Bundle】36.HarmonyOS开发实战:【亲子拼图游戏】应用帐号管理
介绍
本示例选择应用进行注册/登录,并设置帐号相关信息,简要说明应用帐号管理相关功能。效果图如下:
效果预览
使用说明参考鸿蒙文档:qr23.cn/AKFP8k
点击或者转到。
1.首页面选择想要进入的应用,首次进入该应用需要进行注册,如已注册帐号则直接登录。
2.注册页面可设置帐号名、邮箱、个性签名、密码(带*号为必填信息),注册完成后返回登录页面使用注册的帐号进行登录。
3.登录后进入帐号详情界面,点击修改信息按钮可跳转至帐号信息修改页面重新设置帐号信息。
4.点击切换应用按钮则退出该帐号并返回首页面。重新选择想要进入的应用。
5.点击删除帐号按钮则会删除该帐号所有相关信息。
代码解读
entry/src/main/ets/
|---common
| |---AccountInfo.ets // 切换应用组件
| |---BundleInfo.ets // 首页列表组件
| |---LoginInfo.ets // 登录组件
| |---ModifyInfo.ets // 修改信息组件
| |---NavigationBar.ets // 路由跳转组件
| |---RegisterInfo.ets // 注册组件
|---entryAbility
| |---EntryAbility.ts
|---model
| |---AccountData.ts // 数据存储
| |---AccountModel.ts // 数据管理
| |---Logger.ts // 日志工具
|---pages
| |---Index.ets // 首页
| |---Account.ets // 切换应用页面
| |---Login.ets // 登录页面
| |---Modify.ets // 修改信息页面
| |---Register.ets // 注册信息页面
具体实现
-
本示例分为音乐,视频,地图三个模块
-
音乐模块
- 使用Navigation,Button,Text,TextInput组件开发注册,登录,修改信息和切换应用页面, createAppAccountManager方法创建应用帐号管理器对象
- 源码链接:[AccountData.ts]
-
/*
* Copyright (c) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import Logger from '../model/Logger'
import common from '@ohos.app.ability.common'
import preferences from '@ohos.data.preferences'
const TAG: string = '[AccountData]'
export class AccountData {
static instance: AccountData = null
private storage: preferences.Preferences = null
public static getInstance() {
if (this.instance === null) {
this.instance = new AccountData()
}
return this.instance
}
async getFromStorage(context: common.Context, url: string) {
let name = url
Logger.info(TAG, `Name is ${name}`)
try {
this.storage = await preferences.getPreferences(context, `${name}`)
} catch (err) {
Logger.error(`getStorage failed, code is ${err.code}, message is ${err.message}`)
}
if (this.storage === null) {
Logger.info(TAG, `Create stroage is fail.`)
}
}
async getStorage(context: common.Context, url: string) {
this.storage = null
await this.getFromStorage(context, url)
return this.storage
}
async putStorageValue(context: common.Context, key: string, value: string, url: string) {
this.storage = await this.getStorage(context, url)
try {
await this.storage.put(key, value)
await this.storage.flush()
Logger.info(TAG, `put key && value success`)
} catch (err) {
Logger.info(TAG, `aaaaaa put failed`)
}
return
}
async hasStorageValue(context: common.Context, key: string, url: string) {
this.storage = await this.getStorage(context, url)
let result
try {
result = await this.storage.has(key)
} catch (err) {
Logger.error(`hasStorageValue failed, code is ${err.code}, message is ${err.message}`)
}
Logger.info(TAG, `hasStorageValue success result is ${result}`)
return result
}
async getStorageValue(context: common.Context, key: string, url: string) {
this.storage = await this.getStorage(context, url)
let getValue
try {
getValue = await this.storage.get(key, 'null')
} catch (err) {
Logger.error(`getStorageValue failed, code is ${err.code}, message is ${err.message}`)
}
Logger.info(TAG, `getStorageValue success`)
return getValue
}
async deleteStorageValue(context: common.Context, key: string, url: string) {
this.storage = await this.getStorage(context, url)
try {
await this.storage.delete(key)
await this.storage.flush()
} catch (err) {
Logger.error(`deleteStorageValue failed, code is ${err.code}, message is ${err.message}`)
}
Logger.info(TAG, `delete success`)
return
}
}
[AccountModel.ts]
/*
* Copyright (c) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import Logger from '../model/Logger'
import appAccount from '@ohos.account.appAccount'
const TAG: string = '[AccountModel]'
const app: appAccount.AppAccountManager = appAccount.createAppAccountManager()
export class AccountModel {
async addAccount(username: string) {
await app.addAccount(username)
Logger.info(TAG, `addAccount success`)
return
}
async deleteAccount(username: string) {
await app.deleteAccount(username)
Logger.info(TAG, `deleteAccount success`)
return
}
async setAccountCredential(username: string, credentialType: string, credential: string) {
await app.setAccountCredential(username, credentialType, credential)
Logger.info(TAG, `setAccountCredential success`)
return
}
async setAccountExtraInfo(name: string, extraInfo: string) {
await app.setAccountExtraInfo(name, extraInfo)
Logger.info(TAG, `setAccountExtraInfo success`)
return
}
async setAssociatedData(name: string, key: string, value: string) {
await app.setAssociatedData(name, key, value)
Logger.info(TAG, `setAssociatedData success`)
return
}
async getAccountCredential(name: string, credentialType: string) {
let result = await app.getAccountCredential(name, credentialType)
Logger.info(TAG, `getAccountCredential success`)
return result
}
async getAccountExtraInfo(name: string) {
let result = await app.getAccountExtraInfo(name)
Logger.info(TAG, `getAccountExtraInfo success`)
return result
}
async getAssociatedData(name: string, key: string) {
let result = await app.getAssociatedData(name, key)
Logger.info(TAG, `getAssociatedData success`)
return result
}
}
-
接口参考:[@ohos.account.appAccount],[@ohos.data.preferences],[@ohos.router]
-
视频模块
- 使用Navigation,Button,Text,TextInput组件开发注册,登录,修改信息和切换应用页面,createAppAccountManager方法创建应用帐号管理器对象
- 源码链接:[AccountData.ts],[AccountModel.ts]
- 接口参考:[@ohos.account.appAccount],[@ohos.data.preferences],[@ohos.router]
-
地图模块
- 使用Navigation,Button,Text,TextInput组件开发注册,登录,修改信息和切换应用页面,createAppAccountManager方法创建应用帐号管理器对象
- 源码链接:[AccountData.ts],[AccountModel.ts]
- 接口参考:[@ohos.account.appAccount],[@ohos.data.preferences],[@ohos.router]
-
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了