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 { BaseElement, element } from '../../base-ui/BaseElement'; 17import { SpBubblesAIHtml } from './SpBubblesAI.html'; 18import { FlagsConfig, Params } from './SpFlags'; 19import { SpStatisticsHttpUtil } from '../../statistics/util/SpStatisticsHttpUtil'; 20 21@element('sp-bubble-ai') 22export class SpBubblesAI extends BaseElement { 23 static isAIHover: boolean = false; 24 initElements(): void { 25 const xiaoLubanEl: HTMLElement | undefined | null = this.shadowRoot?.querySelector('#xiao-luban-help'); 26 xiaoLubanEl?.addEventListener('click', () => { 27 this.xiaoLubanEvent(); 28 let requestBody = { 29 action: 'AItrace', 30 event: 'AItrace' 31 }; 32 SpStatisticsHttpUtil.addOrdinaryVisitAction(requestBody); 33 SpStatisticsHttpUtil.generalRecord('AI_statistic', 'smart_luban', []); 34 }); 35 let isShowXiaoLuban: boolean = FlagsConfig.getFlagsConfigEnableStatus('AI'); 36 if (isShowXiaoLuban) { 37 xiaoLubanEl?.setAttribute('enabled', ''); 38 } else { 39 xiaoLubanEl?.removeAttribute('enabled'); 40 } 41 // 鼠标进入元素 42 xiaoLubanEl?.addEventListener('mouseenter', function () { 43 SpBubblesAI.isAIHover = true; 44 }); 45 46 // 鼠标离开元素 47 xiaoLubanEl?.addEventListener('mouseleave', function () { 48 SpBubblesAI.isAIHover = false; 49 }); 50 } 51 52 initHtml(): string { 53 return SpBubblesAIHtml; 54 } 55 56 private xiaoLubanEvent(): void { 57 const flagConfig: Params | undefined = FlagsConfig.getFlagsConfig('AI'); 58 const userId: string | undefined = flagConfig!.userId?.toString(); 59 const data = { 60 'sender': `${userId}`, 61 'msgBody': 'rag SmartPerf_ad85b972', 62 'msgType': 'text', 63 'timestamp': new Date().getTime().toString(), 64 'botUser': 'p_xiaoluban', 65 }; 66 fetch(`https://${window.location.host}/xiaoluban/resource`, { 67 method: 'post', 68 body: JSON.stringify(data), 69 headers: { 70 'Content-Type': 'application/json' 71 } 72 }).then(res => { 73 if (res.status === 200) { 74 window.open('im:p_xiaoluban', '_self'); 75 } 76 }); 77 } 78} 79 80 81