当前位置:   article > 正文

SpringBoot + Vue 用户登入token 之租房管理系统后台 模板_vue好客租房项目登录接口

vue好客租房项目登录接口

SpringBoot 项目构建:SpringBoot + MySQL8 + MyBatis-Plus + Redis +定时任务框架(elastic-job) + Spring Security + JWT(前后端分离Token)

csdn 下载地址:https://download.csdn.net/download/zhouzhiwengang/16136915

Vue项目构建:Vue + Vue-Route +Vuex + Element-UI + Axios 项目管理后台模板

Vue项目创建:

  1. #创建一个基于webpack模板的新项目
  2. vue init webpack C:\node_workspace\two
  3. # 切换至项目路径
  4. cd C:\node_workspace\two
  5. # 安装项目依赖文件
  6. cnpm install
  7. # 项目启动
  8. cnpm run dev

Vue 项目集成Vuex + Element-UI + Axios框架依赖

  1. C:\node_workspace\two>cnpm i element-ui -S
  2. √ Installed 1 packages
  3. √ Linked 8 latest versions
  4. Run 0 scripts
  5. All packages installed (6 packages installed from npm registry, used 4s(network 4s), speed 21.08kB/s, json 9(81.21kB), tarball 0B)
  6. C:\node_workspace\two>cnpm i vuex -S
  7. √ Installed 1 packages
  8. √ Linked 1 latest versions
  9. Run 0 scripts
  10. All packages installed (1 packages installed from npm registry, used 260ms(network 255ms), speed 156.22kB/s, json 2(39.84kB), tarball 0B)
  11. C:\node_workspace\two>cnpm i axios -S
  12. √ Installed 1 packages
  13. √ Linked 1 latest versions
  14. Run 0 scripts
  15. All packages installed (1 packages installed from npm registry, used 363ms(network 360ms), speed 26.39kB/s, json 2(9.5kB), tarball 0B)

Vue 之 移除初始化模板样式和数据

1、在App.vue 文件中移除原有Vue的logo 标签和样式文件。下面是修改后的App.Vue 文件

  1. <template>
  2. <div id="app">
  3. <router-view/>
  4. </div>
  5. </template>
  6. <script>
  7. export default {
  8. name: 'App'
  9. }
  10. </script>
  11. <style>
  12. * {
  13. margin: 0;
  14. padding: 0;
  15. box-sizing: border-box;
  16. }
  17. html,body{
  18. width: 100%;
  19. height: 100%;
  20. }
  21. #app {
  22. height: 100%;
  23. }
  24. </style>

Vue 之Vuex 功能封装

1、在src 文件夹下新增store 文件夹并新增index.js 。index.js 新增内容如下:

  1. import Vue from 'vue'
  2. import Vuex from 'vuex'
  3. Vue.use(Vuex);
  4. const store = new Vuex.Store({
  5. state: {
  6. // 存储token
  7. token: localStorage.getItem('token') ? localStorage.getItem('token') : ''
  8. },
  9. mutations: {
  10. // 修改token,并将token存入localStorage
  11. changeLogin (state, user) {
  12. state.token = user.token;
  13. localStorage.setItem('token', user.token);
  14. },
  15. logout (state, user) {
  16. state.token = ''
  17. localStorage.removeItem('token')
  18. }
  19. }
  20. });
  21. export default store;

Vue 之Element-ui、axios、store功能基础

1、在mian.js 集成element-ui、axios、store 。main.js 内容如下:

  1. // The Vue build version to load with the `import` command
  2. // (runtime-only or standalone) has been set in webpack.base.conf with an alias.
  3. import Vue from 'vue'
  4. import App from './App'
  5. import router from './router'
  6. // 集成element-ui
  7. import ElementUI from 'element-ui';
  8. import 'element-ui/lib/theme-chalk/index.css';
  9. // 集成vuex
  10. import store from'./store'
  11. // 集成axios
  12. import axios from '../node_modules/axios'
  13. // 设置跨域
  14. axios.defaults.withCredentials = true
  15. // 设置后端请求地址
  16. axios.defaults.baseURL='http://192.168.1.74:8198/house'
  17. Vue.prototype.$axios = axios
  18. Vue.use(ElementUI);
  19. Vue.config.productionTip = false
  20. // 添加请求拦截器,在请求头中加token
  21. axios.interceptors.request.use(
  22. config => {
  23. if (localStorage.getItem('token')) {
  24. config.headers.token = localStorage.getItem('token');
  25. }
  26. return config;
  27. },
  28. error => {
  29. return Promise.reject(error);
  30. });
  31. // 添加响应拦截器,移除token
  32. axios.interceptors.response.use(
  33. response =>{
  34. return response;
  35. },
  36. error=>{
  37. if(error.response){
  38. switch(error.response.status){
  39. case 401:
  40. localStorage.removeItem('token');
  41. this.$router.push('/login');
  42. }
  43. }
  44. })
  45. /* eslint-disable no-new */
  46. new Vue({
  47. el: '#app',
  48. router,
  49. store,
  50. components: { App },
  51. template: '<App/>'
  52. })

