peng
20 小时以前 b56fcbe8d410c18b3a6ff7e4eb0e24815bbb5e09
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
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
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
196
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
231
232
233
234
<template>
  <div class="verify-content" v-if="show" @mousemove="mouseMove" @mouseup="mouseUp" @touchmove="touchMove" @touchend="touchEnd" @click.stop>
    <div class="imgBox" :style="{width:data.originalWidth+'px',height:data.originalHeight + 'px'}">
      <img :src="data.backImage" style="width:100%;height:100%" alt="">
      <img class="slider" :src="data.slidingImage" :style="{left:distance+'px',top:data.randomY+'px'}" :width="data.sliderWidth" :height="data.sliderHeight" alt="">
      <Icon type="md-refresh" class="refresh" @click="init" />
    </div>
    <div class="handle" :style="{width:data.originalWidth+'px'}">
      <span class="bgcolor" :style="{width:distance + 'px',background:bgColor}"></span>
      <span class="swiper" :style="{left:distance + 'px'}" @mousedown="mouseDown" @touchstart="touchStart">
        <Icon type="md-arrow-round-forward" />
      </span>
      <span class="text">{{verifyText}}</span>
    </div>
  </div>
</template>
<script>
import { getVerifyImg, postVerifyImg } from './verify.js';
export default {
  props: {
    // 传入数据,判断是登录、注册、修改密码
    verifyType: {
      defalut: 'LOGIN',
      type: String
    }
  },
  data () {
    return {
      show: false, // 验证码显隐
      type: 'LOGIN', // 请求类型
      data: { // 验证码数据
        backImage: '',
        slidingImage: '',
        originalHeight: 150,
        originalWidth: 300,
        sliderWidth: 60,
        sliderHeight: 60
      },
      distance: 0, // 拼图移动距离
      flag: false, // 判断滑块是否按下
      downX: 0, // 鼠标按下位置
      bgColor: '#04ad11', // 滑动背景颜色
      verifyText: '拖动滑块解锁' // 文字提示
    };
  },
  methods: {
    // 鼠标按下事件,开始拖动滑块
    mouseDown (e) {
      this.downX = e.clientX;
      this.flag = true;
    },
    // 触摸开始事件,开始拖动滑块
    touchStart (e) {
      // 阻止默认滚动行为
      e.preventDefault();
      this.downX = e.touches[0].clientX;
      this.flag = true;
    },
    // 鼠标移动事件,计算距离
    mouseMove (e) {
      if (this.flag) {
        let offset = e.clientX - this.downX;
 
        if (offset > this.data.originalWidth - 43) {
          this.distance = this.data.originalWidth - 43;
        } else if (offset < 0) {
          this.distance = 0;
        } else {
          this.distance = offset;
        }
      }
    },
    // 触摸移动事件,计算距离
    touchMove (e) {
      // 阻止默认滚动行为
      e.preventDefault();
      if (this.flag) {
        let offset = e.touches[0].clientX - this.downX;
 
        if (offset > this.data.originalWidth - 43) {
          this.distance = this.data.originalWidth - 43;
        } else if (offset < 0) {
          this.distance = 0;
        } else {
          this.distance = offset;
        }
      }
    },
    // 鼠标抬起事件,验证是否正确
    mouseUp () {
      if (!this.flag) return false;
      this.flag = false;
      this.verifySlide();
    },
    // 触摸结束事件,验证是否正确
    touchEnd () {
      if (!this.flag) return false;
      this.flag = false;
      this.verifySlide();
    },
    // 验证滑块位置
    verifySlide() {
      // 四舍五入确保位置是整数,提高验证准确性
      let xPos = Math.round(this.distance);
      
      // 添加调试信息
      console.log('滑块位置:', xPos, '容器宽度:', this.data.originalWidth, '设备像素比:', window.devicePixelRatio);
      
      let params = {
        verificationEnums: this.type,
        xPos: xPos
      };
      
      postVerifyImg(params).then(res => {
        console.log('验证响应:', res);
        if (res.success) {
          if (res.result) {
            this.bgColor = 'green';
            this.verifyText = '解锁成功';
            this.$emit('change', { status: true, distance: xPos });
          } else {
            this.bgColor = 'red';
            this.verifyText = '解锁失败';
            let that = this;
            setTimeout(() => {
              that.init();
            }, 1000);
            this.$emit('change', { status: false, distance: xPos });
          }
        } else {
          this.init()
        }
 
      }).catch((err)=>{
        console.error('验证请求失败:', err);
        this.init()
      });
    },
    init () { // 初始化数据
      this.flag = false;
      this.downX = 0;
      this.distance = 0;
      this.bgColor = '#04ad11';
      this.verifyText = '拖动滑块解锁';
      getVerifyImg(this.type).then(res => {
        if (res.result) {
          this.data = res.result;
          this.show = true;
        } else {
          this.$Message.warning('请求失败请重试!')
        }
      });
    }
  },
  watch: {
    verifyType: {
      immediate: true,
      handler: function (v) {
        this.type = v;
      }
    }
  }
};
</script>
<style lang="scss" scoped>
.verify-content{
  padding: 10px;
  background: #fff;
  border: 1px solid #eee;
  border-radius: 5px;
  box-shadow: 1px 1px 3px #999;
}
.imgBox {
  width: 300px;
  height: 150px;
  position: relative;
  overflow: hidden;
  user-select: none;
 
  .slider {
    position: absolute;
    cursor: pointer;
  }
 
  .refresh {
    position: absolute;
    right: 5px;
    top: 5px;
    font-size: 20px;
    color: #fff;
    cursor: pointer;
  }
}
.handle {
  border: 1px solid #e4dede;
  margin-top: 5px;
  height: 42px;
  background: #ddd;
  position: relative;
 
  .bgcolor {
    position: absolute;
    top: 0;
    left: 0;
    width: 40px;
    height: 40px;
    opacity: 0.5;
    background: #04ad11;
  }
 
  .swiper {
    position: absolute;
    width: 40px;
    height: 40px;
    background-color: #fff;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    .ivu-icon {
      font-size: 20px;
    }
  }
 
  .text {
    display: inline-block;
    width: inherit;
    text-align: center;
    line-height: 42px;
    font-size: 14px;
    user-select: none;
  }
}
</style>