• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 prompt from '@ohos.prompt'
17import router from '@ohos.router'
18import HiLog from '../model/HiLogUtil'
19import account_distributedAccount from '@ohos.account.distributedAccount'
20
21const TAG: string = '[Account.UserInterface]'
22
23class BasicDataSource implements IDataSource {
24  private listeners: DataChangeListener[] = []
25
26  public totalCount() {
27    return 0
28  }
29
30  public getData(index: number) {
31    return undefined
32  }
33
34  registerDataChangeListener(listener: DataChangeListener) {
35    if (this.listeners.indexOf(listener) < 0) {
36      this.listeners.push(listener)
37    }
38  }
39
40  unregisterDataChangeListener(listener: DataChangeListener) {
41    const pos = this.listeners.indexOf(listener)
42    if (pos >= 0) {
43      this.listeners.splice(pos, 1)
44    }
45  }
46
47  notifyDataReload() {
48    this.listeners.forEach(listener => {
49      listener.onDataReloaded()
50    })
51  }
52
53  notifyDataAdd(index: number) {
54    this.listeners.forEach(listener => {
55      listener.onDataAdd(index)
56    })
57  }
58
59  notifyDataChange(index: number) {
60    this.listeners.forEach(listener => {
61      listener.onDataChange(index)
62    })
63  }
64
65  notifyDataDelete(index: number) {
66    this.listeners.forEach(listener => {
67      listener.onDataDelete(index)
68    })
69  }
70
71  notifyDataMove(from: number, to: number) {
72    this.listeners.forEach(listener => {
73      listener.onDataMove(from, to)
74    })
75  }
76}
77
78class MyDataSource extends BasicDataSource {
79  private dataArray: Resource[] = [$r('app.string.exit_login'), $r('app.string.written_off')]
80
81  public totalCount() {
82    return this.dataArray.length
83  }
84
85  public getData(index: number) {
86    return this.dataArray[index]
87  }
88
89  public addData(index: number) {
90    this.dataArray.splice(index, 0)
91    this.notifyDataAdd(index)
92  }
93
94  public pushData(index: number) {
95    this.dataArray.push()
96    this.notifyDataAdd(this.dataArray.length - 1)
97  }
98
99  public replaceData(result: any[]) {
100    this.dataArray = result
101  }
102}
103
104@Entry
105@Component
106struct UserInterface {
107  @State userName: string = <string> router.getParams()["userName"]
108  @State password: string = <string> router.getParams()["password"]
109  @State btnText: MyDataSource = new MyDataSource()
110
111  async onBtnClick(item: Resource) {
112    prompt.showToast({ message: `name:${this.userName} ,pwd:${this.password} `, duration: 2000 })
113    const accountAbility = account_distributedAccount.getDistributedAccountAbility()
114    let accountInfo
115    if (item === this.btnText[0]) {
116      accountInfo = { id: this.password, name: this.userName.toString(), event: 'Ohos.account.event.LOGOUT' }
117      accountAbility.updateOsAccountDistributedInfo(accountInfo, (err) => {
118        if (err) {
119          HiLog.info(TAG, `exit failed!`)
120          prompt.showToast({ message: `${accountInfo.name} 退出失败`, duration: 1500 })
121        } else {
122          HiLog.info(TAG, `退出登录成功`)
123          HiLog.info(TAG, `exitLogin: data_id = ${accountInfo.id} \n data_name = ${accountInfo.name}, err: ${JSON.stringify(err)}`)
124          prompt.showToast({ message: `${accountInfo.name} 已退出登录`, duration: 1500 })
125          router.back()
126        }
127      })
128    } else {
129      accountInfo = { id: this.password, name: this.userName.toString(), event: 'Ohos.account.event.LOGOFF' }
130      accountAbility.updateOsAccountDistributedInfo(accountInfo, (err) => {
131        if (err) {
132          HiLog.info(TAG, `writtenOff failed!`)
133          prompt.showToast({ message: `${accountInfo.name} 注销失败`, duration: 1500 })
134        } else {
135          HiLog.info(TAG, `注销成功`)
136          HiLog.info(TAG, `writtenOff: data_id = ${accountInfo.id} \n data_name = ${accountInfo.name}, err: ${JSON.stringify(err)}`)
137          prompt.showToast({ message: `${accountInfo.name} 已注销`, duration: 1500 })
138          router.back()
139        }
140      })
141    }
142  }
143
144  build() {
145    Column() {
146      Row() {
147        Text($r('app.string.user_name'))
148          .width('35%')
149          .fontSize(30)
150          .fontWeight(FontWeight.Bold)
151        Text(this.userName)
152          .width('75%')
153          .height(50)
154          .fontSize(30)
155          .fontWeight(FontWeight.Regular)
156      }
157      .margin({ top: '30%' })
158      .width('80%')
159
160      LazyForEach(this.btnText, item => {
161        Button() {
162          Text(item)
163            .fontSize(25)
164            .fontWeight(FontWeight.Bold)
165        }
166        .width('80%')
167        .height(50)
168        .margin({ top: 20 })
169        .backgroundColor('#0D9FFB')
170        .borderRadius(30)
171        .onClick(() => {
172          this.onBtnClick(item)
173        })
174      }, item => JSON.stringify(item))
175    }
176    .width('100%')
177    .height('100%')
178  }
179}