關(guān)于vue-admin-template模板連接后端改造登錄功能
首先修改統(tǒng)一請(qǐng)求路徑為我們自己的登陸接口,在.env.development文件中
# base api VUE_APP_BASE_API = 'http://localhost:8081/api/dsxs/company'
打開(kāi)登陸頁(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方法,打開(kāi)store/modules/user.js可以看到login方法。而login方法則是調(diào)用api/user.js中的login方法。
此時(shí)修改store/modules/user.js接收后臺(tái)傳來(lá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)求兩次,第一次通過(guò)用戶名密碼請(qǐng)求后端,后端判斷后,返回對(duì)應(yīng)的token。然后在通過(guò)getInfo方法請(qǐng)求后端,獲取用戶真實(shí)信息。
在編寫(xiě)后端之前還需要修改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)單的編寫(xiě)后端代碼,登陸方法根據(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注解解決的跨域問(wèn)題。
到此這篇關(guān)于關(guān)于vue-admin-template模板連接后端改造登錄功能的文章就介紹到這了,更多相關(guān)vue admin template登錄內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue監(jiān)聽(tīng)scroll的坑的解決方法
這篇文章主要介紹了vue監(jiān)聽(tīng)scroll的坑的解決方法,現(xiàn)在分享給大家,也給大家做個(gè)參考,希望給有同樣經(jīng)歷的人幫助2017-09-09vue?調(diào)用瀏覽器攝像頭實(shí)現(xiàn)及原理解析
這篇文章主要為大家介紹了vue調(diào)用瀏覽器攝像頭實(shí)現(xiàn)及原理解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06vue如何使用process.env搭建自定義運(yùn)行環(huán)境
這篇文章主要介紹了vue如何使用process.env搭建自定義運(yùn)行環(huán)境,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01Vue實(shí)現(xiàn)動(dòng)態(tài)顯示表單項(xiàng)填寫(xiě)進(jìn)度功能
這篇文章主要介紹了Vue實(shí)現(xiàn)動(dòng)態(tài)顯示表單項(xiàng)填寫(xiě)進(jìn)度功能,此功能可以幫助用戶了解表單填寫(xiě)的進(jìn)度和當(dāng)前狀態(tài),提高用戶體驗(yàn),通常實(shí)現(xiàn)的方式是在表單中添加進(jìn)度條,根據(jù)用戶填寫(xiě)狀態(tài)動(dòng)態(tài)更新進(jìn)度條,感興趣的同學(xué)可以參考下文2023-05-05vue中使用router全局守衛(wèi)實(shí)現(xiàn)頁(yè)面攔截的示例
這篇文章主要介紹了vue中使用router全局守衛(wèi)實(shí)現(xiàn)頁(yè)面攔截的示例,幫助大家維護(hù)自己的項(xiàng)目,感興趣的朋友可以了解下2020-10-10