change login by username

This commit is contained in:
Pan 2017-08-22 14:26:19 +08:00 committed by 花裤衩
parent d06d476b50
commit 0094f44910
8 changed files with 109 additions and 93 deletions

View File

@ -1,12 +1,12 @@
import fetch from 'utils/fetch'; import fetch from 'utils/fetch';
export function loginByEmail(email, password) { export function loginByUsername(username, password) {
const data = { const data = {
email, username,
password password
}; };
return fetch({ return fetch({
url: '/login/loginbyemail', url: '/login/login',
method: 'post', method: 'post',
data data
}); });

File diff suppressed because one or more lines are too long

View File

@ -1,25 +1,23 @@
import Mock from 'mockjs'; import Mock from 'mockjs'
import loginAPI from './login'; import loginAPI from './login'
import articleAPI from './article'; import articleAPI from './article'
import article_tableAPI from './article_table'; import article_tableAPI from './article_table'
import remoteSearchAPI from './remoteSearch'; import remoteSearchAPI from './remoteSearch'
// 登录相关 // 登录相关
Mock.mock(/\/login\/loginbyemail/, 'post', loginAPI.loginByEmail); Mock.mock(/\/login\/login/, 'post', loginAPI.loginByUsername)
Mock.mock(/\/login\/logout/, 'post', loginAPI.logout); Mock.mock(/\/login\/logout/, 'post', loginAPI.logout)
Mock.mock(/\/user\/info\.*/, 'get', loginAPI.getInfo) Mock.mock(/\/user\/info\.*/, 'get', loginAPI.getInfo)
// // 文章相关 // 文章相关
Mock.mock(/\/article\/list/, 'get', articleAPI.getList); Mock.mock(/\/article\/list/, 'get', articleAPI.getList)
Mock.mock(/\/article\/detail/, 'get', articleAPI.getArticle); Mock.mock(/\/article\/detail/, 'get', articleAPI.getArticle)
// // table example相关 // table example相关
Mock.mock(/\/article_table\/list/, 'get', article_tableAPI.getList); Mock.mock(/\/article_table\/list/, 'get', article_tableAPI.getList)
Mock.mock(/\/article_table\/p/, 'get', article_tableAPI.getPv); Mock.mock(/\/article_table\/p/, 'get', article_tableAPI.getPv)
// // 搜索相关 // 搜索相关
Mock.mock(/\/search\/user/, 'get', remoteSearchAPI.searchUser); Mock.mock(/\/search\/user/, 'get', remoteSearchAPI.searchUser)
export default Mock
export default Mock;

View File

@ -25,9 +25,9 @@ const userMap = {
} }
export default { export default {
loginByEmail: config => { loginByUsername: config => {
const { email } = JSON.parse(config.body); const { username } = JSON.parse(config.body);
return userMap[email.split('@')[0]]; return userMap[username];
}, },
getInfo: config => { getInfo: config => {
const { token } = param2Obj(config.url); const { token } = param2Obj(config.url);

View File

@ -1,4 +1,4 @@
import { loginByEmail, logout, getInfo } from 'api/login'; import { loginByUsername, logout, getInfo } from 'api/login';
import { getToken, setToken, removeToken } from 'utils/auth'; import { getToken, setToken, removeToken } from 'utils/auth';
const user = { const user = {
@ -50,11 +50,11 @@ const user = {
}, },
actions: { actions: {
// 邮箱登录 // 用户名登录
LoginByEmail({ commit }, userInfo) { LoginByUsername({ commit }, userInfo) {
const email = userInfo.email.trim(); const username = userInfo.username.trim();
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
loginByEmail(email, userInfo.password).then(response => { loginByUsername(username, userInfo.password).then(response => {
const data = response.data; const data = response.data;
setToken(response.data.token); setToken(response.data.token);
commit('SET_TOKEN', data.token); commit('SET_TOKEN', data.token);

View File

@ -3,9 +3,9 @@
*/ */
/* 是否是公司邮箱*/ /* 是否是公司邮箱*/
export function isWscnEmail(str) { export function isvalidUsername(str) {
const reg = /^[a-z0-9](?:[-_.+]?[a-z0-9]+)*@wallstreetcn\.com$/i; const valid_map = ['admin', 'editor']
return reg.test(str.trim()); return valid_map.indexOf(str.trim()) >= 0;
} }
/* 合法uri*/ /* 合法uri*/

View File

@ -21,7 +21,6 @@
...mapGetters([ ...mapGetters([
'name', 'name',
'avatar', 'avatar',
'email',
'introduction', 'introduction',
'roles' 'roles'
]) ])

View File

@ -1,115 +1,123 @@
<template> <template>
<div class="login-container"> <div class="login-container">
<el-form autoComplete="on" :model="loginForm" :rules="loginRules" ref="loginForm" label-position="left" label-width="0px" <el-form class="card-box login-form" autoComplete="on" :model="loginForm" :rules="loginRules" ref="loginForm" label-position="left">
class="card-box login-form">
<h3 class="title">系统登录</h3> <h3 class="title">系统登录</h3>
<el-form-item prop="email">
<span class="svg-container"><icon-svg icon-class="jiedianyoujian"></icon-svg></span> <el-form-item prop="username">
<el-input name="email" type="text" v-model="loginForm.email" autoComplete="on" placeholder="邮箱"></el-input> <span class="svg-container svg-container_login">
<icon-svg icon-class="yonghuming" />
</span>
<el-input name="username" type="text" v-model="loginForm.username" autoComplete="on" placeholder="邮箱" />
</el-form-item> </el-form-item>
<el-form-item prop="password"> <el-form-item prop="password">
<span class="svg-container"><icon-svg icon-class="mima"></icon-svg></span> <span class="svg-container">
<el-input name="password" type="password" @keyup.enter.native="handleLogin" v-model="loginForm.password" autoComplete="on" <icon-svg icon-class="mima" />
placeholder="密码"></el-input> </span>
<el-input name="password" :type="pwdType" @keyup.enter.native="handleLogin" v-model="loginForm.password" autoComplete="on"
placeholder="密码" />
<span class='show-pwd' @click='showPwd'><icon-svg icon-class="yanjing" /></span>
</el-form-item> </el-form-item>
<el-form-item>
<el-button type="primary" style="width:100%;" :loading="loading" @click.native.prevent="handleLogin"> <el-button type="primary" style="width:100%;margin-bottom:30px;" :loading="loading" @click.native.prevent="handleLogin">登录</el-button>
登录
</el-button> <div class='tips'>账号:admin 密码随便填</div>
</el-form-item> <div class='tips'>账号:editor 密码随便填</div>
<div class='tips'>admin账号为:admin@wallstreetcn.com 密码随便填</div>
<div class='tips'>editor账号:editor@wallstreetcn.com 密码随便填</div>
</el-form> </el-form>
<el-dialog title="第三方验证" :visible.sync="showDialog"> <el-dialog title="第三方验证" :visible.sync="showDialog">
邮箱登录成功,请选择第三方验证 邮箱登录成功,请选择第三方验证
<social-sign></social-sign> <social-sign />
</el-dialog> </el-dialog>
</div> </div>
</template> </template>
<script> <script>
import { isWscnEmail } from 'utils/validate'; import { isvalidUsername } from 'utils/validate'
import socialSign from './socialsignin'; import socialSign from './socialsignin'
export default { export default {
components: { socialSign }, components: { socialSign },
name: 'login', name: 'login',
data() { data() {
const validateEmail = (rule, value, callback) => { const validateUsername = (rule, value, callback) => {
if (!isWscnEmail(value)) { if (!isvalidUsername(value)) {
callback(new Error('请输入正确的合法邮箱')); callback(new Error('请输入正确的用户名'))
} else { } else {
callback(); callback()
} }
}; }
const validatePass = (rule, value, callback) => { const validatePassword = (rule, value, callback) => {
if (value.length < 6) { if (value.length < 6) {
callback(new Error('密码不能小于6位')); callback(new Error('密码不能小于6位'))
} else { } else {
callback(); callback()
}
} }
};
return { return {
loginForm: { loginForm: {
email: 'admin@wallstreetcn.com', username: 'admin',
password: '' password: '1111111'
}, },
loginRules: { loginRules: {
email: [ username: [{ required: true, trigger: 'blur', validator: validateUsername }],
{ required: true, trigger: 'blur', validator: validateEmail } password: [{ required: true, trigger: 'blur', validator: validatePassword }]
],
password: [
{ required: true, trigger: 'blur', validator: validatePass }
]
}, },
pwdType: 'password',
loading: false, loading: false,
showDialog: false showDialog: false
} }
}, },
methods: { methods: {
showPwd() {
if (this.pwdType === 'password') {
this.pwdType = ''
} else {
this.pwdType = 'password'
}
},
handleLogin() { handleLogin() {
this.$refs.loginForm.validate(valid => { this.$refs.loginForm.validate(valid => {
if (valid) { if (valid) {
this.loading = true; this.loading = true
this.$store.dispatch('LoginByEmail', this.loginForm).then(() => { this.$store.dispatch('LoginByUsername', this.loginForm).then(() => {
this.loading = false; this.loading = false
this.$router.push({ path: '/' }); this.$router.push({ path: '/' })
// this.showDialog = true; // this.showDialog = true
}).catch(() => { }).catch(() => {
this.loading = false; this.loading = false
}); })
} else { } else {
console.log('error submit!!'); console.log('error submit!!')
return false; return false
} }
}); })
}, },
afterQRScan() { afterQRScan() {
// const hash = window.location.hash.slice(1); // const hash = window.location.hash.slice(1)
// const hashObj = getQueryObject(hash); // const hashObj = getQueryObject(hash)
// const originUrl = window.location.origin; // const originUrl = window.location.origin
// history.replaceState({}, '', originUrl); // history.replaceState({}, '', originUrl)
// const codeMap = { // const codeMap = {
// wechat: 'code', // wechat: 'code',
// tencent: 'code' // tencent: 'code'
// }; // }
// const codeName = hashObj[codeMap[this.auth_type]]; // const codeName = hashObj[codeMap[this.auth_type]]
// if (!codeName) { // if (!codeName) {
// alert(''); // alert('')
// } else { // } else {
// this.$store.dispatch('LoginByThirdparty', codeName).then(() => { // this.$store.dispatch('LoginByThirdparty', codeName).then(() => {
// this.$router.push({ path: '/' }); // this.$router.push({ path: '/' })
// }); // })
// } // }
} }
}, },
created() { created() {
// window.addEventListener('hashchange', this.afterQRScan); // window.addEventListener('hashchange', this.afterQRScan)
}, },
destroyed() { destroyed() {
// window.removeEventListener('hashchange', this.afterQRScan); // window.removeEventListener('hashchange', this.afterQRScan)
} }
} }
</script> </script>
@ -119,7 +127,7 @@
.tips { .tips {
font-size: 14px; font-size: 14px;
color: #fff; color: #fff;
margin-bottom: 5px; margin-bottom: 10px;
} }
.login-container { .login-container {
@ -147,6 +155,12 @@
.svg-container { .svg-container {
padding: 6px 5px 6px 15px; padding: 6px 5px 6px 15px;
color: #889aa4; color: #889aa4;
vertical-align: middle;
width: 30px;
display: inline-block;
&_login{
font-size:20px;
}
} }
.title { .title {
font-size: 26px; font-size: 26px;
@ -170,8 +184,13 @@
border-radius: 5px; border-radius: 5px;
color: #454545; color: #454545;
} }
.forget-pwd { .show-pwd{
color: #fff; position: absolute;
right: 10px;
top: 7px;
font-size: 16px;
color: #889aa4;
cursor: pointer;
} }
} }
</style> </style>