• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2023 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 distributedAccount from '@ohos.account.distributedAccount';
17import Account from '../common/database/Account';
18import { BusinessError } from '@ohos.base';
19import DatabaseUtils from '../common/utils/DatabaseUtils';
20import Logger from './Logger';
21import { GlobalContext } from '../common/database/GlobalContext';
22
23const TAG: string = `[DistributedAccountModel]`
24
25let accountAbility: distributedAccount.DistributedAccountAbility = distributedAccount.getDistributedAccountAbility();
26
27export default class DistributedAccountModel {
28  bind(localId: number, distributedAccountName: string, distributedAccountNickName: string,
29       distributedAccountAvatar: string): Promise<void> {
30    return new Promise((resolve, reject) => {
31      let accountInfo: distributedAccount.DistributedInfo = {
32        // 随机生成帐号id
33        id: Math.floor(Math.random() * 10000 + 1000).toString(),
34        name: distributedAccountName,
35        nickname: distributedAccountNickName,
36        avatar: distributedAccountAvatar,
37        event: 'Ohos.account.event.LOGIN'
38      };
39      try {
40        accountAbility.setOsAccountDistributedInfoByLocalId(localId, accountInfo).then(() => {
41          // 插入到数据表
42          let account = new Account()
43          account.distributedAccountName = accountInfo.name
44          account.distributedAccountID = accountInfo.id
45          account.localId = localId
46          DatabaseUtils.insertAccount(account, GlobalContext.getrdbStore())
47          Logger.info(TAG, 'setOsAccountDistributedInfoByLocalId  Success');
48          resolve()
49        }).catch((err: BusinessError) => {
50          Logger.error(TAG, "setOsAccountDistributedInfoByLocalId  err: " + JSON.stringify(err));
51          reject(err)
52        });
53      } catch (e) {
54        Logger.error(TAG, "setOsAccountDistributedInfoByLocalId  exception: " + e);
55        reject(e)
56      }
57    })
58  }
59
60  unBind(localId: number, distributedAccountID: string, distributedAccountName: string): Promise<void> {
61    return new Promise((resolve, reject) => {
62      let accountInfo: distributedAccount.DistributedInfo = {
63        id: distributedAccountID,
64        name: distributedAccountName,
65        event: 'Ohos.account.event.LOGOUT'
66      };
67      try {
68        accountAbility.setOsAccountDistributedInfoByLocalId(localId, accountInfo).then(() => {
69          // 从数据表中删除
70          DatabaseUtils.deleteAccountData(localId, GlobalContext.getrdbStore())
71          Logger.info(TAG, 'setOsAccountDistributedInfoByLocalId successfully');
72          resolve()
73        }).catch((err: BusinessError) => {
74          Logger.error(TAG, "setOsAccountDistributedInfoByLocalId exception: " + JSON.stringify(err));
75          reject(err)
76        });
77      } catch (e) {
78        Logger.error(TAG, "setOsAccountDistributedInfoByLocalId exception: " + e);
79        reject(e)
80      }
81    })
82  }
83
84  getOsAccountDistributedInfo(): Promise<distributedAccount.DistributedInfo> {
85    return new Promise((resolve, reject) => {
86      try {
87        accountAbility.getOsAccountDistributedInfo().then((data) => {
88          Logger.info(TAG, "getOsAccountDistributedInfo data: " + JSON.stringify(data));
89          resolve(data)
90        }).catch((err: BusinessError) => {
91          Logger.error(TAG, "getOsAccountDistributedInfo err: " + JSON.stringify(err));
92          reject(err)
93        });
94      } catch (e) {
95        Logger.error(TAG, "getOsAccountDistributedInfo exception: " + JSON.stringify(e));
96        reject(e)
97      }
98    })
99  }
100
101  getOsAccountDistributedInfoByLocalId(localId: number): Promise<distributedAccount.DistributedInfo> {
102    return new Promise((resolve, reject) => {
103      try {
104        accountAbility.getOsAccountDistributedInfoByLocalId(localId).then((data) => {
105          Logger.info(TAG, 'distributed information: ' + JSON.stringify(data));
106          resolve(data)
107        }).catch((err: BusinessError) => {
108          Logger.error(TAG, 'getOsAccountDistributedInfoByLocalId exception: ' + JSON.stringify(err));
109          reject(err)
110        });
111      } catch (e) {
112        Logger.error(TAG, 'getOsAccountDistributedInfoByLocalId exception: ' + JSON.stringify(e));
113        reject(e)
114      }
115    })
116  }
117}