perf[chore]: add mock.js to instead of easy-mock (#281)
parent
01018ee1f9
commit
5d82985a76
@ -0,0 +1,26 @@
|
|||||||
|
import Mock from 'mockjs'
|
||||||
|
import userAPI from './user'
|
||||||
|
import tableAPI from './table'
|
||||||
|
|
||||||
|
// Fix an issue with setting withCredentials = true, cross-domain request lost cookies
|
||||||
|
// https://github.com/nuysoft/Mock/issues/300
|
||||||
|
Mock.XHR.prototype.proxy_send = Mock.XHR.prototype.send
|
||||||
|
Mock.XHR.prototype.send = function() {
|
||||||
|
if (this.custom.xhr) {
|
||||||
|
this.custom.xhr.withCredentials = this.withCredentials || false
|
||||||
|
}
|
||||||
|
this.proxy_send(...arguments)
|
||||||
|
}
|
||||||
|
// Mock.setup({
|
||||||
|
// timeout: '350-600'
|
||||||
|
// })
|
||||||
|
|
||||||
|
// User
|
||||||
|
Mock.mock(/\/user\/login/, 'post', userAPI.login)
|
||||||
|
Mock.mock(/\/user\/info/, 'get', userAPI.getInfo)
|
||||||
|
Mock.mock(/\/user\/logout/, 'post', userAPI.logout)
|
||||||
|
|
||||||
|
// Table
|
||||||
|
Mock.mock(/\/table\/list/, 'get', tableAPI.list)
|
||||||
|
|
||||||
|
export default Mock
|
@ -0,0 +1,20 @@
|
|||||||
|
import Mock from 'mockjs'
|
||||||
|
|
||||||
|
export default {
|
||||||
|
list: () => {
|
||||||
|
const items = Mock.mock({
|
||||||
|
'items|30': [{
|
||||||
|
id: '@id',
|
||||||
|
title: '@sentence(10, 20)',
|
||||||
|
'status|1': ['published', 'draft', 'deleted'],
|
||||||
|
author: 'name',
|
||||||
|
display_time: '@datetime',
|
||||||
|
pageviews: '@integer(300, 5000)'
|
||||||
|
}]
|
||||||
|
})
|
||||||
|
return {
|
||||||
|
code: 20000,
|
||||||
|
data: items
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,64 @@
|
|||||||
|
import { param2Obj } from './utils'
|
||||||
|
|
||||||
|
const tokens = {
|
||||||
|
admin: {
|
||||||
|
token: 'admin-token'
|
||||||
|
},
|
||||||
|
editor: {
|
||||||
|
token: 'editor-token'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const users = {
|
||||||
|
'admin-token': {
|
||||||
|
roles: ['admin'],
|
||||||
|
introduction: 'I am a super administrator',
|
||||||
|
avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
|
||||||
|
name: 'Super Admin'
|
||||||
|
},
|
||||||
|
'editor-token': {
|
||||||
|
roles: ['editor'],
|
||||||
|
introduction: 'I am an editor',
|
||||||
|
avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
|
||||||
|
name: 'Normal Editor'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default {
|
||||||
|
login: res => {
|
||||||
|
const { username } = JSON.parse(res.body)
|
||||||
|
const data = tokens[username]
|
||||||
|
|
||||||
|
if (data) {
|
||||||
|
return {
|
||||||
|
code: 20000,
|
||||||
|
data
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
code: 60204,
|
||||||
|
message: 'Account and password are incorrect.'
|
||||||
|
}
|
||||||
|
},
|
||||||
|
getInfo: res => {
|
||||||
|
const { token } = param2Obj(res.url)
|
||||||
|
const info = users[token]
|
||||||
|
|
||||||
|
if (info) {
|
||||||
|
return {
|
||||||
|
code: 20000,
|
||||||
|
data: info
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
code: 50008,
|
||||||
|
message: 'Login failed, unable to get user details.'
|
||||||
|
}
|
||||||
|
},
|
||||||
|
logout: () => {
|
||||||
|
return {
|
||||||
|
code: 20000,
|
||||||
|
data: 'success'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,14 @@
|
|||||||
|
export function param2Obj(url) {
|
||||||
|
const search = url.split('?')[1]
|
||||||
|
if (!search) {
|
||||||
|
return {}
|
||||||
|
}
|
||||||
|
return JSON.parse(
|
||||||
|
'{"' +
|
||||||
|
decodeURIComponent(search)
|
||||||
|
.replace(/"/g, '\\"')
|
||||||
|
.replace(/&/g, '","')
|
||||||
|
.replace(/=/g, '":"') +
|
||||||
|
'"}'
|
||||||
|
)
|
||||||
|
}
|
Loading…
Reference in New Issue