Skip to content

Commit

Permalink
fix: fixed mock server (#2929)
Browse files Browse the repository at this point in the history
* init

* refine

* update
  • Loading branch information
PanJiaChen authored Jan 9, 2020
1 parent fbad7f4 commit c9affcf
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 43 deletions.
10 changes: 5 additions & 5 deletions mock/article.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ for (let i = 0; i < count; i++) {

export default [
{
url: '/article/list',
url: '/vue-element-admin/article/list',
type: 'get',
response: config => {
const { importance, type, title, page = 1, limit = 20, sort } = config.query
Expand Down Expand Up @@ -58,7 +58,7 @@ export default [
},

{
url: '/article/detail',
url: '/vue-element-admin/article/detail',
type: 'get',
response: config => {
const { id } = config.query
Expand All @@ -74,7 +74,7 @@ export default [
},

{
url: '/article/pv',
url: '/vue-element-admin/article/pv',
type: 'get',
response: _ => {
return {
Expand All @@ -92,7 +92,7 @@ export default [
},

{
url: '/article/create',
url: '/vue-element-admin/article/create',
type: 'post',
response: _ => {
return {
Expand All @@ -103,7 +103,7 @@ export default [
},

{
url: '/article/update',
url: '/vue-element-admin/article/update',
type: 'post',
response: _ => {
return {
Expand Down
3 changes: 2 additions & 1 deletion mock/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,10 @@ export function mockXHR() {
// for mock server
const responseFake = (url, type, respond) => {
return {
url: new RegExp(`/mock${url}`),
url: new RegExp(`${process.env.VUE_APP_BASE_API}${url}`),
type: type || 'get',
response(req, res) {
console.log('request invoke:' + req.path)
res.json(Mock.mock(respond instanceof Function ? respond(req, res) : respond))
}
}
Expand Down
4 changes: 2 additions & 2 deletions mock/remote-search.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ NameList.push({ name: 'mock-Pan' })
export default [
// username search
{
url: '/search/user',
url: '/vue-element-admin/search/user',
type: 'get',
response: config => {
const { name } = config.query
Expand All @@ -30,7 +30,7 @@ export default [

// transaction list
{
url: '/transaction/list',
url: '/vue-element-admin/transaction/list',
type: 'get',
response: _ => {
return {
Expand Down
10 changes: 5 additions & 5 deletions mock/role/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const roles = [
export default [
// mock get all routes form server
{
url: '/routes',
url: '/vue-element-admin/routes',
type: 'get',
response: _ => {
return {
Expand All @@ -50,7 +50,7 @@ export default [

// mock get all roles form server
{
url: '/roles',
url: '/vue-element-admin/roles',
type: 'get',
response: _ => {
return {
Expand All @@ -62,7 +62,7 @@ export default [

// add role
{
url: '/role',
url: '/vue-element-admin/role',
type: 'post',
response: {
code: 20000,
Expand All @@ -74,7 +74,7 @@ export default [

// update role
{
url: '/role/[A-Za-z0-9]',
url: '/vue-element-admin/role/[A-Za-z0-9]',
type: 'put',
response: {
code: 20000,
Expand All @@ -86,7 +86,7 @@ export default [

// delete role
{
url: '/role/[A-Za-z0-9]',
url: '/vue-element-admin/role/[A-Za-z0-9]',
type: 'delete',
response: {
code: 20000,
Expand Down
6 changes: 3 additions & 3 deletions mock/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const users = {
export default [
// user login
{
url: '/user/login',
url: '/vue-element-admin/user/login',
type: 'post',
response: config => {
const { username } = config.body
Expand All @@ -49,7 +49,7 @@ export default [

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

// user logout
{
url: '/user/logout',
url: '/vue-element-admin/user/logout',
type: 'post',
response: _ => {
return {
Expand Down
10 changes: 5 additions & 5 deletions src/api/article.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,39 @@ import request from '@/utils/request'

export function fetchList(query) {
return request({
url: '/article/list',
url: '/vue-element-admin/article/list',
method: 'get',
params: query
})
}

export function fetchArticle(id) {
return request({
url: '/article/detail',
url: '/vue-element-admin/article/detail',
method: 'get',
params: { id }
})
}

export function fetchPv(pv) {
return request({
url: '/article/pv',
url: '/vue-element-admin/article/pv',
method: 'get',
params: { pv }
})
}

export function createArticle(data) {
return request({
url: '/article/create',
url: '/vue-element-admin/article/create',
method: 'post',
data
})
}

export function updateArticle(data) {
return request({
url: '/article/update',
url: '/vue-element-admin/article/update',
method: 'post',
data
})
Expand Down
4 changes: 2 additions & 2 deletions src/api/remote-search.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import request from '@/utils/request'

export function searchUser(name) {
return request({
url: '/search/user',
url: '/vue-element-admin/search/user',
method: 'get',
params: { name }
})
}

export function transactionList(query) {
return request({
url: '/transaction/list',
url: '/vue-element-admin/transaction/list',
method: 'get',
params: query
})
Expand Down
10 changes: 5 additions & 5 deletions src/api/role.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,37 @@ import request from '@/utils/request'

export function getRoutes() {
return request({
url: '/routes',
url: '/vue-element-admin/routes',
method: 'get'
})
}

export function getRoles() {
return request({
url: '/roles',
url: '/vue-element-admin/roles',
method: 'get'
})
}

export function addRole(data) {
return request({
url: '/role',
url: '/vue-element-admin/role',
method: 'post',
data
})
}

export function updateRole(id, data) {
return request({
url: `/role/${id}`,
url: `/vue-element-admin/role/${id}`,
method: 'put',
data
})
}

export function deleteRole(id) {
return request({
url: `/role/${id}`,
url: `/vue-element-admin/role/${id}`,
method: 'delete'
})
}
6 changes: 3 additions & 3 deletions src/api/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@ import request from '@/utils/request'

export function login(data) {
return request({
url: '/user/login',
url: '/vue-element-admin/user/login',
method: 'post',
data
})
}

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

export function logout() {
return request({
url: '/user/logout',
url: '/vue-element-admin/user/logout',
method: 'post'
})
}
13 changes: 1 addition & 12 deletions vue.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,7 @@ module.exports = {
warnings: false,
errors: true
},
proxy: {
// change xxx-api/login => mock/login
// detail: https://cli.vuejs.org/config/#devserver-proxy
[process.env.VUE_APP_BASE_API]: {
target: `http://127.0.0.1:${port}/mock`,
changeOrigin: true,
pathRewrite: {
['^' + process.env.VUE_APP_BASE_API]: ''
}
}
},
after: require('./mock/mock-server.js')
before: require('./mock/mock-server.js')
},
configureWebpack: {
// provide the app's title in webpack's name field, so that
Expand Down

1 comment on commit c9affcf

@xianengzhi
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

为了同时用mock数据和真实接口数据,配置了proxy和mock,然而在mock/modules下的文件发生更新时,所有的请求都变404,应该怎么解决?

Please sign in to comment.