zhou zhou
6 小时以前 7b8bb6b1d8d2505c06fa3740349b96798ec8c900
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import request from '@/utils/http'
 
function buildLoginPayload({ username, password, tenantId }) {
  const payload = { username, password }
  if (tenantId !== '' && tenantId !== null && tenantId !== void 0) {
    payload.tenantId = Number(tenantId)
  }
  return payload
}
 
function normalizeLoginParams(params) {
  return {
    username: params?.username || params?.userName || '',
    password: params?.password,
    tenantId: params?.tenantId ?? ''
  }
}
 
function normalizeLoginResponse(payload) {
  const data = payload?.data || payload || {}
  return {
    accessToken: data.accessToken || '',
    refreshToken: data.refreshToken || '',
    user: data.user || {}
  }
}
 
function normalizeUserInfo(data) {
  const normalizedRoles = normalizeRoleCodes(data?.roles)
  const normalizedButtons = normalizeButtonMarks(data)
  return {
    ...data,
    roles: normalizedRoles,
    buttons: normalizedButtons
  }
}
 
function normalizeRoleCodes(roles) {
  if (!Array.isArray(roles)) {
    return []
  }
 
  return Array.from(
    new Set(
      roles
        .map((item) => {
          if (typeof item === 'string') {
            return item.trim()
          }
          if (item && typeof item === 'object') {
            return item.code || item.name || ''
          }
          return ''
        })
        .filter(Boolean)
    )
  )
}
 
function normalizeButtonMarks(data) {
  const directButtons = Array.isArray(data?.buttons) ? data.buttons : []
  const authorityButtons = Array.isArray(data?.authorities)
    ? data.authorities.map((item) => item?.authority || item?.authMark || '')
    : []
 
  return Array.from(
    new Set(
      [...directButtons, ...authorityButtons]
        .map((item) => (typeof item === 'string' ? item.trim() : ''))
        .filter(Boolean)
    )
  )
}
 
function fetchLogin(params) {
  return request
    .post({
      url: '/login',
      params: buildLoginPayload(normalizeLoginParams(params))
      // showSuccessMessage: true // 显示成功消息
      // showErrorMessage: false // 不显示错误消息
    })
    .then((response) => {
      const normalized = normalizeLoginResponse(response)
      return {
        token: normalized.accessToken,
        accessToken: normalized.accessToken,
        refreshToken: normalized.refreshToken,
        user: normalized.user
      }
    })
}
function fetchGetSystemInfo() {
  return request.get({
    url: '/system/info'
  })
}
function fetchGetTenantList(params) {
  return request.get({
    url: '/tenant/list',
    params
  })
}
function fetchGetUserInfo() {
  return request
    .get({
      url: '/auth/user'
      // 自定义请求头
      // headers: {
      //   'X-Custom-Header': 'your-custom-value'
      // }
    })
    .then((response) => normalizeUserInfo(response))
}
function fetchGetMenuList() {
  return request.get({
    url: '/auth/menu'
  })
}
export {
  buildLoginPayload,
  fetchGetMenuList,
  fetchGetSystemInfo,
  fetchGetTenantList,
  fetchGetUserInfo,
  fetchLogin,
  normalizeLoginResponse,
  normalizeUserInfo
}