vue 前端实现login页登陆 验证码

实现效果

复制代码
// template
<el-form :model="loginForm" :rules="fieldRules" ref="loginForm" label-position="left" label-width="0px" class="login-container">
        <span class="tool-bar"></span>
        <h2 class="title">用户登陆</h2>
        <el-form-item prop="account">
          <el-input type="text" v-model.trim="loginForm.account" auto-complete="false" placeholder="账号"></el-input>
        </el-form-item>
        <el-form-item prop="password" class="item-m10">
          <el-input type="password" v-model.trim="loginForm.password" auto-complete="false" placeholder="密码"></el-input>
        </el-form-item>
        <el-form-item prop="code" align="left" style="margin-top: 20px">
          <el-input v-model="loginForm.code" style="width: 170px" placeholder="验证码,点击图片刷新"></el-input>
          <el-tag class="login-tag-code" @click="getCode">{{ viewCode }}</el-tag>
        </el-form-item>
        <div class="checked-item">
          <el-checkbox v-model="checked">记住密码</el-checkbox>
        </div>
        <el-form-item style="width: 100%" class="btn-item">
          <el-button style="width: 100%" @click.native.prevent="loginSubmit" :loading="loading">登录</el-button>
 </el-form-item>


// js
// ---------分割线

  data () {
    return {
      viewCode: '',
      loginForm: {
        account: '',
        password: '',
        src: '',
        code: ''
      },
      fieldRules: {
        account: [{ required: true, message: '请输入账号', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
      },
      checked: false,
      // 加载中的标志
      loading: false
    }
  },


// ------ 分割线


  methods: {
    loginSubmit () {
      if (!this.loginForm.account || !this.loginForm.password) {
        this.$message.error('账号或密码不能为空!')
        return
      }
      if (!this.loginForm.code || this.loginForm.code !== this.viewCode) {
        this.$message.error('验证码不正确!')
        return
      }
      this.loading = true
      let userInfo = {
        account: this.loginForm.account,
        password: this.loginForm.password
      }
      //登陆接口
      this.$api.login
        .login(userInfo)
        .then((res) => {
            if (this.checked) {
              let rememberInfo = JSON.stringify({ ...userInfo })
              localStorage.setItem('rememberInfo', rememberInfo) // 记住密码时 保存login
            } else {
              localStorage.removeItem('rememberInfo')
            }
            this.$router.push('/') // 登录成功,跳转到主页 
            this.loading = false
        })
        .catch((err) => {
          this.$message({ message: err.message, type: 'error' })
        })
    },

  //获取验证码
    getCode () {
      this.viewCode = ''
      let codeString = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
      let codeArray = codeString.split('')
      let num = codeArray.length
      let newCodeArray = []
      for (let i = 0; i < 5; i++) {
        let index = Math.floor(Math.random() * num)
        newCodeArray.push(codeArray[index])
      }
      this.viewCode = newCodeArray.join('')
    },
  },
  mounted () {
    this.getCode()
    if (localStorage.getItem('rememberInfo')) {
      // 有上次登录信息 显示上次登录
      let rememberData = JSON.parse(localStorage.getItem('rememberInfo'))
      const { account, password } = rememberData
      this.loginForm.account = account
      this.loginForm.password = password
    }
  }

样式代码省略。。。

相关推荐
程序员黑豆7 小时前
Java 注释详解:单行、多行与文档注释的完整指南
java·前端·ai编程
剪刀石头布啊7 小时前
antd中可编辑表格中巧用useWatch实现联动高性能效果,以及定制延伸学习
前端
油丶酸萝卜别吃7 小时前
前端转全栈学习路线
前端·学习
浮生望8 小时前
前端路由进阶:History API原理与手写HistoryRouter
前端
陆枫Larry8 小时前
JavaScript 中的竞态是什么,为啥会有竟态?
前端
上海安当技术10 小时前
半天接入:USBKey RESTful API + C 动态库,Web 和 C/S 两套集成路径实战
前端·后端·restful·集成·usbkey
DevUI团队11 小时前
从“即兴创作”到“规格先行”,华为云码道(CodeArts)代码智能体持续深耕企业级规范驱动开发能力
前端·人工智能·后端
weixin_4316004412 小时前
NestJS 入门(3):Guard 如何挡住未登录请求?
前端·后端·学习·nest.js
avi911112 小时前
[AI教做人]AI平台做2项目;一个3D模型展示,另一个框架多人
javascript·人工智能·ai·3d模型·3d引擎·顶点和法线
kyriewen12 小时前
我用Claude Code两天干完了团队两周的排期——周报发出去那一刻我就后悔了
前端·javascript·ai编程