0
  • 聊天消息
  • 系統(tǒng)消息
  • 評(píng)論與回復(fù)
登錄后你可以
  • 下載海量資料
  • 學(xué)習(xí)在線課程
  • 觀看技術(shù)視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會(huì)員中心
創(chuàng)作中心

完善資料讓更多小伙伴認(rèn)識(shí)你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示

HarmonyOS開發(fā)實(shí)例:【app帳號(hào)管理】

jf_46214456 ? 來源:jf_46214456 ? 作者:jf_46214456 ? 2024-04-14 09:46 ? 次閱讀

介紹

本示例選擇應(yīng)用進(jìn)行注冊(cè)/登錄,并設(shè)置帳號(hào)相關(guān)信息,簡(jiǎn)要說明應(yīng)用帳號(hào)管理相關(guān)功能。效果圖如下:

效果預(yù)覽

image.png

使用說明參考鴻蒙文檔:[qr23.cn/AKFP8k]

搜狗高速瀏覽器截圖20240326151344.png

1.首頁(yè)面選擇想要進(jìn)入的應(yīng)用,首次進(jìn)入該應(yīng)用需要進(jìn)行注冊(cè),如已注冊(cè)帳號(hào)則直接登錄。

2.注冊(cè)頁(yè)面可設(shè)置帳號(hào)名、郵箱、個(gè)性簽名、密碼(帶*號(hào)為必填信息),注冊(cè)完成后返回登錄頁(yè)面使用注冊(cè)的帳號(hào)進(jìn)行登錄。

3.登錄后進(jìn)入帳號(hào)詳情界面,點(diǎn)擊修改信息按鈕可跳轉(zhuǎn)至帳號(hào)信息修改頁(yè)面重新設(shè)置帳號(hào)信息。

4.點(diǎn)擊切換應(yīng)用按鈕則退出該帳號(hào)并返回首頁(yè)面。重新選擇想要進(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                     // 首頁(yè)列表組件
|   |---LoginInfo.ets                      // 登錄組件
|   |---ModifyInfo.ets                     // 修改信息組件
|   |---NavigationBar.ets                  // 路由跳轉(zhuǎn)組件
|   |---RegisterInfo.ets                   // 注冊(cè)組件
|---entryAbility
|   |---EntryAbility.ts             
|---model
|   |---AccountData.ts                     // 數(shù)據(jù)存儲(chǔ)
|   |---AccountModel.ts                    // 數(shù)據(jù)管理
|   |---Logger.ts                          // 日志工具
|---pages
|   |---Index.ets                          // 首頁(yè)
|   |---Account.ets                        // 切換應(yīng)用頁(yè)面
|   |---Login.ets                          // 登錄頁(yè)面
|   |---Modify.ets                         // 修改信息頁(yè)面
|   |---Register.ets                       // 注冊(cè)信息頁(yè)面

具體實(shí)現(xiàn)

  • 本示例分為音樂,視頻,地圖三個(gè)模塊
    • 音樂模塊
      • 使用Navigation,Button,Text,TextInput組件開發(fā)注冊(cè),登錄,修改信息和切換應(yīng)用頁(yè)面, 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ā)注冊(cè),登錄,修改信息和切換應(yīng)用頁(yè)面,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ā)注冊(cè),登錄,修改信息和切換應(yīng)用頁(yè)面,createAppAccountManager方法創(chuàng)建應(yīng)用帳號(hào)管理器對(duì)象
      • 源碼鏈接:[AccountData.ts],[AccountModel.ts]
      • 接口參考:[@ohos.account.appAccount],[@ohos.data.preferences],[@ohos.router]

審核編輯 黃宇

聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點(diǎn)僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場(chǎng)。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問題,請(qǐng)聯(lián)系本站處理。 舉報(bào)投訴
  • 鴻蒙
    +關(guān)注

    關(guān)注

    56

    文章

    2267

    瀏覽量

    42489
  • HarmonyOS
    +關(guān)注

    關(guān)注

    79

    文章

    1946

    瀏覽量

    29739
