VerifySlide.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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: '40px',
  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. // 禁止拖拽
  206. this.$el.onselectstart = function () {
  207. return false;
  208. };
  209. console.log(this.defaultImg);
  210. },
  211. methods: {
  212. init() {
  213. this.text = this.explain;
  214. this.getPictrue();
  215. this.$nextTick(() => {
  216. const setSize = this.resetSize(this); // 重新设置宽度高度
  217. for (const key in setSize) {
  218. this.$set(this.setSize, key, setSize[key]);
  219. }
  220. this.$parent.$emit('ready', this);
  221. });
  222. var _this = this;
  223. window.removeEventListener('touchmove', function (e) {
  224. _this.move(e);
  225. });
  226. window.removeEventListener('mousemove', function (e) {
  227. _this.move(e);
  228. });
  229. // 鼠标松开
  230. window.removeEventListener('touchend', function () {
  231. _this.end();
  232. });
  233. window.removeEventListener('mouseup', function () {
  234. _this.end();
  235. });
  236. window.addEventListener('touchmove', function (e) {
  237. _this.move(e);
  238. });
  239. window.addEventListener('mousemove', function (e) {
  240. _this.move(e);
  241. });
  242. // 鼠标松开
  243. window.addEventListener('touchend', function () {
  244. _this.end();
  245. });
  246. window.addEventListener('mouseup', function () {
  247. _this.end();
  248. });
  249. },
  250. // 鼠标按下
  251. start: function (e) {
  252. e = e || window.event;
  253. if (!e.touches) {
  254. // 兼容PC端
  255. var x = e.clientX;
  256. } else {
  257. // 兼容移动端
  258. var x = e.touches[0].pageX;
  259. }
  260. this.startLeft = Math.floor(
  261. x - this.barArea.getBoundingClientRect().left,
  262. );
  263. this.startMoveTime = +new Date(); // 开始滑动的时间
  264. if (this.isEnd == false) {
  265. this.text = '';
  266. this.moveBlockBackgroundColor = '#337ab7';
  267. this.leftBarBorderColor = '#337AB7';
  268. this.iconColor = '#fff';
  269. e.stopPropagation();
  270. this.status = true;
  271. }
  272. },
  273. // 鼠标移动
  274. move: function (e) {
  275. e = e || window.event;
  276. if (this.status && this.isEnd == false) {
  277. if (!e.touches) {
  278. // 兼容PC端
  279. var x = e.clientX;
  280. } else {
  281. // 兼容移动端
  282. var x = e.touches[0].pageX;
  283. }
  284. var bar_area_left = this.barArea.getBoundingClientRect().left;
  285. var move_block_left = x - bar_area_left; // 小方块相对于父元素的left值
  286. if (
  287. move_block_left >=
  288. this.barArea.offsetWidth -
  289. parseInt(parseInt(this.blockSize.width) / 2) -
  290. 2
  291. ) {
  292. move_block_left =
  293. this.barArea.offsetWidth -
  294. parseInt(parseInt(this.blockSize.width) / 2) -
  295. 2;
  296. }
  297. if (move_block_left <= 0) {
  298. move_block_left = parseInt(parseInt(this.blockSize.width) / 2);
  299. }
  300. // 拖动后小方块的left值
  301. this.moveBlockLeft = move_block_left - this.startLeft + 'px';
  302. this.leftBarWidth = move_block_left - this.startLeft + 'px';
  303. }
  304. },
  305. // 鼠标松开
  306. end: function () {
  307. this.endMovetime = +new Date();
  308. var _this = this;
  309. // 判断是否重合
  310. if (this.status && this.isEnd == false) {
  311. var moveLeftDistance = parseInt(
  312. (this.moveBlockLeft || '').replace('px', ''),
  313. );
  314. moveLeftDistance =
  315. (moveLeftDistance * 310) / parseInt(this.setSize.imgWidth);
  316. const data = {
  317. captchaType: this.captchaType,
  318. pointJson: this.secretKey
  319. ? aesEncrypt(
  320. JSON.stringify({ x: moveLeftDistance, y: 5.0 }),
  321. this.secretKey,
  322. )
  323. : JSON.stringify({ x: moveLeftDistance, y: 5.0 }),
  324. token: this.backToken,
  325. };
  326. this.$axios.$post('activity/captcha/check', data).then((res) => {
  327. if (res.repCode == '0000') {
  328. this.moveBlockBackgroundColor = '#5cb85c';
  329. this.leftBarBorderColor = '#5cb85c';
  330. this.iconColor = '#fff';
  331. this.iconClass = 'icon-check';
  332. this.showRefresh = false;
  333. this.isEnd = true;
  334. if (this.mode == 'pop') {
  335. setTimeout(() => {
  336. this.$parent.clickShow = false;
  337. this.refresh();
  338. }, 1500);
  339. }
  340. this.passFlag = true;
  341. this.tipWords = `${(
  342. (this.endMovetime - this.startMoveTime) /
  343. 1000
  344. ).toFixed(2)}s验证成功`;
  345. var captchaVerification = this.secretKey
  346. ? aesEncrypt(
  347. this.backToken +
  348. '---' +
  349. JSON.stringify({ x: moveLeftDistance, y: 5.0 }),
  350. this.secretKey,
  351. )
  352. : this.backToken +
  353. '---' +
  354. JSON.stringify({ x: moveLeftDistance, y: 5.0 });
  355. setTimeout(() => {
  356. this.tipWords = '';
  357. this.$parent.closeBox();
  358. this.$parent.$emit('success', { captchaVerification });
  359. }, 1000);
  360. } else {
  361. this.moveBlockBackgroundColor = '#d9534f';
  362. this.leftBarBorderColor = '#d9534f';
  363. this.iconColor = '#fff';
  364. this.iconClass = 'icon-close';
  365. this.passFlag = false;
  366. setTimeout(function () {
  367. _this.refresh();
  368. }, 1000);
  369. this.$parent.$emit('error', this);
  370. this.tipWords = '验证失败';
  371. setTimeout(() => {
  372. this.tipWords = '';
  373. }, 1000);
  374. }
  375. });
  376. this.status = false;
  377. }
  378. },
  379. refresh: function () {
  380. this.showRefresh = true;
  381. this.finishText = '';
  382. this.transitionLeft = 'left .3s';
  383. this.moveBlockLeft = 0;
  384. this.leftBarWidth = undefined;
  385. this.transitionWidth = 'width .3s';
  386. this.leftBarBorderColor = '#ddd';
  387. this.moveBlockBackgroundColor = '#fff';
  388. this.iconColor = '#000';
  389. this.iconClass = 'icon-right';
  390. this.isEnd = false;
  391. this.getPictrue();
  392. setTimeout(() => {
  393. this.transitionWidth = '';
  394. this.transitionLeft = '';
  395. this.text = this.explain;
  396. }, 300);
  397. },
  398. // 请求背景图片和验证图片
  399. getPictrue() {
  400. const data = {
  401. captchaType: this.captchaType,
  402. clientUid: localStorage.getItem('slider'),
  403. ts: Date.now(), // 现在的时间戳
  404. };
  405. this.$axios.$post('activity/captcha/get', data).then((res) => {
  406. if (res.repCode == '0000') {
  407. this.backImgBase = res.repData.originalImageBase64;
  408. this.blockBackImgBase = res.repData.jigsawImageBase64;
  409. this.backToken = res.repData.token;
  410. this.secretKey = res.repData.secretKey;
  411. } else {
  412. this.tipWords = res.repMsg;
  413. }
  414. // 判断接口请求次数是否失效
  415. if (res.repCode == '6201') {
  416. this.backImgBase = null;
  417. this.blockBackImgBase = null;
  418. }
  419. });
  420. },
  421. },
  422. };
  423. </script>