From 53c4c7da51707e30649f4cb4ef3f6526612d54c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AF=BB=E6=AC=A2=C2=B7=E6=9D=8E?= Date: Thu, 4 Jan 2018 14:09:28 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AEBUG=EF=BC=8C=E5=8A=A0=E5=85=A5master?= =?UTF-8?q?=E7=9A=84=E6=96=B0=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .babelrc | 3 ++- package.json | 2 +- src/api/dict.js | 6 +++++ src/api/login.js | 4 +-- src/main.js | 1 - src/permission.js | 4 +-- src/store/modules/user.js | 23 +++++++++++++++-- src/utils/request.js | 33 +++++++++++++++++++----- src/views/admin/log/index.vue | 46 +++++++++++++++++++++++++++++++--- src/views/admin/menu/index.vue | 2 +- src/views/admin/user/index.vue | 13 ++++++---- src/views/login/index.vue | 34 ++++++++++++++++++++++--- 12 files changed, 144 insertions(+), 27 deletions(-) diff --git a/.babelrc b/.babelrc index 5e6838bd..b4050098 100644 --- a/.babelrc +++ b/.babelrc @@ -3,5 +3,6 @@ ["env", { "modules": false }], "stage-2" ], - "plugins": ["transform-runtime"] + "plugins": ["transform-runtime"], + "comments": false } diff --git a/package.json b/package.json index 79e2b1b0..446614bb 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,7 @@ "dependencies": { "axios": "0.17.1", "clipboard": "1.7.1", - "element-ui": "2.0.8", + "element-ui": "2.0.10", "js-cookie": "2.2.0", "normalize.css": "7.0.0", "nprogress": "0.2.0", diff --git a/src/api/dict.js b/src/api/dict.js index cce7a049..9c92302a 100644 --- a/src/api/dict.js +++ b/src/api/dict.js @@ -38,3 +38,9 @@ export function putObj(obj) { }) } +export function remote(type) { + return fetch({ + url: '/admin/dict/type/' + type, + method: 'get' + }) +} diff --git a/src/api/login.js b/src/api/login.js index 0c2c3385..67daa8d5 100644 --- a/src/api/login.js +++ b/src/api/login.js @@ -1,6 +1,6 @@ import request from '@/utils/request' -export function login(username, password) { +export function login(username, password, randomStr, code) { var grant_type = 'password' var scope = 'server' return request({ @@ -9,7 +9,7 @@ export function login(username, password) { 'Authorization': 'Basic cGlnOnBpZw==' }, method: 'post', - params: { username, password, grant_type, scope } + params: { username, password, randomStr, code, grant_type, scope } }) } diff --git a/src/main.js b/src/main.js index 199ea629..5ab588e9 100644 --- a/src/main.js +++ b/src/main.js @@ -1,7 +1,6 @@ import Vue from 'vue' import ElementUI from 'element-ui' import 'element-ui/lib/theme-chalk/index.css' -import locale from 'element-ui/lib/locale/lang/en' import locale from 'element-ui/lib/locale/lang/zh-CN' import App from './App' import router from './router' diff --git a/src/permission.js b/src/permission.js index 958fe780..5552925f 100644 --- a/src/permission.js +++ b/src/permission.js @@ -22,12 +22,12 @@ router.beforeEach((to, from, next) => { // 开启Progress } else { if (store.getters.roles.length === 0) { // 判断当前用户是否已拉取完user_info信息 store.dispatch('GetInfo').then(res => { // 拉取用户信息 - const roles = res.data.roles + const roles = res.data.data.roles store.dispatch('GenerateRoutes', { roles }).then(() => { // 生成可访问的路由表 router.addRoutes(store.getters.addRouters) // 动态添加可访问路由表 next({ ...to }) // hack方法 确保addRoutes已完成 }) - }).catch(() => { + }).catch((e) => { store.dispatch('FedLogOut').then(() => { Message.error('验证失败,请重新登录') next({ path: '/login' }) diff --git a/src/store/modules/user.js b/src/store/modules/user.js index 2d0b50ff..2674bb47 100644 --- a/src/store/modules/user.js +++ b/src/store/modules/user.js @@ -6,6 +6,7 @@ const user = { token: getToken(), name: '', avatar: '', + permissions: [], roles: [] }, @@ -38,7 +39,7 @@ const user = { Login({ commit }, userInfo) { const username = userInfo.username.trim() return new Promise((resolve, reject) => { - login(username, userInfo.password).then(response => { + login(username, userInfo.password, userInfo.randomStr, userInfo.code).then(response => { const data = response.data setToken(data.access_token) commit('SET_TOKEN', data.access_token) @@ -54,7 +55,7 @@ const user = { GetInfo({ commit, state }) { return new Promise((resolve, reject) => { getInfo(state.token).then(response => { - const data = response.data + const data = response.data.data commit('SET_ROLES', data.roles) commit('SET_NAME', data.sysUser.username) commit('SET_AVATAR', data.sysUser.avatar) @@ -93,7 +94,25 @@ const user = { removeToken() resolve() }) + }, + + // 动态修改权限 + ChangeRole({ commit }, role) { + return new Promise(resolve => { + commit('SET_TOKEN', role) + setToken(role) + getUserInfo(role).then(response => { + const data = response.data.data + commit('SET_ROLES', data.role) + commit('SET_NAME', data.name) + commit('SET_AVATAR', data.avatar) + commit('SET_INTRODUCTION', data.introduction) + resolve() + }) + }) } + + } } diff --git a/src/utils/request.js b/src/utils/request.js index 668f0fe3..428d49b8 100644 --- a/src/utils/request.js +++ b/src/utils/request.js @@ -22,16 +22,37 @@ service.interceptors.request.use(config => { // respone拦截器 service.interceptors.response.use( - response => response, + response => { + const res = response.data + console.log(res) + if (res.code == 1) { + message(res.msg, 'error') + return Promise.reject(res) + } + return response + }, error => { const res = error.response - Message({ - message: res.status + ': ' + res.data.error, - type: 'error', - duration: 5 * 1000 - }) + console.log(res.status) + if (res.status == 478 || res.status == 403) { + message(res.status + ": " + res.data.msg, 'error') + } else if (res.status == 400) { + message(res.status + ": " + res.data.error_description, 'error') + } else { + message(res.status + ": " + res.data.message, 'error') + } return Promise.reject(error) } ) + +export function message(text, type) { + Message({ + message: text, + type: type, + duration: 5 * 1000 + }) +} + + export default service diff --git a/src/views/admin/log/index.vue b/src/views/admin/log/index.vue index 12f876c6..4a0e79f8 100644 --- a/src/views/admin/log/index.vue +++ b/src/views/admin/log/index.vue @@ -1,6 +1,15 @@ + + + + diff --git a/src/views/admin/menu/index.vue b/src/views/admin/menu/index.vue index ee68f06a..9fece934 100644 --- a/src/views/admin/menu/index.vue +++ b/src/views/admin/menu/index.vue @@ -35,7 +35,7 @@ - + diff --git a/src/views/admin/user/index.vue b/src/views/admin/user/index.vue index e77cae4f..5fb6e04e 100644 --- a/src/views/admin/user/index.vue +++ b/src/views/admin/user/index.vue @@ -81,7 +81,7 @@ - + {{ item.roleDesc }} {{ item.roleCode }} @@ -133,11 +133,11 @@ page: 1, limit: 20 }, + role: undefined, form: { username: undefined, password: undefined, - delFlag: undefined, - role: undefined + delFlag: undefined }, rules: { username: [ @@ -194,7 +194,8 @@ statusFilter(status) { const statusMap = { 0: '有效', - 1: '无效' + 1: '无效', + 9: '锁定' } return statusMap[status] } @@ -241,7 +242,7 @@ getObj(row.userId) .then(response => { this.form = response.data - this.form.role = row.roleList[0].roleId + this.role = row.roleList[0].roleId this.dialogFormVisible = true this.dialogStatus = 'update' }) @@ -254,6 +255,7 @@ const set = this.$refs set[formName].validate(valid => { if (valid) { + this.form.role = this.role addObj(this.form) .then(() => { this.dialogFormVisible = false @@ -280,6 +282,7 @@ if (valid) { this.dialogFormVisible = false this.form.password = undefined + this.form.role = this.role putObj(this.form).then(() => { this.dialogFormVisible = false this.getList() diff --git a/src/views/login/index.vue b/src/views/login/index.vue index b4433be3..3a33d485 100644 --- a/src/views/login/index.vue +++ b/src/views/login/index.vue @@ -17,6 +17,22 @@ placeholder="密码"> + + + + + + + + + + + + + + + + 登陆 @@ -40,19 +56,27 @@ export default { } } return { + src: '', loginForm: { username: null, - password: null + password: null, + code: '', + randomStr: Math.ceil(Math.random() * 100000) + "_" + Date.now() }, loginRules: { username: [{ required: true, trigger: 'blur' }], - password: [{ required: true, trigger: 'blur', validator: validatePass }] + password: [{ required: true, trigger: 'blur', validator: validatePass }], + code: [{ required: true, trigger: 'blur' }], }, loading: false, pwdType: 'password' } }, methods: { + refreshCode: function() { + this.loginForm.randomStr = Math.ceil(Math.random() * 100000) + Date.now() + this.src = '/admin/code/' + this.loginForm.randomStr + }, showPwd() { if (this.pwdType === 'password') { this.pwdType = '' @@ -69,13 +93,17 @@ export default { this.$router.push({ path: '/' }) }).catch(() => { this.loading = false + this.refreshCode() }) } else { - console.log('error submit!!') + console.log('想搞事情??') return false } }) } + }, + created() { + this.src = '/admin/code/' + this.loginForm.randomStr } }