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