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 ANY 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 { warn } from '../../log/Log';
17fb726d48Sopenharmony_ciimport { BurialPointRequestBody, pluginUsage } from './SpStatisticsHttpBean';
18fb726d48Sopenharmony_ci
19fb726d48Sopenharmony_ciexport class SpStatisticsHttpUtil {
20fb726d48Sopenharmony_ci  static requestServerInfo: string = '';
21fb726d48Sopenharmony_ci  static serverTime: number = 0;
22fb726d48Sopenharmony_ci  static timeDiff: number = 0;
23fb726d48Sopenharmony_ci  static retryCount: number = 0;
24fb726d48Sopenharmony_ci  static retryMaxCount: number = 5;
25fb726d48Sopenharmony_ci  static pauseRetry: boolean = false;
26fb726d48Sopenharmony_ci  static retryRestTimeOut: boolean = false;
27fb726d48Sopenharmony_ci
28fb726d48Sopenharmony_ci  static initStatisticsServerConfig(): void {
29fb726d48Sopenharmony_ci    if (SpStatisticsHttpUtil.requestServerInfo === '') {
30fb726d48Sopenharmony_ci      SpStatisticsHttpUtil.requestServerInfo = SpStatisticsHttpUtil.getRequestServerInfo();
31fb726d48Sopenharmony_ci    }
32fb726d48Sopenharmony_ci    if (SpStatisticsHttpUtil.serverTime === 0) {
33fb726d48Sopenharmony_ci      SpStatisticsHttpUtil.getServerTime();
34fb726d48Sopenharmony_ci    }
35fb726d48Sopenharmony_ci  }
36fb726d48Sopenharmony_ci
37fb726d48Sopenharmony_ci  static getRequestServerInfo(): string {
38fb726d48Sopenharmony_ci    try {
39fb726d48Sopenharmony_ci      let req = new XMLHttpRequest();
40fb726d48Sopenharmony_ci      req.onreadystatechange = (): void => {
41fb726d48Sopenharmony_ci        if (req.readyState === 4 && req.status === 200) {
42fb726d48Sopenharmony_ci          let requestInfo = req.getResponseHeader('request_info');
43fb726d48Sopenharmony_ci          if (requestInfo && requestInfo.length > 0) {
44fb726d48Sopenharmony_ci            SpStatisticsHttpUtil.requestServerInfo = requestInfo;
45fb726d48Sopenharmony_ci          }
46fb726d48Sopenharmony_ci        }
47fb726d48Sopenharmony_ci      };
48fb726d48Sopenharmony_ci      req.open(
49fb726d48Sopenharmony_ci        'GET',
50fb726d48Sopenharmony_ci        `${window.location.protocol}//${window.location.host.split(':')[0]}:${window.location.port
51fb726d48Sopenharmony_ci        }/application/serverInfo`,
52fb726d48Sopenharmony_ci        true
53fb726d48Sopenharmony_ci      );
54fb726d48Sopenharmony_ci      req.send(null);
55fb726d48Sopenharmony_ci    } catch {
56fb726d48Sopenharmony_ci      warn('Connect Server Failed');
57fb726d48Sopenharmony_ci    }
58fb726d48Sopenharmony_ci    return '';
59fb726d48Sopenharmony_ci  }
60fb726d48Sopenharmony_ci
61fb726d48Sopenharmony_ci  static getServerTime(): void {
62fb726d48Sopenharmony_ci    if (SpStatisticsHttpUtil.requestServerInfo === '') {
63fb726d48Sopenharmony_ci      SpStatisticsHttpUtil.requestServerInfo = SpStatisticsHttpUtil.getRequestServerInfo();
64fb726d48Sopenharmony_ci    }
65fb726d48Sopenharmony_ci    if (SpStatisticsHttpUtil.pauseRetry) {
66fb726d48Sopenharmony_ci      return;
67fb726d48Sopenharmony_ci    }
68fb726d48Sopenharmony_ci    fetch(`https://${SpStatisticsHttpUtil.requestServerInfo}/serverTime`)
69fb726d48Sopenharmony_ci      .then((resp) => {
70fb726d48Sopenharmony_ci        resp.text().then((it) => {
71fb726d48Sopenharmony_ci          if (it && it.length > 0) {
72fb726d48Sopenharmony_ci            SpStatisticsHttpUtil.serverTime = Number(it);
73fb726d48Sopenharmony_ci            SpStatisticsHttpUtil.timeDiff = SpStatisticsHttpUtil.serverTime - Date.now();
74fb726d48Sopenharmony_ci          }
75fb726d48Sopenharmony_ci        });
76fb726d48Sopenharmony_ci      })
77fb726d48Sopenharmony_ci      .catch((e) => {
78fb726d48Sopenharmony_ci        this.handleRequestException();
79fb726d48Sopenharmony_ci      });
80fb726d48Sopenharmony_ci  }
81fb726d48Sopenharmony_ci
82fb726d48Sopenharmony_ci  private static handleRequestException(): void {
83fb726d48Sopenharmony_ci    if (SpStatisticsHttpUtil.retryCount >= SpStatisticsHttpUtil.retryMaxCount) {
84fb726d48Sopenharmony_ci      SpStatisticsHttpUtil.pauseRetry = true;
85fb726d48Sopenharmony_ci      if (SpStatisticsHttpUtil.retryRestTimeOut) {
86fb726d48Sopenharmony_ci        return;
87fb726d48Sopenharmony_ci      }
88fb726d48Sopenharmony_ci      SpStatisticsHttpUtil.retryRestTimeOut = true;
89fb726d48Sopenharmony_ci      setTimeout(() => {
90fb726d48Sopenharmony_ci        SpStatisticsHttpUtil.retryCount = 0;
91fb726d48Sopenharmony_ci        SpStatisticsHttpUtil.pauseRetry = false;
92fb726d48Sopenharmony_ci        SpStatisticsHttpUtil.retryRestTimeOut = false;
93fb726d48Sopenharmony_ci      }, 600000);
94fb726d48Sopenharmony_ci    }
95fb726d48Sopenharmony_ci    ++SpStatisticsHttpUtil.retryCount;
96fb726d48Sopenharmony_ci  }
97fb726d48Sopenharmony_ci
98fb726d48Sopenharmony_ci  static addUserVisitAction(requestUrl: string): void {
99fb726d48Sopenharmony_ci    // @ts-ignore
100fb726d48Sopenharmony_ci    if (window.useWb) {
101fb726d48Sopenharmony_ci      return;
102fb726d48Sopenharmony_ci    }
103fb726d48Sopenharmony_ci    if (SpStatisticsHttpUtil.requestServerInfo === '') {
104fb726d48Sopenharmony_ci      SpStatisticsHttpUtil.requestServerInfo = SpStatisticsHttpUtil.getRequestServerInfo();
105fb726d48Sopenharmony_ci    }
106fb726d48Sopenharmony_ci    if (SpStatisticsHttpUtil.pauseRetry) {
107fb726d48Sopenharmony_ci      return;
108fb726d48Sopenharmony_ci    }
109fb726d48Sopenharmony_ci    let visitId = 0;
110fb726d48Sopenharmony_ci    fetch(`https://${SpStatisticsHttpUtil.requestServerInfo}/${requestUrl}`, {
111fb726d48Sopenharmony_ci      method: 'post',
112fb726d48Sopenharmony_ci      headers: {
113fb726d48Sopenharmony_ci        'Content-Type': 'application/json',
114fb726d48Sopenharmony_ci      },
115fb726d48Sopenharmony_ci    })
116fb726d48Sopenharmony_ci      .then((resp) => {
117fb726d48Sopenharmony_ci        resp.text().then((it) => {
118fb726d48Sopenharmony_ci          let res = JSON.parse(it);
119fb726d48Sopenharmony_ci          if (res && res.data) {
120fb726d48Sopenharmony_ci            visitId = res.data.accessId;
121fb726d48Sopenharmony_ci          }
122fb726d48Sopenharmony_ci        });
123fb726d48Sopenharmony_ci      })
124fb726d48Sopenharmony_ci      .catch((err) => { });
125fb726d48Sopenharmony_ci    setTimeout(() => {
126fb726d48Sopenharmony_ci      fetch(`https://${SpStatisticsHttpUtil.requestServerInfo}/${requestUrl}`, {
127fb726d48Sopenharmony_ci        method: 'post',
128fb726d48Sopenharmony_ci        headers: {
129fb726d48Sopenharmony_ci          'Content-Type': 'application/json',
130fb726d48Sopenharmony_ci        },
131fb726d48Sopenharmony_ci        body: JSON.stringify({
132fb726d48Sopenharmony_ci          effectiveAccess: true,
133fb726d48Sopenharmony_ci          visitId: visitId,
134fb726d48Sopenharmony_ci        }),
135fb726d48Sopenharmony_ci      })
136fb726d48Sopenharmony_ci        .catch((err) => { })
137fb726d48Sopenharmony_ci        .then((resp) => { });
138fb726d48Sopenharmony_ci    }, 1800000);
139fb726d48Sopenharmony_ci  }
140fb726d48Sopenharmony_ci
141fb726d48Sopenharmony_ci  static addOrdinaryVisitAction(requestBody: BurialPointRequestBody): void {
142fb726d48Sopenharmony_ci    // @ts-ignore
143fb726d48Sopenharmony_ci    if (window.useWb) {
144fb726d48Sopenharmony_ci      return;
145fb726d48Sopenharmony_ci    }
146fb726d48Sopenharmony_ci    if (SpStatisticsHttpUtil.requestServerInfo === '') {
147fb726d48Sopenharmony_ci      SpStatisticsHttpUtil.requestServerInfo = SpStatisticsHttpUtil.getRequestServerInfo();
148fb726d48Sopenharmony_ci    }
149fb726d48Sopenharmony_ci    if (SpStatisticsHttpUtil.pauseRetry) {
150fb726d48Sopenharmony_ci      return;
151fb726d48Sopenharmony_ci    }
152fb726d48Sopenharmony_ci    requestBody.ts = SpStatisticsHttpUtil.getCorrectRequestTime();
153fb726d48Sopenharmony_ci    if (SpStatisticsHttpUtil.serverTime === 0) {
154fb726d48Sopenharmony_ci      return;
155fb726d48Sopenharmony_ci    }
156fb726d48Sopenharmony_ci    fetch(`https://${SpStatisticsHttpUtil.requestServerInfo}/record`, {
157fb726d48Sopenharmony_ci      method: 'post',
158fb726d48Sopenharmony_ci      headers: {
159fb726d48Sopenharmony_ci        'Content-Type': 'application/json',
160fb726d48Sopenharmony_ci      },
161fb726d48Sopenharmony_ci      body: JSON.stringify(requestBody),
162fb726d48Sopenharmony_ci    })
163fb726d48Sopenharmony_ci      .catch((err) => {
164fb726d48Sopenharmony_ci      })
165fb726d48Sopenharmony_ci      .then((resp) => { });
166fb726d48Sopenharmony_ci  }
167fb726d48Sopenharmony_ci
168fb726d48Sopenharmony_ci  static recordPluginUsage(requsetBody: pluginUsage) {
169fb726d48Sopenharmony_ci    fetch(`https://${SpStatisticsHttpUtil.requestServerInfo}/recordPluginUsage`, {
170fb726d48Sopenharmony_ci      method: 'post',
171fb726d48Sopenharmony_ci      headers: {
172fb726d48Sopenharmony_ci        'Content-Type': 'application/json'
173fb726d48Sopenharmony_ci      },
174fb726d48Sopenharmony_ci      body: JSON.stringify(requsetBody),
175fb726d48Sopenharmony_ci    }).then(res => {
176fb726d48Sopenharmony_ci    }).catch(err => {
177fb726d48Sopenharmony_ci      this.handleRequestException();
178fb726d48Sopenharmony_ci    });
179fb726d48Sopenharmony_ci  }
180fb726d48Sopenharmony_ci
181fb726d48Sopenharmony_ci  static getNotice() {
182fb726d48Sopenharmony_ci    return fetch(`${document.URL}messagePublish`);
183fb726d48Sopenharmony_ci  }
184fb726d48Sopenharmony_ci
185fb726d48Sopenharmony_ci  static getCorrectRequestTime(): number {
186fb726d48Sopenharmony_ci    if (SpStatisticsHttpUtil.serverTime === 0) {
187fb726d48Sopenharmony_ci      SpStatisticsHttpUtil.getServerTime();
188fb726d48Sopenharmony_ci    }
189fb726d48Sopenharmony_ci    return Date.now() + SpStatisticsHttpUtil.timeDiff;
190fb726d48Sopenharmony_ci  }
191fb726d48Sopenharmony_ci
192fb726d48Sopenharmony_ci  // ai对话接口--获取token
193fb726d48Sopenharmony_ci  static async getAItoken() {
194fb726d48Sopenharmony_ci    let token = ''
195fb726d48Sopenharmony_ci    await window.fetch(`https://${window.location.host}/takeToken`, {
196fb726d48Sopenharmony_ci      method: 'post',
197fb726d48Sopenharmony_ci      headers: {
198fb726d48Sopenharmony_ci        'Content-Type': 'application/json'
199fb726d48Sopenharmony_ci      }
200fb726d48Sopenharmony_ci    }).then(async (res) => {
201fb726d48Sopenharmony_ci      let resp = await res.text();
202fb726d48Sopenharmony_ci      let resj = await JSON.parse(resp);
203fb726d48Sopenharmony_ci      token = resj.token;
204fb726d48Sopenharmony_ci    }).catch(() => { });
205fb726d48Sopenharmony_ci    return token;
206fb726d48Sopenharmony_ci  }
207fb726d48Sopenharmony_ci
208fb726d48Sopenharmony_ci  // ai对话接口--问答
209fb726d48Sopenharmony_ci  // @ts-ignore
210fb726d48Sopenharmony_ci  static async askAi(requestBody) {
211fb726d48Sopenharmony_ci    let answer = '';
212fb726d48Sopenharmony_ci    let res = await window.fetch(`https://${window.location.host}/ask`, {
213fb726d48Sopenharmony_ci      method: 'post',
214fb726d48Sopenharmony_ci      headers: {
215fb726d48Sopenharmony_ci        'Content-Type': 'application/json'
216fb726d48Sopenharmony_ci      },
217fb726d48Sopenharmony_ci      body: JSON.stringify(requestBody)
218fb726d48Sopenharmony_ci    })
219fb726d48Sopenharmony_ci    // 用状态码判断statu = 200?
220fb726d48Sopenharmony_ci    let resp = await res.text();
221fb726d48Sopenharmony_ci    let resj = await JSON.parse(resp);
222fb726d48Sopenharmony_ci    answer = resj.chatbot_reply;
223fb726d48Sopenharmony_ci    return answer;
224fb726d48Sopenharmony_ci  }
225fb726d48Sopenharmony_ci}
226fb726d48Sopenharmony_ci
227