diff --git a/layouts/default.vue b/layouts/default.vue index a51a97b..45348dd 100644 --- a/layouts/default.vue +++ b/layouts/default.vue @@ -570,36 +570,45 @@ async function logout() { }); } +// 🔐 Token 狀態管理(確保 SSR 不報錯) +const authToken = useState("authToken", () => process.client ? localStorage.getItem("token_TCM") || "" : ""); -var token; // 加上分號 -const token_check = ()=>{ - // 检查本地存储中是否存在JWT令牌 - token = localStorage.getItem("token_TCM"); - console.log(token); - // 確認有無token - if (token != null) { - $.ajax({ - type: "GET", - url: $api_host + "/Users/token_check_user", +// 🔍 檢查 Token +const token_check = async () => { + if (!process.client) return; // 確保只在 client-side 執行 + + const token = localStorage.getItem("token_TCM"); + console.log("Token:", token); + + if (!token) { + console.log("無 Token,轉跳首頁"); + router.push("/Home_pages/"); + return; + } + + try { + const response = await fetch(`${$api_host}/Users/token_check_user`, { + method: "GET", headers: { Authorization: token, }, - success: function (response) { - // 若層級為8 則無法登入 - if(response.level<=8 ){ - router.push("/Home_pages/"); - } - }, - error: function (xhr) { - console.log(xhr); - }, }); - } else { - console.log("pass"); + + if (!response.ok) throw new Error("Token 驗證失敗"); + + const data = await response.json(); + console.log("驗證成功:", data); + + // 若 level ≤ 8 則轉跳首頁 + if (data.level <= 8) { + router.push("/Home_pages/"); + } + } catch (error) { + console.error("Token 驗證失敗", error); router.push("/Home_pages/"); } -} +}; -// 自啟 +// 🔄 在頁面載入時執行 onMounted(token_check); diff --git a/layouts/defaultmanager.vue b/layouts/defaultmanager.vue index 82632f0..ddc7485 100644 --- a/layouts/defaultmanager.vue +++ b/layouts/defaultmanager.vue @@ -568,36 +568,46 @@ async function logout() { }); } -var token; // 加上分號 -const token_check = ()=>{ - // 检查本地存储中是否存在JWT令牌 - token = localStorage.getItem("token_TCM"); - console.log(token); - // 確認有無token - if (token != null) { - $.ajax({ - type: "GET", - url: $api_host + "/Users/token_check_user", +// 🔐 Token 狀態管理(確保 SSR 不報錯) +const authToken = useState("authToken", () => process.client ? localStorage.getItem("token_TCM") || "" : ""); + +// 🔍 檢查 Token +const token_check = async () => { + if (!process.client) return; // 確保只在 client-side 執行 + + const token = localStorage.getItem("token_TCM"); + console.log("Token:", token); + + if (!token) { + console.log("無 Token,轉跳首頁"); + router.push("/Home_pages/"); + return; + } + + try { + const response = await fetch(`${$api_host}/Users/token_check_user`, { + method: "GET", headers: { Authorization: token, }, - success: function (response) { - // 若層級為8 則無法登入 - if(response.level<=6 ){ - router.push("/Home_pages/"); - } - }, - error: function (xhr) { - console.log(xhr); - }, }); - } else { - console.log("pass"); + + if (!response.ok) throw new Error("Token 驗證失敗"); + + const data = await response.json(); + console.log("驗證成功:", data); + + // 若 level ≤ 6 則轉跳首頁 + if (data.level <= 6) { + router.push("/Home_pages/"); + } + } catch (error) { + console.error("Token 驗證失敗", error); router.push("/Home_pages/"); } -} +}; - -// 自啟 +// 🔄 在頁面載入時執行 onMounted(token_check); + diff --git a/pages/Lamiter_pages/Company-[guid].vue b/pages/Lamiter_pages/Company-[guid].vue index e5ba81a..2e1c20e 100644 --- a/pages/Lamiter_pages/Company-[guid].vue +++ b/pages/Lamiter_pages/Company-[guid].vue @@ -41,7 +41,7 @@
- @@ -65,7 +65,8 @@ import { ref, onMounted } from 'vue' import { useRouter } from "vue-router"; //匯入路徑 const { $api_host } = useNuxtApp(); //匯入API host const router = useRouter(); // 匯入 - +// 🔐 定義全域 token +const authToken = ref(""); //編輯企業 @@ -80,32 +81,57 @@ function close_edit_Company_view() { edit_Company_view.value?.close(); } -const company_name=ref(""); + //獲取資料 const get_data = async () => { - const now_route = useRoute(); // 取得當前路由資訊 - const company_guid = now_route.params.guid + const Company_data = await get_company_data() + set_Company(Company_data); +}; + + +// 獲取公司資料 +const get_company_data= async () =>{ + const route = useRoute(); // 取得當前路由資訊 + const company_guid = route.params.guid; // 提取路由中的 guid 參數 + + // 確保 company_guid 存在 + if (!company_guid) { + console.error("沒有找到公司 GUID!"); + return; + } + try { - const response = await fetch(`${$api_host}/api/Company_detail_table/get_campany-${company_guid}`, { + const response = await $fetch(`${$api_host}/api/Company_detail_table/get_campany-${company_guid}`, { method: "GET", headers: { "Content-Type": "application/json", + Authorization: authToken.value, // 假設 authToken 已經在其他地方定義 }, }); - if (!response.ok) throw new Error(`HTTP 錯誤! 狀態碼: ${response.status}`); - - const data = await response.json(); - company_name.value=data.company_name - + console.log("✅ 獲取成功", response); + return response } catch (error) { console.error("獲取公司資料失敗:", error); } +} -}; +//設置資料 +const company_name=ref(""); +const set_Company=(data)=>{ + company_name.value =data.company_name +} // 自啟 -onMounted(get_data); +// 🚀 在 mounted 時獲取 token +onMounted(() => { + if (process.client) { + // 讀取 localStorage 並將 token 設置到 reactive 變數中 + authToken.value = localStorage.getItem("token_TCM") || ""; + } + + get_data(); // 確保 token 讀取後再調用 API +}); \ No newline at end of file diff --git a/pages/Lamiter_pages/Company_Control_page.vue b/pages/Lamiter_pages/Company_Control_page.vue index b103c2b..57ff1a5 100644 --- a/pages/Lamiter_pages/Company_Control_page.vue +++ b/pages/Lamiter_pages/Company_Control_page.vue @@ -28,7 +28,7 @@
- +
@@ -69,24 +69,27 @@
- diff --git a/pages/Manage_pages/Health_Control_page.vue b/pages/Manage_pages/Health_Control_page.vue index ab84d13..7fcd22b 100644 --- a/pages/Manage_pages/Health_Control_page.vue +++ b/pages/Manage_pages/Health_Control_page.vue @@ -5,15 +5,15 @@

