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 unknown 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 { SpAdvertisementHtml } from './SpAdvertisement.html'; 18import { SpStatisticsHttpUtil } from '../../statistics/util/SpStatisticsHttpUtil'; 19 20@element('sp-advertisement') 21export class SpAdvertisement extends BaseElement { 22 private advertisementEL: HTMLElement | undefined | null; 23 private closeEL: HTMLElement | undefined | null; 24 private noticeEl: HTMLElement | undefined | null; 25 private message: string = ''; 26 27 initElements(): void { 28 // 整个广告 29 this.advertisementEL = document.querySelector('body > sp-application')?.shadowRoot?. 30 querySelector('#sp-advertisement')?.shadowRoot?.querySelector('#sp-advertisement'); 31 // 关闭按钮 32 this.closeEL = document.querySelector('body > sp-application')?.shadowRoot?. 33 querySelector('#sp-advertisement')?.shadowRoot?.querySelector('#close'); 34 // 公告内容 35 this.noticeEl = document.querySelector('body > sp-application')?.shadowRoot?. 36 querySelector('#sp-advertisement')?.shadowRoot?.querySelector('.text'); 37 this.getMessage(); 38 setInterval(() => { 39 this.getMessage(); 40 }, 300000); 41 this.closeEL?.addEventListener('click', () => { 42 this.advertisementEL!.style!.display = 'none'; 43 localStorage.setItem('isdisplay', 'false'); 44 }); 45 }; 46 47 private getMessage(): void { 48 SpStatisticsHttpUtil.getNotice().then(res => { 49 if (res.status === 200) { 50 res.text().then((it) => { 51 let resp = JSON.parse(it); 52 let publish = localStorage.getItem('message'); 53 if (resp && resp.data && resp.data.data && resp.data.data !== '') { 54 this.message = resp.data.data; 55 localStorage.setItem('message', this.message); 56 let parts = this.message.split(';'); 57 let registrationLinkInfo = (parts[2].match(/报名链接:([^\s]+)/) || [])[1] || ''; 58 let onlineMeetingLinkInfo = (parts[3].match(/线上会议链接:([^\s]+)/) || [])[1] || ''; 59 let registrationLink = `<a href="${registrationLinkInfo}" target="_blank">报名链接</a>`; 60 let onlineMeetingLink = `<a href="${onlineMeetingLinkInfo}" target="_blank">线上会议链接</a>`; 61 let finalString = `${parts[0]}<br>${parts[1]}<br>${registrationLink} ${onlineMeetingLink}<br>${parts[4]}`; 62 this.noticeEl!.innerHTML = `<p>${finalString}</p>`; 63 if (publish) { 64 if (resp.data.data !== publish) { 65 localStorage.setItem('isdisplay', 'true'); 66 } 67 } else { 68 localStorage.setItem('isdisplay', 'true'); 69 } 70 } else { 71 localStorage.setItem('isdisplay', 'false'); 72 } 73 let isdisplay = localStorage.getItem('isdisplay'); 74 this.advertisementEL!.style!.display = isdisplay === 'true' ? 'block' : 'none'; 75 }); 76 } else { 77 this.advertisementEL!.style!.display = 'none'; 78 } 79 }).catch(err => { 80 this.advertisementEL!.style!.display = 'none'; 81 }); 82 } 83 84 initHtml(): string { 85 return SpAdvertisementHtml; 86 } 87}