Vue 之路由配置

1、重点修改router文件夹下的index.js 文件,index.js 配置文件内容如下:

  1. import Vue from 'vue'
  2. import Router from 'vue-router'
  3. import login from '@/components/login';
  4. import home from '@/components/home';
  5. // 新房管理
  6. import newHouse from '@/components/view/newHouse'
  7. // 二手房管理
  8. import secondHouse from '@/components/view/secondHouse'
  9. Vue.use(Router)
  10. const router = new Router({
  11. routes: [
  12. {
  13. path: '/',
  14. redirect: '/login'
  15. },
  16. {
  17. path: '/login',
  18. name: 'login',
  19. component: login
  20. },
  21. {
  22. path: '/home',
  23. name: 'home',
  24. component: home,
  25. children: [{
  26. path: '/newHouse',
  27. name: 'newHouse',
  28. component: newHouse
  29. },
  30. {
  31. path: '/secondHouse',
  32. name: 'secondHouse',
  33. component: secondHouse
  34. }]
  35. }
  36. ]
  37. })
  38. // 导航守卫
  39. // 使用 router.beforeEach 注册一个全局前置守卫,判断用户是否登陆
  40. router.beforeEach((to, from, next) => {
  41. if (to.path === '/login') {
  42. next();
  43. } else {
  44. let token = localStorage.getItem('token');
  45. if (token === null || token === '') {
  46. next('/login');
  47. } else {
  48. next();
  49. }
  50. }
  51. });
  52. export default router;

Vue 组件之 login.vue(登入首页)

  1. <template>
  2. <div class="login-container">
  3. <el-form label-position="left"
  4. label-width="0px"
  5. status-icon>
  6. <h3 class="login_title">系统登录</h3>
  7. <el-form-item>
  8. <el-input
  9. type="text"
  10. v-model="loginForm.username"
  11. auto-coplete="off"
  12. placeholder="账号">
  13. </el-input>
  14. </el-form-item>
  15. <el-form-item>
  16. <el-input
  17. type="password"
  18. v-model="loginForm.password"
  19. auto-coplete="off"
  20. placeholder="密码">
  21. </el-input>
  22. </el-form-item>
  23. <el-form-item style="width: 100%">
  24. <el-button type="primary" @click.native.prevent="login" style="width: 100%">登录</el-button>
  25. <!--
  26. <el-button type="primary" @click.native.prevent="register" style="width: 100%; margin-top: 30px">注册</el-button>
  27. -->
  28. </el-form-item>
  29. </el-form>
  30. </div>
  31. </template>
  32. <script>
  33. import { mapMutations } from 'vuex';
  34. export default {
  35. data () {
  36. return {
  37. loginForm: {
  38. username: '',
  39. password: ''
  40. },
  41. userToken: ''
  42. };
  43. },
  44. methods: {
  45. // 调用store 存储
  46. ...mapMutations(['changeLogin']),
  47. login () {
  48. let _this = this;
  49. if (this.loginForm.username === '' || this.loginForm.password === '') {
  50. alert('账号或密码不能为空');
  51. } else {
  52. // 发起登入请求
  53. this.$axios({
  54. method: 'post',
  55. url: '/auth/login',
  56. headers: {
  57. 'Content-Type':'application/json;charset=utf-8'
  58. },
  59. data:{"username":_this.loginForm.username,"password":_this.loginForm.password},
  60. }).then(res => {
  61. console.log('123456');
  62. console.log(res);
  63. console.log(res.data.datas.token);
  64. _this.userToken = res.data.datas.token;
  65. // 将用户token保存到vuex中
  66. _this.changeLogin({ token: _this.userToken });
  67. // 用户登入成功,自动跳转至系统首页
  68. _this.$router.push('/home');
  69. alert('登陆成功');
  70. }).catch(error => {
  71. alert('账号或密码错误');
  72. console.log(error);
  73. });
  74. }
  75. }
  76. }
  77. };
  78. </script>
  79. <style scoped>
  80. .login-container{
  81. border-radius: 15px;
  82. background-clip: padding-box;
  83. margin: 10% auto;
  84. width: 350px;
  85. padding: 35px 35px 15px 35px;
  86. background: #fff;
  87. border: 1px solid #eaeaea;
  88. box-shadow: 0 0 25px #cac6c6;
  89. }
  90. .login_title{
  91. margin: 0px auto 40px auto;
  92. text-align: center;
  93. color: #505458;
  94. }
  95. </style>

