登录
通过如下函数进行登录,获取 token
ts
// 登录获取 token
const TOKEN = await loginAndGetToken()
CONSTANTS.TOKEN = TOKEN
/**
* 登录
* @param data 登录信息
* @param data.username 用户名
* @param data.password 密码
*/
function login(data: { username: string; password: string }) {
const basicAuth = 'Basic ' + window.btoa(CONSTANTS.OAUTH2_PASSWORD_CLIENT)
// 密码加密
const encPassword = encryption(data.password, CONSTANTS.PWD_ENC_KEY)
const { username } = data
return request({
url: '/admin/oauth2/token',
method: 'post',
params: { username, grant_type: 'password', scope: 'server' },
data: { password: encPassword },
headers: {
skipToken: true,
Authorization: basicAuth,
'Content-Type': 'application/x-www-form-urlencoded'
}
})
}
async function loginAndGetToken() {
try {
const res = await login({ username: 'admin', password: '12345678' })
console.log('登录成功', res)
return res
} catch (error) {
console.error('登录失败', error)
}
}