Skip to content

Commit

Permalink
修改接口参数名
Browse files Browse the repository at this point in the history
  • Loading branch information
xuexiangjys committed Apr 22, 2019
1 parent 4a049f3 commit 2f0a1af
Show file tree
Hide file tree
Showing 13 changed files with 46 additions and 43 deletions.
1 change: 0 additions & 1 deletion build/index.js
Expand Up @@ -9,7 +9,6 @@ if (process.env.npm_config_preview || rawArgv.includes('--preview')) {

run(`vue-cli-service build ${args}`)

const port = 8088
const publicPath = config.publicPath

var connect = require('connect')
Expand Down
10 changes: 5 additions & 5 deletions mock/user.js → mock/account.js
Expand Up @@ -26,11 +26,11 @@ const users = {
export default [
// user login
{
url: '/user/login',
url: '/account/login',
type: 'post',
response: config => {
const { username } = config.body
const token = tokens[username]
const { loginName } = config.body
const token = tokens[loginName]

// mock error
if (!token) {
Expand All @@ -49,7 +49,7 @@ export default [

// get user info
{
url: '/user/info\.*',
url: '/account/info\.*',
type: 'get',
response: config => {
const { token } = config.query
Expand All @@ -72,7 +72,7 @@ export default [

// user logout
{
url: '/user/logout',
url: '/account/logout',
type: 'post',
response: _ => {
return {
Expand Down
4 changes: 2 additions & 2 deletions mock/index.js
@@ -1,10 +1,10 @@
import Mock from 'mockjs'
import { param2Obj } from '../src/utils'

import user from './user'
import account from './account'

const mocks = [
...user,
...account,
]

// for front mock
Expand Down
6 changes: 3 additions & 3 deletions src/api/user.js
Expand Up @@ -2,23 +2,23 @@ import request from '@/utils/request'

export function login(data) {
return request({
url: '/user/login',
url: '/account/login',
method: 'post',
data
})
}

export function getInfo(token) {
return request({
url: '/user/info',
url: '/account/info',
method: 'get',
params: { token }
})
}

export function logout() {
return request({
url: '/user/logout',
url: '/account/logout',
method: 'post'
})
}
2 changes: 1 addition & 1 deletion src/layout/components/Navbar.vue
Expand Up @@ -56,7 +56,7 @@
this.$store.dispatch('app/toggleSideBar')
},
async logout() {
await this.$store.dispatch('user/logout')
await this.$store.dispatch('account/logout')
this.$router.push(`/login?redirect=${this.$route.fullPath}`)
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/permission.js
Expand Up @@ -40,12 +40,12 @@ router.beforeEach(async (to, from, next) => {
} else {
try {
// get user info
await store.dispatch('user/getInfo')
await store.dispatch('account/getInfo')

next()
} catch (error) {
// remove token and go to login page to re-login
await store.dispatch('user/resetToken')
await store.dispatch('account/resetToken')
Message.error(error || 'Has Error')
next(`/login?redirect=${to.path}`)
NProgress.done()
Expand Down
6 changes: 3 additions & 3 deletions src/store/getters.js
@@ -1,8 +1,8 @@
const getters = {
sidebar: state => state.app.sidebar,
device: state => state.app.device,
token: state => state.user.token,
avatar: state => state.user.avatar,
name: state => state.user.name
token: state => state.account.token,
avatar: state => state.account.avatar,
name: state => state.account.name
}
export default getters
4 changes: 2 additions & 2 deletions src/store/index.js
Expand Up @@ -3,15 +3,15 @@ import Vuex from 'vuex'
import getters from './getters'
import app from './modules/app'
import settings from './modules/settings'
import user from './modules/user'
import account from './modules/account'

Vue.use(Vuex)

const store = new Vuex.Store({
modules: {
app,
settings,
user
account
},
getters
})
Expand Down
10 changes: 5 additions & 5 deletions src/store/modules/user.js → src/store/modules/account.js
Expand Up @@ -31,17 +31,17 @@ const mutations = {
}

const actions = {
// user login
// account login
login({
commit
}, userInfo) {
const {
username,
loginName,
password
} = userInfo
return new Promise((resolve, reject) => {
login({
username: username.trim(),
loginName: loginName.trim(),
password: password
}).then(response => {
const {
Expand All @@ -56,7 +56,7 @@ const actions = {
})
},

// get user info
// get account info
getInfo({
commit,
state
Expand Down Expand Up @@ -85,7 +85,7 @@ const actions = {
})
},

// user logout
// account logout
logout({
commit,
state
Expand Down
8 changes: 4 additions & 4 deletions src/utils/request.js
Expand Up @@ -16,11 +16,11 @@ service.interceptors.request.use(
// do something before request is sent

if (store.getters.token) {
// let each request carry token
// ['X-Token'] is a custom headers key
// please modify it according to the actual situation
// 添加请求Token
config.headers['X-Token'] = getToken()
}
// 添加请求时间戳
config.headers['X-TimeStamp'] = (new Date()).getTime();
return config
},
error => {
Expand Down Expand Up @@ -61,7 +61,7 @@ service.interceptors.response.use(
cancelButtonText: 'Cancel',
type: 'warning'
}).then(() => {
store.dispatch('user/resetToken').then(() => {
store.dispatch('account/resetToken').then(() => {
location.reload()
})
})
Expand Down
2 changes: 1 addition & 1 deletion src/utils/validate.js
Expand Up @@ -14,7 +14,7 @@ export function isExternal(path) {
* @param {string} str
* @returns {Boolean}
*/
export function validUsername(str) {
export function validLoginName(str) {
const valid_map = ['admin', 'editor']
return valid_map.indexOf(str.trim()) >= 0
}
22 changes: 13 additions & 9 deletions src/views/login/index.vue
Expand Up @@ -7,11 +7,11 @@
<h3 class="title">版本更新管理系统</h3>
</div>

<el-form-item prop="username">
<el-form-item prop="loginName">
<span class="svg-container">
<svg-icon icon-class="user" />
</span>
<el-input ref="username" v-model="loginForm.username" placeholder="请输入用户名" name="username" type="text"
<el-input ref="loginName" v-model="loginForm.loginName" placeholder="请输入用户名" name="loginName" type="text"
tabindex="1" auto-complete="on" />
</el-form-item>

Expand All @@ -34,14 +34,14 @@

<script>
import {
validUsername
validLoginName
} from '@/utils/validate'
export default {
name: 'Login',
data() {
const validateUsername = (rule, value, callback) => {
if (!validUsername(value)) {
const validateLoginName = (rule, value, callback) => {
if (!validLoginName(value)) {
callback(new Error('请输入正确的用户名!'))
} else {
callback()
Expand All @@ -56,14 +56,14 @@
}
return {
loginForm: {
username: 'admin',
loginName: 'admin',
password: '123456'
},
loginRules: {
username: [{
loginName: [{
required: true,
trigger: 'blur',
validator: validateUsername
validator: validateLoginName
}],
password: [{
required: true,
Expand Down Expand Up @@ -99,10 +99,14 @@
this.$refs.loginForm.validate(valid => {
if (valid) {
this.loading = true
this.$store.dispatch('user/login', this.loginForm).then(() => {
this.$store.dispatch('account/login', this.loginForm).then(() => {
this.$router.push({
path: this.redirect || '/'
})
this.$message({
type: "success",
message: "登录成功!"
});
this.loading = false
}).catch(() => {
this.loading = false
Expand Down
10 changes: 5 additions & 5 deletions tests/unit/utils/validate.spec.js
@@ -1,10 +1,10 @@
import { validUsername, isExternal } from '@/utils/validate.js'
import { validLoginName, isExternal } from '@/utils/validate.js'

describe('Utils:validate', () => {
it('validUsername', () => {
expect(validUsername('admin')).toBe(true)
expect(validUsername('editor')).toBe(true)
expect(validUsername('xxxx')).toBe(false)
it('validLoginName', () => {
expect(validLoginName('admin')).toBe(true)
expect(validLoginName('editor')).toBe(true)
expect(validLoginName('xxxx')).toBe(false)
})
it('isExternal', () => {
expect(isExternal('https://github.com/PanJiaChen/vue-element-admin')).toBe(true)
Expand Down

0 comments on commit 2f0a1af

Please sign in to comment.