From 99fadee7ddfb1df39b0eec8a4b99d329ab8e073a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A4ssin=20Aouani?= Date: Thu, 14 Nov 2024 15:04:36 +0100 Subject: [PATCH 1/2] create new composable and improve stuff --- app.vue | 42 ++++++++-------- composables/useCampus.ts | 43 +++++++++++++++++ middleware/isLoggedIn.global.ts | 2 +- pages/dash/stundenplan.vue | 8 ++-- pages/login.vue | 85 ++++++++++++++------------------- 5 files changed, 105 insertions(+), 75 deletions(-) create mode 100644 composables/useCampus.ts diff --git a/app.vue b/app.vue index 01ecf93..633b78f 100644 --- a/app.vue +++ b/app.vue @@ -7,27 +7,27 @@ diff --git a/composables/useCampus.ts b/composables/useCampus.ts new file mode 100644 index 0000000..58e3af1 --- /dev/null +++ b/composables/useCampus.ts @@ -0,0 +1,43 @@ +import { ref } from 'vue'; + +const baseCampusURL = "https://selfservice.campus-dual.de" +const baseCorsUrl = "https://corsproxy.io/?" + +export function useCampus() { + const username = useCookie("username"); + const password = useCookie("password"); + + async function getCampusData(type: 'room' | 'timeline' | 'credits' | 'semester') { + + const UrlParams = `?userid=${username.value}&hash=${password.value}&t=${Math.floor(Date.now() / 1000)}&_=${Date.now()}` + + switch (type) { + + case 'room': + var urlPath = "/room/json" + break; + case 'timeline': + var urlPath = "/dash/gettimeline" + break; + case 'credits': + var urlPath = "/dash/getcp" + break; + case 'semester': + var urlPath = "/dash/getfs" + break; + + default: + throw new Error('Invalid type'); + } + + const url = baseCorsUrl + encodeURIComponent(baseCampusURL + urlPath + UrlParams) + + return await $fetch(url); + } + + return { + username, + password, + getCampusData + }; +} \ No newline at end of file diff --git a/middleware/isLoggedIn.global.ts b/middleware/isLoggedIn.global.ts index 0d56dcf..6042bd5 100644 --- a/middleware/isLoggedIn.global.ts +++ b/middleware/isLoggedIn.global.ts @@ -13,7 +13,7 @@ export default defineNuxtRouteMiddleware(async (to, from) => { try { const test_url = `https://corsproxy.io/?https%3A%2F%2Fselfservice.campus-dual.de%2Fdash%2Fgetcp%3Fuser%3D${username.value}%26hash%3D${password.value}` const response = await $fetch(test_url) - isLoggedIn.value = !!(response == 0) + isLoggedIn.value = !!(typeof response === 'number') } catch (error) { isLoggedIn.value = false } diff --git a/pages/dash/stundenplan.vue b/pages/dash/stundenplan.vue index 2ee90ca..38baa1a 100644 --- a/pages/dash/stundenplan.vue +++ b/pages/dash/stundenplan.vue @@ -42,6 +42,8 @@ definePageMeta({ key: route => route.fullPath, }); +const { getCampusData } = useCampus() + const router = useRouter(); const localData = ref([]); @@ -117,11 +119,7 @@ onMounted(async () => { localStorage.setItem('stundenplan', JSON.stringify(newValue)); }, { deep: true }); - - - const response = await $fetch( - `https://corsproxy.io/?https%3A%2F%2Fselfservice.campus-dual.de%2Froom%2Fjson%3Fuserid%3D${username.value}%26hash%3D${password.value}%26t%3D${Math.floor(Date.now() / 1000)}%26_%3D${Date.now()}` - ); + const response = await getCampusData('room'); data.value = response; localData.value = data.value; diff --git a/pages/login.vue b/pages/login.vue index 3ff2fa4..50d844c 100644 --- a/pages/login.vue +++ b/pages/login.vue @@ -8,27 +8,16 @@
- +

Bitte geben Sie einen Benutzernamen ein.

- +

Bitte geben Sie ein Passwort ein.

- Login + Login +

Der Login ist fehlgeschlagen. Überprüfe ob du tatsächlich deinen aktuellen Hash von der CampusDual API hast. @@ -37,48 +26,48 @@ From 25a9011675963273420cabb202bcf4ddbd4d2fd4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A4ssin=20Aouani?= Date: Wed, 27 Nov 2024 08:38:25 +0100 Subject: [PATCH 2/2] dont reset data if its the same as cached on 1st load --- pages/dash/stundenplan.vue | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/pages/dash/stundenplan.vue b/pages/dash/stundenplan.vue index 38baa1a..17828ee 100644 --- a/pages/dash/stundenplan.vue +++ b/pages/dash/stundenplan.vue @@ -121,8 +121,21 @@ onMounted(async () => { const response = await getCampusData('room'); - data.value = response; - localData.value = data.value; + function arraysEqual(a, b) { + if (a === b) return true; + if (a == null || b == null) return false; + if (a.length !== b.length) return false; + + for (var i = 0; i < a.length; ++i) { + if (JSON.stringify(a[i]) !== JSON.stringify(b[i])) return false; + } + return true; + } + + if (!arraysEqual(JSON.parse(JSON.stringify(localData.value)), response)) { + data.value = response; + localData.value = data.value; + } hasLoaded.value = true; });