收藏 人收藏

    評(píng)論

    相關(guān)推薦

    HarmonyOS開發(fā)實(shí)例:【狀態(tài)管理

    從數(shù)據(jù)的傳遞形式來看,可以分為只讀的單向傳遞和可變更的雙向傳遞。如下圖所示,開發(fā)框架提供了多種應(yīng)用程序狀態(tài)管理的能力。
    的頭像 發(fā)表于 04-10 09:38 ?706次閱讀
    <b class='flag-5'>HarmonyOS</b><b class='flag-5'>開發(fā)</b><b class='flag-5'>實(shí)例</b>:【狀態(tài)<b class='flag-5'>管理</b>】

    HarmonyOS應(yīng)用開發(fā)-編譯、調(diào)試、應(yīng)用發(fā)布資料

    Studio提供了基于各種編寫代碼及不同設(shè)備的調(diào)試功能,如果使用了多種代碼編寫應(yīng)用,請(qǐng)參考選擇調(diào)試代碼類型進(jìn)行配置后啟動(dòng)調(diào)試,調(diào)試過程中基于不同的代碼進(jìn)行斷點(diǎn)管理。開發(fā)者完成HarmonyOS應(yīng)用
    發(fā)表于 09-21 16:29

    HarmonyOS開發(fā)跨設(shè)備的鴻蒙(HarmonyOSApp

    是圓形(如智能手表),這就給開發(fā)App帶來了麻煩?,F(xiàn)在幾乎每一個(gè)智能設(shè)備廠商,如Apple、華為都面臨這個(gè)問題。這就要求我們開發(fā)App盡可能適合更多的智能設(shè)備。當(dāng)然,最簡(jiǎn)單,最直接的
    發(fā)表于 11-02 15:18

    HarmonyOS開發(fā)跨設(shè)備的鴻蒙(HarmonyOSApp

    手表),這就給開發(fā)App帶來了麻煩?,F(xiàn)在幾乎每一個(gè)智能設(shè)備廠商,如Apple、華為都面臨這個(gè)問題。這就要求我們開發(fā)App盡可能適合更多的智能設(shè)備。當(dāng)然,最簡(jiǎn)單,最直接的方式是為每一類
    發(fā)表于 11-03 16:54

    如何優(yōu)雅地開發(fā)HarmonyOS APP應(yīng)用

    ` 本帖最后由 軟通動(dòng)力HOS 于 2021-3-10 15:29 編輯 研究HarmonyOS有一段時(shí)間了,今天主要結(jié)合自己多年的項(xiàng)目開發(fā)經(jīng)驗(yàn)和各種技術(shù)棧結(jié)合HarmonyOS APP
    發(fā)表于 03-10 15:13

    戈帥 開發(fā)HarmonyOS APP《拼夕夕》演示

    戈帥 開發(fā)HarmonyOS APP《拼夕夕》演示
    發(fā)表于 08-28 17:39

    【視頻】應(yīng)用開發(fā)第4期:原子化服務(wù)帳號(hào)授權(quán)

    介紹HarmonyOS Connect應(yīng)用如何引入華為帳號(hào)能力,以及帳號(hào)能力在工程中的業(yè)務(wù)流。帳號(hào)服務(wù)開發(fā)指南:https://develo
    發(fā)表于 12-14 11:54

    harmonyOS開發(fā)APP如何訪問Webservice?

    我接到一個(gè)項(xiàng)目,需要用到HarmonyOS開發(fā)APP做為移動(dòng)手機(jī)查詢和收到報(bào)警數(shù)據(jù),具體是這樣的,在C/S加B/S的系統(tǒng)框架下我們有數(shù)據(jù)庫(kù)服務(wù)器和Web服務(wù)器,有widows桌面應(yīng)用和Web瀏覽器
    發(fā)表于 03-28 10:14

    HarmonyOS APP打包運(yùn)行和調(diào)試應(yīng)用開發(fā)步驟

    在進(jìn)行HarmonyOS應(yīng)用開發(fā)前,您應(yīng)該掌握HarmonyOS應(yīng)用的邏輯結(jié)構(gòu)。HarmonyOS應(yīng)用發(fā)布形態(tài)為APP Pack(Appli
    發(fā)表于 05-24 14:27

    HarmonyOS Connect “Device Partner”專場(chǎng)FAQ來啦!

    HarmonyOS Connect生態(tài),共同提升消費(fèi)者的智慧生活體驗(yàn)。 在接入HarmonyOS Connect生態(tài)的過程中,你是否對(duì)團(tuán)隊(duì)管理、帳號(hào)找回、產(chǎn)品委托、產(chǎn)品信息查詢等功能的
    發(fā)表于 02-27 11:07

    餐飲管理APP開發(fā)

     【粉果科技】專注于APP開發(fā)—深圳APP開發(fā)公司解釋到:隨著移動(dòng)互聯(lián)網(wǎng)的來了,通過餐飲管理APP
    發(fā)表于 06-26 11:57 ?345次閱讀

    HarmonyOS Connect “Device Partner”專場(chǎng)FAQ來啦!

    HarmonyOS Connect生態(tài),共同提升消費(fèi)者的智慧生活體驗(yàn)。 在接入HarmonyOS Connect生態(tài)的過程中,你是否對(duì)團(tuán)隊(duì)管理、帳號(hào)找回、產(chǎn)品委托、產(chǎn)品信息查詢等功能的
    的頭像 發(fā)表于 02-24 09:10 ?534次閱讀

    鴻蒙開發(fā)設(shè)備管理:ohos.account.appAccount 應(yīng)用帳號(hào)管理

    應(yīng)用帳號(hào)管理:獲取應(yīng)用帳號(hào)模塊對(duì)象。
    的頭像 發(fā)表于 07-06 10:43 ?548次閱讀
    鴻蒙<b class='flag-5'>開發(fā)</b>設(shè)備<b class='flag-5'>管理</b>:ohos.account.appAccount 應(yīng)用<b class='flag-5'>帳號(hào)</b><b class='flag-5'>管理</b>

    鴻蒙開發(fā)管理:ohos.account.distributedAccount 分布式帳號(hào)管理

    獲取分布式帳號(hào)實(shí)例對(duì)象。
    的頭像 發(fā)表于 07-08 10:03 ?165次閱讀
    鴻蒙<b class='flag-5'>開發(fā)</b><b class='flag-5'>管理</b>:ohos.account.distributedAccount 分布式<b class='flag-5'>帳號(hào)</b><b class='flag-5'>管理</b>

    鴻蒙開發(fā)管理:ohos.account.osAccount 系統(tǒng)帳號(hào)管理

    本模塊提供管理系統(tǒng)帳號(hào)的一些基礎(chǔ)能力,包括系統(tǒng)帳號(hào)的添加、刪除、查詢、設(shè)置、訂閱、啟動(dòng)等功能,提供系統(tǒng)帳號(hào)數(shù)據(jù)落盤的能力。
    的頭像 發(fā)表于 07-08 09:54 ?226次閱讀
    鴻蒙<b class='flag-5'>開發(fā)</b><b class='flag-5'>管理</b>:ohos.account.osAccount 系統(tǒng)<b class='flag-5'>帳號(hào)</b><b class='flag-5'>管理</b>