Blame view

yudao-ui-admin/src/components/Verifition/Verify/VerifyPoints.vue 7.54 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
  <template>
    <div style="position: relative" >
      <div class="verify-img-out">
        <div
          class="verify-img-panel"
          :style="{'width': setSize.imgWidth,
                   'height': setSize.imgHeight,
                   'background-size' : setSize.imgWidth + ' '+ setSize.imgHeight,
                   'margin-bottom': vSpace + 'px'}"
        >
          <div v-show="showRefresh" class="verify-refresh" style="z-index:3" @click="refresh">
            <i class="iconfont icon-refresh" />
          </div>
          <img
            ref="canvas"
            :src="pointBackImgBase?('data:image/png;base64,'+pointBackImgBase):defaultImg"
            alt=""
            style="width:100%;height:100%;display:block"
            @click="bindingClick?canvasClick($event):undefined"
          >
  
          <div
            v-for="(tempPoint, index) in tempPoints"
            :key="index"
            class="point-area"
            :style="{
              'background-color':'#1abd6c',
              color:'#fff',
              'z-index':9999,
              width:'20px',
              height:'20px',
              'text-align':'center',
              'line-height':'20px',
              'border-radius': '50%',
              position:'absolute',
              top:parseInt(tempPoint.y-10) + 'px',
              left:parseInt(tempPoint.x-10) + 'px'
            }"
          >
            {{ index + 1 }}
          </div>
        </div>
      </div>
      <!-- 'height': this.barSize.height, -->
      <div
        class="verify-bar-area"
        :style="{'width': setSize.imgWidth,
                 'color': this.barAreaColor,
                 'border-color': this.barAreaBorderColor,
                 'line-height':this.barSize.height}"
      >
        <span class="verify-msg">{{ text }}</span>
      </div>
    </div>
  </template>
  <script type="text/babel">
  /**
   * VerifyPoints
   * @description 点选
   * */
011dc236   xingyu4j   替换var 为const/let
61
  import { resetSize } from './../utils/util'
4a920819   xingyu   vue2 新增行为验证码
62
  import { aesEncrypt } from '@/utils/ase'
7083d991   xingyu4j   refactor: verify
63
  import { reqGet, reqCheck } from '@/api/login'
