Skip to content

Commit

Permalink
chore: optimize services model;:star2:
Browse files Browse the repository at this point in the history
  • Loading branch information
iczer committed Aug 8, 2020
1 parent d741904 commit 45d62e4
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 26 deletions.
3 changes: 2 additions & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
VUE_APP_NAME=admin
VUE_APP_NAME=Admin
VUE_APP_ROUTES_KEY=admin.routes
VUE_APP_PERMISSIONS_KEY=admin.permissions
VUE_APP_ROLES_KEY=admin.roles
VUE_APP_USER_KEY=admin.user
VUE_APP_API_BASE_URL=http://api.iczer.com
6 changes: 1 addition & 5 deletions .env.development
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
VUE_APP_NAME=admin
VUE_APP_ROUTES_KEY=admin.routes
VUE_APP_PERMISSIONS_KEY=admin.permissions
VUE_APP_ROLES_KEY=admin.roles
VUE_APP_USER_KEY=admin.user
VUE_APP_API_BASE_URL=http://dev.iczer.com
2 changes: 1 addition & 1 deletion src/layouts/header/HeaderAvatar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

<script>
import {mapGetters} from 'vuex'
import {logout} from '@/services'
import {logout} from '@/services/user'
export default {
name: 'HeaderAvatar',
Expand Down
3 changes: 1 addition & 2 deletions src/mock/user/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ const user = Mock.mock({
address: '@CITY',
position: '@POSITION'
})

Mock.mock('/login', 'post', ({body}) => {
Mock.mock(`${process.env.VUE_APP_API_BASE_URL}/login`, 'post', ({body}) => {
let result = {}
const {name, password} = JSON.parse(body)

Expand Down
2 changes: 1 addition & 1 deletion src/mock/user/routes.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Mock from 'mockjs'

Mock.mock('/routes', 'get', () => {
Mock.mock(`${process.env.VUE_APP_API_BASE_URL}/routes`, 'get', () => {
let result = {}
result.code = 0
result.data = [{
Expand Down
6 changes: 2 additions & 4 deletions src/pages/login/Login.vue
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@

<script>
import CommonLayout from '@/layouts/CommonLayout'
import {login, getRoutesConfig} from '@/services'
import {login, getRoutesConfig} from '@/services/user'
import {setAuthorization} from '@/utils/request'
import {loadRoutes} from '@/utils/routerUtil'
import {mapMutations} from 'vuex'
Expand Down Expand Up @@ -112,9 +112,7 @@ export default {
this.logging = false
const loginRes = res.data
if (loginRes.code >= 0) {
const user = loginRes.data.user
const permissions = loginRes.data.permissions
const roles = loginRes.data.roles
const {user, permissions, roles} = loginRes.data
this.setUser(user)
this.setPermissions(permissions)
this.setRoles(roles)
Expand Down
6 changes: 4 additions & 2 deletions src/services/api.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// const BASE_URL = 'http://localhost:8080' your service base url
const BASE_URL = '' // mock base url
//跨域代理前缀
// const API_PROXY_PREFIX='/api'
// const BASE_URL = process.env.NODE_ENV === 'production' ? process.env.VUE_APP_API_BASE_URL : API_PROXY_PREFIX
const BASE_URL = process.env.VUE_APP_API_BASE_URL
module.exports = {
LOGIN: `${BASE_URL}/login`,
ROUTES: `${BASE_URL}/routes`
Expand Down
6 changes: 2 additions & 4 deletions src/services/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import {login, logout, getRoutesConfig} from './user'
import userService from './user'

export {
login,
logout,
getRoutesConfig
userService
}
13 changes: 8 additions & 5 deletions src/services/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,28 @@ import {request, METHOD, removeAuthorization} from '@/utils/request'
* @param password 账户密码
* @returns {Promise<AxiosResponse<T>>}
*/
async function login(name, password) {
export async function login(name, password) {
return request(LOGIN, METHOD.POST, {
name: name,
password: password
})
}

async function getRoutesConfig() {
export async function getRoutesConfig() {
return request(ROUTES, METHOD.GET)
}

/**
* 退出登录
*/
function logout() {
export function logout() {
localStorage.removeItem(process.env.VUE_APP_ROUTES_KEY)
localStorage.removeItem(process.env.VUE_APP_PERMISSIONS_KEY)
localStorage.removeItem(process.env.VUE_APP_ROLES_KEY)
removeAuthorization()
}

export {login, logout, getRoutesConfig}
export default {
login,
logout,
getRoutesConfig
}
12 changes: 11 additions & 1 deletion vue.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,18 @@ let path = require('path')
const ThemeColorReplacer = require('webpack-theme-color-replacer')
const {getThemeColors, modifyVars} = require('./src/utils/themeUtil')
const {resolveCss} = require('./src/utils/theme-color-replacer-extend')

module.exports = {
devServer: {
// proxy: {
// '/api': { //此处要与 /services/api.js 中的 API_PROXY_PREFIX 值保持一致
// target: process.env.VUE_APP_API_BASE_URL,
// changeOrigin: true,
// pathRewrite: {
// '^/api': ''
// }
// }
// }
},
pluginOptions: {
'style-resources-loader': {
preProcessor: 'less',
Expand Down

0 comments on commit 45d62e4

Please sign in to comment.