slider-verify.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <template>
  2. <view class="slider-verify-box" v-if="isShow">
  3. <view class="verifyBox">
  4. <view class="slider-title">图形验证</view>
  5. <view class="slide-content">
  6. <view class="slide-tips">拖动下方滑块完成拼图</view>
  7. <view class="slider-pintu">
  8. <!-- <image id="pintuImg" :src="'../../images/slider-verify/' + img + '.jpg'" class="pintu"></image> -->
  9. <!-- <image id="pintuImg" :src="imgSrc" class="pintu"></image> -->
  10. <image id="pintuImg" src="../../static/slider-verify/3.jpg" class="pintu"></image>
  11. <view class="pintukuai" :style="{ top: top + 'px', left: oldx + 'px' }">
  12. <!-- <image :src="'../../images/slider-verify/' + img + '.jpg'" :style="{ top: '-' + top + 'px', left: '-' + left + 'px'}"></image> -->
  13. <image src="../../static/slider-verify/3.jpg"
  14. :style="{ top: '-' + top + 'px', left: '-' + left + 'px'}"></image>
  15. </view>
  16. <view class="yinying" :style="{ top: top + 'px', left: left + 'px' }"></view>
  17. </view>
  18. <view class="slider-movearea" @touchend="endTouchMove">
  19. <movable-area :animation="true">
  20. <movable-view :x="x" direction="horizontal" @change="startMove"></movable-view>
  21. </movable-area>
  22. <view class="huadao">拖动左边滑块完成上方拼图</view>
  23. </view>
  24. </view>
  25. <view class="slider-btn-group">
  26. <view class="slider-btn" @tap="closeSlider">关闭</view>
  27. <view class="slider-btn slide-btn-refresh" @tap="refreshVerify">刷新</view>
  28. </view>
  29. </view>
  30. </view>
  31. </template>
  32. <script>
  33. export default {
  34. name: 'slider-verify',
  35. props: {
  36. isShow: true
  37. },
  38. data() {
  39. return {
  40. x: 0, //初始距离
  41. oldx: 0, //移动的距离
  42. img: 1, //显示哪张图片
  43. left: 0, //随机拼图的最终X轴距离
  44. top: 0, //拼图的top距离
  45. //imgSrc: '@/static/slider-verify/1.jpg'
  46. //imgSrc: '../../static/slider-verify/1.jpg'
  47. };
  48. },
  49. watch: {
  50. // 每次打开重新刷新拼图
  51. isShow(newValue, oldValue) {
  52. if (newValue) {
  53. this.refreshVerify(); //刷新
  54. }
  55. }
  56. },
  57. mounted() {
  58. var that = this;
  59. that.refreshVerify();
  60. },
  61. methods: {
  62. //刷新验证
  63. refreshVerify() {
  64. var gl = Math.random().toFixed(2);
  65. this.left = uni.upx2px(560) * gl > uni.upx2px(280) ? uni.upx2px(280) : uni.upx2px(560) * gl + uni.upx2px(150); //生成随机X轴最终距离
  66. this.top = uni.upx2px(190) * gl; //生成随机Y轴初始距离
  67. if (gl <= 0.2) {
  68. this.img = 1;
  69. }
  70. if (gl > 0.2 && gl <= 0.4) {
  71. this.img = 2;
  72. }
  73. if (gl > 0.4 && gl <= 0.6) {
  74. this.img = 3;
  75. }
  76. if (gl > 0.6 && gl <= 0.8) {
  77. this.img = 4;
  78. }
  79. if (gl > 0.8 && gl <= 1) {
  80. this.img = 5;
  81. }
  82. this.resetMove(); //重置阴影位置
  83. },
  84. /* 滑动中 */
  85. startMove(e) {
  86. this.oldx = e.detail.x;
  87. },
  88. /* 滑动结束 */
  89. endTouchMove() {
  90. var that = this;
  91. if (Math.abs(that.oldx - that.left) <= 5) {
  92. uni.showToast({
  93. title: '验证成功',
  94. duration: 2500,
  95. success() {
  96. that.$emit('touchSliderResult', true);
  97. }
  98. });
  99. } else {
  100. that.refreshVerify();
  101. }
  102. },
  103. /* 重置阴影位置 */
  104. resetMove() {
  105. this.x = 1;
  106. this.oldx = 1;
  107. setTimeout(() => {
  108. this.x = 0;
  109. this.oldx = 0;
  110. }, 300);
  111. },
  112. // 关闭
  113. closeSlider() {
  114. this.$emit('touchSliderResult', false);
  115. }
  116. }
  117. };
  118. </script>
  119. <style lang="less">
  120. .slider-verify-box {
  121. position: fixed;
  122. top: 0;
  123. left: 0;
  124. width: 100%;
  125. height: 100%;
  126. background-color: rgba(0, 0, 0, 0.5);
  127. z-index: 999;
  128. }
  129. .verifyBox {
  130. position: absolute;
  131. top: 50%;
  132. left: 50%;
  133. transform: translate(-50%, -50%);
  134. // width: 85%;
  135. background-color: #fff;
  136. border-radius: 20 upx;
  137. box-shadow: 0 0 5 upx rgba(0, 0, 0);
  138. .slider-title {
  139. font-size: 36 upx;
  140. text-align: center;
  141. padding: 1em 0;
  142. color: rgba(2, 20, 33, 0.85);
  143. border-bottom: 1px solid rgba(2, 20, 33, 0.15);
  144. }
  145. .slide-content {
  146. width: 560rpx;
  147. padding: 0 1em;
  148. margin: 0 auto;
  149. .slide-tips {
  150. font-size: 28rpx;
  151. color: rgba(2, 20, 33, 0.45);
  152. padding: 0.5em 0;
  153. }
  154. .slider-pintu {
  155. position: relative;
  156. width: 100%;
  157. border-radius: 10rpx;
  158. overflow: hidden;
  159. .pintu {
  160. width: 560rpx;
  161. height: 315rpx;
  162. display: block;
  163. margin: 0 auto;
  164. }
  165. .pintukuai {
  166. position: absolute;
  167. top: 0;
  168. left: 0;
  169. width: 120rpx;
  170. height: 120rpx;
  171. z-index: 100;
  172. box-shadow: 0 0 5 upx rgba(0, 0, 0, 0.3);
  173. overflow: hidden;
  174. image {
  175. display: block;
  176. position: absolute;
  177. top: 0;
  178. left: 0;
  179. width: 560rpx;
  180. height: 315rpx;
  181. }
  182. }
  183. }
  184. .yinying {
  185. position: absolute;
  186. width: 120rpx;
  187. height: 120rpx;
  188. background-color: rgba(0, 0, 0, 0.7);
  189. //background-color: #ccc;
  190. }
  191. }
  192. }
  193. .slider-movearea {
  194. position: relative;
  195. height: 80 upx;
  196. width: 100%;
  197. margin: 25 upx auto;
  198. movable-area {
  199. height: 80 upx;
  200. width: 100%;
  201. movable-view {
  202. width: 80 upx;
  203. height: 80 upx;
  204. border-radius: 50%;
  205. background-color: #007cff;
  206. background-image: url(../../static/slider-verify/icon-button-normal.png);
  207. background-repeat: no-repeat;
  208. background-size: auto 30 upx;
  209. background-position: center;
  210. position: relative;
  211. z-index: 100;
  212. }
  213. }
  214. }
  215. .huadao {
  216. width: 100%;
  217. height: 66 upx;
  218. line-height: 66 upx;
  219. background: #eee;
  220. box-shadow: inset 0 0 5 upx #ccc;
  221. border-radius: 40rpx;
  222. color: #999;
  223. text-align: center;
  224. box-sizing: border-box;
  225. position: absolute;
  226. top: 7rpx;
  227. left: 0;
  228. font-size: 28rpx;
  229. z-index: 99;
  230. }
  231. .slider-btn-group {
  232. width: 100%;
  233. display: flex;
  234. justify-content: center;
  235. align-items: center;
  236. border-top: 1px solid rgba(2, 20, 33, 0.15);
  237. .slider-btn {
  238. flex: 1;
  239. height: 100rpx;
  240. line-height: 100rpx;
  241. text-align: center;
  242. font-size: 36rpx;
  243. color: rgba(2, 20, 33, 0.85);
  244. &:active {
  245. opacity: 0.8;
  246. }
  247. }
  248. .slide-btn-refresh {
  249. color: rgba(14, 107, 176, 1);
  250. border-left: 1px solid rgba(2, 20, 33, 0.15);
  251. }
  252. }
  253. </style>