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() { 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.open( 41 'GET', 42 `${window.location.protocol}//${window.location.host.split(':')[0]}:${ 43 window.location.port 44 }/application/serverInfo`, 45 true 46 ); 47 req.send(null); 48 if (req.status == 200) { 49 let requestInfo = req.getResponseHeader('request_info'); 50 if (requestInfo && requestInfo.length > 0) { 51 return requestInfo; 52 } 53 } 54 } catch { 55 warn('Connect Server Failed') 56 } 57 58 return ''; 59 } 60 61 static getServerTime() { 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() { 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) { 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) { 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 this.handleRequestException(); 165 }) 166 .then((resp) => {}); 167 } 168 169 static getCorrectRequestTime(): number { 170 if (SpStatisticsHttpUtil.serverTime === 0) { 171 SpStatisticsHttpUtil.getServerTime(); 172 } 173 return Date.now() + SpStatisticsHttpUtil.timeDiff; 174 } 175} 176