@@ -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 }; |
@@ -13,3 +13,4 @@ | |||||
* @see https://gitee.com/dotnetmoyu/SimpleAdmin | * @see https://gitee.com/dotnetmoyu/SimpleAdmin | ||||
*/ | */ | ||||
export * from "./login"; | export * from "./login"; | ||||
export * from "./auth"; |
@@ -14,7 +14,7 @@ | |||||
*/ | */ | ||||
import axios, { AxiosInstance, AxiosError, AxiosRequestConfig, InternalAxiosRequestConfig, AxiosResponse } from "axios"; | import axios, { AxiosInstance, AxiosError, AxiosRequestConfig, InternalAxiosRequestConfig, AxiosResponse } from "axios"; | ||||
import { showFullScreenLoading, tryHideFullScreenLoading } from "@/components/Loading/fullScreen"; | 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 { ElMessage } from "element-plus"; | ||||
import { ResultData } from "@/api/interface"; | import { ResultData } from "@/api/interface"; | ||||
import { ResultEnum, TokenEnum } from "@/enums"; | import { ResultEnum, TokenEnum } from "@/enums"; | ||||
@@ -149,15 +149,20 @@ export default class RequestHttp { | |||||
}, | }, | ||||
async (error: AxiosError) => { | async (error: AxiosError) => { | ||||
const { response } = error; | 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); | |||||
} | |||||
} | } | ||||
); | ); | ||||
} | } | ||||
@@ -19,6 +19,9 @@ export const HOME_URL: string = "/home/index"; | |||||
// 登录页地址(默认) | // 登录页地址(默认) | ||||
export const LOGIN_URL: string = "/login"; | export const LOGIN_URL: string = "/login"; | ||||
// 授权页地址(默认) | |||||
export const AUTH_URL: string = "/auth"; | |||||
// 登录页地址(默认) | // 登录页地址(默认) | ||||
export const USER_CENTER_URL: string = "/userCenter"; | export const USER_CENTER_URL: string = "/userCenter"; | ||||
@@ -15,7 +15,7 @@ | |||||
import { createRouter, createWebHashHistory, createWebHistory } from "vue-router"; | import { createRouter, createWebHashHistory, createWebHistory } from "vue-router"; | ||||
import { useUserStore } from "@/stores/modules/user"; | import { useUserStore } from "@/stores/modules/user"; | ||||
import { useAuthStore } from "@/stores/modules/auth"; | 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 { initDynamicRouter } from "@/routers/modules/dynamicRouter"; | ||||
import { staticRouter, errorRouter } from "@/routers/modules/staticRouter"; | import { staticRouter, errorRouter } from "@/routers/modules/staticRouter"; | ||||
import NProgress from "@/config/nprogress"; | import NProgress from "@/config/nprogress"; | ||||
@@ -80,7 +80,11 @@ router.beforeEach(async (to, from, next) => { | |||||
// 5.判断是否有 Token,没有重定向到 login 页面 | // 5.判断是否有 Token,没有重定向到 login 页面 | ||||
if (!userStore.accessToken) { | 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.如果没有菜单列表,就重新请求菜单列表并添加动态路由 | // 6.如果没有菜单列表,就重新请求菜单列表并添加动态路由 | ||||
@@ -13,7 +13,7 @@ | |||||
* @see https://gitee.com/dotnetmoyu/SimpleAdmin | * @see https://gitee.com/dotnetmoyu/SimpleAdmin | ||||
*/ | */ | ||||
import { RouteRecordRaw } from "vue-router"; | import { RouteRecordRaw } from "vue-router"; | ||||
import { HOME_URL, LOGIN_URL } from "@/config"; | |||||
import { HOME_URL, LOGIN_URL, AUTH_URL } from "@/config"; | |||||
/** | /** | ||||
* staticRouter (静态路由) | * staticRouter (静态路由) | ||||
@@ -31,6 +31,14 @@ export const staticRouter: RouteRecordRaw[] = [ | |||||
title: "登录" | title: "登录" | ||||
} | } | ||||
}, | }, | ||||
{ | |||||
path: AUTH_URL, | |||||
name: "auth", | |||||
component: () => import("@/views/login/auth.vue"), | |||||
meta: { | |||||
title: "授权" | |||||
} | |||||
}, | |||||
{ | { | ||||
path: "/layout", | path: "/layout", | ||||
name: "layout", | name: "layout", | ||||
@@ -63,7 +71,7 @@ export const staticRouter: RouteRecordRaw[] = [ | |||||
component: () => import("@/views/screen/index.vue"), | component: () => import("@/views/screen/index.vue"), | ||||
meta: { | meta: { | ||||
title: "AI智能预警分析平台" | title: "AI智能预警分析平台" | ||||
}, | |||||
} | |||||
}, | }, | ||||
{ | { | ||||
name: "AI智能预警分析平台-智慧课堂", | name: "AI智能预警分析平台-智慧课堂", | ||||
@@ -80,7 +88,7 @@ export const staticRouter: RouteRecordRaw[] = [ | |||||
}, | }, | ||||
path: "/screen/stureturn", | path: "/screen/stureturn", | ||||
component: () => import("@/views/screen/stureturn.vue") | component: () => import("@/views/screen/stureturn.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> |