verify.vue 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. <template>
  2. <div class="modal-overlay" @click.self="closeDialog">
  3. <div class="modal-content">
  4. <button class="modal-close" @click="closeDialog">×</button>
  5. <div class="code-dialog">
  6. <div class="title">请完成安全验证</div>
  7. <div class="pt">
  8. <div class="pt-verification-box">
  9. <div class="pt-verification-images">
  10. <div class="iconfont refresh" @click="refresh">&#xe64c;</div>
  11. <img :src="'data:image/jpeg;base64,' + bgImg" class="bg-img" alt="验证背景图">
  12. <img :src="'data:image/jpeg;base64,' + maskImg" class="drag-img" alt="拼图块"
  13. :style="{ left: dragWidth + 'px', top: top + 'px' }">
  14. <div class="mask"></div>
  15. </div>
  16. <div class="pt-dragbar">
  17. <div :class="['pt-drag-area', { fail: isFail, success: isSuccess }]" :style="{ width: dragWidth + 'px' }"
  18. v-if="dragWidth"></div>
  19. <div class="pt-dragbar-area" @mousemove="dragMove" @mouseup="dragEnd" @touchmove="dragMove"
  20. @touchend="dragEnd">
  21. <div :class="['pt-dragbar-view', {
  22. active: dragWidth > 2,
  23. fail: isFail,
  24. success: isSuccess
  25. }]" @mousedown="dragStart" @touchstart="dragStart"
  26. :style="{ left: dragWidth + 'px' }">
  27. <span class="iconfont">
  28. <span v-if="isSuccess">&#xe687;</span>
  29. <span v-else-if="isFail">&#xe65c;</span>
  30. <span v-else>&#xe62a;</span>
  31. </span>
  32. </div>
  33. <span v-if="dragWidth === 0" class="tips">{{ tips }}</span>
  34. </div>
  35. </div>
  36. </div>
  37. </div>
  38. </div>
  39. </div>
  40. </div>
  41. </template>
  42. <script>
  43. import { toRefs, ref, watch } from 'vue'
  44. const SliderVerifySetup = {
  45. name: 'SliderVerifySetup',
  46. props: {
  47. show: {
  48. type: Boolean,
  49. default: false
  50. },
  51. bgImg: {
  52. type: [String, Number],
  53. default: ''
  54. },
  55. maskImg: {
  56. type: [String, Number],
  57. default: ''
  58. },
  59. top: {
  60. type: [String, Number],
  61. default: 0
  62. },
  63. direction: {
  64. type: [String, Number],
  65. default: 'horizontal'
  66. },
  67. isSuccess: {
  68. type: Boolean,
  69. default: false,
  70. },
  71. isFail: {
  72. type: Boolean,
  73. default: false,
  74. }
  75. },
  76. emits: ['update:show', 'close', 'finish', 'refresh'],
  77. setup(props, { emit }) {
  78. // 使用 toRefs 解构 props
  79. const { show, isSuccess: propIsSuccess, isFail: propIsFail } = toRefs(props);
  80. // 响应式数据
  81. const tips = ref('向右拖动滑块填充拼图');
  82. const disabled = ref(false);
  83. const dragWidth = ref(0);
  84. const x = ref(0);
  85. const isDragging = ref(false);
  86. const startX = ref(0);
  87. const isSuccess = ref(propIsSuccess.value);
  88. const isFail = ref(propIsFail.value);
  89. // 计算属性
  90. const maxDragWidth = ref(300 - 40);
  91. // 方法
  92. const closeDialog = () => {
  93. emit('update:show', false);
  94. emit('close');
  95. };
  96. const dragStart = (e) => {
  97. isDragging.value = true;
  98. const clientX = e.type.includes('touch') ? e.touches[0].clientX : e.clientX;
  99. startX.value = clientX;
  100. document.addEventListener('mousemove', dragMove);
  101. document.addEventListener('touchmove', dragMove);
  102. document.addEventListener('mouseup', dragEnd);
  103. document.addEventListener('touchend', dragEnd);
  104. };
  105. const dragMove = (e) => {
  106. if (!isDragging.value) return;
  107. e.preventDefault();
  108. const clientX = e.type.includes('touch') ? e.touches[0].clientX : e.clientX;
  109. const deltaX = clientX - startX.value;
  110. dragWidth.value = Math.max(0, Math.min(deltaX, maxDragWidth.value));
  111. };
  112. const dragEnd = (e) => {
  113. if (!isDragging.value) return;
  114. isDragging.value = false;
  115. x.value = dragWidth.value;
  116. emit('finish', dragWidth.value);
  117. document.removeEventListener('mousemove', dragMove);
  118. document.removeEventListener('touchmove', dragMove);
  119. document.removeEventListener('mouseup', dragEnd);
  120. document.removeEventListener('touchend', dragEnd);
  121. };
  122. const refresh = () => {
  123. dragWidth.value = 0;
  124. isFail.value = false;
  125. isSuccess.value = false;
  126. x.value = 0;
  127. disabled.value = false;
  128. emit('refresh');
  129. };
  130. // 监听器
  131. watch(() => propIsSuccess, (newVal) => {
  132. isSuccess.value = newVal;
  133. });
  134. watch(() => propIsFail, (newVal) => {
  135. isFail.value = newVal;
  136. });
  137. watch(() => show, (newVal) => {
  138. if (!newVal) {
  139. dragWidth.value = 0;
  140. isFail.value = false;
  141. isSuccess.value = false;
  142. x.value = 0;
  143. disabled.value = false;
  144. }
  145. });
  146. return {
  147. tips,
  148. disabled,
  149. dragWidth,
  150. x,
  151. isDragging,
  152. isSuccess,
  153. isFail,
  154. closeDialog,
  155. dragStart,
  156. dragMove,
  157. dragEnd,
  158. refresh
  159. };
  160. }
  161. };
  162. export default SliderVerifySetup
  163. </script>
  164. <style lang="less" scoped>
  165. /* 滑块验证组件样式 */
  166. @font-face {
  167. font-family: 'iconfont';
  168. src: url('https://at.alicdn.com/t/font_2047533_o8axbabfs3.ttf') format('truetype');
  169. }
  170. .iconfont {
  171. font-family: iconfont !important;
  172. font-size: 16px;
  173. font-style: normal;
  174. -webkit-font-smoothing: antialiased;
  175. -moz-osx-font-smoothing: grayscale;
  176. }
  177. .modal-overlay {
  178. position: fixed;
  179. top: 0;
  180. left: 0;
  181. width: 100%;
  182. height: 100%;
  183. background: rgba(0, 0, 0, 0.5);
  184. display: flex;
  185. justify-content: center;
  186. align-items: center;
  187. z-index: 1000;
  188. }
  189. .modal-content {
  190. background: white;
  191. border-radius: 10px;
  192. position: relative;
  193. animation: modalAppear 0.3s ease;
  194. max-width: 90vw;
  195. }
  196. @keyframes modalAppear {
  197. from {
  198. opacity: 0;
  199. transform: scale(0.8);
  200. }
  201. to {
  202. opacity: 1;
  203. transform: scale(1);
  204. }
  205. }
  206. .modal-close {
  207. position: absolute;
  208. top: 10px;
  209. right: 10px;
  210. background: none;
  211. border: none;
  212. font-size: 20px;
  213. cursor: pointer;
  214. color: #999;
  215. z-index: 10;
  216. width: 30px;
  217. height: 30px;
  218. border-radius: 50%;
  219. display: flex;
  220. align-items: center;
  221. justify-content: center;
  222. }
  223. .modal-close:hover {
  224. background: #f5f5f5;
  225. color: #333;
  226. }
  227. .code-dialog {
  228. padding: 20px;
  229. border-radius: 10px;
  230. display: flex;
  231. flex-direction: column;
  232. align-items: center;
  233. }
  234. .title {
  235. font-size: 18px;
  236. color: #333333;
  237. width: 100%;
  238. text-align: left;
  239. margin-bottom: 10px;
  240. font-weight: 600;
  241. }
  242. .pt {
  243. width: 300px;
  244. max-width: 100%;
  245. margin: 0 auto;
  246. }
  247. .pt-verification-images {
  248. position: relative;
  249. border-radius: 8px;
  250. overflow: hidden;
  251. box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  252. }
  253. .refresh {
  254. position: absolute;
  255. right: 10px;
  256. top: 10px;
  257. z-index: 10;
  258. color: #FFF;
  259. font-weight: bold;
  260. cursor: pointer;
  261. background: rgba(0, 0, 0, 0.5);
  262. padding: 8px;
  263. border-radius: 4px;
  264. width: 30px;
  265. height: 30px;
  266. display: flex;
  267. align-items: center;
  268. justify-content: center;
  269. }
  270. .bg-img {
  271. width: 100%;
  272. height: auto;
  273. display: block;
  274. }
  275. .drag-img {
  276. position: absolute;
  277. width: 56px;
  278. height: 45px;
  279. top: 0;
  280. left: 0;
  281. z-index: 1;
  282. border: 2px dashed #1890ff;
  283. border-radius: 4px;
  284. pointer-events: none;
  285. }
  286. .mask {
  287. position: absolute;
  288. top: 0;
  289. left: 0;
  290. width: 100%;
  291. height: 100%;
  292. background: rgba(0, 0, 0, 0.2);
  293. pointer-events: none;
  294. }
  295. .pt-dragbar {
  296. position: relative;
  297. height: 40px;
  298. background-color: #F7F7F7;
  299. border: solid 1px #EEE;
  300. margin-top: 10px;
  301. border-radius: 20px;
  302. overflow: hidden;
  303. }
  304. .pt-drag-area {
  305. position: absolute;
  306. height: 40px;
  307. border: solid 1px #1890ff;
  308. background-color: #D1E9F1;
  309. top: -1px;
  310. border-radius: 20px;
  311. transition: all 0.3s ease;
  312. }
  313. .pt-drag-area.fail {
  314. border-color: #ff4d4f;
  315. background-color: #ffdbdb;
  316. }
  317. .pt-drag-area.success {
  318. border-color: #52c41a;
  319. background-color: #d7ffe1;
  320. }
  321. .pt-dragbar-area {
  322. position: absolute;
  323. width: 100%;
  324. height: 100%;
  325. left: 0;
  326. top: 0;
  327. border-radius: 20px;
  328. cursor: pointer;
  329. }
  330. .tips {
  331. font-size: 12px;
  332. color: #999;
  333. position: absolute;
  334. left: 50%;
  335. top: 50%;
  336. transform: translate(-50%, -50%);
  337. pointer-events: none;
  338. user-select: none;
  339. }
  340. .pt-dragbar-view {
  341. position: absolute;
  342. width: 40px;
  343. height: 40px;
  344. display: flex;
  345. align-items: center;
  346. justify-content: center;
  347. border: solid 1px #EEE;
  348. background-color: #FFF;
  349. top: -1px;
  350. left: 0;
  351. border-radius: 50%;
  352. cursor: grab;
  353. transition: all 0.3s ease;
  354. user-select: none;
  355. z-index: 2;
  356. box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  357. }
  358. .pt-dragbar-view.active {
  359. background-color: #1890ff;
  360. border-color: #1890ff;
  361. color: #FFF;
  362. }
  363. .pt-dragbar-view.fail {
  364. background-color: #ff4d4f;
  365. border-color: #ff4d4f;
  366. }
  367. .pt-dragbar-view.success {
  368. border-color: #52c41a;
  369. background-color: #00a029;
  370. }
  371. .pt-dragbar-view:active {
  372. cursor: grabbing;
  373. }
  374. </style>