-
-
+
+
健管師名稱:
-
{{ health.health_name }}
+
{{ healther.health_name }}
@@ -68,6 +68,8 @@ import { ref, onMounted } from 'vue' import { useRouter } from "vue-router"; //匯入路徑 const { $api_host } = useNuxtApp(); //匯入API host const router = useRouter(); // 匯入 +const authToken = ref("");// 🔐 定義全域 token + const add_Health_view = ref(null); definePageMeta({ @@ -75,7 +77,7 @@ definePageMeta({ }) //新增健管師 -async function add_health_manager() { +const add_health_manager = async () => { healthName.value = ""; healthPhone.value = ""; healthaccount.value = ""; @@ -84,7 +86,7 @@ async function add_health_manager() { } //關閉 <新增健管師-彈跳視窗> -function close_add_Health_view() { +const close_add_Health_view = () => { add_Health_view.value?.close(); } @@ -93,31 +95,30 @@ const healthName = ref(""); const healthPhone = ref(""); const healthaccount = ref(""); const healthpassword = ref(""); -async function submitCompany() { +const submitHealth = async () => { try { const healthData = { - lastname: companyName.value, - username: companyaccount.value, - password: companypassword.value + lastname: healthName.value, + username: healthaccount.value, + password: healthpassword.value, + company_guid: company_guid.value }; - console.log(companyData) + console.log(healthData) - const response = await fetch(`${$api_host}/api/Company_detail_table/Add_campany`, { + const response = await $fetch(`${$api_host}/api/Health_detail_table/Add_health`, { method: 'POST', headers: { - 'Content-Type': 'application/json' + 'Content-Type': 'application/json', + Authorization: authToken.value, }, - body: JSON.stringify(companyData) + body: JSON.stringify(healthData) }); - - if (!response.ok) { - throw new Error(`HTTP error! Status: ${response.status}`); - } - add_Company_view.value?.close(); + add_Health_view.value?.close(); alert('上傳成功!'); get_data(); } catch (error) { + console.error(error); alert('上傳失敗,請檢查後端!'); } @@ -125,42 +126,72 @@ async function submitCompany() { -const company_guid = ref ("") +const company_guid = ref("") //企業GUID //獲取資料 const get_data = async () => { - const authToken = useState("authToken", () => process.client ? localStorage.getItem("token_TCM") || "" : ""); - console.log(authToken) - await get_company_basic_data() - await get_all_health_data() + await get_company_basic_data(); + const All_Health_data = await get_all_health_data(); + set_All_Health_data(All_Health_data); } + // 獲取企業基礎資料 const get_company_basic_data = async () => { try { - const response = await fetch(`${$api_host}/Users/token_check_user`, { + const response = await $fetch(`${$api_host}/Users/token_check_user`, { + method: 'GET', headers: { - Authorization: token, + "Content-Type": "application/json", + Authorization: authToken.value, }, }); - if (!response.ok) throw new Error(`HTTP 錯誤! 狀態碼: ${response.status}`); - const data = await response.json(); - company_guid.value = data.guid + company_guid.value = response.guid } catch (error) { console.error('企業基礎資料失敗:', error); } } + // 獲取所有健康管理師資料 -const get_all_health_data = async()=>{ - console.log(company_guid.value) +const get_all_health_data = async () => { + + try { + const response = await $fetch(`${$api_host}/api/Health_detail_table/get_company_all_health-${company_guid.value}`, { + method: 'GET', + headers: { + "Content-Type": "application/json", + Authorization: authToken.value, + }, + }); + return response; + } catch (error) { + console.error(error); + } +} + +//設置資料 +const healthers = ref([]); +const set_All_Health_data = (data) => { + console.log(data) + healthers.value = data } -// 自啟 -onMounted(get_data); +// 自啟 +// 🚀 在 mounted 時獲取 token +onMounted(() => { + if (process.client) { + // 讀取 localStorage 並將 token 設置到 reactive 變數中 + authToken.value = localStorage.getItem("token_TCM") || ""; + } + + get_data(); // 確保 token 讀取後再調用 API +}); + +