36 lines
1.0 KiB
JavaScript
36 lines
1.0 KiB
JavaScript
import JSEncrypt from 'jsencrypt'
|
|
import { API_BASE, AUTH_API } from '@/config/api'
|
|
|
|
function fetchLoginPublicKey() {
|
|
const url = `${API_BASE}${AUTH_API.LOGIN_RSA_PUBLIC_KEY}?_t=${Date.now()}`
|
|
return new Promise((resolve, reject) => {
|
|
uni.request({
|
|
url,
|
|
method: 'GET',
|
|
success: (res) => resolve(res),
|
|
fail: (err) => reject(err)
|
|
})
|
|
})
|
|
}
|
|
|
|
export async function buildMiniEncryptedLoginBody(username, password) {
|
|
const keyRes = await fetchLoginPublicKey()
|
|
const body = keyRes.data
|
|
if (!body || body.code !== 200 || !body.data || !body.data.publicKeyPem) {
|
|
throw new Error((body && body.message) || '获取登录公钥失败')
|
|
}
|
|
|
|
const crypt = new JSEncrypt()
|
|
crypt.setPublicKey(body.data.publicKeyPem)
|
|
const payload = JSON.stringify({
|
|
username,
|
|
password,
|
|
ts: Date.now()
|
|
})
|
|
const encryptedLogin = crypt.encrypt(payload)
|
|
if (!encryptedLogin) {
|
|
throw new Error('登录加密失败,请重试')
|
|
}
|
|
return { encryptedLogin }
|
|
}
|