slider-verify.vue 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. <template>
  2. <uni-popup ref="popupRef">
  3. <view class="popup-box">
  4. <view class="verifyBox">
  5. <view class="verify-title">
  6. <text>安全验证</text>
  7. <text class="verify-close iconfont">&#xe605;</text>
  8. </view>
  9. <view class="verify-hint">
  10. 滑动下方滑块完成拼图
  11. </view>
  12. <view class="slide-content">
  13. <view class="slider-pintu">
  14. <image id="pintuImg" src="@/static/logo.png" class="pintu">
  15. </image>
  16. <!-- <image id="pintuImg" :src="'@/static/images/slider-verify/' + img + '.png'" class="pintu">
  17. </image> -->
  18. <view class="pintukuai" :style="{ top: top + 'px', left: oldx + 'px' }">
  19. <image src="@/static/logo.png" :style="{ top: '-' + top + 'px', left: '-' + left + 'px'}">
  20. </image>
  21. </view>
  22. <view class="yinying" :style="{ top: top + 'px', left: left + 'px' }"></view>
  23. <view class="slider-success" v-show="moveSuccess">
  24. <text class="slider-success-icon iconfont">&#xe627;</text>
  25. <text class="slider-success-text">只用了{{ moveTime }}S,简直比闪电还快</text>
  26. </view>
  27. </view>
  28. <view class="slider-movearea" @touchend="endTouchMove">
  29. <movable-area :animation="true">
  30. <movable-view :x="x" direction="horizontal" @change="startMove">
  31. <text class="movable-item"></text>
  32. <text class="movable-item"></text>
  33. <text class="movable-item"></text>
  34. </movable-view>
  35. </movable-area>
  36. <view class="huadao"></view>
  37. </view>
  38. </view>
  39. <view class="slider-btn-group">
  40. <text class="slider-err"> {{ sliderErrText }} </text>
  41. <view class="slide-btn-refresh iconfont" @tap="refreshVerify">&#xec08;</view>
  42. </view>
  43. </view>
  44. </view>
  45. </uni-popup>
  46. </template>
  47. <script>
  48. export default {
  49. data() {
  50. return {
  51. x: 0, //初始距离
  52. oldx: 0, //移动的距离
  53. img: 'logo', //显示哪张图片
  54. left: 0, //随机拼图的最终X轴距离
  55. top: 0, //拼图的top距离
  56. moveTime: 0,
  57. moveInterval: null,
  58. moveSuccess: false,
  59. sliderErrText: '',
  60. };
  61. },
  62. mounted() {
  63. var that = this;
  64. that.refreshVerify();
  65. },
  66. methods: {
  67. open() {
  68. this.$nextTick(() => {
  69. this.$refs.popupRef.open();
  70. })
  71. },
  72. //刷新验证
  73. refreshVerify() {
  74. this.clearMoveTime()
  75. var gl = Math.random().toFixed(2);
  76. const leftNum = uni.upx2px(560) * gl > uni.upx2px(280) ? uni.upx2px(280) : uni.upx2px(560) * gl + uni.upx2px(
  77. 150); //生成随机X轴最终距离
  78. this.top = uni.upx2px(190) * gl; //生成随机Y轴初始距离
  79. const maxLeft = uni.upx2px(560) - uni.upx2px(90) - uni.upx2px(70)
  80. this.left = leftNum > maxLeft ? maxLeft : leftNum
  81. if (gl <= 0.2) {
  82. this.img = 1;
  83. }
  84. if (gl > 0.2 && gl <= 0.4) {
  85. this.img = 2;
  86. }
  87. if (gl > 0.4 && gl <= 0.6) {
  88. this.img = 3;
  89. }
  90. if (gl > 0.6 && gl <= 0.8) {
  91. this.img = 4;
  92. }
  93. if (gl > 0.8 && gl <= 1) {
  94. this.img = 5;
  95. }
  96. this.resetMove(); //重置阴影位置
  97. },
  98. /* 滑动中 */
  99. startMove(e) {
  100. if (e.detail.source) {
  101. this.setMoveTime();
  102. const maxNum = uni.upx2px(560) - uni.upx2px(70)
  103. if (e.detail.x <= maxNum) {
  104. this.oldx = e.detail.x;
  105. }
  106. };
  107. },
  108. setMoveTime(clear = false) {
  109. if (!this.moveInterval) {
  110. this.moveTime = 0;
  111. this.moveInterval = setInterval(() => {
  112. this.moveTime = Math.floor((this.moveTime + 0.1) * 100) / 100;
  113. console.log('this.moveTime = ', this.moveTime)
  114. }, 100)
  115. }
  116. if (clear) {
  117. try {
  118. setTimeout(() => {
  119. clearInterval(this.moveInterval)
  120. this.moveInterval = null;
  121. }, 100)
  122. console.log('this.moveInterval 1= ', this.moveInterval)
  123. console.log('this.moveInterval 2= ', this.moveInterval)
  124. } catch {}
  125. }
  126. },
  127. clearMoveTime() {
  128. this.setMoveTime(true)
  129. },
  130. /* 滑动结束 */
  131. endTouchMove() {
  132. var that = this;
  133. this.clearMoveTime()
  134. if (Math.abs(that.oldx - that.left) <= 5) {
  135. // uni.showToast({
  136. // title: '验证成功',
  137. // duration: 2500,
  138. // success() {
  139. // that.$emit('touchSliderResult', true);
  140. // }
  141. // });
  142. this.moveSuccess = true;
  143. setTimeout(() => {
  144. this.$emit('slideImgSuccess')
  145. },1000)
  146. } else {
  147. this.sliderErrText = '请控制拼图对齐切口'
  148. setTimeout(() => {
  149. this.sliderErrText = ''
  150. that.refreshVerify();
  151. }, 1000)
  152. }
  153. },
  154. /* 重置阴影位置 */
  155. resetMove() {
  156. this.x = 1;
  157. this.oldx = 1;
  158. setTimeout(() => {
  159. this.x = 0;
  160. this.oldx = 0;
  161. }, 300);
  162. },
  163. // 关闭
  164. closeSlider() {
  165. this.$emit('touchSliderResult', false);
  166. },
  167. }
  168. }
  169. </script>
  170. <style lang="scss" scoped>
  171. $imgSize: 70rpx;
  172. .verifyBox {
  173. // position: absolute;
  174. // top: 50%;
  175. // left: 50%;
  176. // transform: translate(-50%, -50%);
  177. // width: 560rpx;
  178. background-color: #fff;
  179. border-radius: 4rpx;
  180. padding: 5px;
  181. // box-shadow: 0 0 5upx rgba(0, 0, 0);
  182. .verify-title {
  183. width: 100%;
  184. display: flex;
  185. align-items: center;
  186. justify-content: space-between;
  187. font-size: 20rpx;
  188. color: #ccc;
  189. .verify-close {
  190. font-size: 40rpx;
  191. color: #666;
  192. }
  193. }
  194. .verify-hint {
  195. font-size: 28rpx;
  196. }
  197. .slide-content {
  198. width: 100%;
  199. padding: 5px 0;
  200. margin: 0 auto;
  201. .slide-tips {
  202. font-size: 28rpx;
  203. color: rgba(2, 20, 33, 0.45);
  204. padding: 0.5em 0;
  205. }
  206. .slider-pintu {
  207. position: relative;
  208. width: 100%;
  209. border-radius: 10rpx;
  210. overflow: hidden;
  211. .pintu {
  212. width: 560rpx;
  213. height: 300rpx;
  214. display: block;
  215. margin: 0 auto;
  216. }
  217. .pintukuai {
  218. position: absolute;
  219. top: 0;
  220. left: 0;
  221. width: $imgSize;
  222. height: $imgSize;
  223. z-index: 100;
  224. box-shadow: 0 0 5upx rgba(0, 0, 0, 0.3);
  225. overflow: hidden;
  226. image {
  227. display: block;
  228. position: absolute;
  229. top: 0;
  230. left: 0;
  231. width: 560rpx;
  232. height: 300rpx;
  233. }
  234. }
  235. }
  236. .yinying {
  237. position: absolute;
  238. width: $imgSize;
  239. height: $imgSize;
  240. background-color: rgba(0, 0, 0, 0.5);
  241. }
  242. // <view class="slider-success">
  243. // <text class="slider-success-icon">&#xe627;</text>
  244. // <text class="slider-success-text">只用了2S,</text>
  245. // </view>
  246. .slider-success {
  247. width: 100%;
  248. height: 100%;
  249. position: absolute;
  250. z-index: 101;
  251. left: 0;
  252. top: 0;
  253. background-color: rgba(255, 255, 255, 0.8);
  254. display: flex;
  255. justify-content: center;
  256. align-items: center;
  257. flex-direction: column;
  258. .slider-success-icon {
  259. font-size: 80rpx;
  260. color: $Theme-Color;
  261. }
  262. .slider-success-text {
  263. padding-top: 20rpx;
  264. font-size: 26rpx;
  265. color: $Theme-Color;
  266. }
  267. }
  268. }
  269. }
  270. .slider-movearea {
  271. position: relative;
  272. height: 50rpx;
  273. width: 100%;
  274. margin: 18rpx auto;
  275. movable-area {
  276. height: 50rpx;
  277. width: calc(100% - 90rpx);
  278. movable-view {
  279. width: 90rpx;
  280. height: 50rpx;
  281. border-radius: 25rpx;
  282. background-color: #007cff;
  283. position: relative;
  284. z-index: 100;
  285. box-shadow: 0px 0px 10px -1px #007cff;
  286. display: flex;
  287. justify-content: center;
  288. align-items: center;
  289. .movable-item {
  290. width: 3rpx;
  291. height: 20rpx;
  292. background-color: #fff;
  293. margin-right: 4px;
  294. &:last-child {
  295. margin-right: 0;
  296. }
  297. }
  298. }
  299. }
  300. }
  301. .huadao {
  302. width: 100%;
  303. height: 22rpx;
  304. background: #c2c2c2;
  305. box-shadow: inset 0 0 5upx #ccc;
  306. border-radius: 40rpx;
  307. position: absolute;
  308. top: 50%;
  309. left: 0;
  310. transform: translateY(-50%);
  311. font-size: 28rpx;
  312. z-index: 99;
  313. }
  314. .slider-btn-group {
  315. width: 100%;
  316. display: flex;
  317. justify-content: space-between;
  318. align-items: center;
  319. .slider-err {
  320. font-size: 24rpx;
  321. color: red;
  322. }
  323. .slide-btn-refresh {
  324. font-size: 40rpx;
  325. color: #c1c1c1;
  326. }
  327. }
  328. </style>