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 } 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]}:${ 51 window.location.port 52 }/application/serverInfo`, 53 true 54 ); 55 req.send(null); 56 } catch { 57 warn('Connect Server Failed'); 58 } 59 return ''; 60 } 61 62 static getServerTime(): void { 63 if (SpStatisticsHttpUtil.requestServerInfo === '') { 64 SpStatisticsHttpUtil.requestServerInfo = SpStatisticsHttpUtil.getRequestServerInfo(); 65 } 66 if (SpStatisticsHttpUtil.pauseRetry) { 67 return; 68 } 69 fetch(`https://${SpStatisticsHttpUtil.requestServerInfo}/serverTime`) 70 .then((resp) => { 71 resp.text().then((it) => { 72 if (it && it.length > 0) { 73 SpStatisticsHttpUtil.serverTime = Number(it); 74 SpStatisticsHttpUtil.timeDiff = SpStatisticsHttpUtil.serverTime - Date.now(); 75 } 76 }); 77 }) 78 .catch((e) => { 79 this.handleRequestException(); 80 }); 81 } 82 83 private static handleRequestException(): void { 84 if (SpStatisticsHttpUtil.retryCount >= SpStatisticsHttpUtil.retryMaxCount) { 85 SpStatisticsHttpUtil.pauseRetry = true; 86 if (SpStatisticsHttpUtil.retryRestTimeOut) { 87 return; 88 } 89 SpStatisticsHttpUtil.retryRestTimeOut = true; 90 setTimeout(() => { 91 SpStatisticsHttpUtil.retryCount = 0; 92 SpStatisticsHttpUtil.pauseRetry = false; 93 SpStatisticsHttpUtil.retryRestTimeOut = false; 94 }, 600000); 95 } 96 ++SpStatisticsHttpUtil.retryCount; 97 } 98 99 static addUserVisitAction(requestUrl: string): void { 100 // @ts-ignore 101 if (window.useWb) { 102 return; 103 } 104 if (SpStatisticsHttpUtil.requestServerInfo === '') { 105 SpStatisticsHttpUtil.requestServerInfo = SpStatisticsHttpUtil.getRequestServerInfo(); 106 } 107 if (SpStatisticsHttpUtil.pauseRetry) { 108 return; 109 } 110 let visitId = 0; 111 fetch(`https://${SpStatisticsHttpUtil.requestServerInfo}/${requestUrl}`, { 112 method: 'post', 113 headers: { 114 'Content-Type': 'application/json', 115 }, 116 }) 117 .then((resp) => { 118 resp.text().then((it) => { 119 let res = JSON.parse(it); 120 if (res && res.data) { 121 visitId = res.data.accessId; 122 } 123 }); 124 }) 125 .catch((err) => {}); 126 setTimeout(() => { 127 fetch(`https://${SpStatisticsHttpUtil.requestServerInfo}/${requestUrl}`, { 128 method: 'post', 129 headers: { 130 'Content-Type': 'application/json', 131 }, 132 body: JSON.stringify({ 133 effectiveAccess: true, 134 visitId: visitId, 135 }), 136 }) 137 .catch((err) => {}) 138 .then((resp) => {}); 139 }, 1800000); 140 } 141 142 static addOrdinaryVisitAction(requestBody: BurialPointRequestBody): void { 143 // @ts-ignore 144 if (window.useWb) { 145 return; 146 } 147 if (SpStatisticsHttpUtil.requestServerInfo === '') { 148 SpStatisticsHttpUtil.requestServerInfo = SpStatisticsHttpUtil.getRequestServerInfo(); 149 } 150 if (SpStatisticsHttpUtil.pauseRetry) { 151 return; 152 } 153 requestBody.ts = SpStatisticsHttpUtil.getCorrectRequestTime(); 154 if (SpStatisticsHttpUtil.serverTime === 0) { 155 return; 156 } 157 fetch(`https://${SpStatisticsHttpUtil.requestServerInfo}/record`, { 158 method: 'post', 159 headers: { 160 'Content-Type': 'application/json', 161 }, 162 body: JSON.stringify(requestBody), 163 }) 164 .catch((err) => { 165 this.handleRequestException(); 166 }) 167 .then((resp) => {}); 168 } 169 170 static getCorrectRequestTime(): number { 171 if (SpStatisticsHttpUtil.serverTime === 0) { 172 SpStatisticsHttpUtil.getServerTime(); 173 } 174 return Date.now() + SpStatisticsHttpUtil.timeDiff; 175 } 176} 177