VerifySlide.vue 12 KB

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