From 47b24ca8b97cc25b4e1d6ccfa04ab8aca242f075 Mon Sep 17 00:00:00 2001 From: Pan Date: Mon, 24 Apr 2017 14:15:42 +0800 Subject: [PATCH] refine login --- package.json | 2 +- src/api/login.js | 30 ++++++++++++++ src/main.js | 1 - src/mock/index.js | 9 ++++- src/mock/login.js | 30 +++++++++++++- src/store/modules/user.js | 59 ++++++++++++++++------------ src/views/dashboard/editor/index.vue | 1 - src/views/layout/Layout.vue | 2 +- src/views/layout/Sidebar.vue | 1 - src/views/login/index.vue | 5 +-- 10 files changed, 104 insertions(+), 36 deletions(-) create mode 100644 src/api/login.js diff --git a/package.json b/package.json index bf4e6041..e3340062 100644 --- a/package.json +++ b/package.json @@ -52,7 +52,7 @@ "eslint-config-airbnb-base": "11.0.1", "eslint-friendly-formatter": "2.0.7", "eslint-import-resolver-webpack": "0.8.1", - "eslint-loader": "1.6.1", + "eslint-loader": "1.6.3", "eslint-plugin-html": "2.0.0", "eslint-plugin-import": "2.2.0", "eventsource-polyfill": "0.9.6", diff --git a/src/api/login.js b/src/api/login.js new file mode 100644 index 00000000..639f650f --- /dev/null +++ b/src/api/login.js @@ -0,0 +1,30 @@ +import { fetch } from 'utils/fetch'; + +export function loginByEmail(email, password) { + const data = { + email, + password + }; + return fetch({ + url: '/login/loginbyemail', + method: 'post', + params: data + }); +} + +export function logout() { + return fetch({ + url: '/login/logout', + method: 'post' + }); +} + + +export function getInfo(token) { + return fetch({ + url: '/user/info', + method: 'get', + params: { token } + }); +} + diff --git a/src/main.js b/src/main.js index 6a8ed647..569706fa 100644 --- a/src/main.js +++ b/src/main.js @@ -49,7 +49,6 @@ router.beforeEach((to, from, next) => { if (to.path === '/login') { next({ path: '/' }); } else { - console.log('a') if (to.meta && to.meta.role) { if (hasPermission(store.getters.roles, to.meta.role)) { next(); diff --git a/src/mock/index.js b/src/mock/index.js index b82820c3..e6002e0b 100644 --- a/src/mock/index.js +++ b/src/mock/index.js @@ -1,18 +1,25 @@ import axios from 'axios'; import MockAdapter from 'axios-mock-adapter'; +import loginAPI from './login'; import articleAPI from './article'; import article_tableAPI from './article_table'; import remoteSearchAPI from './remoteSearch'; const mock = new MockAdapter(axios); +// 登录相关 +mock.onPost('/login/loginbyemail').reply(loginAPI.loginByEmail); +mock.onPost('/login/logout').reply(loginAPI.logout); +mock.onGet('/user/info').reply(loginAPI.getInfo); +// 文章相关 mock.onGet('/article/list').reply(articleAPI.getList); mock.onGet('/article/detail').reply(articleAPI.getArticle); +// table example相关 mock.onGet('/article_table/list').reply(article_tableAPI.getList); mock.onGet('/article_table/pv').reply(article_tableAPI.getPv); - +// 搜索相关 mock.onGet('/search/user').reply(remoteSearchAPI.searchUser); diff --git a/src/mock/login.js b/src/mock/login.js index 3bd1c9e0..e852929a 100644 --- a/src/mock/login.js +++ b/src/mock/login.js @@ -22,4 +22,32 @@ const userMap = { name: '工程师小王' } } -export default userMap + +export default { + loginByEmail: config => { + const { email } = config.params; + return new Promise(resolve => { + setTimeout(() => { + resolve([200, { + data: userMap[email.split('@')[0]] + }]); + }, 500); + }) + }, + getInfo: config => { + const { token } = config.params; + console.log(userMap[token]) + return new Promise(resolve => { + setTimeout(() => { + resolve([200, { + data: userMap[token] + }]); + }, 100); + }) + }, + logout: () => new Promise(resolve => { + setTimeout(() => { + resolve([200, { data: 'success' }]); + }, 100); + }) +}; diff --git a/src/store/modules/user.js b/src/store/modules/user.js index b3f51dee..b6130f19 100644 --- a/src/store/modules/user.js +++ b/src/store/modules/user.js @@ -1,7 +1,5 @@ -// import { loginByEmail, loginByThirdparty } from 'api/login'; -// import { userInfo, userLogout } from 'api/adminUser'; +import { loginByEmail, logout, getInfo } from 'api/login'; import Cookies from 'js-cookie'; -import userMap from 'mock/login'; const user = { state: { @@ -64,21 +62,39 @@ const user = { }, actions: { - // 邮箱登录 + // 邮箱登录 LoginByEmail({ commit }, userInfo) { return new Promise((resolve, reject) => { - const email = userInfo.email.split('@')[0]; - if (userMap[email]) { - commit('SET_ROLES', userMap[email].role); - commit('SET_TOKEN', userMap[email].token); - Cookies.set('X-Ivanka-Token', userMap[email].token); + loginByEmail(userInfo.email, userInfo.password).then(response => { + const data = response.data; + Cookies.set('X-Ivanka-Token', response.data.token); + commit('SET_TOKEN', data.token); + commit('SET_EMAIL', userInfo.email); resolve(); - } else { - reject('账号不正确'); - } + }).catch(error => { + reject(error); + }); }); }, - // 第三方验证登录 + + + // 获取用户信息 + GetInfo({ commit, state }) { + return new Promise((resolve, reject) => { + getInfo(state.token).then(response => { + const data = response.data; + commit('SET_ROLES', data.role); + commit('SET_NAME', data.name); + commit('SET_AVATAR', data.avatar); + commit('SET_INTRODUCTION', data.introduction); + resolve(response); + }).catch(error => { + reject(error); + }); + }); + }, + + // 第三方验证登录 LoginByThirdparty({ commit, state }, code) { return new Promise((resolve, reject) => { commit('SET_CODE', code); @@ -91,21 +107,12 @@ const user = { }); }); }, - // 获取用户信息 - GetInfo({ commit, state }) { - return new Promise(resolve => { - const token = state.token; - commit('SET_ROLES', userMap[token].role); - commit('SET_NAME', userMap[token].name); - commit('SET_AVATAR', userMap[token].avatar); - commit('SET_INTRODUCTION', userMap[token].introduction); - resolve(); - }); - }, - // 登出 + + + // 登出 LogOut({ commit, state }) { return new Promise((resolve, reject) => { - userLogout(state.token).then(() => { + logout(state.token).then(() => { commit('SET_TOKEN', ''); Cookies.remove('X-Ivanka-Token'); resolve(); diff --git a/src/views/dashboard/editor/index.vue b/src/views/dashboard/editor/index.vue index 2ea108a7..d3ca0f42 100644 --- a/src/views/dashboard/editor/index.vue +++ b/src/views/dashboard/editor/index.vue @@ -121,7 +121,6 @@ methods: { fetchData() { getList(this.listQuery).then(response => { - console.log(response.data) this.list = response.data; }) } diff --git a/src/views/layout/Layout.vue b/src/views/layout/Layout.vue index 3a6fe230..507ce125 100644 --- a/src/views/layout/Layout.vue +++ b/src/views/layout/Layout.vue @@ -30,12 +30,12 @@ } }, beforeRouteEnter: (to, from, next) => { - console.log('b') const roles = store.getters.roles; if (roles.length !== 0) { next(); return } + // loadingInstance = Loading.service({ fullscreen: true, text: '玩命加载中' }); store.dispatch('GetInfo').then(() => { permission.init({ diff --git a/src/views/layout/Sidebar.vue b/src/views/layout/Sidebar.vue index 2dea521e..c4c54f14 100644 --- a/src/views/layout/Sidebar.vue +++ b/src/views/layout/Sidebar.vue @@ -27,7 +27,6 @@