slider-verify.vue 5.7 KB

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