import CryptoJS from 'crypto-js'
import uuid from '@/utils/uuid'
import { SECRET_KEY } from '@/utils/config'
// 签名校验
const nonceStr = uuid.uuid()
const timestamp = new Date().getTime()
// const sign = CryptoJS.MD5('nonceStr=' + nonceStr + '&secretKey=' + SECRET_KEY + '×tamp=' + timestamp).toString(CryptoJS.enc.Hex).toUpperCase()
const sign = CryptoJS.MD5('nonceStr=' + nonceStr + '&secretKey=' + SECRET_KEY + '×tamp=' + timestamp).toString().toUpperCase()
// 发送请求
axios.post('http://localhost:xxx', {
nonceStr,
timestamp,
sign
}).then(res => {
console.log(res.data)
}).catch(() => {
this.$alert('本地授权客户端连接失败', '提示', { type: 'error' })
})
uuid.js
/**
* 生成UUID
*/
export default {
uuid() {
const s = []
const hexDigits = '0123456789abcdef'
for (let i = 0; i < 36; i++) {
s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1)
}
s[14] = '4' // bits 12-15 of the time_hi_and_version field to 0010
s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1) // bits 6-7 of the clock_seq_hi_and_reserved to 01
s[8] = s[13] = s[18] = s[23] = '-'
return s.join('').replaceAll('-', '')
}
}
config.js
/**
* @description: 加密配置信息
*/
// 签名校验
export const SECRET_KEY = 'xxx32bitxxx'
注:npm install crypto-js
axios.post 改为同步请求
// 签名校验
const nonceStr = uuid.uuid()
const timestamp = new Date().getTime()
const sign = CryptoJS.MD5('nonceStr=' + nonceStr + '&secretKey=' + SECRET_KEY + '×tamp=' + timestamp).toString().toUpperCase()
// 发送请求
this.loading = true
const resp = await axios.post('http://localhost:xxx', {
nonceStr,
timestamp,
sign
}).catch(() => {
this.$alert('本地授权客户端连接失败', '提示', { type: 'error' })
})
this.loading = false
// 请求结果异常
if (!resp) {
return
}
// 请求结果处理
const res = resp.data
if (res.code !== 200) {
this.$message.error(res.msg)
return
}