Blame view

yudao-ui-admin/src/components/Verifition/Verify/VerifySlide.vue 11 KB
4a920819   xingyu   vue2 新增行为验证码
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
  <template>
    <div style="position: relative;">
      <div
        v-if="type === '2'"
        class="verify-img-out"
        :style="{height: (parseInt(setSize.imgHeight) + vSpace) + 'px'}"
      >
        <div
          class="verify-img-panel"
          :style="{width: setSize.imgWidth,
                   height: setSize.imgHeight,}"
        >
          <img :src="backImgBase?('data:image/png;base64,'+backImgBase):defaultImg" alt="" style="width:100%;height:100%;display:block">
          <div v-show="showRefresh" class="verify-refresh" @click="refresh"><i class="iconfont icon-refresh" />
          </div>
          <transition name="tips">
            <span v-if="tipWords" class="verify-tips" :class="passFlag ?'suc-bg':'err-bg'">{{ tipWords }}</span>
          </transition>
        </div>
      </div>
      <!-- 公共部分 -->
      <div
        class="verify-bar-area"
        :style="{width: setSize.imgWidth,
                 height: barSize.height,
                 'line-height':barSize.height}"
      >
        <span class="verify-msg" v-text="text" />
        <div
          class="verify-left-bar"
          :style="{width: (leftBarWidth!==undefined)?leftBarWidth: barSize.height, height: barSize.height, 'border-color': leftBarBorderColor, transaction: transitionWidth}"
        >
          <span class="verify-msg" v-text="finishText" />
          <div
            class="verify-move-block"
            :style="{width: barSize.height, height: barSize.height, 'background-color': moveBlockBackgroundColor, left: moveBlockLeft, transition: transitionLeft}"
            @touchstart="start"
            @mousedown="start"
          >
            <i
              :class="['verify-icon iconfont', iconClass]"
              :style="{color: iconColor}"
            />
            <div
              v-if="type === '2'"
              class="verify-sub-block"
              :style="{'width':Math.floor(parseInt(setSize.imgWidth)*47/310)+ 'px',
                       'height': setSize.imgHeight,
                       'top':'-' + (parseInt(setSize.imgHeight) + vSpace) + 'px',
                       'background-size': setSize.imgWidth + ' ' + setSize.imgHeight,
              }"
            >
              <img :src="'data:image/png;base64,'+blockBackImgBase" alt="" style="width:100%;height:100%;display:block">
            </div>
          </div>
        </div>
      </div>
    </div>
  </template>
  <script type="text/babel">
  /**
   * VerifySlide
   * @description 滑块
   * */
  import { aesEncrypt } from '@/utils/ase'
  import { resetSize } from './../utils/util'
7083d991   xingyu4j   refactor: verify
67
  import { reqGet, reqCheck } from '@/api/login'
