- Catalogo: Aggiunta di Schede

This commit is contained in:
Surya Paolo
2024-10-31 23:23:06 +01:00
parent b6f73019fe
commit 2ea6468100
123 changed files with 3382 additions and 3595327 deletions

View File

@@ -109,7 +109,7 @@ export const Api = {
// Reset the refresh token if it was reset by the server
if (response.data.refreshToken) {
// console.log('salva refreshtoken', response.data.refreshToken)
userStore.setRefreshToken(response.data.refreshToken)
userStore.setRefreshToken(response.data.refreshToken)
}
// Return the new access token
@@ -131,27 +131,22 @@ export const Api = {
},
async SendReq(url: string, method: string, mydata: any, setAuthToken = false, evitaloop = false): Promise<Types.AxiosSuccess | Types.AxiosError> {
async SendReqBase(url: string, method: string, mydata: any, setAuthToken = false, evitaloop = false): Promise<Types.AxiosSuccess | Types.AxiosError> {
const mydataout = {
...mydata,
keyappid: process.env.PAO_APP_ID,
idapp: process.env.APP_ID,
}
// console.log('INIZIO - SendReq', url)
// console.log('mydata', mydata)
const userStore = useUserStore()
const globalStore = useGlobalStore()
const $router = useRouter()
userStore.setServerCode(tools.EMPTY)
userStore.setResStatus(0)
// eslint-disable-next-line @typescript-eslint/no-misused-promises
return new Promise((resolve, reject) => sendRequest(url, method, mydataout)
.then((res) => {
// console.log('status:', res.status)
setTimeout(() => {
if (method === 'get') {
globalStore.connData.downloading_server = 0
@@ -164,11 +159,8 @@ export const Api = {
if (res.status) {
userStore.setResStatus(res.status)
if (res.status === serv_constants.RIS_CODE__HTTP_FORBIDDEN_INVALID_TOKEN) {
// Forbidden
// You probably is connectiong with other page...
userStore.setServerCode(toolsext.ERR_AUTHENTICATION)
userStore.setAuth('', '')
// $router.push('/signin')
return reject({ code: toolsext.ERR_AUTHENTICATION })
}
}
@@ -187,41 +179,92 @@ export const Api = {
}, 1000)
if (error.status === serv_constants.RIS_CODE__HTTP_FORBIDDEN_TOKEN_EXPIRED) {
// console.log('Token Expired')
// Prova ad ottenere un nuovo token di accesso
try {
// Se il token è scaduto, allora faccio la richiesta di un NUOVO TOKEN, passandogli refreshToken
const newAccessToken = await this.refreshToken();
if (newAccessToken) {
userStore.setAuth(newAccessToken, userStore.refreshToken);
// Riprova l'originale SendReq con il nuovo token.
// Assicurati di evitare un loop infinito in caso di errori continui
if (!evitaloop)
return resolve(this.SendReq(url, method, mydata, setAuthToken, true));
} else {
$router.push('/signin')
}
} catch (err2: any) {
// Gestisci errore di refresh token (es. redirect a signin)
console.error('err2', err2)
if (err2 && err2.hasOwnProperty('code') && err2.code === serv_constants.RIS_CODE__HTTP_FORBIDDEN_INVALID_TOKEN) {
// Forbidden
// You probably is connectiong with other page...
if (err2?.code === serv_constants.RIS_CODE__HTTP_FORBIDDEN_INVALID_TOKEN) {
userStore.setServerCode(toolsext.ERR_AUTHENTICATION)
userStore.setAuth('', '')
return reject({ code: toolsext.ERR_AUTHENTICATION })
}
// return reject(err2);
}
}
console.log('ERROR', error)
return reject(error)
}))
},
/**
* Creates a Promise that resolves after a specified number of milliseconds.
* Useful for creating delayed operations or pause in async functions.
*
* @param ms - The number of milliseconds to delay
* @returns A Promise that resolves after the specified delay
*
* @example
* // Basic usage
* await delay(1000); // waits for 1 second
*
* @example
* // Usage in an async function
* async function example() {
* console.log('Start');
* await delay(2000);
* console.log('2 seconds later');
* }
*
* @example
* // Usage with Promise chaining
* delay(1000).then(() => console.log('1 second passed'));
*/
async delay (ms: number): Promise<void> {
// Input validation
if (ms < 0) {
throw new Error('Delay time cannot be negative');
}
return new Promise(resolve => setTimeout(resolve, ms));
},
async SendReq(
url: string,
method: string,
mydata: any,
setAuthToken = false,
evitaloop = false,
retryCount = 5,
retryDelay = 3000
): Promise<Types.AxiosSuccess | Types.AxiosError> {
try {
const response = await this.SendReqBase(url, method, mydata, setAuthToken, evitaloop);
return response;
} catch (error) {
if (retryCount > 0) {
console.log(`❌❌❌ Retrying request. Attempts remaining: ${retryCount}`);
await this.delay(retryDelay);
return this.SendReq(
url,
method,
mydata,
setAuthToken,
evitaloop,
retryCount - 1,
retryDelay
);
}
throw error;
}
},
async syncAlternative(mystrparam: string) {
console.log('[ALTERNATIVE Background syncing', mystrparam)