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("#notice"); 37 this.getMessage(); 38 setInterval(() => { 39 this.getMessage(); 40 }, 10000); 41 this.closeEL?.addEventListener('click', () => { 42 this.advertisementEL!.style!.display = 'none'; 43 }) 44 } 45 46 private getMessage() { 47 SpStatisticsHttpUtil.getNotice().then(res => { 48 res.text().then((it) => { 49 let resp = JSON.parse(it); 50 if (resp && resp.data && resp.data.data && resp.data.data !== this.message && resp.data.data !== '') { 51 this.message = resp.data.data; 52 if (this.message.startsWith('图片:')) { 53 this.noticeEl!.style.display = "flex"; 54 this.noticeEl!.style.justifyContent = "center"; 55 this.noticeEl!.innerHTML = `<img src ="${this.message.substring(3, this.message.length)}" style="height:150px" 56 alt = "图片加载失败"></img>` 57 } else if (this.message.startsWith('链接:')) { 58 this.noticeEl!.style.height = "auto"; 59 this.noticeEl!.style.color = "#000"; 60 this.noticeEl!.innerHTML = `链接:<a href = "${this.message.substring(3, this.message.length)}" target = "black"> 61 ${this.message.substring(3, this.message.length)}</a>` 62 } else { 63 this.noticeEl!.style.color = "red"; 64 this.noticeEl!.innerHTML = this.message; 65 } 66 this.advertisementEL!.style!.display = 'block'; 67 } 68 }); 69 }) 70 } 71 72 initHtml(): string { 73 return SpAdvertisementHtml; 74 } 75}