Browse Source

perf[chore]: add mock.js to instead of easy-mock (#281)

花裤衩 6 years ago
parent
commit
5d82985a76
7 changed files with 136 additions and 1 deletions
  1. 26 0
      mock/index.js
  2. 20 0
      mock/table.js
  3. 64 0
      mock/user.js
  4. 14 0
      mock/utils.js
  5. 1 0
      package.json
  6. 10 0
      src/main.js
  7. 1 1
      src/router/index.js

+ 26 - 0
mock/index.js

@@ -0,0 +1,26 @@
1
+import Mock from 'mockjs'
2
+import userAPI from './user'
3
+import tableAPI from './table'
4
+
5
+// Fix an issue with setting withCredentials = true, cross-domain request lost cookies
6
+// https://github.com/nuysoft/Mock/issues/300
7
+Mock.XHR.prototype.proxy_send = Mock.XHR.prototype.send
8
+Mock.XHR.prototype.send = function() {
9
+  if (this.custom.xhr) {
10
+    this.custom.xhr.withCredentials = this.withCredentials || false
11
+  }
12
+  this.proxy_send(...arguments)
13
+}
14
+// Mock.setup({
15
+//   timeout: '350-600'
16
+// })
17
+
18
+// User
19
+Mock.mock(/\/user\/login/, 'post', userAPI.login)
20
+Mock.mock(/\/user\/info/, 'get', userAPI.getInfo)
21
+Mock.mock(/\/user\/logout/, 'post', userAPI.logout)
22
+
23
+// Table
24
+Mock.mock(/\/table\/list/, 'get', tableAPI.list)
25
+
26
+export default Mock

+ 20 - 0
mock/table.js

@@ -0,0 +1,20 @@
1
+import Mock from 'mockjs'
2
+
3
+export default {
4
+  list: () => {
5
+    const items = Mock.mock({
6
+      'items|30': [{
7
+        id: '@id',
8
+        title: '@sentence(10, 20)',
9
+        'status|1': ['published', 'draft', 'deleted'],
10
+        author: 'name',
11
+        display_time: '@datetime',
12
+        pageviews: '@integer(300, 5000)'
13
+      }]
14
+    })
15
+    return {
16
+      code: 20000,
17
+      data: items
18
+    }
19
+  }
20
+}

+ 64 - 0
mock/user.js

@@ -0,0 +1,64 @@
1
+import { param2Obj } from './utils'
2
+
3
+const tokens = {
4
+  admin: {
5
+    token: 'admin-token'
6
+  },
7
+  editor: {
8
+    token: 'editor-token'
9
+  }
10
+}
11
+
12
+const users = {
13
+  'admin-token': {
14
+    roles: ['admin'],
15
+    introduction: 'I am a super administrator',
16
+    avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
17
+    name: 'Super Admin'
18
+  },
19
+  'editor-token': {
20
+    roles: ['editor'],
21
+    introduction: 'I am an editor',
22
+    avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
23
+    name: 'Normal Editor'
24
+  }
25
+}
26
+
27
+export default {
28
+  login: res => {
29
+    const { username } = JSON.parse(res.body)
30
+    const data = tokens[username]
31
+
32
+    if (data) {
33
+      return {
34
+        code: 20000,
35
+        data
36
+      }
37
+    }
38
+    return {
39
+      code: 60204,
40
+      message: 'Account and password are incorrect.'
41
+    }
42
+  },
43
+  getInfo: res => {
44
+    const { token } = param2Obj(res.url)
45
+    const info = users[token]
46
+
47
+    if (info) {
48
+      return {
49
+        code: 20000,
50
+        data: info
51
+      }
52
+    }
53
+    return {
54
+      code: 50008,
55
+      message: 'Login failed, unable to get user details.'
56
+    }
57
+  },
58
+  logout: () => {
59
+    return {
60
+      code: 20000,
61
+      data: 'success'
62
+    }
63
+  }
64
+}

+ 14 - 0
mock/utils.js

@@ -0,0 +1,14 @@
1
+export function param2Obj(url) {
2
+  const search = url.split('?')[1]
3
+  if (!search) {
4
+    return {}
5
+  }
6
+  return JSON.parse(
7
+    '{"' +
8
+      decodeURIComponent(search)
9
+        .replace(/"/g, '\\"')
10
+        .replace(/&/g, '","')
11
+        .replace(/=/g, '":"') +
12
+      '"}'
13
+  )
14
+}

+ 1 - 0
package.json

@@ -17,6 +17,7 @@
17 17
     "axios": "0.18.0",
18 18
     "element-ui": "2.4.6",
19 19
     "js-cookie": "2.2.0",
20
+    "mockjs": "1.0.1-beta3",
20 21
     "normalize.css": "7.0.0",
21 22
     "nprogress": "0.2.0",
22 23
     "vue": "2.5.17",

+ 10 - 0
src/main.js

@@ -15,6 +15,16 @@ import router from './router'
15 15
 import '@/icons' // icon
16 16
 import '@/permission' // permission control
17 17
 
18
+/**
19
+ * This project originally used easy-mock to simulate data,
20
+ * but its official service is very unstable,
21
+ * and you can build your own service if you need it.
22
+ * So here I use Mock.js for local emulation,
23
+ * it will intercept your request, so you won't see the request in the network.
24
+ * If you remove `../mock` it will automatically request easy-mock data.
25
+ */
26
+import '../mock' // simulation data
27
+
18 28
 Vue.use(ElementUI, { locale })
19 29
 
20 30
 Vue.config.productionTip = false

+ 1 - 1
src/router/index.js

@@ -17,7 +17,7 @@ import Layout from '../views/layout/Layout'
17 17
 * redirect: noredirect           if `redirect:noredirect` will no redirect in the breadcrumb
18 18
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
19 19
 * meta : {
20
-    title: 'title'               the name show in submenu and breadcrumb (recommend set)
20
+    title: 'title'               the name show in subMenu and breadcrumb (recommend set)
21 21
     icon: 'svg-name'             the icon show in the sidebar
22 22
     breadcrumb: false            if false, the item will hidden in breadcrumb(default is true)
23 23
   }