Commit 8f091636 authored by 罗超's avatar 罗超

处理数据访问问题

parent f99b2f0b
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
"@quasar/extras": "^1.0.0", "@quasar/extras": "^1.0.0",
"axios": "^0.18.1", "axios": "^0.18.1",
"core-js": "^3.6.5", "core-js": "^3.6.5",
"lockr": "^0.8.5",
"quasar": "^1.0.0", "quasar": "^1.0.0",
"vue-i18n": "^8.0.0" "vue-i18n": "^8.0.0"
}, },
......
...@@ -19,7 +19,6 @@ module.exports = function(ctx) { ...@@ -19,7 +19,6 @@ module.exports = function(ctx) {
// --> boot files are part of "main.js" // --> boot files are part of "main.js"
// https://quasar.dev/quasar-cli/boot-files // https://quasar.dev/quasar-cli/boot-files
boot: [ boot: [
'i18n', 'i18n',
'axios', 'axios',
'permission' 'permission'
...@@ -48,7 +47,7 @@ module.exports = function(ctx) { ...@@ -48,7 +47,7 @@ module.exports = function(ctx) {
build: { build: {
vueRouterMode: 'hash', // available values: 'hash', 'history' vueRouterMode: 'hash', // available values: 'hash', 'history'
env: ctx.dev ? { env: ctx.dev ? {
API: 'https://dev.api.com' API: 'http://192.168.1.214:8300/api'
} : { } : {
API: 'https://prod.api.com' API: 'https://prod.api.com'
}, },
......
...@@ -3,16 +3,17 @@ ...@@ -3,16 +3,17 @@
*/ */
import request from '../utils/request' import request from '../utils/request'
export function login({ account, pwd }) { export function login({ Account, Password }) {
return request({
url: '/login',
method: "post",
// headers: { // headers: {
// 'Content-Type': 'application/json;charset=UTF-8' // 'Content-Type': 'application/json;charset=UTF-8'
// }, 申明Headers信息,不包含token ,token通过注入添加 // }, 申明Headers信息,不包含token ,token通过注入添加
return request({
url: '/login/login',
method: "post",
data: { data: {
account, Account,
pwd Password,
AccountType: 1
} }
}); });
} }
......
import router from "../router/index"; import router from "../router/index";
import store from "../store"; import store from "../store/index";
import { LoadingBar, Notify } from "quasar"; import { LoadingBar, Notify } from "quasar";
import { getAuth } from "../utils/auth"; // 验权 import { getAuth } from "../utils/auth"; // 验权
LoadingBar.setDefaults({ LoadingBar.setDefaults({
color: "primary", color: "primary",
size: "10px", size: "5px",
position: "bottom" position: "bottom"
}); });
let loadAsyncRouter = false; let loadAsyncRouter = false;
const whiteList = ["/login", "/"]; // 不重定向白名单 const whiteList = ["/login", "/"]; // 不重定向白名单
console.log(router) console.log(router)
...@@ -16,9 +15,10 @@ router.beforeEach((to, from, next) => { ...@@ -16,9 +15,10 @@ router.beforeEach((to, from, next) => {
LoadingBar.start(); LoadingBar.start();
/** 请求头包含授权信息 并且 页面必须授权 直接进入 */ /** 请求头包含授权信息 并且 页面必须授权 直接进入 */
if (getAuth()) { if (getAuth()) {
if (to.path === "/login") { //debugger;
if (to.path === "/login" || to.path === "/") {
next({ next({
path: "/" path: "/home"
}); });
LoadingBar.stop(); LoadingBar.stop();
} else { } else {
...@@ -28,7 +28,7 @@ router.beforeEach((to, from, next) => { ...@@ -28,7 +28,7 @@ router.beforeEach((to, from, next) => {
if (store.getters.allAuth) { if (store.getters.allAuth) {
store.dispatch("GenerateRoutes", store.getters.allAuth).then(() => { store.dispatch("GenerateRoutes", store.getters.allAuth).then(() => {
// 根据auths权限生成可访问的路由表 // 根据auths权限生成可访问的路由表
router.addRoutes(store.getters.addRouters); // 动态添加可访问路由表 //router.addRoutes(store.getters.addRouters); // 动态添加可访问路由表
if (to.path === "/404") { if (to.path === "/404") {
next({ next({
path: to.redirectedFrom || "/", path: to.redirectedFrom || "/",
......
...@@ -127,8 +127,8 @@ export default { ...@@ -127,8 +127,8 @@ export default {
// }) // })
this.$store this.$store
.dispatch('Login', { .dispatch('Login', {
account: this.account, Account: this.account,
pwd: this.pwd Password: this.pwd,
}) })
.then(res => { .then(res => {
this.$router.push({ this.$router.push({
......
const getters = {
userInfo: state => state.user.userInfo,
logo: state => {
//TODO 根据登录信息返回
return '' //require('@/assets/img/logo.png')
},
name: state => {
// if (state.app.name) {
// return state.app.name
// }
//TODO 根据登录信息返回
return '悟空CRM系统'
},
// 权限
allAuth: state => state.user.allAuth,
}
export default getters
\ No newline at end of file
import Vue from 'vue' import Vue from 'vue'
import Vuex from 'vuex' import Vuex from 'vuex'
import user from './modules/user' import user from './modules/user'
// import example from './module-example' import permission from './modules/permission'
import getters from './getters'
Vue.use(Vuex) Vue.use(Vuex)
// export default function( /* { ssrContext } */ ) {
// const Store = new Vuex.Store({
// modules: {
// // example
// user,
// permission
// },
// getters,
// // enable strict mode (adds overhead!)
// // for dev mode only
/* // })
* If not building with SSR mode, you can
* directly export the Store instantiation;
*
* The function below can be async too; either use
* async/await or return a Promise which resolves
* with the Store instance.
*/
export default function( /* { ssrContext } */ ) { // return Store
const Store = new Vuex.Store({ // }
const store = new Vuex.Store({
modules: { modules: {
// example user,
user permission
}, },
getters,
// enable strict mode (adds overhead!)
// for dev mode only
strict: process.env.DEV strict: process.env.DEV
}) })
return Store export default store
} \ No newline at end of file
\ No newline at end of file
import Vue from 'vue'
/**
*
* @param {*} router
* @param {*} authInfo
*/
function checkAuth(router, authInfo) {
// 判断当前路由在权限数组中是否存在
if (router.meta) {
const metaInfo = router.meta
if (!metaInfo.requiresAuth) {
return true
} else {
if (metaInfo.index == 0) {
return !!authInfo[metaInfo.type]
} else if (metaInfo.index == 1) {
if (authInfo[metaInfo.type]) {
return !!authInfo[metaInfo.type][metaInfo.subType]
}
return false
} else {
var typeAuth = authInfo[metaInfo.type]
for (let index = 0; index < metaInfo.subType.length; index++) {
const field = metaInfo.subType[index]
typeAuth = typeAuth[field]
if (typeAuth && metaInfo.subType.length - 1 == index) {
return true
} else if (!typeAuth) {
return false
}
}
}
}
}
return true
}
/**
*
* @param {*} routers
* @param {*} authInfo
*/
const filterAsyncRouter = function(routers, authInfo) {
const res = []
routers.forEach(router => {
const tmp = {
...router
}
if (checkAuth(tmp, authInfo)) {
if (tmp.children) {
tmp.children = filterAsyncRouter(tmp.children, authInfo)
}
res.push(tmp)
}
})
return res
}
const permission = {
state: {
addRouters: []
},
mutations: {
SET_ROUTERS: (state, routers) => {
state.addRouters = routers
}
},
actions: {
GenerateRoutes({
commit
}, data) {
return new Promise(resolve => {
// TODO 实现权限验证
resolve()
})
}
}
}
export default permission
\ No newline at end of file
...@@ -7,7 +7,7 @@ const user = { ...@@ -7,7 +7,7 @@ const user = {
state: { state: {
userInfo: null, // 用户信息 userInfo: null, // 用户信息
//TODO 用户权限 //TODO 用户权限
allAuth: null, allAuth: ['/home'],
}, },
mutations: { mutations: {
...@@ -23,22 +23,26 @@ const user = { ...@@ -23,22 +23,26 @@ const user = {
actions: { actions: {
// 登录 // 登录
Login({ commit }, userInfo) { Login({ commit }, userInfo) {
const username = userInfo.account.trim(); userInfo.Account = userInfo.Account.trim();
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
login(username, userInfo.password) login(userInfo)
.then(data => { .then(data => {
Lockr.set("Token", data["Token"]); let r = data.Data
Lockr.set("loginUserInfo", data.user); let token = r.Token
delete r.Token
Lockr.set("Token", token);
Lockr.set("loginUserInfo", r);
Lockr.set("authList", data.auth); //Lockr.set("authList", data.auth);
addAuth(data["Token"]); addAuth(token);
commit("SET_USERINFO", data.user); // commit("SET_USERINFO", data.user);
commit('SET_ALLAUTH', data.auth) // commit('SET_ALLAUTH', data.auth)
// TODO 完成登录 读取权限 // TODO 完成登录 读取权限
resolve(data); resolve(data);
}) })
.catch(error => { .catch(error => {
console.log(error)
reject(error); reject(error);
}); });
}); });
......
...@@ -23,7 +23,8 @@ const cache = { ...@@ -23,7 +23,8 @@ const cache = {
*/ */
updateAxiosCache: function() { updateAxiosCache: function() {
axios.defaults.headers['Token'] = Lockr.get('Token') axios.defaults.headers['Token'] = Lockr.get('Token')
store.dispatch('GetUserInfo') //TODO 更新用户信息
//store.dispatch('GetUserInfo')
}, },
/** /**
* 移除登录信息 * 移除登录信息
......
...@@ -4,7 +4,7 @@ import qs from "qs"; ...@@ -4,7 +4,7 @@ import qs from "qs";
import { Dialog, Notify } from 'quasar' import { Dialog, Notify } from 'quasar'
var showLoginMessageBox = false; var showLoginMessageBox = false;
axios.defaults.headers.post["Content-Type"] = "application/json;charset=UTF-8"; //application/x-www-form-urlencoded;charset=UTF-8 axios.defaults.headers["Content-Type"] = "application/json;charset=UTF-8"; //application/x-www-form-urlencoded;charset=UTF-8
const service = axios.create({ const service = axios.create({
baseURL: process.env.API, baseURL: process.env.API,
...@@ -14,6 +14,7 @@ const service = axios.create({ ...@@ -14,6 +14,7 @@ const service = axios.create({
// request拦截器 // request拦截器
service.interceptors.request.use( service.interceptors.request.use(
config => { config => {
console.log(config.headers)
const flag = const flag =
config.headers["Content-Type"] && config.headers["Content-Type"] &&
config.headers["Content-Type"].indexOf("application/json") !== -1; config.headers["Content-Type"].indexOf("application/json") !== -1;
...@@ -26,6 +27,10 @@ service.interceptors.request.use( ...@@ -26,6 +27,10 @@ service.interceptors.request.use(
} else { } else {
config.data = qs.stringify(config.data); config.data = qs.stringify(config.data);
} }
} else {
config.data = {
"Msg": config.data
}
} }
return config; return config;
}, },
...@@ -43,9 +48,9 @@ service.interceptors.response.use( ...@@ -43,9 +48,9 @@ service.interceptors.response.use(
if (response.status === 200 && response.config.responseType === "blob") { if (response.status === 200 && response.config.responseType === "blob") {
// 文件类型特殊处理 // 文件类型特殊处理
return response; return response;
} else if (res.code !== 0) { } else if (res.Code != 1) {
// TODO 确定后台的CODE码 // TODO 确定后台的CODE码
if (res.code === 302) { if (res.Code == 302) {
if (!showLoginMessageBox) { if (!showLoginMessageBox) {
showLoginMessageBox = true; showLoginMessageBox = true;
Dialog.create({ Dialog.create({
...@@ -63,11 +68,11 @@ service.interceptors.response.use( ...@@ -63,11 +68,11 @@ service.interceptors.response.use(
}); });
} }
} else { } else {
if (res.msg) { if (res.Message) {
Notify.create({ Notify.create({
icon: "error", icon: "error",
color: "negative", color: "negative",
message: res.msg, message: res.Message,
position: "top" position: "top"
}); });
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment