当前位置 博文首页 > 暴走的山交君的博客:Vue+SpringBoot+Mybatis-plus前后端交互实

    暴走的山交君的博客:Vue+SpringBoot+Mybatis-plus前后端交互实

    作者:[db:作者] 时间:2021-08-17 13:24

    Vue+SpringBoot前后端分离之实现简单的登录功能

    源码在我码云上:
    后端 https://gitee.com/codeyuaiiao/webzp-java
    前端 https://gitee.com/codeyuaiiao/webzp
    觉得有帮助的,进去后麻烦帮忙star一下谢谢!!

    技术栈

    Vue
    SpringBoot
    Mybatis-plus


    Vue前端

    这是我的vue前端结构:
    在这里插入图片描述

    一:首先创建Vue项目

    • src下创建一个views目录存放vue页面
      创建两个页面login.vuesuccess.vue用来测试
      在这里插入图片描述

    成功后根据具体的业务修改.

    • 我的前端页面代码:
      login.vue
    <template>
      <div class="login">
        <vue-particles
          color="#dedede"
          :particleOpacity="0.7"
          :particlesNumber="80"
          shapeType="circle"
          :particleSize="4"
          linesColor="#dedede"
          :linesWidth="1"
          :lineLinked="true"
          :lineOpacity="0.4"
          :linesDistance="150"
          :moveSpeed="3"
          :hoverEffect="true"
          hoverMode="grab"
          :clickEffect="true"
          clickMode="push"
        ></vue-particles>
        <!--    登录框-->
        <div class="login_box">
          <el-form ref="loginFormRef" :model="loginForm" :rules="loginFormRules" label-width="0px" class="login_form">
            <!--        用户名-->
            <el-form-item label="账号" label-width="60px" prop="username">
              <el-input placeholder="请输入账号" v-model="loginForm.username" prefix-icon="el-icon-user"></el-input>
            </el-form-item>
            <!--        密码-->
            <el-form-item label="密码" label-width="60px" prop="password">
              <el-input placeholder="请输入密码" v-model="loginForm.password" show-password
                        prefix-icon="el-icon-lock"></el-input>
            </el-form-item>
            <!--        按钮-->
            <el-form-item class="btns">
              <!-- 回车键盘监听 -->
              <!-- @keyup.enter="login('loginFormRef')" -->
              <el-button type="primary" @click="login('loginFormRef')">登录</el-button>
              <el-button type="success">注册</el-button>
              <el-button type="info" @click="reset('loginFormRef')">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
    
    </template>
    
    <script>
      export default {
        name: 'login',
        data() {
          return {
            // 这是登录表单的数据绑定对象
            loginForm: {
              username: '',
              password: '',
              pwdType: "password",
            },
            msg: '',
            loginFormRules: {
              username: [
                {required: true, message: '请输入登录账号', trigger: 'blur'},
                {min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur'},
              ],
              password: [
                {required: true, message: '请输入登陆密码', trigger: 'blur'},
                {min: 5, max: 15, message: '长度在 5 到 15 个字符', trigger: 'blur'},
              ]
            }
          };
        },
        methods: {
          //点击重置按钮,重置登录表单
          reset() {
            this.$refs.loginFormRef.resetFields();
          },
    
          login() {
            this.$refs.loginFormRef.validate(valid => {
              if (valid) {
                this.$axios.post('/admin/login', {
                  username: this.loginForm.username,
                  password: this.loginForm.password
                }).then((response) => {
                  if (response.code === 200) {
                    this.$router.push({path: '/success'})
                  }
                }).catch(() => {
                  console.log("用户名或密码错误!");
                  });
              } else {
                console.log("参数验证不合法!");
                return false;
              }
            })
          }
        }
      }
    </script>
    
    <style scoped>
      .login {
        background: url("../assets/image/background.jpg");
        width: 100%;
        height: 100%;
        position: fixed;
        background-size: 100% 100%;
      }
    
      .login_box {
        height: 300px;
        width: 450px;
        background: #fff;
        border-radius: 3px;
        position: absolute;
        left: 50%;
        top: 50%;
        transform: translate(-50%, -50%);
      }
    
      .login_form {
        position: absolute;
        bottom: 20px;
        width: 100%;
        padding: 0px 30px;
        box-sizing: border-box;
      }
    
      .btns {
        display: flex;
        justify-content: flex-end;
      }
    </style>
    
    

    这个页面只是用来测试使用
    success.vue

    <template>
      <div>
        <h1>登录成功!success</h1>
      </div>
    </template>
    <script>
      export default {
        data() {}
      }
    </script>
    
    <style>
    
    </style>
    
    

    主要功能是login.vue中的 表单功能以及登录按钮的对应的login方法
    登录表单设计代码:

    <div class="login_box">
          <el-form ref="loginFormRef" :model="loginForm" :rules="loginFormRules" label-width="0px" class="login_form">
            <!--        用户名-->
            <el-form-item label="账号" label-width="60px" prop="username">
              <el-input placeholder="请输入账号" v-model="loginForm.username" prefix-icon="el-icon-user"></el-input>
            </el-form-item>
            <!--        密码-->
            <el-form-item label="密码" label-width