VerifySlide.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. <template>
  2. <div style="position: relative">
  3. <div
  4. v-if="type === '2'"
  5. class="verify-img-out"
  6. :style="{ height: parseInt(setSize.imgHeight) + vSpace + 'px' }"
  7. >
  8. <div class="verify-img-panel" :style="{ width: setSize.imgWidth, height: setSize.imgHeight }">
  9. <img
  10. :src="'data:image/png;base64,' + backImgBase"
  11. alt=""
  12. style="width: 100%; height: 100%; display: block"
  13. />
  14. <div class="verify-refresh" @click="refresh" v-show="showRefresh">
  15. <i class="iconfont icon-refresh"></i>
  16. </div>
  17. <transition name="tips">
  18. <span class="verify-tips" v-if="tipWords" :class="passFlag ? 'suc-bg' : 'err-bg'">
  19. {{ tipWords }}
  20. </span>
  21. </transition>
  22. </div>
  23. </div>
  24. <!-- 公共部分 -->
  25. <div
  26. class="verify-bar-area"
  27. :style="{ width: setSize.imgWidth, height: barSize.height, 'line-height': barSize.height }"
  28. >
  29. <span class="verify-msg" v-text="text"></span>
  30. <div
  31. class="verify-left-bar"
  32. :style="{
  33. width: leftBarWidth !== undefined ? leftBarWidth : barSize.height,
  34. height: barSize.height,
  35. 'border-color': leftBarBorderColor,
  36. transaction: transitionWidth
  37. }"
  38. >
  39. <span class="verify-msg" v-text="finishText"></span>
  40. <div
  41. class="verify-move-block"
  42. @touchstart="start"
  43. @mousedown="start"
  44. :style="{
  45. width: barSize.height,
  46. height: barSize.height,
  47. 'background-color': moveBlockBackgroundColor,
  48. left: moveBlockLeft,
  49. transition: transitionLeft
  50. }"
  51. >
  52. <i :class="['verify-icon iconfont', iconClass]" :style="{ color: iconColor }"></i>
  53. <div
  54. v-if="type === '2'"
  55. class="verify-sub-block"
  56. :style="{
  57. width: Math.floor((parseInt(setSize.imgWidth) * 47) / 310) + 'px',
  58. height: setSize.imgHeight,
  59. top: '-' + (parseInt(setSize.imgHeight) + vSpace) + 'px',
  60. 'background-size': setSize.imgWidth + ' ' + setSize.imgHeight
  61. }"
  62. >
  63. <img
  64. :src="'data:image/png;base64,' + blockBackImgBase"
  65. alt=""
  66. style="width: 100%; height: 100%; display: block; -webkit-user-drag: none"
  67. />
  68. </div>
  69. </div>
  70. </div>
  71. </div>
  72. </div>
  73. </template>
  74. <script type="text/babel" setup>
  75. /**
  76. * VerifySlide
  77. * @description 滑块
  78. * */
  79. import { aesEncrypt } from './../utils/ase'
  80. import { resetSize } from './../utils/util'
  81. import { getCode, reqCheck } from '@/api/login'
  82. const props = defineProps({
  83. captchaType: {
  84. type: String
  85. },
  86. type: {
  87. type: String,
  88. default: '1'
  89. },
  90. //弹出式pop,固定fixed
  91. mode: {
  92. type: String,
  93. default: 'fixed'
  94. },
  95. vSpace: {
  96. type: Number,
  97. default: 5
  98. },
  99. explain: {
  100. type: String,
  101. default: ''
  102. },
  103. imgSize: {
  104. type: Object,
  105. default() {
  106. return {
  107. width: '310px',
  108. height: '155px'
  109. }
  110. }
  111. },
  112. blockSize: {
  113. type: Object,
  114. default() {
  115. return {
  116. width: '50px',
  117. height: '50px'
  118. }
  119. }
  120. },
  121. barSize: {
  122. type: Object,
  123. default() {
  124. return {
  125. width: '310px',
  126. height: '30px'
  127. }
  128. }
  129. }
  130. })
  131. const { t } = useI18n()
  132. const { mode, captchaType, type, blockSize, explain } = toRefs(props)
  133. const { proxy } = getCurrentInstance()
  134. let secretKey = ref(''), //后端返回的ase加密秘钥
  135. passFlag = ref(''), //是否通过的标识
  136. backImgBase = ref(''), //验证码背景图片
  137. blockBackImgBase = ref(''), //验证滑块的背景图片
  138. backToken = ref(''), //后端返回的唯一token值
  139. startMoveTime = ref(''), //移动开始的时间
  140. endMovetime = ref(''), //移动结束的时间
  141. tipWords = ref(''),
  142. text = ref(''),
  143. finishText = ref(''),
  144. setSize = reactive({
  145. imgHeight: 0,
  146. imgWidth: 0,
  147. barHeight: 0,
  148. barWidth: 0
  149. }),
  150. moveBlockLeft = ref(undefined),
  151. leftBarWidth = ref(undefined),
  152. // 移动中样式
  153. moveBlockBackgroundColor = ref(undefined),
  154. leftBarBorderColor = ref('#ddd'),
  155. iconColor = ref(undefined),
  156. iconClass = ref('icon-right'),
  157. status = ref(false), //鼠标状态
  158. isEnd = ref(false), //是够验证完成
  159. showRefresh = ref(true),
  160. transitionLeft = ref(''),
  161. transitionWidth = ref(''),
  162. startLeft = ref(0)
  163. const barArea = computed(() => {
  164. return proxy.$el.querySelector('.verify-bar-area')
  165. })
  166. const init = () => {
  167. if (explain.value === '') {
  168. text.value = t('captcha.slide')
  169. } else {
  170. text.value = explain.value
  171. }
  172. getPictrue()
  173. nextTick(() => {
  174. let { imgHeight, imgWidth, barHeight, barWidth } = resetSize(proxy)
  175. setSize.imgHeight = imgHeight
  176. setSize.imgWidth = imgWidth
  177. setSize.barHeight = barHeight
  178. setSize.barWidth = barWidth
  179. proxy.$parent.$emit('ready', proxy)
  180. })
  181. window.removeEventListener('touchmove', function (e) {
  182. move(e)
  183. })
  184. window.removeEventListener('mousemove', function (e) {
  185. move(e)
  186. })
  187. //鼠标松开
  188. window.removeEventListener('touchend', function () {
  189. end()
  190. })
  191. window.removeEventListener('mouseup', function () {
  192. end()
  193. })
  194. window.addEventListener('touchmove', function (e) {
  195. move(e)
  196. })
  197. window.addEventListener('mousemove', function (e) {
  198. move(e)
  199. })
  200. //鼠标松开
  201. window.addEventListener('touchend', function () {
  202. end()
  203. })
  204. window.addEventListener('mouseup', function () {
  205. end()
  206. })
  207. }
  208. watch(type, () => {
  209. init()
  210. })
  211. onMounted(() => {
  212. // 禁止拖拽
  213. init()
  214. proxy.$el.onselectstart = function () {
  215. return false
  216. }
  217. })
  218. //鼠标按下
  219. const start = (e) => {
  220. e = e || window.event
  221. if (!e.touches) {
  222. //兼容PC端
  223. var x = e.clientX
  224. } else {
  225. //兼容移动端
  226. var x = e.touches[0].pageX
  227. }
  228. startLeft.value = Math.floor(x - barArea.value.getBoundingClientRect().left)
  229. startMoveTime.value = +new Date() //开始滑动的时间
  230. if (isEnd.value == false) {
  231. text.value = ''
  232. moveBlockBackgroundColor.value = '#337ab7'
  233. leftBarBorderColor.value = '#337AB7'
  234. iconColor.value = '#fff'
  235. e.stopPropagation()
  236. status.value = true
  237. }
  238. }
  239. //鼠标移动
  240. const move = (e) => {
  241. e = e || window.event
  242. if (status.value && isEnd.value == false) {
  243. if (!e.touches) {
  244. //兼容PC端
  245. var x = e.clientX
  246. } else {
  247. //兼容移动端
  248. var x = e.touches[0].pageX
  249. }
  250. var bar_area_left = barArea.value.getBoundingClientRect().left
  251. var move_block_left = x - bar_area_left //小方块相对于父元素的left值
  252. if (
  253. move_block_left >=
  254. barArea.value.offsetWidth - parseInt(parseInt(blockSize.value.width) / 2) - 2
  255. ) {
  256. move_block_left =
  257. barArea.value.offsetWidth - parseInt(parseInt(blockSize.value.width) / 2) - 2
  258. }
  259. if (move_block_left <= 0) {
  260. move_block_left = parseInt(parseInt(blockSize.value.width) / 2)
  261. }
  262. //拖动后小方块的left值
  263. moveBlockLeft.value = move_block_left - startLeft.value + 'px'
  264. leftBarWidth.value = move_block_left - startLeft.value + 'px'
  265. }
  266. }
  267. //鼠标松开
  268. const end = () => {
  269. endMovetime.value = +new Date()
  270. //判断是否重合
  271. if (status.value && isEnd.value == false) {
  272. var moveLeftDistance = parseInt((moveBlockLeft.value || '').replace('px', ''))
  273. moveLeftDistance = (moveLeftDistance * 310) / parseInt(setSize.imgWidth)
  274. let data = {
  275. captchaType: captchaType.value,
  276. pointJson: secretKey.value
  277. ? aesEncrypt(JSON.stringify({ x: moveLeftDistance, y: 5.0 }), secretKey.value)
  278. : JSON.stringify({ x: moveLeftDistance, y: 5.0 }),
  279. token: backToken.value
  280. }
  281. reqCheck(data).then((res) => {
  282. if (res.repCode == '0000') {
  283. moveBlockBackgroundColor.value = '#5cb85c'
  284. leftBarBorderColor.value = '#5cb85c'
  285. iconColor.value = '#fff'
  286. iconClass.value = 'icon-check'
  287. showRefresh.value = false
  288. isEnd.value = true
  289. if (mode.value == 'pop') {
  290. setTimeout(() => {
  291. proxy.$parent.clickShow = false
  292. refresh()
  293. }, 1500)
  294. }
  295. passFlag.value = true
  296. tipWords.value = `${((endMovetime.value - startMoveTime.value) / 1000).toFixed(2)}s
  297. ${t('captcha.success')}`
  298. var captchaVerification = secretKey.value
  299. ? aesEncrypt(
  300. backToken.value + '---' + JSON.stringify({ x: moveLeftDistance, y: 5.0 }),
  301. secretKey.value
  302. )
  303. : backToken.value + '---' + JSON.stringify({ x: moveLeftDistance, y: 5.0 })
  304. setTimeout(() => {
  305. tipWords.value = ''
  306. proxy.$parent.closeBox()
  307. proxy.$parent.$emit('success', { captchaVerification })
  308. }, 1000)
  309. } else {
  310. moveBlockBackgroundColor.value = '#d9534f'
  311. leftBarBorderColor.value = '#d9534f'
  312. iconColor.value = '#fff'
  313. iconClass.value = 'icon-close'
  314. passFlag.value = false
  315. setTimeout(function () {
  316. refresh()
  317. }, 1000)
  318. proxy.$parent.$emit('error', proxy)
  319. tipWords.value = t('captcha.fail')
  320. setTimeout(() => {
  321. tipWords.value = ''
  322. }, 1000)
  323. }
  324. })
  325. status.value = false
  326. }
  327. }
  328. const refresh = async () => {
  329. showRefresh.value = true
  330. finishText.value = ''
  331. transitionLeft.value = 'left .3s'
  332. moveBlockLeft.value = 0
  333. leftBarWidth.value = undefined
  334. transitionWidth.value = 'width .3s'
  335. leftBarBorderColor.value = '#ddd'
  336. moveBlockBackgroundColor.value = '#fff'
  337. iconColor.value = '#000'
  338. iconClass.value = 'icon-right'
  339. isEnd.value = false
  340. await getPictrue()
  341. setTimeout(() => {
  342. transitionWidth.value = ''
  343. transitionLeft.value = ''
  344. text.value = explain.value
  345. }, 300)
  346. }
  347. // 请求背景图片和验证图片
  348. const getPictrue = async () => {
  349. let data = {
  350. captchaType: captchaType.value
  351. }
  352. const res = await getCode(data)
  353. if (res.repCode == '0000') {
  354. backImgBase.value = res.repData.originalImageBase64
  355. blockBackImgBase.value = res.repData.jigsawImageBase64
  356. backToken.value = res.repData.token
  357. secretKey.value = res.repData.secretKey
  358. } else {
  359. tipWords.value = res.repMsg
  360. }
  361. }
  362. </script>