Blame view

mock/sys/user.ts 3.18 KB
陈文彬 authored
1
import { MockMethod } from 'vite-plugin-mock';
2
import { resultError, resultSuccess, getRequestToken, requestParams } from '../_util';
陈文彬 authored
3
4
export function createFakeUserList() {
陈文彬 authored
5
6
7
8
  return [
    {
      userId: '1',
      username: 'vben',
vben authored
9
      realName: 'Vben Admin',
10
      avatar: 'https://q1.qlogo.cn/g?b=qq&nk=190848757&s=640',
陈文彬 authored
11
12
13
      desc: 'manager',
      password: '123456',
      token: 'fakeToken1',
14
      homePath: '/dashboard/analysis',
15
16
17
18
19
20
      roles: [
        {
          roleName: 'Super Admin',
          value: 'super',
        },
      ],
陈文彬 authored
21
22
23
24
25
26
    },
    {
      userId: '2',
      username: 'test',
      password: '123456',
      realName: 'test user',
27
      avatar: 'https://q1.qlogo.cn/g?b=qq&nk=339449197&s=640',
陈文彬 authored
28
29
      desc: 'tester',
      token: 'fakeToken2',
30
      homePath: '/dashboard/workbench',
31
32
33
34
35
36
      roles: [
        {
          roleName: 'Tester',
          value: 'test',
        },
      ],
陈文彬 authored
37
38
39
40
41
42
43
44
45
46
47
48
    },
  ];
}

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

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

      return resultSuccess(codeList);
    },
  },
100
101
102
103
104
105
106
107
108
109
110
111
112
113
  {
    url: '/basic-api/logout',
    timeout: 200,
    method: 'get',
    response: (request: requestParams) => {
      const token = getRequestToken(request);
      if (!token) return resultError('Invalid token');
      const checkUser = createFakeUserList().find((item) => item.token === token);
      if (!checkUser) {
        return resultError('Invalid token!');
      }
      return resultSuccess(undefined, { message: 'Token has been destroyed' });
    },
  },
114
115
116
117
118
119
120
121
  {
    url: '/basic-api/testRetry',
    statusCode: 405,
    method: 'get',
    response: () => {
      return resultError('Error!');
    },
  },
陈文彬 authored
122
] as MockMethod[];