index.html 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta
  6. name="viewport"
  7. content="width=device-width, initial-scale=1.0" />
  8. <title>Document</title>
  9. </head>
  10. <body>
  11. <div>
  12. 机器码:<input
  13. id="input1"
  14. type="text" />
  15. </div>
  16. <div>
  17. 类型:
  18. <input
  19. type="radio"
  20. id="option1"
  21. name="group1"
  22. value="dz" />
  23. <label for="option1">调证</label>
  24. <input
  25. type="radio"
  26. id="option2"
  27. name="group1"
  28. value="sp" />
  29. <label for="option2">审批</label>
  30. </div>
  31. <button id="submit">生成License</button
  32. ><button id="copy">复制License</button>
  33. <div id="result"></div>
  34. </body>
  35. <script>
  36. const getLicense = () => {
  37. const machineCode = document.querySelector("#input1").value;
  38. const type = document.querySelector('input[name="group1"]:checked').value;
  39. console.log(machineCode, type);
  40. if (machineCode && type) {
  41. fetch(
  42. `http://221.226.175.250:2230/license/create?machineCode=${machineCode}&type=${type}`,
  43. {
  44. method: "get",
  45. }
  46. )
  47. .then((res) => {
  48. return res.text();
  49. })
  50. .then((res) => {
  51. const result = document.querySelector("#result");
  52. result.innerText = res;
  53. });
  54. }
  55. }
  56. const copyText = async (val, callback) => {
  57. if (navigator.clipboard && navigator.permissions) {
  58. await navigator.clipboard.writeText(val);
  59. if (callback) callback();
  60. } else {
  61. const textArea = document.createElement("textArea");
  62. textArea.value = val;
  63. textArea.style.width = 0;
  64. textArea.style.position = "fixed";
  65. textArea.style.left = "-999px";
  66. textArea.style.top = "10px";
  67. textArea.setAttribute("readonly", "readonly");
  68. document.body.appendChild(textArea);
  69. textArea.select();
  70. document.execCommand("copy");
  71. document.body.removeChild(textArea);
  72. if (callback) callback();
  73. }
  74. };
  75. const handleCopyText = ()=>{
  76. const result = document.querySelector("#result").innerText.trim();
  77. if(result){
  78. copyText(result,()=>{
  79. alert('复制成功,复制内容为:' + result)
  80. })
  81. }
  82. }
  83. window.onload = () => {
  84. const btn = document.querySelector("#submit");
  85. const btn2 = document.querySelector("#copy");
  86. btn.addEventListener("click", getLicense);
  87. btn2.addEventListener("click", handleCopyText);
  88. };
  89. </script>
  90. </html>