44 <el-button type =" primary" size =" medium" >
55 <router-link to =" /api_excel/add" >上传测试</router-link >
66 </el-button >
7+ <el-button :loading =" reload" type =" primary" class =" reload" plain @click =" fetchData" >{{ reload_name }}</el-button >
78 </el-row >
89 <el-table
910 v-loading =" listLoading"
5556 </template >
5657 </el-table-column >
5758
58- <el-table-column label =" 进度条" width =" 1 " align =" center" display =" none" >
59+ <el-table-column label =" 进度条" width =" 100 " align =" center" display =" none" >
5960 <template slot-scope="scope">
6061 <div v-if =" scope.row.state === 0" >
6162 <el-progress :text-inside =" true" :stroke-width =" 18" :percentage =" 0" />
6768 <el-progress :text-inside =" true" :stroke-width =" 18" :percentage =" 100" status =" success" />
6869 </div >
6970 <div v-else >
70- <el-progress :text-inside =" true" :stroke-width =" 18" :percentage =" 50 " status =" exception" />
71+ <el-progress :text-inside =" true" :stroke-width =" 18" :percentage =" scope.row.rate " status =" exception" />
7172 </div >
7273 </template >
7374 </el-table-column >
126127</template >
127128
128129<script >
130+ import { getToken } from ' @/utils/auth'
129131import { getList , deleteAct , search , startTask , download_log } from ' @/api/api_excel'
130132
131133export default {
@@ -141,6 +143,8 @@ export default {
141143 },
142144 data () {
143145 return {
146+ reload: false ,
147+ reload_name: ' 点击刷新' ,
144148 list: null ,
145149 listLoading: true ,
146150 perpage: 10 ,
@@ -156,7 +160,8 @@ export default {
156160 this .currentpage = parseInt (this .listQuery .page )
157161 const perPage = parseInt (this .$route .query .perPage )
158162 this .perpage = isNaN (perPage) ? this .perpage : perPage
159- this .fetchData ()
163+ // this.fetchData()
164+ this .initWebSocket ()
160165 },
161166 destroyed () {
162167 this .websock .close () // 离开路由之后断开 websocket 连接
@@ -209,20 +214,22 @@ export default {
209214 type: msg,
210215 message: res .reason
211216 })
217+ this .initWebSocket ()
212218 })
213219 }
214220 }
215221 })
216222 },
217- initWebSocket (id ) { // 初始化 weosocket
223+ initWebSocket () { // 初始化 weosocket
218224 if (' WebSocket' in window ) {
219- const url = ' wss://www.guke1.com/ws?id= ' + id
225+ const url = process . env . WEBSOCKET + ' ?action=api_excel&token= ' + getToken ()
220226 this .websock = new WebSocket (url)
221227 this .websock .onmessage = this .onmessage
222228 this .websock .onopen = this .onopen
223229 this .websock .onerror = this .onerror
224230 this .websock .onclose = this .close
225231 } else {
232+ this .fetchData ()
226233 // 浏览器不支持 WebSocket,使用 ajax 轮询
227234 console .log (' Your browser does not support WebSocket!' )
228235 }
@@ -232,44 +239,60 @@ export default {
232239 // const rs = this.send(JSON.stringify(actions))
233240 // console.log(rs)
234241 },
235- onerror () { // 连接建立失败重连
242+ onerror () {
243+ // 连接建立失败, 发送 http 请求获取数据
236244 // this.initWebSocket()
245+ this .fetchData ()
237246 },
238247 onmessage (e ) { // 数据接收
239- console .log (e .data )
248+ // console.log(e.data)
240249 const data = JSON .parse (e .data )
241250 // this.list[2].rate = parseInt(data.data.rate)
242251 // console.log(this.list[2].rate)
243- console .log (data)
252+ // console.log(data)
253+ // websocket 返回的数据
254+ this .list = data .data .data
255+ this .listLoading = false
256+ this .total = data .data .total
257+ this .url = data .data .appUrl
258+ // console.log('type', Object.prototype.toString.call(this.list))
259+ setTimeout (() => {
260+ this .reload = false
261+ this .reload_name = ' 刷新'
262+ }, 800 )
244263 },
245264 send (Data ) {
246265 this .websock .send (Data)
247266 },
248267 close () { // 关闭
249- console .log (' 断开连接' )
268+ // console.log('断开连接')
250269 },
251270 download (index , row ) {
252271 window .location .href = this .url + row .finish_url
253272 },
254273 download_log (index , row ) {
255274 download_log ({ id: row .id }).then (res => {
256- console .log (res)
275+ // console.log(res)
257276 if (res .code === 200 ) {
258277 window .location .href = this .url + res .data .failed_done_file
259278 }
260279 })
261280 },
262281 fetchData () {
263- this .listLoading = true
282+ this .listLoading = this .reload = true
283+ this .reload_name = ' 加载中'
264284 const params = Object .assign ({ ' page' : this .listQuery .page }, { ' perPage' : this .perpage })
265285 getList (params).then (response => {
266286 this .list = response .data .data
267287 this .listLoading = false
268288 this .total = response .data .total
269289 this .url = response .data .appUrl
270- console .log (' type' , Object .prototype .toString .call (this .list ))
271-
272- this .initWebSocket (8 )
290+ // console.log('type', Object.prototype.toString.call(this.list))
291+ setTimeout (() => {
292+ this .reload = false
293+ this .reload_name = ' 刷新'
294+ }, 800 )
295+ // this.initWebSocket()
273296 })
274297 },
275298 handleEdit (index , row ) {
@@ -354,4 +377,8 @@ export default {
354377 .pagination {
355378 margin : 20px auto ;
356379 }
380+ .reload {
381+ margin-right : 300px ;
382+ float : right ;
383+ }
357384 </style >
0 commit comments