Blame view

mock/sys/user.ts 2.5 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
15
16
17
18
19
      roles: [
        {
          roleName: 'Super Admin',
          value: 'super',
        },
      ],
陈文彬 authored
20
21
22
23
24
25
    },
    {
      userId: '2',
      username: 'test',
      password: '123456',
      realName: 'test user',
26
      avatar: 'https://q1.qlogo.cn/g?b=qq&nk=339449197&s=640',
陈文彬 authored
27
28
      desc: 'tester',
      token: 'fakeToken2',
29
30
31
32
33
34
      roles: [
        {
          roleName: 'Tester',
          value: 'test',
        },
      ],
陈文彬 authored
35
36
37
38
39
40
41
42
43
44
45
46
    },
  ];
}

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

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

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