4a920819   xingyu   vue2 新增行为验证码
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
  
  //  "captchaType":"blockPuzzle",
  export default {
    name: 'VerifySlide',
    props: {
      captchaType: {
        type: String,
      },
      type: {
        type: String,
        default: '1'
      },
      // 弹出式pop,固定fixed
      mode: {
        type: String,
        default: 'fixed'
      },
      vSpace: {
        type: Number,
        default: 5
      },
      explain: {
        type: String,
        default: '向右滑动完成验证'
      },
      imgSize: {
        type: Object,
        default() {
          return {
            width: '310px',
            height: '155px'
          }
        }
      },
      blockSize: {
        type: Object,
        default() {
          return {
            width: '50px',
            height: '50px'
          }
        }
      },
      barSize: {
        type: Object,
        default() {
          return {
            width: '310px',
            height: '40px'
          }
        }
      },
      defaultImg: {
        type: String,
        default: ''
      }
    },
    data() {
      return {
        secretKey: '', // 后端返回的加密秘钥 字段
        passFlag: '', // 是否通过的标识
        backImgBase: '', // 验证码背景图片
        blockBackImgBase: '', // 验证滑块的背景图片
        backToken: '', // 后端返回的唯一token值
        startMoveTime: '', // 移动开始的时间
        endMovetime: '', // 移动结束的时间
        tipsBackColor: '', // 提示词的背景颜色
        tipWords: '',
        text: '',
        finishText: '',
        setSize: {
          imgHeight: 0,
          imgWidth: 0,
          barHeight: 0,
          barWidth: 0
        },
        top: 0,
        left: 0,
        moveBlockLeft: undefined,
        leftBarWidth: undefined,
        // 移动中样式
        moveBlockBackgroundColor: undefined,
        leftBarBorderColor: '#ddd',
        iconColor: undefined,
        iconClass: 'icon-right',
        status: false, // 鼠标状态
        isEnd: false,		// 是够验证完成
        showRefresh: true,
        transitionLeft: '',
        transitionWidth: ''
      }
    },
    computed: {
      barArea() {
        return this.$el.querySelector('.verify-bar-area')
      },
      resetSize() {
        return resetSize
      }
    },
    watch: {
      // type变化则全面刷新
      type: {
        immediate: true,
        handler() {
          this.init()
        }
      }
    },
    mounted() {
      // 禁止拖拽
      this.$el.onselectstart = function() {
        return false
      }
      console.log(this.defaultImg)
    },
    methods: {
      init() {
        this.text = this.explain
        this.getPictrue()
        this.$nextTick(() => {
          const setSize = this.resetSize(this)	// 重新设置宽度高度
          for (const key in setSize) {
            this.$set(this.setSize, key, setSize[key])
          }
          this.$parent.$emit('ready', this)
        })
  
011dc236   xingyu4j   替换var 为const/let
196
        const _this = this
4a920819   xingyu   vue2 新增行为验证码
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
  
        window.removeEventListener('touchmove', function(e) {
          _this.move(e)
        })
        window.removeEventListener('mousemove', function(e) {
          _this.move(e)
        })
  
        // 鼠标松开
        window.removeEventListener('touchend', function() {
          _this.end()
        })
        window.removeEventListener('mouseup', function() {
          _this.end()
        })
  
        window.addEventListener('touchmove', function(e) {
          _this.move(e)
        })
        window.addEventListener('mousemove', function(e) {
          _this.move(e)
        })
  
        // 鼠标松开
        window.addEventListener('touchend', function() {
          _this.end()
        })
        window.addEventListener('mouseup', function() {
          _this.end()
        })
      },
  
      // 鼠标按下
      start: function(e) {
011dc236   xingyu4j   替换var 为const/let
231
        let x
4a920819   xingyu   vue2 新增行为验证码
232
233
        e = e || window.event
        if (!e.touches) { // 兼容PC端
011dc236   xingyu4j   替换var 为const/let
234
          x = e.clientX
4a920819   xingyu   vue2 新增行为验证码
235
        } else { // 兼容移动端
011dc236   xingyu4j   替换var 为const/let
236
          x = e.touches[0].pageX
4a920819   xingyu   vue2 新增行为验证码
237
238
239
240
241
242
243
244
245
246
247
248
249
250
        }
        this.startLeft = Math.floor(x - this.barArea.getBoundingClientRect().left)
        this.startMoveTime = +new Date() // 开始滑动的时间
        if (this.isEnd === false) {
          this.text = ''
          this.moveBlockBackgroundColor = '#337ab7'
          this.leftBarBorderColor = '#337AB7'
          this.iconColor = '#fff'
          e.stopPropagation()
          this.status = true
        }
      },
      // 鼠标移动
      move: function(e) {
011dc236   xingyu4j   替换var 为const/let
251
        let x
4a920819   xingyu   vue2 新增行为验证码
252
253
254
        e = e || window.event
        if (this.status && this.isEnd === false) {
          if (!e.touches) { // 兼容PC端
011dc236   xingyu4j   替换var 为const/let
255
            x = e.clientX
4a920819   xingyu   vue2 新增行为验证码
256
          } else { // 兼容移动端
011dc236   xingyu4j   替换var 为const/let
257
            x = e.touches[0].pageX
4a920819   xingyu   vue2 新增行为验证码
258
          }
011dc236   xingyu4j   替换var 为const/let
259
260
          const bar_area_left = this.barArea.getBoundingClientRect().left
          let move_block_left = x - bar_area_left  // 小方块相对于父元素的left值
4a920819   xingyu   vue2 新增行为验证码
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
          if (move_block_left >= this.barArea.offsetWidth - parseInt(parseInt(this.blockSize.width) / 2) - 2) {
            move_block_left = this.barArea.offsetWidth - parseInt(parseInt(this.blockSize.width) / 2) - 2
          }
          if (move_block_left <= 0) {
            move_block_left = parseInt(parseInt(this.blockSize.width) / 2)
          }
          // 拖动后小方块的left值
          this.moveBlockLeft = (move_block_left - this.startLeft) + 'px'
          this.leftBarWidth = (move_block_left - this.startLeft) + 'px'
        }
      },
  
      // 鼠标松开
      end: function() {
        this.endMovetime = +new Date()
011dc236   xingyu4j   替换var 为const/let
276
        const _this = this
4a920819   xingyu   vue2 新增行为验证码
277
278
        // 判断是否重合
        if (this.status && this.isEnd === false) {
423256c0   xingyu4j   fix 滑块验证码error
279
          let moveLeftDistance = parseInt((this.moveBlockLeft || '').replace('px', ''))
4a920819   xingyu   vue2 新增行为验证码
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
          moveLeftDistance = moveLeftDistance * 310 / parseInt(this.setSize.imgWidth)
          const data = {
            captchaType: this.captchaType,
            'pointJson': this.secretKey ? aesEncrypt(JSON.stringify({ x: moveLeftDistance, y: 5.0 }), this.secretKey) : JSON.stringify({ x: moveLeftDistance, y: 5.0 }),
            'token': this.backToken
          }
          reqCheck(data).then(res => {
            if (res.repCode === '0000') {
              this.moveBlockBackgroundColor = '#5cb85c'
              this.leftBarBorderColor = '#5cb85c'
              this.iconColor = '#fff'
              this.iconClass = 'icon-check'
              this.showRefresh = false
              this.isEnd = true
              if (this.mode === 'pop') {
                setTimeout(() => {
                  this.$parent.clickShow = false
                  this.refresh()
                }, 1500)
              }
              this.passFlag = true
              this.tipWords = `${((this.endMovetime - this.startMoveTime) / 1000).toFixed(2)}s验证成功`
011dc236   xingyu4j   替换var 为const/let
302
303
304
305
              const captchaVerification = this.secretKey ? aesEncrypt(this.backToken + '---' + JSON.stringify({
                x: moveLeftDistance,
                y: 5.0
              }), this.secretKey) : this.backToken + '---' + JSON.stringify({ x: moveLeftDistance, y: 5.0 })
4a920819   xingyu   vue2 新增行为验证码
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
              setTimeout(() => {
                this.tipWords = ''
                this.$parent.closeBox()
                this.$parent.$emit('success', { captchaVerification })
              }, 1000)
            } else {
              this.moveBlockBackgroundColor = '#d9534f'
              this.leftBarBorderColor = '#d9534f'
              this.iconColor = '#fff'
              this.iconClass = 'icon-close'
              this.passFlag = false
              setTimeout(function() {
                _this.refresh()
              }, 1000)
              this.$parent.$emit('error', this)
              this.tipWords = '验证失败'
              setTimeout(() => {
                this.tipWords = ''
              }, 1000)
            }
          })
          this.status = false
        }
      },
  
      refresh: function() {
        this.showRefresh = true
        this.finishText = ''
  
        this.transitionLeft = 'left .3s'
        this.moveBlockLeft = 0
  
        this.leftBarWidth = undefined
        this.transitionWidth = 'width .3s'
  
        this.leftBarBorderColor = '#ddd'
        this.moveBlockBackgroundColor = '#fff'
        this.iconColor = '#000'
        this.iconClass = 'icon-right'
        this.isEnd = false
  
        this.getPictrue()
        setTimeout(() => {
          this.transitionWidth = ''
          this.transitionLeft = ''
          this.text = this.explain
        }, 300)
      },
  
      // 请求背景图片和验证图片
      getPictrue() {
        const data = {
          captchaType: this.captchaType,
          clientUid: localStorage.getItem('slider'),
          ts: Date.now(), // 现在的时间戳
        }
        reqGet(data).then(res => {
          if (res.repCode === '0000') {
            this.backImgBase = res.repData.originalImageBase64
            this.blockBackImgBase = res.repData.jigsawImageBase64
            this.backToken = res.repData.token
            this.secretKey = res.repData.secretKey
          } else {
            this.tipWords = res.repMsg
          }
  
          // 判断接口请求次数是否失效
          if (res.repCode === '6201') {
            this.backImgBase = null
            this.blockBackImgBase = null
          }
        })
      },
    },
  }
  </script>