1fb726d48Sopenharmony_ci/*
2fb726d48Sopenharmony_ci * Copyright (C) 2022 Huawei Device Co., Ltd.
3fb726d48Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4fb726d48Sopenharmony_ci * you may not use this file except in compliance with the License.
5fb726d48Sopenharmony_ci * You may obtain a copy of the License at
6fb726d48Sopenharmony_ci *
7fb726d48Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8fb726d48Sopenharmony_ci *
9fb726d48Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10fb726d48Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11fb726d48Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF unknown KIND, either express or implied.
12fb726d48Sopenharmony_ci * See the License for the specific language governing permissions and
13fb726d48Sopenharmony_ci * limitations under the License.
14fb726d48Sopenharmony_ci */
15fb726d48Sopenharmony_ci
16fb726d48Sopenharmony_ciimport { BaseElement, element } from '../../base-ui/BaseElement';
17fb726d48Sopenharmony_ciimport { SpAdvertisementHtml } from "./SpAdvertisement.html";
18fb726d48Sopenharmony_ciimport { SpStatisticsHttpUtil } from '../../statistics/util/SpStatisticsHttpUtil';
19fb726d48Sopenharmony_ci
20fb726d48Sopenharmony_ci@element('sp-advertisement')
21fb726d48Sopenharmony_ciexport class SpAdvertisement extends BaseElement {
22fb726d48Sopenharmony_ci    private advertisementEL: HTMLElement | undefined | null;
23fb726d48Sopenharmony_ci    private closeEL: HTMLElement | undefined | null;
24fb726d48Sopenharmony_ci    private noticeEl: HTMLElement | undefined | null;
25fb726d48Sopenharmony_ci    private message: string = '';
26fb726d48Sopenharmony_ci
27fb726d48Sopenharmony_ci    initElements(): void {
28fb726d48Sopenharmony_ci        // 整个广告
29fb726d48Sopenharmony_ci        this.advertisementEL = document.querySelector("body > sp-application")?.shadowRoot?.
30fb726d48Sopenharmony_ci            querySelector("#sp-advertisement")?.shadowRoot?.querySelector("#sp-advertisement");
31fb726d48Sopenharmony_ci        // 关闭按钮
32fb726d48Sopenharmony_ci        this.closeEL = document.querySelector("body > sp-application")?.shadowRoot?.
33fb726d48Sopenharmony_ci            querySelector("#sp-advertisement")?.shadowRoot?.querySelector("#close");
34fb726d48Sopenharmony_ci        // 公告内容
35fb726d48Sopenharmony_ci        this.noticeEl = document.querySelector("body > sp-application")?.shadowRoot?.
36fb726d48Sopenharmony_ci            querySelector("#sp-advertisement")?.shadowRoot?.querySelector("#notice");
37fb726d48Sopenharmony_ci        this.getMessage();
38fb726d48Sopenharmony_ci        setInterval(() => {
39fb726d48Sopenharmony_ci            this.getMessage();
40fb726d48Sopenharmony_ci        }, 10000);
41fb726d48Sopenharmony_ci        this.closeEL?.addEventListener('click', () => {
42fb726d48Sopenharmony_ci            this.advertisementEL!.style!.display = 'none';
43fb726d48Sopenharmony_ci        })
44fb726d48Sopenharmony_ci    }
45fb726d48Sopenharmony_ci
46fb726d48Sopenharmony_ci    private getMessage() {
47fb726d48Sopenharmony_ci        SpStatisticsHttpUtil.getNotice().then(res => {
48fb726d48Sopenharmony_ci            res.text().then((it) => {
49fb726d48Sopenharmony_ci                let resp = JSON.parse(it);
50fb726d48Sopenharmony_ci                if (resp && resp.data && resp.data.data && resp.data.data !== this.message && resp.data.data !== '') {
51fb726d48Sopenharmony_ci                    this.message = resp.data.data;
52fb726d48Sopenharmony_ci                    if (this.message.startsWith('图片:')) {
53fb726d48Sopenharmony_ci                        this.noticeEl!.style.display = "flex";
54fb726d48Sopenharmony_ci                        this.noticeEl!.style.justifyContent = "center";
55fb726d48Sopenharmony_ci                        this.noticeEl!.innerHTML = `<img src ="${this.message.substring(3, this.message.length)}" style="height:150px" 
56fb726d48Sopenharmony_ci                    alt = "图片加载失败"></img>`
57fb726d48Sopenharmony_ci                    } else if (this.message.startsWith('链接:')) {
58fb726d48Sopenharmony_ci                        this.noticeEl!.style.height = "auto";
59fb726d48Sopenharmony_ci                        this.noticeEl!.style.color = "#000";
60fb726d48Sopenharmony_ci                        this.noticeEl!.innerHTML = `链接:<a href = "${this.message.substring(3, this.message.length)}" target = "black">
61fb726d48Sopenharmony_ci                    ${this.message.substring(3, this.message.length)}</a>`
62fb726d48Sopenharmony_ci                    } else {
63fb726d48Sopenharmony_ci                        this.noticeEl!.style.color = "red";
64fb726d48Sopenharmony_ci                        this.noticeEl!.innerHTML = this.message;
65fb726d48Sopenharmony_ci                    }
66fb726d48Sopenharmony_ci                    this.advertisementEL!.style!.display = 'block';
67fb726d48Sopenharmony_ci                }
68fb726d48Sopenharmony_ci            });
69fb726d48Sopenharmony_ci        })
70fb726d48Sopenharmony_ci    }
71fb726d48Sopenharmony_ci
72fb726d48Sopenharmony_ci    initHtml(): string {
73fb726d48Sopenharmony_ci        return SpAdvertisementHtml;
74fb726d48Sopenharmony_ci    }
75fb726d48Sopenharmony_ci}