Vue 组件之 home.vue(系统首页)

  1. <style>
  2. .el-main {
  3. background-color: #f5f7f9;
  4. }
  5. .el-header,
  6. .el-footer {
  7. background-color: white;
  8. box-sizing: border-box;
  9. border-bottom: 1px solid #f5f1f1;
  10. }
  11. .el-container {
  12. height: 100%;
  13. }
  14. </style>
  15. <template>
  16. <el-container>
  17. <!-- header部分 -->
  18. <el-header>
  19. <navtop></navtop>
  20. </el-header>
  21. <el-container>
  22. <!-- aside部分 -->
  23. <leftNav></leftNav>
  24. <el-main>
  25. <!-- main部分 -->
  26. <router-view />
  27. </el-main>
  28. </el-container>
  29. </el-container>
  30. </template>
  31. <script>
  32. import navtop from "@/components/part/nav-top.vue";
  33. import leftNav from "@/components/part/nav.vue";
  34. export default {
  35. components: {
  36. navtop,
  37. leftNav
  38. },
  39. data() {
  40. return {};
  41. },
  42. methods: {}
  43. };
  44. </script>

Vue 组件之 系统首页涉及公共页面(nav-top.vue 和nav.vue)

注意:在src/components/part 新增上述两个vue 文件

nav-top.vue

  1. <style scoped>
  2. .el-aside {
  3. display: flex;
  4. justify-content: center;
  5. align-items: center;
  6. }
  7. section{
  8. height: 100%;
  9. display: flex;
  10. justify-content: space-between;
  11. align-items: center;
  12. padding: 0 20px;
  13. }
  14. .logo {
  15. width: 200px;
  16. }
  17. .headerLogo,.logo{
  18. cursor: pointer;
  19. }
  20. </style>
  21. <template>
  22. <el-container>
  23. <el-aside width="auto" class="header-logo tap" >
  24. <img class="logo" src="@/assets/logo.png" alt="Logo" />
  25. </el-aside>
  26. <el-aside width="auto" class="header-logo tap" >
  27. <el-avatar icon="el-icon-user-solid" class="headerLogo"></el-avatar>
  28. </el-aside>
  29. </el-container>
  30. </template>
  31. <script>
  32. export default {
  33. data() {
  34. return {
  35. activeIndex: "1",
  36. };
  37. },
  38. methods: {
  39. handleSelect(key, keyPath) {
  40. console.log(key, keyPath);
  41. }
  42. }
  43. };
  44. </script>

nav.vue

  1. <style scoped>
  2. .el-row{
  3. height: 100%;
  4. }
  5. .el-menu{
  6. border-right:none;
  7. }
  8. .el-aside{
  9. border-right: 1px solid #f5f1f1;
  10. }
  11. </style>
  12. <template>
  13. <el-aside width="200px">
  14. <el-row class="tac">
  15. <el-col>
  16. <el-menu
  17. default-active="1"
  18. class="el-menu-vertical-demo"
  19. @open="handleOpen"
  20. @close="handleClose"
  21. >
  22. <el-menu-item index="1" @click="goPage('centre')">
  23. <i class="el-icon-menu"></i>
  24. <span slot="title">首页</span>
  25. </el-menu-item>
  26. <el-submenu index="3">
  27. <template slot="title">
  28. <i class="el-icon-location"></i>
  29. <span>房屋管理</span>
  30. </template>
  31. <el-menu-item-group>
  32. <el-menu-item index="3-1" @click="goPage('newHouse')">新房管理</el-menu-item>
  33. </el-menu-item-group>
  34. <el-menu-item-group>
  35. <el-menu-item index="3-2" @click="goPage('secondHouse')">二手房管理</el-menu-item>
  36. </el-menu-item-group>
  37. </el-submenu>
  38. </el-menu>
  39. </el-col>
  40. </el-row>
  41. </el-aside>
  42. </template>
  43. <script>
  44. var $this = {};
  45. export default {
  46. data() {
  47. return {};
  48. },
  49. beforeCreate() {
  50. $this = this;
  51. },
  52. methods: {
  53. handleOpen(key, keyPath) {
  54. console.log(key, keyPath);
  55. },
  56. handleClose(key, keyPath) {
  57. console.log(key, keyPath);
  58. },
  59. goPage(link) {
  60. // 跳转至首页
  61. if (link === "centre") {
  62. $this.$router.push("/centre").catch(error => error);
  63. }
  64. // 跳转至会员管理
  65. else if ((link === "newHouse")) {
  66. $this.$router.push("/newHouse").catch(error => error);
  67. }
  68. // 跳转至收获地址
  69. else if ((link === "secondHouse")) {
  70. $this.$router.push("/secondHouse").catch(error => error);
  71. }
  72. }
  73. }
  74. };
  75. </script>

Vue 组件之业务组件

注意:在src/components/view 新增上述业务组件Vue

业务组件之newHouse.vue(新房管理)

  1. <style>
  2. </style>
  3. <template>
  4. <div id="new_house">
  5. <h1>新房管理</h1>
  6. </div>
  7. </template>
  8. <script>
  9. export default{
  10. name: 'new_house'
  11. }
  12. </script>

业务组件之secondHouse.vue(二手房管理)

  1. <style>
  2. </style>
  3. <template>
  4. <div id="second_house">
  5. <h1>二手房管理</h1>
  6. </div>
  7. </template>
  8. <script>
  9. export default{
  10. name: 'second_house'
  11. }
  12. </script>

Vue 后台租房管理系统功能演示:

 

 

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/379144
推荐阅读
相关标签
  

闽ICP备14008679号