欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

關(guān)于vue-admin-template模板連接后端改造登錄功能

 更新時間:2022年05月18日 09:20:12   作者:遠走與夢游  
這篇文章主要介紹了關(guān)于vue-admin-template模板連接后端改造登錄功能,登陸方法根據(jù)賬號密碼查出用戶信息,根據(jù)用戶id與name生成token并返回,userinfo則是對token進行獲取,在查出對應(yīng)值進行返回,感興趣的朋友一起看看吧

首先修改統(tǒng)一請求路徑為我們自己的登陸接口,在.env.development文件中

# base api
VUE_APP_BASE_API = 'http://localhost:8081/api/dsxs/company'

打開登陸頁面,src/views/login/index.vue

<template>
  <div class="login-container">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">
      <div class="title-container">
        <h3 class="title">Login Form</h3>
      </div>
      <el-form-item prop="username">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          ref="username"
          v-model="loginForm.username"
          placeholder="Username"
          name="username"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>
      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="Password"
          name="password"
          tabindex="2"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>
      <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleLogin">Login</el-button>
      <div class="tips">
        <span style="margin-right:20px;">username: admin</span>
        <span> password: any</span>
      </div>
    </el-form>
  </div>
</template>

可以看到頁面使用組件對loginForm進行名稱和密碼的綁定

 data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error('Please enter the correct user name'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('The password can not be less than 6 digits'))
      } else {
        callback()
      }
    }

這段代碼則為對輸入的內(nèi)容進行驗證

看登陸的方法

handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('user/login', this.loginForm).then(() => {
            this.$router.push({ path: this.redirect || '/' })
            this.loading = false
          }).catch(() => {
            this.loading = false
          })
        } else {
          return false
        }
      })
    }

其中 this.$store.dispatch('user/login', this.loginForm),不是請求后臺user/login接口,而是轉(zhuǎn)到modules下的user.js中的login方法,打開store/modules/user.js可以看到login方法。而login方法則是調(diào)用api/user.js中的login方法。

此時修改store/modules/user.js接收后臺傳來的響應(yīng)數(shù)據(jù)

const actions = {
  // user login
  login({ commit }, userInfo) {
    const { username, password } = userInfo
    return new Promise((resolve, reject) => {
      login({ username: username.trim(), password: password }).then(response => {
        console.log(response)
        const { data } = response
        commit('SET_TOKEN', response.data.token)
        setToken(response.data.token)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

同時在api/user.js中修改為我們后臺的請求地址

import request from '@/utils/request'
export function login(data) {
  return request({
    url: 'userlogin',
    method: 'post',
    data
  })
}
export function getInfo(token) {
  return request({
    url: 'userinfo',
    method: 'get',
    params: { token }
  })
}

此時可以發(fā)現(xiàn)模板采用的登陸方式是請求兩次,第一次通過用戶名密碼請求后端,后端判斷后,返回對應(yīng)的token。然后在通過getInfo方法請求后端,獲取用戶真實信息。

在編寫后端之前還需要修改utils/request.js,因為默認狀態(tài)碼是20000為成功,而我們平時返回的是200

 // if the custom code is not 20000, it is judged as an error.
    if (res.code !== 200) {
      Message({
        message: res.message || 'Error',
        type: 'error',
        duration: 5 * 1000
      })

簡單的編寫后端代碼,登陸方法根據(jù)賬號密碼查出用戶信息,根據(jù)用戶id與name生成token并返回,userinfo則是對token進行獲取,在查出對應(yīng)值進行返回。

@CrossOrigin
@RestController
@RequestMapping("/api/dsxs/company")
public class CompanyuserController {
    @Autowired
    private CompanyuserService companyuserService;
    //后臺登陸
    @PostMapping("userlogin")
    @ResponseBody
    public R userlogin(@RequestBody UserVo userVo){
        Companyuser companyuser = companyuserService.login(userVo);
        String token = JwtHelper.createToken(companyuser.getId(), companyuser.getName());
        return R.ok().data("token",token);
    }
    //返回信息
    @GetMapping("userinfo")
    public R userinfo( String token){
        Integer userId = JwtHelper.getUserId(token);
        System.out.println("====");
        Companyuser user = companyuserService.getById(userId);
        HashMap<String, String> map = new HashMap<>();
        map.put("name",user.getName());
        map.put("avatar",user.getAvatar());
        return R.ok().data("name",user.getName()).data("avatar",user.getAvatar());
    }
}

我這里使用@CrossOrigin注解解決的跨域問題。

到此這篇關(guān)于關(guān)于vue-admin-template模板連接后端改造登錄功能的文章就介紹到這了,更多相關(guān)vue admin template登錄內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • vue監(jiān)聽scroll的坑的解決方法

    vue監(jiān)聽scroll的坑的解決方法

    這篇文章主要介紹了vue監(jiān)聽scroll的坑的解決方法,現(xiàn)在分享給大家,也給大家做個參考,希望給有同樣經(jīng)歷的人幫助
    2017-09-09
  • 封裝一下vue中的axios示例代碼詳解

    封裝一下vue中的axios示例代碼詳解

    這篇文章主要介紹了封裝一下vue中的axios,本文通過示例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-02-02
  • vue?調(diào)用瀏覽器攝像頭實現(xiàn)及原理解析

    vue?調(diào)用瀏覽器攝像頭實現(xiàn)及原理解析

    這篇文章主要為大家介紹了vue調(diào)用瀏覽器攝像頭實現(xiàn)及原理解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-06-06
  • vue如何使用process.env搭建自定義運行環(huán)境

    vue如何使用process.env搭建自定義運行環(huán)境

    這篇文章主要介紹了vue如何使用process.env搭建自定義運行環(huán)境,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • Vue的Options用法說明

    Vue的Options用法說明

    這篇文章主要介紹了Vue的Options用法說明,具有很好的參考價值,希望對大家有所
    2020-08-08
  • Vue實現(xiàn)動態(tài)顯示表單項填寫進度功能

    Vue實現(xiàn)動態(tài)顯示表單項填寫進度功能

    這篇文章主要介紹了Vue實現(xiàn)動態(tài)顯示表單項填寫進度功能,此功能可以幫助用戶了解表單填寫的進度和當(dāng)前狀態(tài),提高用戶體驗,通常實現(xiàn)的方式是在表單中添加進度條,根據(jù)用戶填寫狀態(tài)動態(tài)更新進度條,感興趣的同學(xué)可以參考下文
    2023-05-05
  • Vue自動生成組件示例總結(jié)

    Vue自動生成組件示例總結(jié)

    在Vue中,我們可以使用unplugin-generate-component-name插件自動基于目錄名稱生成組件名稱,這個插件使得在大型代碼庫中找到和管理組件更加容易和直觀,這篇文章主要介紹了Vue自動生成組件示例總結(jié),需要的朋友可以參考下
    2023-12-12
  • vue項目中引入js-base64方式

    vue項目中引入js-base64方式

    這篇文章主要介紹了vue項目中引入js-base64方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • vue中使用router全局守衛(wèi)實現(xiàn)頁面攔截的示例

    vue中使用router全局守衛(wèi)實現(xiàn)頁面攔截的示例

    這篇文章主要介紹了vue中使用router全局守衛(wèi)實現(xiàn)頁面攔截的示例,幫助大家維護自己的項目,感興趣的朋友可以了解下
    2020-10-10
  • vue對枚舉值轉(zhuǎn)換方式

    vue對枚舉值轉(zhuǎn)換方式

    這篇文章主要介紹了vue對枚舉值轉(zhuǎn)換方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-09-09

最新評論