Blame view

mock/user.js 4.21 KB
d7d9c38c2   Adam   auto commit the c...
1

50760eab9   Adam   auto commit the c...
2
  import Mock from 'mockjs'
80a28914e   吉鹏   init
3
4
5
6
  const tokens = {
    admin: {
      token: 'admin-token'
    },
3d3cdb68f   Adam   auto commit the c...
7
8
9
10
11
12
13
14
    assistant: {
      token: 'assistant-token'
    },
    runner: {
      token: 'runner-token'
    },
    shoper: {
      token: 'shoper-token'
80a28914e   吉鹏   init
15
16
17
18
    }
  }
  
  const users = {
d7d9c38c2   Adam   auto commit the c...
19
    'admin-token': {
80a28914e   吉鹏   init
20
21
22
23
24
      roles: ['admin'],
      introduction: 'I am a super administrator',
      avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
      name: 'Super Admin'
    },
3d3cdb68f   Adam   auto commit the c...
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
    'assistant-token': {
      roles: ['assistant'],
      introduction: 'I am an assistant',
      avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
      name: 'Normal assistant'
    },
    'runner-token': {
      roles: ['runner'],
      introduction: 'I am an runner',
      avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
      name: 'Normal runner'
    },
    'shoper-token': {
      roles: ['shoper'],
      introduction: 'I am an shoper',
c44ba96f6   Adam   auto commit the c...
40
      avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
3d3cdb68f   Adam   auto commit the c...
41
      name: 'Normal shoper'
80a28914e   吉鹏   init
42
43
    }
  }
50760eab9   Adam   auto commit the c...
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
70
71
72
  const List = []
  const count = 100
  
  const baseContent = '<p>I am testing data, I am testing data.</p><p><img src="https://wpimg.wallstcn.com/4c69009c-0fd4-4153-b112-6cb53d1cf943"></p>'
  const image_uri = 'https://wpimg.wallstcn.com/e4558086-631c-425c-9430-56ffb46e70b3'
  
  for (let i = 0; i < count; i++) {
    List.push(Mock.mock({
      uid: '@increment',
      create_time: +Mock.Random.date('T'),
      nickname: '@first',
      reviewer: '@first',
      title: '@title(5, 10)',
      openid:'@sentence(12,12)',
      content_short: 'mock data',
      content: baseContent,
      forecast: '@float(0, 100, 2, 2)',
      importance: '@integer(1, 3)',
      'type|1': ['CN', 'US', 'JP', 'EU'],
      'status|1': ['published', 'draft'],
      display_time: '@datetime',
      comment_disabled: true,
      son_of_adv: '@integer(300, 5000)',//下线
      souceof:'@integer(300, 5000)',//源自
      image_uri,
      'roles|1': ['admin', 'assistant', 'shoper', 'runner'],
      platforms: ['a-platform']//哪个平台
    }))
  }
d7d9c38c2   Adam   auto commit the c...
73
74
75
  export default [
    // user login
    {
50760eab9   Adam   auto commit the c...
76
      url: '/yp/user/login',
80a28914e   吉鹏   init
77
78
      type: 'post',
      response: config => {
d7d9c38c2   Adam   auto commit the c...
79
80
81
82
83
        const { username } = config.body
        const token = tokens[username]
  
        // mock error
        if (!token) {
80a28914e   吉鹏   init
84
85
          return {
            code: 60204,
d7d9c38c2   Adam   auto commit the c...
86
            message: 'Account and password are incorrect.'
80a28914e   吉鹏   init
87
88
          }
        }
d7d9c38c2   Adam   auto commit the c...
89
90
91
92
93
  
        return {
          code: 20000,
          data: token
        }
80a28914e   吉鹏   init
94
95
96
97
98
      }
    },
  
    // get user info
    {
50760eab9   Adam   auto commit the c...
99
      url: '/yp/user/info\.*',
80a28914e   吉鹏   init
100
101
      type: 'get',
      response: config => {
d7d9c38c2   Adam   auto commit the c...
102
        const { token } = config.query
80a28914e   吉鹏   init
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
        const info = users[token]
  
        // mock error
        if (!info) {
          return {
            code: 50008,
            message: 'Login failed, unable to get user details.'
          }
        }
  
        return {
          code: 20000,
          data: info
        }
      }
    },
  
    // user logout
    {
50760eab9   Adam   auto commit the c...
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
      url: '/yp/user/logout',
      type: 'post',
      response: _ => {
        return {
          code: 20000,
          data: 'success'
        }
      }
    },
    // user create
    {
      url: '/yp/user/create',
      type: 'post',
      response: _ => {
        return {
          code: 20000,
          data: 'success'
        }
      }
    },
    // user update
    {
      url: '/yp/user/update',
80a28914e   吉鹏   init
145
146
147
148
149
150
151
      type: 'post',
      response: _ => {
        return {
          code: 20000,
          data: 'success'
        }
      }
50760eab9   Adam   auto commit the c...
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
    },
    // user del
    {
      url: '/yp/user/del',
      type: 'post',
      response: _ => {
        return {
          code: 20000,
          data: 'success'
        }
      }
    },
    // user list
    {
      url: '/yp/user/list',
      type: 'get',
      response: config => {
        const {
          importance,
          type,
          title,
          page = 1,
          limit = 20,
          sort
        } = config.query
  
        let mockList = List.filter(item => {
          if (importance && item.importance !== +importance) return false
          if (type && item.type !== type) return false
          if (title && item.title.indexOf(title) < 0) return false
          return true
        })
  
        if (sort === '-id') {
          mockList = mockList.reverse()
        }
  
        const pageList = mockList.filter((item, index) => index < limit * page && index >= limit * (page - 1))
  
        return {
          code: 20000,
          data: {
            total: mockList.length,
            items: pageList
          }
        }
      }
80a28914e   吉鹏   init
199
200
    }
  ]