From f54b618b28353233b0343daeebea679f87233c61 Mon Sep 17 00:00:00 2001 From: suyanyan <1321717064@qq.com> Date: Mon, 9 Dec 2024 13:42:30 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=8E=88=E6=9D=83=E9=A1=B5?= =?UTF-8?q?=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/api/modules/sys/auth/auth.ts | 19 ++++++ .../src/api/modules/sys/auth/index.ts | 1 + SafeCampus.WEB/src/api/request/instance.ts | 25 ++++--- SafeCampus.WEB/src/config/index.ts | 3 + SafeCampus.WEB/src/routers/index.ts | 8 ++- .../src/routers/modules/staticRouter.ts | 14 +++- SafeCampus.WEB/src/views/login/auth.vue | 68 +++++++++++++++++++ 7 files changed, 123 insertions(+), 15 deletions(-) create mode 100644 SafeCampus.WEB/src/api/modules/sys/auth/auth.ts create mode 100644 SafeCampus.WEB/src/views/login/auth.vue diff --git a/SafeCampus.WEB/src/api/modules/sys/auth/auth.ts b/SafeCampus.WEB/src/api/modules/sys/auth/auth.ts new file mode 100644 index 0000000..3f79dfd --- /dev/null +++ b/SafeCampus.WEB/src/api/modules/sys/auth/auth.ts @@ -0,0 +1,19 @@ +/** + * @description 授权模块接口 + */ + +import { moduleRequest } from "@/api/request"; +const http = moduleRequest("/sys/auth/activate/"); + +const authsApi = { + /** 获取机器码 */ + machineCode() { + return http.post("machineCode", {}, { loading: false }); // 正常 post json 请求 = application/json + }, + /** 激活 */ + activation(params: any) { + return http.post("activation", JSON.stringify(params), { loading: false, headers: { "Content-Type": "application/json" } }); + } +}; + +export { authsApi }; diff --git a/SafeCampus.WEB/src/api/modules/sys/auth/index.ts b/SafeCampus.WEB/src/api/modules/sys/auth/index.ts index 070b49d..19c0794 100644 --- a/SafeCampus.WEB/src/api/modules/sys/auth/index.ts +++ b/SafeCampus.WEB/src/api/modules/sys/auth/index.ts @@ -13,3 +13,4 @@ * @see https://gitee.com/dotnetmoyu/SimpleAdmin */ export * from "./login"; +export * from "./auth"; diff --git a/SafeCampus.WEB/src/api/request/instance.ts b/SafeCampus.WEB/src/api/request/instance.ts index afe2c66..449d9e7 100644 --- a/SafeCampus.WEB/src/api/request/instance.ts +++ b/SafeCampus.WEB/src/api/request/instance.ts @@ -14,7 +14,7 @@ */ import axios, { AxiosInstance, AxiosError, AxiosRequestConfig, InternalAxiosRequestConfig, AxiosResponse } from "axios"; import { showFullScreenLoading, tryHideFullScreenLoading } from "@/components/Loading/fullScreen"; -import { LOGIN_URL } from "@/config"; +import { LOGIN_URL, AUTH_URL } from "@/config"; import { ElMessage } from "element-plus"; import { ResultData } from "@/api/interface"; import { ResultEnum, TokenEnum } from "@/enums"; @@ -149,15 +149,20 @@ export default class RequestHttp { }, async (error: AxiosError) => { const { response } = error; - tryHideFullScreenLoading(); - // 请求超时 && 网络错误单独判断,没有 response - if (error.message.indexOf("timeout") !== -1) ElMessage.error("请求超时!请您稍后重试"); - if (error.message.indexOf("Network Error") !== -1) ElMessage.error("网络错误!请您稍后重试"); - // 根据服务器响应的错误状态码,做不同的处理 - if (response) checkStatus(response.status); - // 服务器结果都没有返回(可能服务器错误可能客户端断网),断网处理:可以跳转到断网页面 - if (!window.navigator.onLine) router.replace("/500"); - return Promise.reject(error); + console.log(response, "err"); + if (response?.status === 421) { + router.replace(AUTH_URL); + } else { + tryHideFullScreenLoading(); + // 请求超时 && 网络错误单独判断,没有 response + if (error.message.indexOf("timeout") !== -1) ElMessage.error("请求超时!请您稍后重试"); + if (error.message.indexOf("Network Error") !== -1) ElMessage.error("网络错误!请您稍后重试"); + // 根据服务器响应的错误状态码,做不同的处理 + if (response) checkStatus(response.status); + // 服务器结果都没有返回(可能服务器错误可能客户端断网),断网处理:可以跳转到断网页面 + if (!window.navigator.onLine) router.replace("/500"); + return Promise.reject(error); + } } ); } diff --git a/SafeCampus.WEB/src/config/index.ts b/SafeCampus.WEB/src/config/index.ts index dd5fccd..d856138 100644 --- a/SafeCampus.WEB/src/config/index.ts +++ b/SafeCampus.WEB/src/config/index.ts @@ -19,6 +19,9 @@ export const HOME_URL: string = "/home/index"; // 登录页地址(默认) export const LOGIN_URL: string = "/login"; +// 授权页地址(默认) +export const AUTH_URL: string = "/auth"; + // 登录页地址(默认) export const USER_CENTER_URL: string = "/userCenter"; diff --git a/SafeCampus.WEB/src/routers/index.ts b/SafeCampus.WEB/src/routers/index.ts index c361d7f..4f2b957 100644 --- a/SafeCampus.WEB/src/routers/index.ts +++ b/SafeCampus.WEB/src/routers/index.ts @@ -15,7 +15,7 @@ import { createRouter, createWebHashHistory, createWebHistory } from "vue-router"; import { useUserStore } from "@/stores/modules/user"; import { useAuthStore } from "@/stores/modules/auth"; -import { LOGIN_URL, ROUTER_WHITE_LIST } from "@/config"; +import { LOGIN_URL, AUTH_URL, ROUTER_WHITE_LIST } from "@/config"; import { initDynamicRouter } from "@/routers/modules/dynamicRouter"; import { staticRouter, errorRouter } from "@/routers/modules/staticRouter"; import NProgress from "@/config/nprogress"; @@ -80,7 +80,11 @@ router.beforeEach(async (to, from, next) => { // 5.判断是否有 Token,没有重定向到 login 页面 if (!userStore.accessToken) { - return next({ path: LOGIN_URL, replace: true }); + if (to.path.toLocaleLowerCase() === AUTH_URL) { + return next(); + } else { + return next({ path: LOGIN_URL, replace: true }); + } } // 6.如果没有菜单列表,就重新请求菜单列表并添加动态路由 diff --git a/SafeCampus.WEB/src/routers/modules/staticRouter.ts b/SafeCampus.WEB/src/routers/modules/staticRouter.ts index f6570d0..5f0f69b 100644 --- a/SafeCampus.WEB/src/routers/modules/staticRouter.ts +++ b/SafeCampus.WEB/src/routers/modules/staticRouter.ts @@ -13,7 +13,7 @@ * @see https://gitee.com/dotnetmoyu/SimpleAdmin */ import { RouteRecordRaw } from "vue-router"; -import { HOME_URL, LOGIN_URL } from "@/config"; +import { HOME_URL, LOGIN_URL, AUTH_URL } from "@/config"; /** * staticRouter (静态路由) @@ -31,6 +31,14 @@ export const staticRouter: RouteRecordRaw[] = [ title: "登录" } }, + { + path: AUTH_URL, + name: "auth", + component: () => import("@/views/login/auth.vue"), + meta: { + title: "授权" + } + }, { path: "/layout", name: "layout", @@ -63,7 +71,7 @@ export const staticRouter: RouteRecordRaw[] = [ component: () => import("@/views/screen/index.vue"), meta: { title: "AI智能预警分析平台" - }, + } }, { name: "AI智能预警分析平台-智慧课堂", @@ -80,7 +88,7 @@ export const staticRouter: RouteRecordRaw[] = [ }, path: "/screen/stureturn", component: () => import("@/views/screen/stureturn.vue") - }, + } ]; /** diff --git a/SafeCampus.WEB/src/views/login/auth.vue b/SafeCampus.WEB/src/views/login/auth.vue new file mode 100644 index 0000000..2ccbadc --- /dev/null +++ b/SafeCampus.WEB/src/views/login/auth.vue @@ -0,0 +1,68 @@ +<template> + <div class="authStyle"> + <el-form ref="SysDormitoryFormRef" :rules="rules" :model="authForm" label-width="auto" label-suffix=" :" label-position="top"> + <div> + <el-row :gutter="20"> + <el-col :span="16" :offset="4"> + <s-form-item label="机器码" prop="machineCode"> + <el-input v-model="authForm.machineCode" :autosize="{ minRows: 10 }" :disabled="true" type="textarea" placeholder=" " /> + </s-form-item> + <s-form-item label="激活码" prop="activationCode"> + <el-input v-model="authForm.activationCode" :autosize="{ minRows: 10 }" type="textarea" placeholder=" " /> + </s-form-item> + <div class="btns" style="text-align: center"><el-button type="primary" @click="handleSubmit"> 激 活 </el-button></div> + </el-col> + </el-row> + </div> + </el-form> + </div> +</template> +<script setup lang="ts" name="auth"> +import { required } from "@/utils/formRules"; +import { FormInstance } from "element-plus"; +import { LOGIN_URL } from "@/config"; +import { authsApi } from "@/api"; +import { useRouter } from "vue-router"; +const router = useRouter(); +// 表单验证规则 +const rules = reactive({ + activationCode: [required("请输入激活码")] +}); +const authForm = reactive({ + machineCode: "", + activationCode: "" +}); +const SysDormitoryFormRef = ref<FormInstance>(); +const handleSubmit = () => { + SysDormitoryFormRef.value?.validate(async valid => { + if (!valid) return; //表单验证失败 + await authsApi + .activation(authForm.activationCode) + .then(() => { + ElMessage({ + message: "激活成功", + type: "success" + }); + router.push(LOGIN_URL); + }) + .catch((err: any) => { + ElMessage({ + message: err.msg, + type: "error" + }); + }); + }); +}; +onMounted(() => { + authsApi.machineCode().then((res: any) => { + authForm.machineCode = res.data; + }); +}); +</script> +<style lang="scss" scoped> +.authStyle { + padding: 10px; + background-color: #ffffff; + border-radius: 5px; +} +</style>