Blame view

mock/user.js 4.33 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
  const List = []
a6e433928   Adam   auto commit the c...
45
  const count = 10
50760eab9   Adam   auto commit the c...
46
47
48
49
50
51
52
53
54
  
  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',
a6e433928   Adam   auto commit the c...
55
56
57
58
59
60
61
62
63
64
      // reviewer: '@first',
      // title: '@title(5, 10)',
      openid: '@sentence(12,12)',
      comefromperson: '@title(1, 4)',
      buyvalue:'@integer(1,100)',
      buywill:'@integer(1,100)',
      // content_short: 'mock data',
      // content: baseContent,
      // forecast: '@float(0, 100, 2, 2)',
      // importance: '@integer(1, 3)',
50760eab9   Adam   auto commit the c...
65
66
      'type|1': ['CN', 'US', 'JP', 'EU'],
      'status|1': ['published', 'draft'],
a6e433928   Adam   auto commit the c...
67
68
      // display_time: '@datetime',
      // comment_disabled: true,
50760eab9   Adam   auto commit the c...
69
70
71
72
73
74
75
      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...
76
77
78
  export default [
    // user login
    {
50760eab9   Adam   auto commit the c...
79
      url: '/yp/user/login',
80a28914e   吉鹏   init
80
81
      type: 'post',
      response: config => {
d7d9c38c2   Adam   auto commit the c...
82
83
84
85
86
        const { username } = config.body
        const token = tokens[username]
  
        // mock error
        if (!token) {
80a28914e   吉鹏   init
87
88
          return {
            code: 60204,
d7d9c38c2   Adam   auto commit the c...
89
            message: 'Account and password are incorrect.'
80a28914e   吉鹏   init
90
91
          }
        }
d7d9c38c2   Adam   auto commit the c...
92
93
94
95
96
  
        return {
          code: 20000,
          data: token
        }
80a28914e   吉鹏   init
97
98
99
100
101
      }
    },
  
    // get user info
    {
50760eab9   Adam   auto commit the c...
102
      url: '/yp/user/info\.*',
80a28914e   吉鹏   init
103
104
      type: 'get',
      response: config => {
d7d9c38c2   Adam   auto commit the c...
105
        const { token } = config.query
80a28914e   吉鹏   init
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
        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...
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
      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
148
149
150
151
152
153
154
      type: 'post',
      response: _ => {
        return {
          code: 20000,
          data: 'success'
        }
      }
50760eab9   Adam   auto commit the c...
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
199
200
201
    },
    // 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
202
203
    }
  ]