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 userAuth from '@ohos.userIAM.userAuth'; 17import UIExtensionContentSession from '@ohos.app.ability.UIExtensionContentSession'; 18import Constants, { TipCode } from '../vm/Constants'; 19import LogUtils from './LogUtils'; 20import JSON from '@ohos.util.json'; 21 22const TAG = 'AuthUtils'; 23 24export default class AuthUtils { 25 private static authUtilsInstance: AuthUtils; 26 private widgetContextId: bigint = BigInt(AppStorage.get<string>('widgetContextId')) ?? BigInt(-1); 27 28 public static getInstance(): AuthUtils { 29 if (!AuthUtils.authUtilsInstance) { 30 AuthUtils.authUtilsInstance = new AuthUtils(); 31 } 32 return AuthUtils.authUtilsInstance; 33 } 34 35 sendNotice(cmd: string, type: Array<string>, tipCode: TipCode = TipCode.NORMAL): void { 36 try { 37 const eventData = { 38 widgetContextId: this.widgetContextId, 39 event: cmd, 40 version: Constants.noticeVersion, 41 payload: { 42 type: type, 43 tipCode: tipCode 44 } 45 }; 46 const jsonEventData = JSON.stringify(eventData); 47 LogUtils.info(TAG, 'sendNotice start eventData: ' + jsonEventData); 48 userAuth.sendNotice(userAuth.NoticeType.WIDGET_NOTICE, jsonEventData); 49 LogUtils.info(TAG, 'sendNotice success'); 50 } catch (error) { 51 LogUtils.error(TAG, 'sendNotice catch error: ' + error?.code); 52 (AppStorage.get('session') as UIExtensionContentSession)?.terminateSelf(); 53 } 54 } 55}