介紹
本示例選擇應(yīng)用進(jìn)行注冊/登錄,并設(shè)置帳號(hào)相關(guān)信息,簡要說明應(yīng)用帳號(hào)管理相關(guān)功能。效果圖如下:
效果預(yù)覽
使用說明參考鴻蒙文檔:[qr23.cn/AKFP8k
]
1.首頁面選擇想要進(jìn)入的應(yīng)用,首次進(jìn)入該應(yīng)用需要進(jìn)行注冊,如已注冊帳號(hào)則直接登錄。
2.注冊頁面可設(shè)置帳號(hào)名、郵箱、個(gè)性簽名、密碼(帶*號(hào)為必填信息),注冊完成后返回登錄頁面使用注冊的帳號(hào)進(jìn)行登錄。
3.登錄后進(jìn)入帳號(hào)詳情界面,點(diǎn)擊修改信息按鈕可跳轉(zhuǎn)至帳號(hào)信息修改頁面重新設(shè)置帳號(hào)信息。
4.點(diǎn)擊切換應(yīng)用按鈕則退出該帳號(hào)并返回首頁面。重新選擇想要進(jìn)入的應(yīng)用。
5.點(diǎn)擊刪除帳號(hào)按鈕則會(huì)刪除該帳號(hào)所有相關(guān)信息。
代碼解讀
Harmony與OpenHarmoy鴻蒙文檔添加
mau123789是v直接拿取
entry/src/main/ets/
|---common
| |---AccountInfo.ets // 切換應(yīng)用組件
| |---BundleInfo.ets // 首頁列表組件
| |---LoginInfo.ets // 登錄組件
| |---ModifyInfo.ets // 修改信息組件
| |---NavigationBar.ets // 路由跳轉(zhuǎn)組件
| |---RegisterInfo.ets // 注冊組件
|---entryAbility
| |---EntryAbility.ts
|---model
| |---AccountData.ts // 數(shù)據(jù)存儲(chǔ)
| |---AccountModel.ts // 數(shù)據(jù)管理
| |---Logger.ts // 日志工具
|---pages
| |---Index.ets // 首頁
| |---Account.ets // 切換應(yīng)用頁面
| |---Login.ets // 登錄頁面
| |---Modify.ets // 修改信息頁面
| |---Register.ets // 注冊信息頁面
具體實(shí)現(xiàn)
- 本示例分為音樂,視頻,地圖三個(gè)模塊
- 音樂模塊
- 使用Navigation,Button,Text,TextInput組件開發(fā)注冊,登錄,修改信息和切換應(yīng)用頁面, createAppAccountManager方法創(chuàng)建應(yīng)用帳號(hào)管理器對(duì)象
- 源碼鏈接:[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組件開發(fā)注冊,登錄,修改信息和切換應(yīng)用頁面,createAppAccountManager方法創(chuàng)建應(yīng)用帳號(hào)管理器對(duì)象
- 源碼鏈接:[AccountData.ts],[AccountModel.ts]
- 接口參考:[@ohos.account.appAccount],[@ohos.data.preferences],[@ohos.router]
- 地圖模塊
- 使用Navigation,Button,Text,TextInput組件開發(fā)注冊,登錄,修改信息和切換應(yīng)用頁面,createAppAccountManager方法創(chuàng)建應(yīng)用帳號(hào)管理器對(duì)象
- 源碼鏈接:[AccountData.ts],[AccountModel.ts]
- 接口參考:[@ohos.account.appAccount],[@ohos.data.preferences],[@ohos.router]
- 視頻模塊
審核編輯 黃宇
-
鴻蒙
+關(guān)注
關(guān)注
57文章
2310瀏覽量
42743 -
HarmonyOS
+關(guān)注
關(guān)注
79文章
1967瀏覽量
30018
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論