zhou zhou
7 小时以前 e9283ffe6822b12ec5dd2ccf4dc13a369b227a61
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
import request from '@/utils/http'
 
function buildLoginPayload({ username, password }) {
  return { username, password }
}
 
function normalizeLoginParams(params) {
  return {
    username: params?.username || params?.userName || '',
    password: params?.password
  }
}
 
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 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,
  fetchGetUserInfo,
  fetchLogin,
  normalizeLoginResponse,
  normalizeUserInfo
}