VerifySlide.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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
  9. class="verify-img-panel"
  10. :style="{ width: setSize.imgWidth, height: setSize.imgHeight }"
  11. >
  12. <img
  13. :src="
  14. backImgBase ? 'data:image/png;base64,' + backImgBase : defaultImg
  15. "
  16. alt=""
  17. style="width: 100%; height: 100%; display: block"
  18. />
  19. <div v-show="showRefresh" class="verify-refresh" @click="refresh">
  20. <i class="iconfont icon-refresh" />
  21. </div>
  22. <transition name="tips">
  23. <span
  24. v-if="tipWords"
  25. class="verify-tips"
  26. :class="passFlag ? 'suc-bg' : 'err-bg'"
  27. >{{ tipWords }}</span
  28. >
  29. </transition>
  30. </div>
  31. </div>
  32. <!-- 公共部分 -->
  33. <div
  34. class="verify-bar-area"
  35. :style="{
  36. width: setSize.imgWidth,
  37. height: barSize.height,
  38. 'line-height': barSize.height,
  39. }"
  40. >
  41. <span class="verify-msg" v-text="text" />
  42. <div
  43. class="verify-left-bar"
  44. :style="{
  45. width: leftBarWidth !== undefined ? leftBarWidth : barSize.height,
  46. height: barSize.height,
  47. 'border-color': leftBarBorderColor,
  48. transaction: transitionWidth,
  49. }"
  50. >
  51. <span class="verify-msg" v-text="finishText" />
  52. <div
  53. class="verify-move-block"
  54. :style="{
  55. width: barSize.height,
  56. height: barSize.height,
  57. 'background-color': moveBlockBackgroundColor,
  58. left: moveBlockLeft,
  59. transition: transitionLeft,
  60. }"
  61. @touchstart="start"
  62. @mousedown="start"
  63. >
  64. <i
  65. :class="['verify-icon iconfont', iconClass]"
  66. :style="{ color: iconColor }"
  67. />
  68. <div
  69. v-if="type === '2'"
  70. class="verify-sub-block"
  71. :style="{
  72. width: Math.floor((parseInt(setSize.imgWidth) * 47) / 310) + 'px',
  73. height: setSize.imgHeight,
  74. top: '-' + (parseInt(setSize.imgHeight) + vSpace) + 'px',
  75. 'background-size': setSize.imgWidth + ' ' + setSize.imgHeight,
  76. }"
  77. >
  78. <img
  79. :src="'data:image/png;base64,' + blockBackImgBase"
  80. alt=""
  81. style="width: 100%; height: 100%; display: block"
  82. />
  83. </div>
  84. </div>
  85. </div>
  86. </div>
  87. </div>
  88. </template>
  89. <script type="text/babel">
  90. /**
  91. * VerifySlide
  92. * @description 滑块
  93. * */
  94. import { aesEncrypt } from './../utils/ase';
  95. import { resetSize } from './../utils/util';
  96. // "captchaType":"blockPuzzle",
  97. export default {
  98. name: 'VerifySlide',
  99. props: {
  100. captchaType: {
  101. type: String,
  102. },
  103. type: {
  104. type: String,
  105. default: '1',
  106. },
  107. // 弹出式pop,固定fixed
  108. mode: {
  109. type: String,
  110. default: 'fixed',
  111. },
  112. vSpace: {
  113. type: Number,
  114. default: 5,
  115. },
  116. explain: {
  117. type: String,
  118. default: '向右滑动完成验证',
  119. },
  120. imgSize: {
  121. type: Object,
  122. default() {
  123. return {
  124. width: '310px',
  125. height: '155px',
  126. };
  127. },
  128. },
  129. blockSize: {
  130. type: Object,
  131. default() {
  132. return {
  133. width: '50px',
  134. height: '50px',
  135. };
  136. },
  137. },
  138. barSize: {
  139. type: Object,
  140. default() {
  141. return {
  142. width: '310px',
  143. height: '50px',
  144. };
  145. },
  146. },
  147. defaultImg: {
  148. type: String,
  149. default: '',
  150. },
  151. },
  152. data() {
  153. return {
  154. secretKey: '', // 后端返回的加密秘钥 字段
  155. passFlag: '', // 是否通过的标识
  156. backImgBase: '', // 验证码背景图片
  157. blockBackImgBase: '', // 验证滑块的背景图片
  158. backToken: '', // 后端返回的唯一token值
  159. startMoveTime: '', // 移动开始的时间
  160. endMovetime: '', // 移动结束的时间
  161. tipsBackColor: '', // 提示词的背景颜色
  162. tipWords: '',
  163. text: '',
  164. finishText: '',
  165. setSize: {
  166. imgHeight: 0,
  167. imgWidth: 0,
  168. barHeight: 0,
  169. barWidth: 0,
  170. },
  171. top: 0,
  172. left: 0,
  173. moveBlockLeft: undefined,
  174. leftBarWidth: undefined,
  175. // 移动中样式
  176. moveBlockBackgroundColor: undefined,
  177. leftBarBorderColor: '#ddd',
  178. iconColor: undefined,
  179. iconClass: 'icon-right',
  180. status: false, // 鼠标状态
  181. isEnd: false, // 是够验证完成
  182. showRefresh: true,
  183. transitionLeft: '',
  184. transitionWidth: '',
  185. };
  186. },
  187. computed: {
  188. barArea() {
  189. return this.$el.querySelector('.verify-bar-area');
  190. },
  191. resetSize() {
  192. return resetSize;
  193. },
  194. },
  195. watch: {
  196. // type变化则全面刷新
  197. type: {
  198. immediate: true,
  199. handler() {
  200. this.init();
  201. },
  202. },
  203. },
  204. mounted() {
  205. let verifyMoveBlock = document.querySelector('.verify-move-block');
  206. verifyMoveBlock.addEventListener('touchmove', function (event) {
  207. // 阻止默认的滑动行为
  208. event.preventDefault();
  209. });
  210. // 禁止拖拽
  211. this.$el.onselectstart = function () {
  212. return false;
  213. };
  214. console.log(this.defaultImg);
  215. },
  216. methods: {
  217. init() {
  218. this.text = this.explain;
  219. this.getPictrue();
  220. this.$nextTick(() => {
  221. const setSize = this.resetSize(this); // 重新设置宽度高度
  222. for (const key in setSize) {
  223. this.$set(this.setSize, key, setSize[key]);
  224. }
  225. this.$parent.$emit('ready', this);
  226. });
  227. var _this = this;
  228. window.removeEventListener('touchmove', function (e) {
  229. _this.move(e);
  230. });
  231. window.removeEventListener('mousemove', function (e) {
  232. _this.move(e);
  233. });
  234. // 鼠标松开
  235. window.removeEventListener('touchend', function () {
  236. _this.end();
  237. });
  238. window.removeEventListener('mouseup', function () {
  239. _this.end();
  240. });
  241. window.addEventListener('touchmove', function (e) {
  242. _this.move(e);
  243. });
  244. window.addEventListener('mousemove', function (e) {
  245. _this.move(e);
  246. });
  247. // 鼠标松开
  248. window.addEventListener('touchend', function () {
  249. _this.end();
  250. });
  251. window.addEventListener('mouseup', function () {
  252. _this.end();
  253. });
  254. },
  255. // 鼠标按下
  256. start: function (e) {
  257. e = e || window.event;
  258. let x = null;
  259. if (!e.touches) {
  260. // 兼容PC端
  261. x = e.clientX;
  262. } else {
  263. // 兼容移动端
  264. x = e.touches[0].pageX;
  265. }
  266. this.startLeft = Math.floor(
  267. x - this.barArea.getBoundingClientRect().left,
  268. );
  269. this.startMoveTime = +new Date(); // 开始滑动的时间
  270. if (this.isEnd == false) {
  271. this.text = '';
  272. this.moveBlockBackgroundColor = '#337ab7';
  273. this.leftBarBorderColor = '#337AB7';
  274. this.iconColor = '#fff';
  275. // e.stopPropagation();
  276. this.status = true;
  277. }
  278. },
  279. // 鼠标移动
  280. move: function (e) {
  281. e = e || window.event;
  282. if (this.status && this.isEnd == false) {
  283. if (!e.touches) {
  284. // 兼容PC端
  285. var x = e.clientX;
  286. } else {
  287. // 兼容移动端
  288. var x = e.touches[0].pageX;
  289. }
  290. var bar_area_left = this.barArea.getBoundingClientRect().left;
  291. var move_block_left = x - bar_area_left; // 小方块相对于父元素的left值
  292. if (
  293. move_block_left >=
  294. this.barArea.offsetWidth -
  295. parseInt(parseInt(this.blockSize.width) / 2) -
  296. 2
  297. ) {
  298. move_block_left =
  299. this.barArea.offsetWidth -
  300. parseInt(parseInt(this.blockSize.width) / 2) -
  301. 2;
  302. }
  303. if (move_block_left <= 0) {
  304. move_block_left = parseInt(parseInt(this.blockSize.width) / 2);
  305. }
  306. // 拖动后小方块的left值
  307. this.moveBlockLeft = move_block_left - this.startLeft + 'px';
  308. this.leftBarWidth = move_block_left - this.startLeft + 'px';
  309. }
  310. },
  311. // 鼠标松开
  312. end: function () {
  313. this.endMovetime = +new Date();
  314. var _this = this;
  315. // 判断是否重合
  316. if (this.status && this.isEnd == false) {
  317. var moveLeftDistance = parseInt(
  318. (this.moveBlockLeft || '').replace('px', ''),
  319. );
  320. moveLeftDistance =
  321. (moveLeftDistance * 310) / parseInt(this.setSize.imgWidth);
  322. const data = {
  323. captchaType: this.captchaType,
  324. pointJson: this.secretKey
  325. ? aesEncrypt(
  326. JSON.stringify({ x: moveLeftDistance, y: 5.0 }),
  327. this.secretKey,
  328. )
  329. : JSON.stringify({ x: moveLeftDistance, y: 5.0 }),
  330. token: this.backToken,
  331. };
  332. this.$axios.$post('activity/captcha/check', data).then((res) => {
  333. if (res.repCode == '0000') {
  334. this.moveBlockBackgroundColor = '#5cb85c';
  335. this.leftBarBorderColor = '#5cb85c';
  336. this.iconColor = '#fff';
  337. this.iconClass = 'icon-check';
  338. this.showRefresh = false;
  339. this.isEnd = true;
  340. if (this.mode == 'pop') {
  341. setTimeout(() => {
  342. this.$parent.clickShow = false;
  343. this.refresh();
  344. }, 1500);
  345. }
  346. this.passFlag = true;
  347. this.tipWords = `${(
  348. (this.endMovetime - this.startMoveTime) /
  349. 1000
  350. ).toFixed(2)}s验证成功`;
  351. var captchaVerification = this.secretKey
  352. ? aesEncrypt(
  353. this.backToken +
  354. '---' +
  355. JSON.stringify({ x: moveLeftDistance, y: 5.0 }),
  356. this.secretKey,
  357. )
  358. : this.backToken +
  359. '---' +
  360. JSON.stringify({ x: moveLeftDistance, y: 5.0 });
  361. setTimeout(() => {
  362. this.tipWords = '';
  363. this.$parent.closeBox();
  364. this.$parent.$emit('success', { captchaVerification });
  365. }, 1000);
  366. } else {
  367. this.moveBlockBackgroundColor = '#d9534f';
  368. this.leftBarBorderColor = '#d9534f';
  369. this.iconColor = '#fff';
  370. this.iconClass = 'icon-close';
  371. this.passFlag = false;
  372. setTimeout(function () {
  373. _this.refresh();
  374. }, 1000);
  375. this.$parent.$emit('error', this);
  376. this.tipWords = '验证失败';
  377. setTimeout(() => {
  378. this.tipWords = '';
  379. }, 1000);
  380. }
  381. });
  382. this.status = false;
  383. }
  384. },
  385. refresh: function () {
  386. this.showRefresh = true;
  387. this.finishText = '';
  388. this.transitionLeft = 'left .3s';
  389. this.moveBlockLeft = 0;
  390. this.leftBarWidth = undefined;
  391. this.transitionWidth = 'width .3s';
  392. this.leftBarBorderColor = '#ddd';
  393. this.moveBlockBackgroundColor = '#fff';
  394. this.iconColor = '#000';
  395. this.iconClass = 'icon-right';
  396. this.isEnd = false;
  397. this.getPictrue();
  398. setTimeout(() => {
  399. this.transitionWidth = '';
  400. this.transitionLeft = '';
  401. this.text = this.explain;
  402. }, 300);
  403. },
  404. // 请求背景图片和验证图片
  405. getPictrue() {
  406. const data = {
  407. captchaType: this.captchaType,
  408. clientUid: localStorage.getItem('slider'),
  409. ts: Date.now(), // 现在的时间戳
  410. };
  411. this.$axios.$post('activity/captcha/get', data).then((res) => {
  412. if (res.repCode == '0000') {
  413. this.backImgBase = res.repData.originalImageBase64;
  414. this.blockBackImgBase = res.repData.jigsawImageBase64;
  415. this.backToken = res.repData.token;
  416. this.secretKey = res.repData.secretKey;
  417. } else {
  418. this.tipWords = res.repMsg;
  419. }
  420. // 判断接口请求次数是否失效
  421. if (res.repCode == '6201') {
  422. this.backImgBase = null;
  423. this.blockBackImgBase = null;
  424. }
  425. });
  426. },
  427. },
  428. };
  429. </script>