1/* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import Logger from '../model/Logger' 17import dataStorage from '@ohos.data.storage' 18import featureAbility from '@ohos.ability.featureAbility' 19 20const TAG: string = '[AccountData]' 21 22export class AccountData { 23 static instance: AccountData = null 24 private storage: dataStorage.Storage = null 25 private accountData = [] 26 27 public static getInstance() { 28 if (this.instance === null) { 29 this.instance = new AccountData() 30 } 31 return this.instance 32 } 33 34 async getFromStorage(url: string) { 35 const context = featureAbility.getContext() 36 let path = await context.getOrCreateLocalDir() 37 Logger.info(TAG, `Path is ${path}`) 38 try { 39 this.storage = await dataStorage.getStorage(`${path}/files/${url}`) 40 } catch (err) { 41 Logger.error(`getStorage failed, code is ${err.code}, message is ${err.message}`) 42 } 43 if (this.storage === null) { 44 Logger.info(TAG, `Create stroage is fail.`) 45 } 46 } 47 48 async getStorage(url: string) { 49 this.storage = null 50 await this.getFromStorage(url) 51 return this.storage 52 } 53 54 async putStorageValue(key: string, value: string, url: string) { 55 this.storage = await this.getStorage(url) 56 try { 57 await this.storage.put(key, value) 58 await this.storage.flush() 59 Logger.info(TAG, `put key && value success`) 60 } catch (err) { 61 Logger.info(TAG, `aaaaaa put failed`) 62 } 63 return 64 } 65 66 async hasStorageValue(key: string, url: string) { 67 this.storage = await this.getStorage(url) 68 let result 69 try { 70 result = await this.storage.has(key) 71 } catch (err) { 72 Logger.error(`hasStorageValue failed, code is ${err.code}, message is ${err.message}`) 73 } 74 Logger.info(TAG, `hasStorageValue success result is ${result}`) 75 return result 76 } 77 78 async getStorageValue(key: string, url: string) { 79 this.storage = await this.getStorage(url) 80 let getValue 81 try { 82 getValue = await this.storage.get(key, 'null') 83 } catch (err) { 84 Logger.error(`getStorageValue failed, code is ${err.code}, message is ${err.message}`) 85 } 86 Logger.info(TAG, `getStorageValue success`) 87 return getValue 88 } 89 90 async deleteStorageValue(key: string, url: string) { 91 this.storage = await this.getStorage(url) 92 try { 93 await this.storage.delete(key) 94 await this.storage.flush() 95 } catch (err) { 96 Logger.error(`deleteStorageValue failed, code is ${err.code}, message is ${err.message}`) 97 } 98 Logger.info(TAG, `delete success`) 99 return 100 } 101}