许成详 6 年 前
コミット
306882a970
共有4 個のファイルを変更した130 個の追加124 個の削除を含む
  1. 8
    0
      src/apis.js
  2. 7
    9
      src/pages/admin.vue
  3. 115
    83
      src/pages/draw.vue
  4. 0
    32
      src/utils/websocket.js

+ 8
- 0
src/apis.js ファイルの表示

@@ -28,6 +28,14 @@ const apis = {
28 28
     url: `${prefix}/draw`,
29 29
     method: 'post',
30 30
   },
31
+  drawStatus: { // 获取抽奖状态
32
+    url: `${prefix}/lp/xxx`,
33
+    method: 'get',
34
+  },
35
+  postDrawStatus: { // 发送抽奖状态
36
+    url: `${prefix}/lp/xxx`,
37
+    method: 'post',
38
+  },
31 39
   // 奖品维护
32 40
   prize: {
33 41
     list: {

+ 7
- 9
src/pages/admin.vue ファイルの表示

@@ -15,8 +15,8 @@
15 15
             :data="settings"
16 16
             :prizes="prizes"
17 17
             @submit="saveSetting"
18
-            @startDraw="wsSend({type: 'start'})"
19
-            @endDraw="wsSend({type: 'end'})"
18
+            @startDraw="postDrawStatus({type: 'start'})"
19
+            @endDraw="postDrawStatus({type: 'stop'})"
20 20
           ></prize-setting>
21 21
         </el-tab-pane>
22 22
         <el-tab-pane label="中奖名单" name="winner">
@@ -39,7 +39,6 @@
39 39
 <script>
40 40
 import getApi from '../apis.js'
41 41
 import request from '../utils/request.js'
42
-import { newWebsocket, sendMsg } from '../utils/websocket.js'
43 42
 
44 43
 export default {
45 44
   name: 'admin-board',
@@ -49,7 +48,6 @@ export default {
49 48
     'prize': () => import('../components/prize.vue')
50 49
   },
51 50
   created () {
52
-    this.initWebSocket()
53 51
     this.getSettingList()
54 52
     this.getPrizeList()
55 53
   },
@@ -68,11 +66,11 @@ export default {
68 66
     }
69 67
   },
70 68
   methods: {
71
-    initWebSocket () {
72
-      newWebsocket('ws://192.168.0.11:8080/api/ws?id=admin')
73
-    },
74
-    wsSend (target) {
75
-      sendMsg(target)
69
+    postDrawStatus (data) {
70
+      const api = getApi('postDrawStatus')
71
+      request({ ...api, params: { message: JSON.stringify(data) || '' } }).then((res) => {
72
+        window.console.log(res)
73
+      })
76 74
     },
77 75
     tabChange (tab) {
78 76
       switch (tab.name) {

+ 115
- 83
src/pages/draw.vue ファイルの表示

@@ -187,7 +187,6 @@
187 187
 import '../assets/reset.css'
188 188
 import getApi from '../apis.js'
189 189
 import request from '../utils/request.js'
190
-import { newWebsocket } from '../utils/websocket.js'
191 190
 export default {
192 191
   name: 'draw',
193 192
   data () {
@@ -217,23 +216,30 @@ export default {
217 216
       randomArr: [],
218 217
       windowWidth: window.innerWidth,
219 218
       windowHeight: window.innerHeight,
220
-      drawWidth: Math.floor(window.innerWidth * .7)
219
+      drawWidth: Math.floor(window.innerWidth * .7),
220
+      getDrawStatus: true
221 221
     }
222 222
   },
223 223
   created () {
224
-    this.initWebSocket()
225 224
     this.getUserList()
226 225
     this.getPrizeList()
227 226
   },
228 227
   mounted () {
229 228
     this.$nextTick(() => {
230 229
       this.canvasInit()
230
+      this.drawStatus()
231
+      // var timer = null
232
+      // timer = window.setInterval(() => {
233
+      //   if (this.getDrawStatus) {
234
+      //     this.drawStatus()
235
+      //   } else {
236
+      //     window.clearInterval(timer)
237
+      //     timer = null
238
+      //   }
239
+      // }, 60000)
231 240
     })
232 241
   },
233 242
   methods: {
234
-    initWebSocket () {
235
-      newWebsocket('ws://192.168.0.11:8080/api/ws?id=web', this.webSocketCallback)
236
-    },
237 243
     webSocketCallback (e) {
238 244
       console.log(e)
239 245
     },
@@ -343,6 +349,26 @@ export default {
343 349
         this.initUsers = res.users.map(x => ({ ...x, active: false, show: true, delete: false, orgName: x.UserOrg }))
344 350
       })
345 351
     },
352
+    drawStatus () {
353
+      if (this.getDrawStatus) {
354
+        this.getDrawStatus = false
355
+        const api = getApi('drawStatus')
356
+        request(api).then((res) => {
357
+          res = JSON.parse(res)
358
+          this.getDrawStatus = true
359
+          // this.initDraw(res)
360
+          if (res.type === 'start') {
361
+            this.startDrawStep()
362
+            this.drawStatus()
363
+          } else if (res.type === 'stop') {
364
+            this.stopDrawStep()
365
+          }
366
+        }).catch(() => {
367
+          this.getDrawStatus = true
368
+          this.drawStatus()
369
+        })
370
+      }
371
+    },
346 372
     getPrizeList () { // 获取奖品数据
347 373
       const api = getApi('prize.list')
348 374
       request(api).then((res) => {
@@ -355,93 +381,99 @@ export default {
355 381
         this.initDraw()
356 382
       }
357 383
     },
358
-    initDraw () { // 初始化抽奖
384
+    initDraw (res) { // 初始化抽奖
359 385
       document.getElementsByTagName('html')[0].onkeydown = (e) => { // 页面添加键盘点击事件监听
360 386
         e = e || event
361 387
         if (e.ctrlKey && e.keyCode == 13) { // 启停抽奖组合键:Ctrl + Enter
362 388
           this.sureStatus = true
363 389
           if (!this.status) { // 重新抽奖
364
-            const api = getApi('draw')
365
-            request(api).then((res) => {
366
-              this.currentPrize = {
367
-                img: this.prizeList.filter((item) => { if (item.PrizeName === res.winner[0].PrizeName) return item.Picture })[0].Picture,
368
-                name: res.winner[0].PrizeName || '未知奖品',
369
-                num: res.winner.length || 0,
370
-                type: this.prizeList.filter((item) => { if (item.PrizeName === res.winner[0].PrizeName) return item.PrizeType })[0].PrizeType
371
-              }
372
-              this.winner = res.winner || []
373
-              this.drawCount = res.winner.length || 0
374
-              this.bgGrey = true
375
-              this.projectStart = false
376
-              this.status = !this.status
377
-              this.users = []
378
-              this.drawUsersArr = [] // 置空中奖数据
379
-              this.calcRandomArr(this.initUsers.length > 90 ? 90 : this.initUsers.length, this.initUsers, this.users, 'UserId', this.drawCount)
380
-              this.arrTimer = window.setInterval(() => {
381
-                this.users = []
382
-                this.calcRandomArr(this.initUsers.length > 90 ? 90 : this.initUsers.length, this.initUsers, this.users, 'UserId', this.drawCount)
383
-              }, this.time)
384
-            })
390
+            this.startDrawStep()
385 391
           } else { // 抽奖变换动画暂定
386
-            this.status = !this.status
387
-            clearInterval(this.arrTimer)
388
-            this.arrTimer = null
389
-            var num = 0
390
-            this.users.map((item) => { // 记录中奖数据,以便读取过渡动画结束位置坐标
391
-              if (item.active) {
392
-                item.UserName = this.winner[num].UserName
393
-                item.orgName = this.winner[num].UserOrg
394
-                item.isReset = true
395
-                num += 1
396
-                this.drawUsersArr.push({ ...item, width: 0, startX: '', startY: '', listX: '', listY: '' })
397
-              }
398
-            })
399
-            this.$nextTick(() => {
400
-              this.drawUsersArr.map((item, index) => { // 记录中奖名单结束位置坐标
401
-                item.endX = document.getElementsByClassName('targetLi')[index].getBoundingClientRect().x
402
-                item.endY = document.getElementsByClassName('targetLi')[index].getBoundingClientRect().y
403
-              })
404
-              window.setTimeout(() => {
405
-                this.drawUsersArr.map((item, index) => { // 记录中奖名单结束位置坐标
406
-                  item.listX = document.getElementsByClassName('endListLi')[index].getBoundingClientRect().x
407
-                  item.listY = document.getElementsByClassName('endListLi')[index].getBoundingClientRect().y
408
-                })
409
-              }, 50)
410
-              for (var n = 0; n < this.drawCount; n++) { // 记录中奖名单开始位置坐标
411
-                this.drawUsersArr[n].startX = document.getElementsByClassName('activeLi')[n].getBoundingClientRect().x
412
-                this.drawUsersArr[n].startY = document.getElementsByClassName('activeLi')[n].getBoundingClientRect().y
413
-                this.drawUsersArr[n].width = document.getElementsByClassName('activeLi')[n].getBoundingClientRect().width
414
-              }
415
-              this.drawUsersArr.map((item, index) => {
416
-                item.UserId = this.winner[index].UserId,
417
-                  item.UserName = this.winner[index].UserName,
418
-                  item.orgName = this.winner[index].UserOrg
419
-              })
420
-              window.setTimeout(() => {
421
-                this.users.map((item) => { // 隐藏未抽中数据
422
-                  item.show = false
423
-                })
424
-                this.drawUsersArr.map((item) => {
425
-                  item.startX = item.endX
426
-                  item.startY = item.endY
427
-                })
428
-                window.setTimeout(() => {
429
-                  this.animationOff = true
430
-                }, 1000)
431
-                window.setTimeout(() => {
432
-                  this.bgGrey = false
433
-                  this.hidePrize = false
434
-                  this.drawUsersArr.map((item) => {
435
-                    item.startX = item.listX
436
-                    item.startY = item.listY
437
-                  })
438
-                }, 2500)
439
-              }, 1000)
440
-            })
392
+            this.stopDrawStep()
441 393
           }
442 394
         }
443 395
       }
444 396
     },
397
+    startDrawStep () {
398
+      const api = getApi('draw')
399
+      request(api).then((res) => {
400
+        this.currentPrize = {
401
+          img: this.prizeList.filter((item) => { if (item.PrizeName === res.winner[0].PrizeName) return item.Picture })[0].Picture,
402
+          name: res.winner[0].PrizeName || '未知奖品',
403
+          num: res.winner.length || 0,
404
+          type: this.prizeList.filter((item) => { if (item.PrizeName === res.winner[0].PrizeName) return item.PrizeType })[0].PrizeType
405
+        }
406
+        this.winner = res.winner || []
407
+        this.drawCount = res.winner.length || 0
408
+        this.bgGrey = true
409
+        this.projectStart = false
410
+        this.status = !this.status
411
+        this.users = []
412
+        this.drawUsersArr = [] // 置空中奖数据
413
+        this.calcRandomArr(this.initUsers.length > 90 ? 90 : this.initUsers.length, this.initUsers, this.users, 'UserId', this.drawCount)
414
+        this.arrTimer = window.setInterval(() => {
415
+          this.users = []
416
+          this.calcRandomArr(this.initUsers.length > 90 ? 90 : this.initUsers.length, this.initUsers, this.users, 'UserId', this.drawCount)
417
+        }, this.time)
418
+      })
419
+    },
420
+    stopDrawStep () {
421
+      this.status = !this.status
422
+      clearInterval(this.arrTimer)
423
+      this.arrTimer = null
424
+      var num = 0
425
+      this.users.map((item) => { // 记录中奖数据,以便读取过渡动画结束位置坐标
426
+        if (item.active) {
427
+          item.UserName = this.winner[num].UserName
428
+          item.orgName = this.winner[num].UserOrg
429
+          item.isReset = true
430
+          num += 1
431
+          this.drawUsersArr.push({ ...item, width: 0, startX: '', startY: '', listX: '', listY: '' })
432
+        }
433
+      })
434
+      this.$nextTick(() => {
435
+        this.drawUsersArr.map((item, index) => { // 记录中奖名单结束位置坐标
436
+          item.endX = document.getElementsByClassName('targetLi')[index].getBoundingClientRect().x
437
+          item.endY = document.getElementsByClassName('targetLi')[index].getBoundingClientRect().y
438
+        })
439
+        window.setTimeout(() => {
440
+          this.drawUsersArr.map((item, index) => { // 记录中奖名单结束位置坐标
441
+            item.listX = document.getElementsByClassName('endListLi')[index].getBoundingClientRect().x
442
+            item.listY = document.getElementsByClassName('endListLi')[index].getBoundingClientRect().y
443
+          })
444
+        }, 50)
445
+        for (var n = 0; n < this.drawCount; n++) { // 记录中奖名单开始位置坐标
446
+          this.drawUsersArr[n].startX = document.getElementsByClassName('activeLi')[n].getBoundingClientRect().x
447
+          this.drawUsersArr[n].startY = document.getElementsByClassName('activeLi')[n].getBoundingClientRect().y
448
+          this.drawUsersArr[n].width = document.getElementsByClassName('activeLi')[n].getBoundingClientRect().width
449
+        }
450
+        this.drawUsersArr.map((item, index) => {
451
+          item.UserId = this.winner[index].UserId,
452
+            item.UserName = this.winner[index].UserName,
453
+            item.orgName = this.winner[index].UserOrg
454
+        })
455
+        window.setTimeout(() => {
456
+          this.users.map((item) => { // 隐藏未抽中数据
457
+            item.show = false
458
+          })
459
+          this.drawUsersArr.map((item) => {
460
+            item.startX = item.endX
461
+            item.startY = item.endY
462
+          })
463
+          window.setTimeout(() => {
464
+            this.animationOff = true
465
+          }, 1000)
466
+          window.setTimeout(() => {
467
+            this.bgGrey = false
468
+            this.hidePrize = false
469
+            this.drawUsersArr.map((item) => {
470
+              item.startX = item.listX
471
+              item.startY = item.listY
472
+            })
473
+          }, 2500)
474
+        }, 1000)
475
+      })
476
+    },
445 477
     calcRandomArr (num, pArr, aArr, key, activeNum) {
446 478
       var randomNum = Math.floor(Math.random() * pArr.length), bool = false
447 479
       if (pArr.length > num) {

+ 0
- 32
src/utils/websocket.js ファイルの表示

@@ -1,32 +0,0 @@
1
-let ws
2
-
3
-export function newWebsocket (url, cb) {
4
-  ws = new WebSocket(url)
5
-  cb = cb || function () { }
6
-  ws.onopen = () => {
7
-    window.console.log('开始连接...')
8
-    ws.send('ping')
9
-    window.setInterval(() => {
10
-      window.console.log('send ping')
11
-      ws.send('ping')
12
-    }, 60000)
13
-  }
14
-  ws.onmessage = (e) => {
15
-    window.console.log(e)
16
-    if (e === 'ping') {
17
-      ws.sendMsg('pong')
18
-    } else if (e === 'pong') {
19
-      window.console.log('心跳完成' + Date.now())
20
-    } else {
21
-      cb(e)
22
-    }
23
-    window.console.log(e)
24
-  }
25
-  ws.onclose = () => {
26
-    window.console.log('关闭连接...')
27
-  }
28
-}
29
-
30
-export function sendMsg (msg) {
31
-  ws.send(msg)
32
-}