4a920819   xingyu   vue2 新增行为验证码
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
168
169
  
  export default {
    name: 'VerifyPoints',
    props: {
      // 弹出式pop,固定fixed
      mode: {
        type: String,
        default: 'fixed'
      },
      captchaType: {
        type: String,
      },
      // 间隔
      vSpace: {
        type: Number,
        default: 5
      },
      imgSize: {
        type: Object,
        default() {
          return {
            width: '310px',
            height: '155px'
          }
        }
      },
      barSize: {
        type: Object,
        default() {
          return {
            width: '310px',
            height: '40px'
          }
        }
      },
      defaultImg: {
        type: String,
        default: ''
      }
    },
    data() {
      return {
        secretKey: '', // 后端返回的ase加密秘钥
        checkNum: 3, // 默认需要点击的字数
        fontPos: [], // 选中的坐标信息
        checkPosArr: [], // 用户点击的坐标
        num: 1, // 点击的记数
        pointBackImgBase: '', // 后端获取到的背景图片
        poinTextList: [], // 后端返回的点击字体顺序
        backToken: '', // 后端返回的token值
        setSize: {
          imgHeight: 0,
          imgWidth: 0,
          barHeight: 0,
          barWidth: 0
        },
        tempPoints: [],
        text: '',
        barAreaColor: undefined,
        barAreaBorderColor: undefined,
        showRefresh: true,
        bindingClick: true
      }
    },
    computed: {
      resetSize() {
        return resetSize
      }
    },
    watch: {
      // type变化则全面刷新
      type: {
        immediate: true,
        handler() {
          this.init()
        }
      }
    },
    mounted() {
      // 禁止拖拽
      this.$el.onselectstart = function() {
        return false
      }
    },
    methods: {
      init() {
        // 加载页面
        this.fontPos.splice(0, this.fontPos.length)
        this.checkPosArr.splice(0, this.checkPosArr.length)
        this.num = 1
        this.getPictrue()
        this.$nextTick(() => {
          this.setSize = this.resetSize(this)	// 重新设置宽度高度
          this.$parent.$emit('ready', this)
        })
      },
      canvasClick(e) {
        this.checkPosArr.push(this.getMousePos(this.$refs.canvas, e))
        if (this.num === this.checkNum) {
          this.num = this.createPoint(this.getMousePos(this.$refs.canvas, e))
          // 按比例转换坐标值
          this.checkPosArr = this.pointTransfrom(this.checkPosArr, this.setSize)
          // 等创建坐标执行完
          setTimeout(() => {
            // var flag = this.comparePos(this.fontPos, this.checkPosArr);
            // 发送后端请求
011dc236   xingyu4j   替换var 为const/let
170
            const captchaVerification = this.secretKey ? aesEncrypt(this.backToken + '---' + JSON.stringify(this.checkPosArr), this.secretKey) : this.backToken + '---' + JSON.stringify(this.checkPosArr)
4a920819   xingyu   vue2 新增行为验证码
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
196
197
198
199
200
201
202
203
204
205
206
207
            const data = {
              captchaType: this.captchaType,
              'pointJson': this.secretKey ? aesEncrypt(JSON.stringify(this.checkPosArr), this.secretKey) : JSON.stringify(this.checkPosArr),
              'token': this.backToken
            }
            reqCheck(data).then(res => {
              if (res.repCode === '0000') {
                this.barAreaColor = '#4cae4c'
                this.barAreaBorderColor = '#5cb85c'
                this.text = '验证成功'
                this.bindingClick = false
                if (this.mode === 'pop') {
                  setTimeout(() => {
                    this.$parent.clickShow = false
                    this.refresh()
                  }, 1500)
                }
                this.$parent.$emit('success', { captchaVerification })
              } else {
                this.$parent.$emit('error', this)
                this.barAreaColor = '#d9534f'
                this.barAreaBorderColor = '#d9534f'
                this.text = '验证失败'
                setTimeout(() => {
                  this.refresh()
                }, 700)
              }
            })
          }, 400)
        }
        if (this.num < this.checkNum) {
          this.num = this.createPoint(this.getMousePos(this.$refs.canvas, e))
        }
      },
  
      // 获取坐标
      getMousePos: function(obj, e) {
011dc236   xingyu4j   替换var 为const/let
208
209
        const x = e.offsetX
        const y = e.offsetY
4a920819   xingyu   vue2 新增行为验证码
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
        return { x, y }
      },
      // 创建坐标点
      createPoint: function(pos) {
        this.tempPoints.push(Object.assign({}, pos))
        return ++this.num
      },
      refresh: function() {
        this.tempPoints.splice(0, this.tempPoints.length)
        this.barAreaColor = '#000'
        this.barAreaBorderColor = '#ddd'
        this.bindingClick = true
        this.fontPos.splice(0, this.fontPos.length)
        this.checkPosArr.splice(0, this.checkPosArr.length)
        this.num = 1
        this.getPictrue()
4a920819   xingyu   vue2 新增行为验证码
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
        this.showRefresh = true
      },
  
      // 请求背景图片和验证图片
      getPictrue() {
        const data = {
          captchaType: this.captchaType,
          clientUid: localStorage.getItem('point'),
          ts: Date.now(), // 现在的时间戳
        }
        reqGet(data).then(res => {
          if (res.repCode === '0000') {
            this.pointBackImgBase = res.repData.originalImageBase64
            this.backToken = res.repData.token
            this.secretKey = res.repData.secretKey
            this.poinTextList = res.repData.wordList
            this.text = '请依次点击【' + this.poinTextList.join(',') + '】'
          } else {
            this.text = res.repMsg
          }
  
          // 判断接口请求次数是否失效
          if (res.repCode === '6201') {
            this.pointBackImgBase = null
          }
        })
      },
      // 坐标转换函数
      pointTransfrom(pointArr, imgSize) {
011dc236   xingyu4j   替换var 为const/let
255
        const newPointArr = pointArr.map(p => {
4a920819   xingyu   vue2 新增行为验证码
256
257
258
259
260
261
262
263
264
265
          const x = Math.round(310 * p.x / parseInt(imgSize.imgWidth))
          const y = Math.round(155 * p.y / parseInt(imgSize.imgHeight))
          return { x, y }
        })
        // console.log(newPointArr,"newPointArr");
        return newPointArr
      }
    },
  }
  </script>