-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathshort.html
167 lines (148 loc) · 5.46 KB
/
short.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="https://cdn.bootcdn.net/ajax/libs/vue/2.5.16/vue.min.js"></script>
<!-- 这里使用了element ui框架 -->
<link href="https://cdn.bootcdn.net/ajax/libs/element-ui/2.15.9/theme-chalk/index.min.css" rel="stylesheet">
<script src="https://cdn.bootcdn.net/ajax/libs/element-ui/2.15.9/index.min.js"></script>
<style>
body, div {
padding: 0px;
margin: 0;
}
#app {
width: 100%;
height: 100vh;
}
.box {
height: 40px;
width: 100%;
margin-top: 50px;
display: flex;
flex-direction: row;
justify-content: center;
}
.box-result {
margin-top: 50px;
}
.el-input {
width: 300px;
}
.el-button {
margin-left: 10px;
}
h3 {
margin-top: 200px;
}
</style>
</head>
<body>
<div id="app">
<h3 align="center">功能:缩短链接</h3>
<div class="box">
<el-input v-model="longUrl" placeholder="请输入待转换链接" @change="handleChange"></el-input>
<el-button type="primary" simi="mini" :loading="btnLoading" @click="handleSubmit">转换</el-button>
</div>
<div class="box box-result">
<el-input v-model="result" placeholder="转换结果" ref="textToCopy"></el-input>
<el-button type="primary" simi="mini" @click="handleCopy">复制</el-button>
</div>
</div>
<script src="https://cdn.bootcdn.net/ajax/libs/axios/1.1.3/axios.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/vue-clipboard2/0.3.3/vue-clipboard.js"></script>
<script>
const apis = ["https://v1.mk/short","https://suo.yt/short","https://s.ops.ci/short","https://dlj.tf/short","https://d1.mk/short"]
new Vue({
el: '#app',
data: {
longUrl: '',
result: '',
btnLoading: false
},
methods: {
//在这里写方法
promiseFetch(apiUrl, formdataBody, isPreheat) {
return new Promise((resolve, reject) => {
axios.post(apiUrl, formdataBody, {timeout: isPreheat ? 200 : 1000})
.then((res) => {
data = res.data
this.btnLoading = false;
if (!isPreheat) {
this.result = data.ShortUrl
}
resolve();
}).catch(e => {
console.log(e)
resolve();
});
/*fetch(apiUrl, {
method: "POST",
body: formdataBody
}).then((response) => {
return response.json()
}).then((res) => {
this.btnLoading = false;
this.result = res.ShortUrl
resolve();
}).catch(e => {
console.log(e)
resolve();
})*/
});
},
async handleSubmit(preheat) {
console.log("preheat",preheat)
isPreheat = preheat === true
let lu = isPreheat ? 'http://qq.com' : this.longUrl
// let lu = `https://www.baidu.com`;
let param = {longUrl: btoa(lu)}
const formdata = new FormData();
formdata.append("longUrl", param.longUrl);
formdata.append("shortKey", '');
this.btnLoading = true;
for (let i = 0; i < apis.length ; i++) {
let api = apis[i]
console.log("循环", i, api);
await this.promiseFetch(api, formdata, isPreheat);
if (this.result) {
break;
}
}
if (!this.result) {
this.btnLoading = false;
if(!isPreheat) {
alert('短链接服务异常,请重试')
}
}
},
handleCopy() {
let textToCopy = this.$refs.textToCopy.$el.querySelector('input')
textToCopy.select()
try {
const successful = document.execCommand('copy')
const msg = successful ? 'successful' : 'unsuccessful'
console.log('Copying text command was ' + msg)
alert('复制成功')
} catch (err) {
console.log('Oops, unable to copy')
alert('复制失败')
}
},
handleChange() {
console.log(this.longUrl, '=====url===')
}
},
mounted: function() {
console.log('init');
// 这里可以执行其他初始化操作
// 接口预热,不知道为啥第一次调用一定失败,没找到原因,所以预先调用一次
this.handleSubmit(true)
}
})
</script>
</body>
</html>