Browse Source

自动初始化中间件拦截 防止误操作无法进入初始化功能

main
pixel 4 years ago
parent
commit
ceee5a8d7f
  1. 3
      server/router/sys_base.go
  2. 10
      web/src/App.vue
  3. 11
      web/src/utils/request.js

3
server/router/sys_base.go

@ -2,11 +2,12 @@ package router
import ( import (
"gin-vue-admin/api/v1" "gin-vue-admin/api/v1"
"gin-vue-admin/middleware"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
func InitBaseRouter(Router *gin.RouterGroup) (R gin.IRoutes) { func InitBaseRouter(Router *gin.RouterGroup) (R gin.IRoutes) {
BaseRouter := Router.Group("base")
BaseRouter := Router.Group("base").Use(middleware.NeedInit())
{ {
BaseRouter.POST("login", v1.Login) BaseRouter.POST("login", v1.Login)
BaseRouter.POST("captcha", v1.Captcha) BaseRouter.POST("captcha", v1.Captcha)

10
web/src/App.vue

@ -9,15 +9,7 @@ import { checkDB } from "@/api/initdb"
export default { export default {
name: 'app', name: 'app',
async created(){ async created(){
const res = await checkDB()
if(res.code == 0 && res.data.needInit){
this.$message({
type:"info",
message:"您是第一次使用,请初始化"
})
this.$store.commit("user/NeedInit")
this.$router.push({name:"init"})
}
await checkDB()
} }
} }
</script> </script>

11
web/src/utils/request.js

@ -2,6 +2,8 @@ import axios from 'axios'; // 引入axios
import { Message } from 'element-ui'; import { Message } from 'element-ui';
import { store } from '@/store/index' import { store } from '@/store/index'
import context from '@/main.js' import context from '@/main.js'
import router from '@/router/index'
const service = axios.create({ const service = axios.create({
baseURL: process.env.VUE_APP_BASE_API, baseURL: process.env.VUE_APP_BASE_API,
timeout: 99999 timeout: 99999
@ -59,10 +61,19 @@ service.interceptors.request.use(
service.interceptors.response.use( service.interceptors.response.use(
response => { response => {
closeLoading() closeLoading()
if (response.headers["new-token"]) { if (response.headers["new-token"]) {
store.commit('user/setToken', response.headers["new-token"]) store.commit('user/setToken', response.headers["new-token"])
} }
if (response.data.code == 0 || response.headers.success === "true") { if (response.data.code == 0 || response.headers.success === "true") {
if(response.data.data.needInit){
Message({
type:"info",
message:"您是第一次使用,请初始化"
})
store.commit("user/NeedInit")
router.push({name:"init"})
}
return response.data return response.data
} else { } else {
Message({ Message({

Loading…
Cancel
Save