Complete login procedure

pull/3445/head
johnniang 2019-04-29 20:35:20 +08:00
parent 91aa2739e8
commit 26cd673f73
2 changed files with 64 additions and 12 deletions

View File

@ -1,3 +1,7 @@
import Vue from 'vue'
import { ACCESS_TOKEN } from '@/store/mutation-types'
import adminApi from '@/api/admin'
const user = { const user = {
state: { state: {
token: '', token: '',
@ -9,6 +13,7 @@ const user = {
mutations: { mutations: {
SET_TOKEN: (state, token) => { SET_TOKEN: (state, token) => {
Vue.ls.set(ACCESS_TOKEN, token)
state.token = token state.token = token
}, },
SET_NAME: (state, { name }) => { SET_NAME: (state, { name }) => {
@ -24,8 +29,23 @@ const user = {
state.info = info state.info = info
} }
}, },
actions: { actions: {
login({ commit }, { username, password }) {
return new Promise((resolve, reject) => {
adminApi
.login(username, password)
.then(response => {
const token = response.data.data
Vue.$log.debug('Got token', token)
commit('SET_TOKEN', token)
resolve(response)
})
.catch(error => {
reject(error)
})
})
}
} }
} }

View File

@ -4,12 +4,18 @@
Halo Halo
</div> </div>
<div class="loginBody animated"> <div class="loginBody animated">
<a-form layout="vertical"> <a-form
layout="vertical"
@keyup.enter.native="handleLogin"
>
<a-form-item <a-form-item
class="animated fadeInUp" class="animated fadeInUp"
:style="{'animation-delay': '0.1s'}" :style="{'animation-delay': '0.1s'}"
> >
<a-input placeholder="用户名/邮箱"> <a-input
placeholder="用户名/邮箱"
v-model="username"
>
<a-icon <a-icon
slot="prefix" slot="prefix"
type="user" type="user"
@ -22,6 +28,7 @@
:style="{'animation-delay': '0.2s'}" :style="{'animation-delay': '0.2s'}"
> >
<a-input <a-input
v-model="password"
type="password" type="password"
placeholder="密码" placeholder="密码"
> >
@ -38,7 +45,8 @@
> >
<a-button <a-button
type="primary" type="primary"
block="true" :block="true"
@click="handleLogin"
>登录</a-button> >登录</a-button>
</a-form-item> </a-form-item>
</a-form> </a-form>
@ -47,18 +55,42 @@
</template> </template>
<script> <script>
import { mapActions } from 'vuex'
export default { export default {
beforeCreate() { data() {
this.form = this.$form.createForm(this) return {
username: null,
password: null
}
}, },
methods: { methods: {
handleSubmit(e) { ...mapActions(['login']),
e.preventDefault() handleLogin() {
this.form.validateFields((err, values) => { if (!this.username) {
if (!err) { this.$message.warn('用户名不能为空')
console.log('Received values of form: ', values) return
} }
if (!this.password) {
this.$message.warn('密码不能为空')
return
}
this.login({ username: this.username, password: this.password }).then(response => {
// Go to dashboard
this.loginSuccess()
}) })
},
loginSuccess() {
this.$router.push({ name: 'Dashboard' })
// 1
setTimeout(() => {
this.$notification.success({
message: '欢迎',
description: `欢迎回来`
})
}, 1000)
} }
} }
} }