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 ,
@@ -153,10 +157,12 @@ export default {
153157 },
154158 created () {
155159 this .listQuery = this .$route .query
156- this .currentpage = parseInt (this .listQuery .page )
160+ const page = parseInt (this .listQuery .page )
161+ this .currentpage = isNaN (page) ? 1 : page
157162 const perPage = parseInt (this .$route .query .perPage )
158163 this .perpage = isNaN (perPage) ? this .perpage : perPage
159- this .fetchData ()
164+ // this.fetchData()
165+ this .initWebSocket ()
160166 },
161167 destroyed () {
162168 this .websock .close () // 离开路由之后断开 websocket 连接
@@ -209,20 +215,22 @@ export default {
209215 type: msg,
210216 message: res .reason
211217 })
218+ this .initWebSocket ()
212219 })
213220 }
214221 }
215222 })
216223 },
217- initWebSocket (id ) { // 初始化 weosocket
224+ initWebSocket () { // 初始化 weosocket
218225 if (' WebSocket' in window ) {
219- const url = ' wss://www.guke1.com/ws?id= ' + id
226+ const url = process . env . WEBSOCKET + ' ?action=api_excel&token= ' + getToken () + ' &page= ' + this . currentpage + ' &perPage= ' + this . perpage
220227 this .websock = new WebSocket (url)
221228 this .websock .onmessage = this .onmessage
222229 this .websock .onopen = this .onopen
223230 this .websock .onerror = this .onerror
224231 this .websock .onclose = this .close
225232 } else {
233+ this .fetchData ()
226234 // 浏览器不支持 WebSocket,使用 ajax 轮询
227235 console .log (' Your browser does not support WebSocket!' )
228236 }
@@ -232,44 +240,60 @@ export default {
232240 // const rs = this.send(JSON.stringify(actions))
233241 // console.log(rs)
234242 },
235- onerror () { // 连接建立失败重连
243+ onerror () {
244+ // 连接建立失败, 发送 http 请求获取数据
236245 // this.initWebSocket()
246+ this .fetchData ()
237247 },
238248 onmessage (e ) { // 数据接收
239- console .log (e .data )
249+ // console.log(e.data)
240250 const data = JSON .parse (e .data )
241251 // this.list[2].rate = parseInt(data.data.rate)
242252 // console.log(this.list[2].rate)
243- console .log (data)
253+ // console.log(data)
254+ // websocket 返回的数据
255+ this .list = data .data .data
256+ this .listLoading = false
257+ this .total = data .data .total
258+ this .url = data .data .appUrl
259+ // console.log('type', Object.prototype.toString.call(this.list))
260+ setTimeout (() => {
261+ this .reload = false
262+ this .reload_name = ' 刷新'
263+ }, 800 )
244264 },
245265 send (Data ) {
246266 this .websock .send (Data)
247267 },
248268 close () { // 关闭
249- console .log (' 断开连接' )
269+ // console.log('断开连接')
250270 },
251271 download (index , row ) {
252272 window .location .href = this .url + row .finish_url
253273 },
254274 download_log (index , row ) {
255275 download_log ({ id: row .id }).then (res => {
256- console .log (res)
276+ // console.log(res)
257277 if (res .code === 200 ) {
258278 window .location .href = this .url + res .data .failed_done_file
259279 }
260280 })
261281 },
262282 fetchData () {
263- this .listLoading = true
283+ this .listLoading = this .reload = true
284+ this .reload_name = ' 加载中'
264285 const params = Object .assign ({ ' page' : this .listQuery .page }, { ' perPage' : this .perpage })
265286 getList (params).then (response => {
266287 this .list = response .data .data
267288 this .listLoading = false
268289 this .total = response .data .total
269290 this .url = response .data .appUrl
270- console .log (' type' , Object .prototype .toString .call (this .list ))
271-
272- this .initWebSocket (8 )
291+ // console.log('type', Object.prototype.toString.call(this.list))
292+ setTimeout (() => {
293+ this .reload = false
294+ this .reload_name = ' 刷新'
295+ }, 800 )
296+ // this.initWebSocket()
273297 })
274298 },
275299 handleEdit (index , row ) {
@@ -354,4 +378,8 @@ export default {
354378 .pagination {
355379 margin : 20px auto ;
356380 }
381+ .reload {
382+ margin-right : 300px ;
383+ float : right ;
384+ }
357385 </style >
0 commit comments