最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

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

打開登陸頁(yè)面,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>

可以看到頁(yè)面使用組件對(duì)loginForm進(jìn)行名稱和密碼的綁定

 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()
      }
    }

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

看登陸的方法

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),不是請(qǐng)求后臺(tái)user/login接口,而是轉(zhuǎn)到modules下的user.js中的login方法,打開store/modules/user.js可以看到login方法。而login方法則是調(diào)用api/user.js中的login方法。

此時(shí)修改store/modules/user.js接收后臺(tái)傳來的響應(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)
      })
    })
  },

同時(shí)在api/user.js中修改為我們后臺(tái)的請(qǐng)求地址

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 }
  })
}

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

在編寫后端之前還需要修改utils/request.js,因?yàn)槟J(rèn)狀態(tài)碼是20000為成功,而我們平時(shí)返回的是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
      })

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

@CrossOrigin
@RestController
@RequestMapping("/api/dsxs/company")
public class CompanyuserController {
    @Autowired
    private CompanyuserService companyuserService;
    //后臺(tái)登陸
    @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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Vue的生命周期一起來看看

    Vue的生命周期一起來看看

    這篇文章主要為大家詳細(xì)介紹了Vue的生命周期,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-02-02
  • 利用HBuilder打包前端開發(fā)webapp為apk的方法

    利用HBuilder打包前端開發(fā)webapp為apk的方法

    下面小編就為大家?guī)硪黄肏Builder打包前端開發(fā)webapp為apk的方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-11-11
  • vue項(xiàng)目實(shí)現(xiàn)多語(yǔ)言切換的思路

    vue項(xiàng)目實(shí)現(xiàn)多語(yǔ)言切換的思路

    這篇文章主要介紹了vue項(xiàng)目實(shí)現(xiàn)多語(yǔ)言切換的思路,幫助大家完成多語(yǔ)言翻譯,感興趣的朋友可以了解下
    2020-09-09
  • vue2.0+vue-dplayer實(shí)現(xiàn)hls播放的示例

    vue2.0+vue-dplayer實(shí)現(xiàn)hls播放的示例

    這篇文章主要介紹了vue2.0+vue-dplayer實(shí)現(xiàn)hls播放的示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-03-03
  • Vue中的@blur/@focus事件解讀

    Vue中的@blur/@focus事件解讀

    這篇文章主要介紹了Vue中的@blur/@focus事件解讀,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • vue內(nèi)嵌iframe跨域通信的實(shí)例代碼

    vue內(nèi)嵌iframe跨域通信的實(shí)例代碼

    這篇文章主要介紹了vue內(nèi)嵌iframe跨域通信,主要介紹了Vue組件中如何引入iframe,vue如何獲取iframe對(duì)象以及iframe內(nèi)的window對(duì)象,結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì)需要的朋友可以參考下
    2022-11-11
  • vite的搭建與使用的詳細(xì)步驟

    vite的搭建與使用的詳細(xì)步驟

    本文主要介紹了vite的搭建與使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-06-06
  • vue3標(biāo)簽中的ref屬性詳解及如何使用$refs獲取元素

    vue3標(biāo)簽中的ref屬性詳解及如何使用$refs獲取元素

    這篇文章主要給大家介紹了關(guān)于vue3標(biāo)簽中的ref屬性詳解及如何使用$refs獲取元素的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2024-11-11
  • vue-form表單驗(yàn)證是否為空值的實(shí)例詳解

    vue-form表單驗(yàn)證是否為空值的實(shí)例詳解

    今天小編就為大家分享一篇vue-form表單驗(yàn)證是否為空值的實(shí)例詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-10-10
  • 1分鐘Vue實(shí)現(xiàn)右鍵菜單

    1分鐘Vue實(shí)現(xiàn)右鍵菜單

    今天給大家分享的是,如何在最短的時(shí)候內(nèi)實(shí)現(xiàn)右鍵菜單。高效實(shí)現(xiàn)需求,避免重復(fù)造輪子。感興趣的可以了解一下
    2021-10-10

最新評(píng)論

南投县| 双鸭山市| 苗栗县| 金川县| 赤壁市| 五寨县| 花莲县| 南充市| 南木林县| 兴宁市| 定西市| 泰州市| 聂拉木县| 门头沟区| 庆元县| 尼木县| 临武县| 蒲江县| 丹江口市| 香格里拉县| 清水县| 新乐市| 集安市| 龙胜| 剑河县| 邻水| 响水县| 彰武县| 连州市| 龙海市| 文成县| 彰化市| 胶州市| 阜康市| 忻城县| 象山县| 江油市| 遵义市| 花莲县| 新郑市| 琼结县|