index.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <template>
  2. <div class="verify-content" v-if="show" @mousemove="mouseMove" @mouseup="mouseUp" @click.stop>
  3. <div class="imgBox" :style="{width:data.originalWidth+'px',height:data.originalHeight + 'px'}">
  4. <img :src="data.backImage" style="width:100%;height:100%" alt="">
  5. <img class="slider" :src="data.slidingImage" :style="{left:distance+'px',top:data.randomY+'px'}" :width="data.sliderWidth" :height="data.sliderHeight" alt="">
  6. <Icon type="md-refresh" class="refresh" @click="init" />
  7. </div>
  8. <div class="handle" :style="{width:data.originalWidth+'px'}">
  9. <span class="bgcolor" :style="{width:distance + 'px',background:bgColor}"></span>
  10. <span class="swiper" :style="{left:distance + 'px'}" @mousedown="mouseDown">
  11. <Icon type="md-arrow-round-forward" />
  12. </span>
  13. <span class="text">{{verifyText}}</span>
  14. </div>
  15. </div>
  16. </template>
  17. <script>
  18. import { getVerifyImg, postVerifyImg } from './verify.js';
  19. export default {
  20. props: {
  21. // 传入数据,判断是登录、注册、修改密码
  22. verifyType: {
  23. defalut: 'LOGIN',
  24. type: String
  25. }
  26. },
  27. data () {
  28. return {
  29. show: false, // 验证码显隐
  30. type: 'LOGIN', // 请求类型
  31. data: { // 验证码数据
  32. backImage: '',
  33. slidingImage: '',
  34. originalHeight: 150,
  35. originalWidth: 300,
  36. sliderWidth: 60,
  37. sliderHeight: 60
  38. },
  39. distance: 0, // 拼图移动距离
  40. flag: false, // 判断滑块是否按下
  41. downX: 0, // 鼠标按下位置
  42. bgColor: '#04ad11', // 滑动背景颜色
  43. verifyText: '拖动滑块解锁' // 文字提示
  44. };
  45. },
  46. methods: {
  47. // 鼠标按下事件,开始拖动滑块
  48. mouseDown (e) {
  49. this.downX = e.clientX;
  50. this.flag = true;
  51. },
  52. // 鼠标移动事件,计算距离
  53. mouseMove (e) {
  54. if (this.flag) {
  55. let offset = e.clientX - this.downX;
  56. if (offset > this.data.originalWidth - 43) {
  57. this.distance = this.data.originalWidth - 43;
  58. } else if (offset < 0) {
  59. this.distance = 0;
  60. } else {
  61. this.distance = offset;
  62. }
  63. }
  64. },
  65. // 鼠标抬起事件,验证是否正确
  66. mouseUp () {
  67. if (!this.flag) return false;
  68. this.flag = false;
  69. let params = {
  70. verificationEnums: this.type,
  71. xPos: this.distance
  72. };
  73. postVerifyImg(params).then(res => {
  74. if (res.success) {
  75. if (res.result) {
  76. this.bgColor = 'green';
  77. this.verifyText = '解锁成功';
  78. this.$emit('change', { status: true, distance: this.distance });
  79. } else {
  80. this.bgColor = 'red';
  81. this.verifyText = '解锁失败';
  82. let that = this;
  83. setTimeout(() => {
  84. that.init();
  85. }, 1000);
  86. this.$emit('change', { status: false, distance: this.distance });
  87. }
  88. } else {
  89. this.init()
  90. }
  91. }).catch(()=>{
  92. this.init()
  93. });
  94. },
  95. init () { // 初始化数据
  96. this.flag = false;
  97. this.downX = 0;
  98. this.distance = 0;
  99. this.bgColor = '#04ad11';
  100. this.verifyText = '拖动滑块解锁';
  101. getVerifyImg(this.type).then(res => {
  102. if (res.result) {
  103. this.data = res.result;
  104. this.show = true;
  105. } else {
  106. this.$Message.warning('请求失败请重试!')
  107. }
  108. });
  109. }
  110. },
  111. watch: {
  112. verifyType: {
  113. immediate: true,
  114. handler: function (v) {
  115. this.type = v;
  116. }
  117. }
  118. }
  119. };
  120. </script>
  121. <style lang="scss" scoped>
  122. .verify-content{
  123. padding: 10px;
  124. background: #fff;
  125. border: 1px solid #eee;
  126. border-radius: 5px;
  127. box-shadow: 1px 1px 3px #999;
  128. }
  129. .imgBox {
  130. width: 300px;
  131. height: 150px;
  132. position: relative;
  133. overflow: hidden;
  134. .slider {
  135. position: absolute;
  136. cursor: pointer;
  137. }
  138. .refresh {
  139. position: absolute;
  140. right: 5px;
  141. top: 5px;
  142. font-size: 20px;
  143. color: #fff;
  144. cursor: pointer;
  145. }
  146. }
  147. .handle {
  148. border: 1px solid #e4dede;
  149. margin-top: 5px;
  150. height: 42px;
  151. background: #ddd;
  152. position: relative;
  153. .bgcolor {
  154. position: absolute;
  155. top: 0;
  156. left: 0;
  157. width: 40px;
  158. height: 40px;
  159. opacity: 0.5;
  160. background: #04ad11;
  161. }
  162. .swiper {
  163. position: absolute;
  164. width: 40px;
  165. height: 40px;
  166. background-color: #fff;
  167. cursor: pointer;
  168. display: flex;
  169. align-items: center;
  170. justify-content: center;
  171. .ivu-icon {
  172. font-size: 20px;
  173. }
  174. }
  175. .text {
  176. display: inline-block;
  177. width: inherit;
  178. text-align: center;
  179. line-height: 42px;
  180. font-size: 14px;
  181. user-select: none;
  182. }
  183. }
  184. </style>