<el-form-item label="密 码">
<el-input v-model="editObj.password" type="text" placeholder="请输入密码">
<template #append>
<el-button type="primary" @click="autoGeneratePwd">点击自动生成</el-button>
</template>
</el-input>
</el-form-item>
// 自动生产随机密码
const autoGeneratePwd = () => {
editObj.value.password = generateRandomPassword()
}
const generateRandomPassword = () => {
const length = Math.floor(Math.random() * 9) + 8 // 随机生成8-16之间的密码长度
const lowerCaseChars = 'abcdefghijklmnopqrstuvwxyz'
const upperCaseChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
const numberChars = '0123456789'
let password = ''
while (password.length < length) {
const randomCharType = Math.floor(Math.random() * 3) // 随机选择字符类型:0代表小写字母,1代表大写字母,2代表数字
if (randomCharType === 0) {
password += lowerCaseChars[Math.floor(Math.random() * lowerCaseChars.length)]
} else if (randomCharType === 1) {
password += upperCaseChars[Math.floor(Math.random() * upperCaseChars.length)]
} else {
password += numberChars[Math.floor(Math.random() * numberChars.length)]
}
}
return password
}
点击按钮即可生成密码串