Blame view

mock/sys/user.ts 2.09 KB
陈文彬 authored
1
2
3
4
5
6
7
8
import { MockMethod } from 'vite-plugin-mock';
import { resultError, resultSuccess } from '../_util';

function createFakeUserList() {
  return [
    {
      userId: '1',
      username: 'vben',
vben authored
9
      realName: 'Vben Admin',
陈文彬 authored
10
11
12
      desc: 'manager',
      password: '123456',
      token: 'fakeToken1',
13
14
15
16
17
18
      roles: [
        {
          roleName: 'Super Admin',
          value: 'super',
        },
      ],
陈文彬 authored
19
20
21
22
23
24
25
26
    },
    {
      userId: '2',
      username: 'test',
      password: '123456',
      realName: 'test user',
      desc: 'tester',
      token: 'fakeToken2',
27
28
29
30
31
32
      roles: [
        {
          roleName: 'Tester',
          value: 'test',
        },
      ],
陈文彬 authored
33
34
35
36
37
38
39
40
41
42
43
44
45
    },
  ];
}

const fakeCodeList: any = {
  '1': ['1000', '3000', '5000'],

  '2': ['2000', '4000', '6000'],
};
export default [
  // mock user login
  {
    url: '/api/login',
46
    timeout: 200,
陈文彬 authored
47
48
49
50
51
52
53
54
55
    method: 'post',
    response: ({ body }) => {
      const { username, password } = body;
      const checkUser = createFakeUserList().find(
        (item) => item.username === username && password === item.password
      );
      if (!checkUser) {
        return resultError('Incorrect account or password!');
      }
56
      const { userId, username: _username, token, realName, desc, roles } = checkUser;
陈文彬 authored
57
      return resultSuccess({
58
        roles,
陈文彬 authored
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
        userId,
        username: _username,
        token,
        realName,
        desc,
      });
    },
  },
  {
    url: '/api/getUserInfoById',
    method: 'get',
    response: ({ query }) => {
      const { userId } = query;
      const checkUser = createFakeUserList().find((item) => item.userId === userId);
      if (!checkUser) {
        return resultError('The corresponding user information was not obtained!');
      }
      return resultSuccess(checkUser);
    },
  },
  {
    url: '/api/getPermCodeByUserId',
    timeout: 200,
    method: 'get',
    response: ({ query }) => {
      const { userId } = query;
      if (!userId) {
        return resultError('userId is not null!');
      }
      const codeList = fakeCodeList[userId];

      return resultSuccess(codeList);
    },
  },
] as MockMethod[];