• 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 osAccount from '@ohos.account.osAccount';
17import { BusinessError } from '@ohos.base';
18import account_osAccount from '@ohos.account.osAccount';
19import { staffItem } from './staff';
20import Constants from '../constant';
21import { getAlertMessage, isPC } from '../utils';
22import GlobalContext from '../GlobalContext';
23import { AccountTips, AccountTipsConfig } from '../AccountTipsConfig';
24import { DlpAlertDialog } from '../components/dlp_alert_dialog';
25
26interface Staff {
27  authAccount: string;
28  textContent?: string;
29}
30
31const TAG: string = "[DLPManager_AddStaff]";
32
33@Extend(Text) function inputMessageText() {
34  .fontSize($r('sys.float.ohos_fa_text_size_body'))
35  .lineHeight(Constants.PP_TEXT_LINE_HEIGHT2)
36  .fontColor($r('sys.color.ohos_id_color_handup'))
37  .fontWeight(FontWeight.Medium)
38  .margin({ top: Constants.ENCRYPTION_ADD_STAFF_BORDER_MARGIN_TOP })
39  .textAlign(TextAlign.Start)
40}
41@Extend(Text) function TipsTextStyle(){
42  .fontSize($r('sys.float.ohos_id_text_size_body2'))
43  .fontColor($r('sys.color.ohos_id_color_primary_dark'))
44  .textOverflow({
45    overflow: TextOverflow.Ellipsis
46  })
47  .maxLines(Constants.PP_TEXT_MAX_LINE)
48}
49
50@Component
51struct AddStaff {
52  @State succ: number = 0;
53  @State fail: number = 0;
54  @State inputId: string = '';
55  @State isAccountCheckSuccess: boolean = true;
56  @State staffArrayLength: boolean = false;
57  @State isInputInvalid: boolean = false;
58  @State isNetworkInvalid: boolean = false;
59  @State activeStaffIndex: number = -1;
60  @State domainTips: string = '';
61  @State accountNameTips: string = '';
62  @State handlePopupTips: boolean = false;
63  @State textContent: string = '';
64  @State accountTipsArray: Array<AccountTips> = new Array<AccountTips>();
65  @Link staffArray: Staff[];
66  @State focusFlag: boolean = false;
67  @Prop isDisable: boolean = false;
68  dlpAlertDialog?: CustomDialogController;
69
70  @Builder MenuBuilder(index: number) {
71    Flex({ direction: FlexDirection.Column, justifyContent: FlexAlign.Center, alignItems: ItemAlign.Center }) {
72      Text($r('app.string.delete_account'))
73        .fontSize($r('sys.float.ohos_id_text_size_body1'))
74        .textAlign(TextAlign.Start)
75        .width(Constants.ENCRYPTION_ADD_STAFF_MENU_WIDTH)
76        .height(Constants.ENCRYPTION_ADD_STAFF_MENU_HEIGHT)
77        .onClick(() => {
78          this.removeItem(index)
79        })
80    }.width(Constants.ENCRYPTION_ADD_STAFF_MENU_WIDTH)
81  }
82
83  @Builder popupBuilderTips() {
84    Column() {
85      if(GlobalContext.load('domainAccount')){
86        ForEach(this.accountTipsArray, (item: AccountTips) => {
87          if (item.isShow) {
88            Row() {
89              if (AccountTipsConfig.isSysLanguage()) {
90                Text(item.description?.replace('%s', item.value)).TipsTextStyle()
91              } else {
92                Text(item.descriptionEn?.replace('%s', item.value)).TipsTextStyle()
93              }
94            }.height(Constants.TIPS_HEIGHT_ITEM)
95          }
96        }, (item: AccountTips) => item.key)
97      }else{
98        Row() {
99          Text($r('app.string.encrypt_employee_id', this.accountNameTips))
100            .wordBreak(WordBreak.BREAK_ALL)
101            .TipsTextStyle()
102        }
103        .width(Constants.HEADER_COLUMN_MESSAGE_TIPS)
104      }
105    }
106    .alignItems(HorizontalAlign.Start)
107    .padding({
108      left: Constants.ROW_FONT_SIZE,
109      right: Constants.ROW_FONT_SIZE,
110      top: Constants.DA_MARGIN_TOP,
111      bottom: Constants.DA_MARGIN_TOP
112    })
113  }
114
115  removeItem(i: number) {
116    this.staffArray.splice(i, 1)
117    this.activeStaffIndex = -1
118    this.staffArrayLength = false;
119  }
120
121  private showErrorDialog(title: Resource, message: Resource) {
122    this.dlpAlertDialog = new CustomDialogController({
123      builder: DlpAlertDialog({
124        title: title,
125        message: message,
126        action: () => {
127        }
128      }),
129      autoCancel: true,
130      customStyle: true,
131      maskColor: Constants.TRANSPARENT_BACKGROUND_COLOR
132    });
133    this.dlpAlertDialog.open();
134  }
135
136  async aboutToAppear(){
137    if (GlobalContext.load('domainAccount')) {
138      this.accountTipsArray = await AccountTipsConfig.getConfigTips();
139      this.textContent = AccountTipsConfig.showContentKey;
140    }
141  }
142
143  build() {
144    Column() {
145      Flex({
146        direction: FlexDirection.Row,
147        wrap: FlexWrap.Wrap,
148      }) {
149        ForEach(
150          this.staffArray,
151          (item: Staff, index) => {
152            Stack() {
153              staffItem({
154                authAccount: item.authAccount,
155                textContent: item.textContent,
156                isActive: this.activeStaffIndex === index,
157                changeIndex: Number(index),
158              })
159              Text(item.authAccount)
160                .opacity(Constants.PP_ROW_RADIUS)
161                .focusable(true)
162                .focusOnTouch(true)
163                .borderRadius(Constants.ENCRYPTION_SUCCESS_CHANGE_TOP)
164                .onKeyEvent((event?: KeyEvent) => {
165                  if (event?.keyText === 'KEYCODE_DEL' &&
166                    event.keyCode === 2055 && this.activeStaffIndex === index) {
167                    this.removeItem(index)
168                  }
169                  if (event?.keyText === 'KEYCODE_FORWARD_DEL' && event.keyCode === 2071 && this.activeStaffIndex === index) {
170                    this.removeItem(index)
171                  }
172                })
173                .onClick(() => {
174                  this.activeStaffIndex = index!;
175                  if (GlobalContext.load('domainAccount')) {
176                    try {
177                      AccountTipsConfig.getAccountInfo(item.authAccount)
178                        .then(async (result: account_osAccount.DomainAccountInfo) => {
179                          this.accountTipsArray = await AccountTipsConfig.getConfigTips();
180                          this.handlePopupTips = true;
181                          this.accountTipsArray?.forEach((accountTips: AccountTips) => {
182                            accountTips.value = result[accountTips.key];
183                          })
184                        }).catch((err: BusinessError) => {
185                        console.error(`${TAG} call getAccountInfo failed, error: ${JSON.stringify(err)}`);
186                        let errInfo = getAlertMessage(err);
187                        this.showErrorDialog(errInfo.title, errInfo.msg);
188                      });
189                    } catch (error) {
190                      console.error(TAG, 'getAccountInfo exception = ' + JSON.stringify(error));
191                    }
192                  } else {
193                    this.handlePopupTips = true;
194                    this.accountNameTips = item.authAccount;
195                  }
196                })
197                .padding(
198                  {
199                    top: Constants.FOOTER_ROW_PAD_LEFT,
200                    bottom: Constants.FOOTER_ROW_PAD_RIGHT,
201                  }
202                )
203                .margin({
204                  right: Constants.ENCRYPTION_ADD_STAFF_MARGIN_RIGHT,
205                  bottom: Constants.ENCRYPTION_ADD_STAFF_MARGIN_BOTTOM
206                })
207                .bindPopup(this.handlePopupTips && this.activeStaffIndex === index, {
208                  builder: this.popupBuilderTips,
209                  placement: Placement.BottomLeft,
210                  popupColor: ($r('sys.color.ohos_id_color_tooltip_background_dark')),
211                  enableArrow: true,
212                  showInSubWindow: false,
213                  arrowOffset: Constants.TIPS_ARROW_OFFSET,
214                  onStateChange: (e) => {
215                    if (!e.isVisible) {
216                      this.activeStaffIndex = -1;
217                      this.handlePopupTips = false;
218                    }
219                  }
220                })
221                .bindContextMenu(this.MenuBuilder(index!), isPC() ? ResponseType.RightClick : ResponseType.LongPress)
222                .width(item.authAccount === item.textContent ? Constants.ENCRYPTION_ADD_STAFF_WIDTH : Constants.TIPS_STAFF_WIDTH)
223                .height(Constants.ENCRYPTION_ADD_STAFF_HEIGHT)
224            }
225          },
226          (item: Staff) => item.authAccount
227        )
228        TextInput({
229          text: this.inputId,
230          placeholder: !this.staffArray.length ?
231            ((GlobalContext.load('domainAccount') as boolean)
232              ? $r('app.string.enter_a_complete_work_ID')
233              : $r('app.string.enter_a_complete_account')) : undefined,
234        })
235          .flexGrow(Constants.ENCRYPTION_ADD_STAFF_FLEX_GROW)
236          .backgroundColor($r('sys.color.ohos_id_color_dialog_bg'))
237          .borderRadius(Constants.PP_ROW_RADIUS)
238          .fontSize($r('sys.float.ohos_id_text_size_body1'))
239          .fontColor(this.isInputInvalid ? $r('sys.color.ohos_id_color_handup') : $r('sys.color.ohos_id_color_text_primary'))
240          .padding({
241            top: Constants.PP_BUTTON_PAD,
242            bottom: Constants.PP_BUTTON_PAD,
243            left: Constants.PP_TEXT_PAD_RIGHT,
244            right: Constants.PP_BUTTON_PAD
245          })
246          .width(
247            this.staffArray.length ? Constants.ENCRYPTION_ADD_STAFF_WIDTH : Constants.FOOTER_ROW_WIDTH
248          )
249          .height(Constants.ENCRYPTION_ADD_STAFF_HEIGHT)
250          .onChange((value) => {
251            this.inputId = value
252            if (this.isInputInvalid || this.isNetworkInvalid) {
253              this.isInputInvalid = false;
254              this.isNetworkInvalid = false;
255            }
256          })
257          .onSubmit(() => {
258            if (this.staffArray.length >= Constants.ENCRYPTION_ADD_STAFF_LENGTH_MAX) {
259              this.inputId = '';
260              this.staffArrayLength = true;
261              return;
262            }
263            if (!this.isAccountCheckSuccess) return;
264            if (!this.inputId) {
265              return
266            }
267            try {
268              let domainAccountInfo: osAccount.DomainAccountInfo = {
269                domain: 'china',
270                accountName: this.inputId.toLocaleLowerCase().trim()
271              }
272              if (GlobalContext.load('domainAccount')) {
273                this.isAccountCheckSuccess = false;
274                account_osAccount.DomainAccountManager.hasAccount(domainAccountInfo, (err, isExist) => {
275                  this.isAccountCheckSuccess = true;
276                  if (isExist) {
277                    AccountTipsConfig.getAccountInfo(this.inputId)
278                      .then((result: account_osAccount.DomainAccountInfo) => {
279                        let o1: Staff = {
280                          authAccount: result.accountName,
281                          textContent: result[this.textContent] as string
282                        }
283                        this.staffArray.push(o1);
284                        this.succ = GlobalContext.load('hiAccountVerifySucc') + 1;
285                        GlobalContext.store('hiAccountVerifySucc', this.succ);
286                        this.inputId = '';
287                    }).catch((error: BusinessError) => {
288                      if ([Constants.ERR_JS_ACCOUNT_NOT_FOUND].includes(error.code)){
289                        this.isInputInvalid = true;
290                        return;
291                      } else {
292                        this.isNetworkInvalid = true;
293                        return;
294                      }
295                    })
296                  } else {
297                    this.fail = GlobalContext.load('hiAccountVerifyFail') + 1;
298                    GlobalContext.store('hiAccountVerifyFail', this.fail);
299                    if ([Constants.ERR_JS_INVALID_PARAMETER, Constants.ERR_JS_ACCOUNT_NOT_FOUND].includes(err.code)){
300                      this.isInputInvalid = true;
301                      return;
302                    } else {
303                      this.isNetworkInvalid = true;
304                      return;
305                    }
306                  }
307                })
308              } else {
309                this.isAccountCheckSuccess = true;
310                let o2: Staff = {
311                  authAccount: this.inputId,
312                  textContent: this.inputId
313                }
314                this.staffArray.push(o2);
315                this.succ = GlobalContext.load('hiAccountVerifySucc') + 1;
316                GlobalContext.store('hiAccountVerifySucc', this.succ);
317                this.inputId = ''
318              }
319            } catch (err) {
320              this.isAccountCheckSuccess = true;
321              this.fail = GlobalContext.load('hiAccountVerifyFail') + 1;
322              GlobalContext.store('hiAccountVerifyFail', this.fail);
323              console.error(TAG, (err as BusinessError).code);
324            }
325          })
326          .onFocus(() => {
327            this.focusFlag = !this.focusFlag;
328            this.activeStaffIndex = -1;
329          })
330          .onBlur(() => {
331            this.focusFlag = !this.focusFlag;
332          })
333      }
334      .padding({
335        top: Constants.ENCRYPTION_ADD_STAFF_PADDING_TOP,
336      })
337      .border({
338        width: { bottom: this.focusFlag ? px2vp(Constants.ENCRYPTION_ADD_STAFF_BORDER2) : px2vp(Constants.ENCRYPTION_ADD_STAFF_BORDER) },
339        color: (this.isInputInvalid || this.staffArrayLength || this.isNetworkInvalid )
340          ? $r('sys.color.ohos_id_color_handup') :
341          this.focusFlag ? $r('sys.color.ohos_id_color_primary') : $r('sys.color.ohos_id_color_list_separator'),
342      })
343
344      Flex({ direction: FlexDirection.Row }) {
345        if (this.isInputInvalid) {
346          Text((GlobalContext
347          .load('domainAccount') as boolean) ? $r('app.string.incorrect_work_ID') : $r('app.string.incorrect_account'))
348            .inputMessageText()
349        }
350        if (this.isNetworkInvalid) {
351          Text((GlobalContext
352          .load('domainAccount') as boolean) ? $r('app.string.network_invalid') : $r('app.string.incorrect_account'))
353            .inputMessageText()
354        }
355        Blank()
356        if (this.staffArray.length >= Constants.ENCRYPTION_ADD_STAFF_LENGTH_MAX * Constants.ENCRYPTION_ADD_STAFF_LENGTH) {
357          Text(`${this.staffArray.length}/${Constants.ENCRYPTION_ADD_STAFF_LENGTH_MAX}`)
358            .fontSize($r('sys.float.ohos_fa_text_size_body'))
359            .lineHeight(Constants.PP_TEXT_LINE_HEIGHT2)
360            .fontColor(this.staffArrayLength ? $r('sys.color.ohos_id_color_handup') : $r('sys.color.ohos_id_color_text_secondary'))
361            .fontWeight(FontWeight.Medium)
362            .margin({ top: Constants.ENCRYPTION_ADD_STAFF_BORDER_MARGIN_TOP })
363            .textAlign(TextAlign.End)
364        }
365      }
366    }
367    .opacity(this.isDisable ? Constants.DU_LINE_WIDTH : Constants.FOOTER_OPACITY_ONE)
368    .enabled(this.isDisable ? false : true)
369  }
370}
371
372export { AddStaff };
373