-
Notifications
You must be signed in to change notification settings - Fork 0
/
mock.js
69 lines (59 loc) · 1.46 KB
/
mock.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/**
* mock.js 提供应用截获ajax请求,为脱离后台测试使用
* 模拟查询更改内存中mockData,并返回数据
*/
import { fetch } from 'mk-utils'
const mockData = fetch.mockData
function initMockData() {
//计量单位
if (!mockData.meaUnits) {
mockData.meaUnits = [{
id: 1,
name: '个'
}, {
id: 2,
name: '件'
}, {
id: 3,
name: '台'
}, {
id: 4,
name: '斤'
}, {
id: 5,
name: '双'
}]
}
}
function findNode(id, types) {
for (let t of types) {
if (t.id == id)
return t
else if (t.children) {
let n = findNode(id, t.children)
if (n) return n
}
}
}
fetch.mock('/v1/meaUnit/create', (option) => {
initMockData()
var maxId = 0
mockData.meaUnits.forEach(o => {
maxId = maxId > o.id ? maxId : o.id
})
var newId = maxId + 1
const v = { name: option.name, id: newId }
mockData.meaUnits.push(v)
return { result: true, value: v }
})
fetch.mock('/v1/meaUnit/update', (option) => {
initMockData()
const node = findNode(option.id, mockData.meaUnits)
node.name = option.name
return { result: true, value: node }
})
fetch.mock('/v1/meaUnit/findById', (id) => {
initMockData()
const node = findNode(id, mockData.meaUnits)
return { result: true, value: node }
})