index.html 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>MyUrls</title>
  7. <link rel="stylesheet" href="https://unpkg.com/[email protected]/lib/theme-chalk/index.css">
  8. <script src="https://unpkg.com/[email protected]/dist/vue.min.js"></script>
  9. <script src="https://unpkg.com/[email protected]/dist/axios.min.js"></script>
  10. <script src="https://unpkg.com/[email protected]/lib/index.js"></script>
  11. <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue-clipboard.min.js"></script>
  12. </head>
  13. <body>
  14. <div id="app">
  15. <el-container>
  16. <el-header></el-header>
  17. <el-main>
  18. <div :class="[isPc ? 'body-center body-width-pc' : 'body-center body-width-mb']">
  19. <img width="300" src="https://cdn.jsdelivr.net/gh/CareyWang/MyUrls@master/public/logo.png" @click="goToGayHub">
  20. <el-input ref="long" v-model="longUrl" size="medium" @keyup.enter.native="enterToDoShort">
  21. <el-button slot="append" icon="el-icon-magic-stick" @click="doShort" :loading="loading"></el-button>
  22. </el-input>
  23. <el-input ref="shortUrl" @dblclick.native="changeDisableStatus" class="copy-content" v-model="shortUrl" size="medium">
  24. <el-button slot="append" v-clipboard:copy="shortUrl" v-clipboard:success="onCopy" ref="copy-btn"
  25. icon="el-icon-document-copy"></el-button>
  26. </el-input>
  27. </div>
  28. </el-main>
  29. </el-container>
  30. </div>
  31. <script>
  32. const repo = 'https://github.com/CareyWang/MyUrls'
  33. let app = new Vue({
  34. el: "#app",
  35. data() {
  36. return {
  37. isPc: true,
  38. loading: false,
  39. longUrl: '',
  40. shortUrl: ''
  41. }
  42. },
  43. created() {
  44. const os = this.getOS()
  45. if (os.isPc !== true) {
  46. this.isPc = false
  47. }
  48. },
  49. mounted() {
  50. this.$refs.long.focus()
  51. },
  52. methods: {
  53. enterToDoShort(ev) {
  54. ev.keyCode === 13 && this.doShort()
  55. },
  56. doShort() {
  57. let re = new RegExp('http(s*)://[^\s]*')
  58. if (re.exec(this.longUrl) === null) {
  59. this.$message.warning('请输入正确格式的长链接')
  60. return
  61. }
  62. this.loading = true
  63. let data = new FormData();
  64. data.append("longUrl", btoa(this.longUrl));
  65. data.append("shortKey", this.shortUrl.indexOf('http') < 0 ? this.shortUrl : '');
  66. axios.post('/short', data, {
  67. header: {
  68. "Content-Type": "application/form-data; charset=utf-8"
  69. }
  70. })
  71. .then(res => {
  72. if (res.data.Code === 1 && res.data.ShortUrl !== "") {
  73. this.shortUrl = res.data.ShortUrl;
  74. this.$copyText(this.shortUrl)
  75. this.$refs.shortUrl.disabled = true
  76. this.$message.success("短链接已复制到剪贴板");
  77. } else {
  78. this.$message.error("短链接获取失败:" + res.data.Message);
  79. }
  80. })
  81. .catch(() => {
  82. this.$message.error("短链接获取失败");
  83. })
  84. .finally(() => {
  85. this.loading = false;
  86. });
  87. },
  88. goToGayHub() {
  89. window.open(repo)
  90. },
  91. getOS() {
  92. let ua = navigator.userAgent,
  93. isWindowsPhone = /(?:Windows Phone)/.test(ua),
  94. isSymbian = /(?:SymbianOS)/.test(ua) || isWindowsPhone,
  95. isAndroid = /(?:Android)/.test(ua),
  96. isFireFox = /(?:Firefox)/.test(ua),
  97. isChrome = /(?:Chrome|CriOS)/.test(ua),
  98. isTablet = /(?:iPad|PlayBook)/.test(ua) || (isAndroid && !/(?:Mobile)/.test(ua)) || (isFireFox && /(?:Tablet)/.test(ua)),
  99. isPhone = /(?:iPhone)/.test(ua) && !isTablet,
  100. isPc = !isPhone && !isAndroid && !isSymbian;
  101. return {
  102. isTablet: isTablet,
  103. isPhone: isPhone,
  104. isAndroid: isAndroid,
  105. isPc: isPc
  106. };
  107. },
  108. getBodyClass() {
  109. return this.isPc ? 'body-center body-width-pc' : 'body-center'
  110. },
  111. onCopy() {
  112. this.$message.success("Copied!");
  113. },
  114. changeDisableStatus(event) {
  115. this.$refs.shortUrl.disabled = false
  116. }
  117. },
  118. })
  119. </script>
  120. <style>
  121. .body-center {
  122. width: 40%;
  123. position: absolute;
  124. left: 50%;
  125. top: 30%;
  126. transform: translate(-50%, -50%);
  127. text-align: center;
  128. }
  129. .body-width-pc {
  130. width: 40%;
  131. }
  132. .body-width-mb {
  133. width: 90%;
  134. }
  135. .el-input {
  136. margin-top: 20px;
  137. }
  138. </style>
  139. </body>
  140. </html>