index.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /**
  2. * FeHelper 密码随机生成工具
  3. */
  4. new Vue({
  5. el: '#pageContainer',
  6. data: {
  7. number: true,
  8. lowerLetter: true,
  9. upperLetter: true,
  10. specialChar: false,
  11. length: 16,
  12. chars: {
  13. number: '0123456789',
  14. lowerLetter: 'abcdefghijklmnopqrstuvwxyz',
  15. upperLetter: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
  16. specialChar: '~!@#$%^&*()[{]}-_=+\|;:\'\",<.>/?`'
  17. },
  18. resultContent: ''
  19. },
  20. methods: {
  21. convert: function () {
  22. this.$nextTick(() => {
  23. let exceptedChars = ['number', 'lowerLetter', 'upperLetter', 'specialChar'].filter(item => this[item]).map(item => this.chars[item]).join('');
  24. let password = [], rands = [], rand = 0;
  25. for (let index = 0; index < this.length; index++) {
  26. // 尽可能不让字符重复
  27. do {
  28. rand = Math.floor(Math.random() * exceptedChars.length);
  29. } while (rands.includes(rand) && rands.length < exceptedChars.length);
  30. rands.push(rand);
  31. password.push(exceptedChars[rand]);
  32. }
  33. this.resultContent = password.join('');
  34. });
  35. },
  36. getResult: function () {
  37. this.$refs.rstCode.select();
  38. },
  39. copyResult: function () {
  40. // 选中要复制的内容
  41. this.getResult();
  42. if ('clipboard' in navigator) {
  43. navigator.clipboard.writeText(this.resultContent)
  44. .catch(err => {
  45. console.error('复制失败: ', err);
  46. });
  47. }else{
  48. alert("您的浏览器不支持 clipboard API, 请手动复制")
  49. }
  50. }
  51. }
  52. });