verify.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  1. /*! Verify&admin MIT License by anji-plus*/
  2. /*! J2eeFAST 优化兼容IE浏览器*/
  3. ;(function($, window, document,undefined) {
  4. // 初始话 uuid
  5. uuid()
  6. function uuid() {
  7. var s = [];
  8. var hexDigits = "0123456789abcdef";
  9. for (var i = 0; i < 36; i++) {
  10. s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
  11. }
  12. s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
  13. s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
  14. s[8] = s[13] = s[18] = s[23] = "-";
  15. var slider = 'slider'+ '-'+s.join("");
  16. var point = 'point'+ '-'+s.join("");
  17. // 判断下是否存在 slider
  18. console.log(localStorage.getItem('slider'))
  19. if(!localStorage.getItem('slider')) {
  20. localStorage.setItem('slider', slider)
  21. }
  22. if(!localStorage.getItem('point')) {
  23. localStorage.setItem("point",point);
  24. }
  25. }
  26. var startX,startY;
  27. document.addEventListener("touchstart",function(e){
  28. startX = e.targetTouches[0].pageX;
  29. startY = e.targetTouches[0].pageY;
  30. });
  31. document.addEventListener("touchmove",function(e){
  32. var moveX = e.targetTouches[0].pageX;
  33. var moveY = e.targetTouches[0].pageY;
  34. if(Math.abs(moveX-startX)>Math.abs(moveY-startY)){
  35. e.preventDefault();
  36. }
  37. },{passive:false});
  38. //请求图片get事件
  39. function getPictrue(data,baseUrl,resolve,reject){
  40. $.ajax({
  41. type : "post",
  42. contentType: "application/json;charset=UTF-8",
  43. url : baseUrl + "/captcha/get",
  44. data :JSON.stringify(data),
  45. cache: false,
  46. crossDomain: true == !(document.all),
  47. success:function(res){
  48. resolve(res)
  49. },
  50. fail: function(err) {
  51. reject(err)
  52. }
  53. })
  54. }
  55. //验证图片check事件
  56. function checkPictrue(data,baseUrl,resolve,reject){
  57. $.ajax({
  58. type : "post",
  59. contentType: "application/json;charset=UTF-8",
  60. url : baseUrl + "/captcha/check",
  61. data :JSON.stringify(data),
  62. cache: false,
  63. crossDomain: true == !(document.all),
  64. success:function(res){
  65. resolve(res)
  66. },
  67. fail: function(err) {
  68. reject(err)
  69. }
  70. })
  71. }
  72. //定义Slide的构造函数
  73. var Slide = function(ele, opt) {
  74. this.$element = ele,
  75. this.backToken = null,
  76. this.moveLeftDistance = 0,
  77. this.secretKey = '',
  78. this.defaults = {
  79. baseUrl:"https://captcha.anji-plus.com/captcha-api",
  80. containerId:'',
  81. captchaType:"blockPuzzle",
  82. mode : 'fixed', //弹出式pop,固定fixed
  83. vOffset: 5,
  84. vSpace : 5,
  85. explain : '向右滑动完成验证',
  86. imgSize : {
  87. width: '310px',
  88. height: '155px',
  89. },
  90. blockSize : {
  91. width: '50px',
  92. height: '50px',
  93. },
  94. circleRadius: '10px',
  95. barSize : {
  96. width : '310px',
  97. height : '50px',
  98. },
  99. beforeCheck:function(){ return true},
  100. ready : function(){},
  101. success : function(){},
  102. error : function(){}
  103. },
  104. this.options = $.extend({}, this.defaults, opt)
  105. };
  106. //定义Slide的方法
  107. Slide.prototype = {
  108. init: function() {
  109. var _this = this;
  110. //加载页面
  111. this.loadDom();
  112. _this.refresh();
  113. this.options.ready();
  114. this.$element[0].onselectstart = document.body.ondrag = function(){
  115. return false;
  116. };
  117. if(this.options.mode == 'pop') {
  118. _this.$element.find('.verifybox-close').on('click', function() {
  119. _this.$element.find(".mask").css("display","none");
  120. _this.refresh();
  121. });
  122. console.log(this.options.containerId)
  123. var clickBtn = document.getElementById(this.options.containerId);
  124. console.log(clickBtn)
  125. clickBtn && (clickBtn.onclick = function(){
  126. if (_this.options.beforeCheck()) {
  127. _this.$element.find(".mask").css("display","block");
  128. }
  129. })
  130. }
  131. //按下
  132. this.htmlDoms.move_block.on('touchstart', function(e) {
  133. _this.start(e);
  134. });
  135. this.htmlDoms.move_block.on('mousedown', function(e) {
  136. _this.start(e);
  137. });
  138. this.htmlDoms.sub_block.on('mousedown', function(e) {
  139. e.stopPropagation()
  140. });
  141. //拖动
  142. window.addEventListener("touchmove", function(e) {
  143. _this.move(e);
  144. });
  145. window.addEventListener("mousemove", function(e) {
  146. _this.move(e);
  147. });
  148. //鼠标松开
  149. window.addEventListener("touchend", function() {
  150. _this.end();
  151. });
  152. window.addEventListener("mouseup", function() {
  153. _this.end();
  154. });
  155. //刷新
  156. _this.$element.find('.verify-refresh').on('click', function() {
  157. _this.refresh();
  158. });
  159. },
  160. //初始化加载
  161. loadDom : function() {
  162. this.status = false; //鼠标状态
  163. this.isEnd = false; //是够验证完成
  164. this.setSize = this.resetSize(this); //重新设置宽度高度
  165. this.plusWidth = 0;
  166. this.plusHeight = 0;
  167. this.x = 0;
  168. this.y = 0;
  169. var panelHtml = '';
  170. var wrapHtml = '';
  171. this.lengthPercent = (parseInt(this.setSize.img_width)-parseInt(this.setSize.block_width)- parseInt(this.setSize.circle_radius) - parseInt(this.setSize.circle_radius) * 0.8)/(parseInt(this.setSize.img_width)-parseInt(this.setSize.bar_height));
  172. if(!/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)){
  173. this.setSize.img_width = '220px'
  174. this.setSize.img_height = '100px'
  175. this.setSize.bar_height = '38px'
  176. }
  177. wrapStartHtml = '<div class="mask">'+
  178. '<div class="verifybox" style="width:'+(parseInt(this.setSize.img_width)+30)+'px">'+
  179. '<div class="verifybox-top">'+
  180. '请完成安全验证'+
  181. '<span class="verifybox-close">'+
  182. '<i class="iconfont icon-close"></i>'+
  183. '</span>'+
  184. '</div>'+
  185. '<div class="verifybox-bottom" style="padding:15px">'+
  186. '<div style="position: relative;">';
  187. if (this.options.mode == 'pop') {
  188. panelHtml = wrapStartHtml
  189. }
  190. panelHtml += '<div class="verify-img-out">'+
  191. '<div class="verify-img-panel">'+
  192. '<div class="verify-refresh" style="z-index:3">'+
  193. '<i class="iconfont icon-refresh"></i>'+
  194. '</div>'+
  195. '<span class="verify-tips" class="suc-bg"></span>'+
  196. '<img src="" class="backImg" style="width:100%;height:100%;display:block">'+
  197. '</div>'+
  198. '</div>';
  199. this.plusWidth = parseInt(this.setSize.block_width) + parseInt(this.setSize.circle_radius) * 2 - parseInt(this.setSize.circle_radius) * 0.2;
  200. this.plusHeight = parseInt(this.setSize.block_height) + parseInt(this.setSize.circle_radius) * 2 - parseInt(this.setSize.circle_radius) * 0.2;
  201. panelHtml +='<div class="verify-bar-area" style="width:'+this.setSize.img_width+',height:'+this.setSize.bar_height+',line-height:'+this.setSize.bar_height+'">'+
  202. '<span class="verify-msg">'+this.options.explain+'</span>'+
  203. '<div class="verify-left-bar">'+
  204. '<span class="verify-msg"></span>'+
  205. '<div class="verify-move-block">'+
  206. '<i class="verify-icon iconfont icon-right"></i>'+
  207. '<div class="verify-sub-block">'+
  208. '<img src="" class="bock-backImg" alt="" style="width:100%;height:100%;display:block">'+
  209. '</div>'+
  210. '</div>'+
  211. '</div>'+
  212. '</div>';
  213. wrapEndHtml = '</div></div></div></div>';
  214. if (this.options.mode == 'pop') {
  215. panelHtml += wrapEndHtml
  216. }
  217. this.$element.append(panelHtml);
  218. this.htmlDoms = {
  219. tips: this.$element.find('.verify-tips'),
  220. sub_block : this.$element.find('.verify-sub-block'),
  221. out_panel : this.$element.find('.verify-img-out'),
  222. img_panel : this.$element.find('.verify-img-panel'),
  223. img_canvas : this.$element.find('.verify-img-canvas'),
  224. bar_area : this.$element.find('.verify-bar-area'),
  225. move_block : this.$element.find('.verify-move-block'),
  226. left_bar : this.$element.find('.verify-left-bar'),
  227. msg : this.$element.find('.verify-msg'),
  228. icon : this.$element.find('.verify-icon'),
  229. refresh :this.$element.find('.verify-refresh')
  230. };
  231. this.$element.css('position', 'relative');
  232. this.htmlDoms.sub_block.css({'height':this.setSize.img_height,'width':Math.floor(parseInt(this.setSize.img_width)*47/310)+ 'px',
  233. 'top':-(parseInt(this.setSize.img_height) + this.options.vSpace) + 'px'})
  234. this.htmlDoms.out_panel.css('height', parseInt(this.setSize.img_height) + this.options.vSpace + 'px');
  235. this.htmlDoms.img_panel.css({'width': this.setSize.img_width, 'height': this.setSize.img_height});
  236. this.htmlDoms.bar_area.css({'width': this.setSize.img_width, 'height': this.setSize.bar_height, 'line-height':this.setSize.bar_height});
  237. this.htmlDoms.move_block.css({'width': this.setSize.bar_height, 'height': this.setSize.bar_height});
  238. this.htmlDoms.left_bar.css({'width': this.setSize.bar_height, 'height': this.setSize.bar_height});
  239. },
  240. //鼠标按下
  241. start: function(e) {
  242. if(!e.originalEvent.targetTouches) { //兼容移动端
  243. var x = e.clientX;
  244. }else { //兼容PC端
  245. var x = e.originalEvent.targetTouches[0].pageX;
  246. }
  247. // if(!e.touches) { //兼容移动端
  248. // var x = e.clientX;
  249. // }else { //兼容PC端
  250. // var x = e.touches[0].pageX;
  251. // }
  252. this.startLeft = Math.floor(x - this.htmlDoms.bar_area[0].getBoundingClientRect().left);
  253. this.startMoveTime = new Date().getTime();
  254. if(this.isEnd == false) {
  255. this.htmlDoms.msg.text('');
  256. this.htmlDoms.move_block.css('background-color', '#337ab7');
  257. this.htmlDoms.left_bar.css('border-color', '#337AB7');
  258. this.htmlDoms.icon.css('color', '#fff');
  259. e.stopPropagation();
  260. this.status = true;
  261. }
  262. },
  263. //鼠标移动
  264. move: function(e) {
  265. if(this.status && this.isEnd == false) {
  266. if(!e.touches) { //兼容移动端
  267. var x = e.clientX;
  268. }else { //兼容PC端
  269. var x = e.touches[0].pageX;
  270. }
  271. var bar_area_left = this.htmlDoms.bar_area[0].getBoundingClientRect().left;
  272. var move_block_left = x - bar_area_left; //小方块相对于父元素的left值
  273. if(move_block_left >= (this.htmlDoms.bar_area[0].offsetWidth - parseInt(this.setSize.bar_height) + parseInt(parseInt(this.setSize.block_width)/2) - 2) ) {
  274. move_block_left = (this.htmlDoms.bar_area[0].offsetWidth - parseInt(this.setSize.bar_height) + parseInt(parseInt(this.setSize.block_width)/2)- 2);
  275. }
  276. if(move_block_left <= parseInt(parseInt(this.setSize.block_width)/2)) {
  277. move_block_left = parseInt(parseInt(this.setSize.block_width)/2);
  278. }
  279. //拖动后小方块的left值
  280. this.htmlDoms.move_block.css('left', move_block_left-this.startLeft + "px");
  281. this.htmlDoms.left_bar.css('width', move_block_left-this.startLeft + "px");
  282. this.htmlDoms.sub_block.css('left', "0px");
  283. this.moveLeftDistance = move_block_left - this.startLeft
  284. }
  285. },
  286. //鼠标松开
  287. end: function() {
  288. this.endMovetime = new Date().getTime();
  289. var _this = this;
  290. //判断是否重合
  291. if(this.status && this.isEnd == false) {
  292. var vOffset = parseInt(this.options.vOffset);
  293. this.moveLeftDistance = this.moveLeftDistance * 310/ parseInt(this.setSize.img_width)
  294. //图片滑动
  295. var data = {
  296. captchaType:this.options.captchaType,
  297. "pointJson": this.secretKey ? aesEncrypt(JSON.stringify({x:this.moveLeftDistance,y:5.0}),this.secretKey):JSON.stringify({x:this.moveLeftDistance,y:5.0}),
  298. "token":this.backToken,
  299. clientUid: localStorage.getItem('slider'),
  300. ts: Date.now()
  301. }
  302. var captchaVerification = this.secretKey ? aesEncrypt(this.backToken+'---'+JSON.stringify({x:this.moveLeftDistance,y:5.0}),this.secretKey):this.backToken+'---'+JSON.stringify({x:this.moveLeftDistance,y:5.0})
  303. checkPictrue(data,this.options.baseUrl,function(res){
  304. // 请求反正成功的判断
  305. if (res.repCode=="0000") {
  306. _this.htmlDoms.move_block.css('background-color', '#5cb85c');
  307. _this.htmlDoms.left_bar.css({'border-color': '#5cb85c', 'background-color': '#fff'});
  308. _this.htmlDoms.icon.css('color', '#fff');
  309. _this.htmlDoms.icon.removeClass('icon-right');
  310. _this.htmlDoms.icon.addClass('icon-check');
  311. //提示框
  312. _this.htmlDoms.tips.addClass('suc-bg').removeClass('err-bg')
  313. // _this.htmlDoms.tips.css({"display":"block",animation:"move 1s cubic-bezier(0, 0, 0.39, 1.01)"});
  314. _this.htmlDoms.tips.animate({"bottom":"0px"});
  315. _this.htmlDoms.tips.text(((_this.endMovetime-_this.startMoveTime)/1000).toFixed(2) + 's验证成功');
  316. _this.isEnd = true;
  317. setTimeout(function(){
  318. _this.$element.find(".mask").css("display","none");
  319. // _this.htmlDoms.tips.css({"display":"none",animation:"none"});
  320. _this.htmlDoms.tips.animate({"bottom":"-35px"});
  321. _this.refresh();
  322. },1000)
  323. _this.options.success({'captchaVerification':captchaVerification});
  324. }else{
  325. _this.htmlDoms.move_block.css('background-color', '#d9534f');
  326. _this.htmlDoms.left_bar.css('border-color', '#d9534f');
  327. _this.htmlDoms.icon.css('color', '#fff');
  328. _this.htmlDoms.icon.removeClass('icon-right');
  329. _this.htmlDoms.icon.addClass('icon-close');
  330. _this.htmlDoms.tips.addClass('err-bg').removeClass('suc-bg')
  331. // _this.htmlDoms.tips.css({"display":"block",animation:"move 1.3s cubic-bezier(0, 0, 0.39, 1.01)"});
  332. _this.htmlDoms.tips.animate({"bottom":"0px"});
  333. _this.htmlDoms.tips.text(res.repMsg)
  334. setTimeout(function () {
  335. _this.refresh();
  336. _this.htmlDoms.tips.animate({"bottom":"-35px"});
  337. }, 1000);
  338. // setTimeout(function () {
  339. // // _this.htmlDoms.tips.css({"display":"none",animation:"none"});
  340. // },1300)
  341. _this.options.error(this);
  342. }
  343. })
  344. this.status = false;
  345. }
  346. },
  347. resetSize : function(obj) {
  348. var img_width,img_height,bar_width,bar_height,block_width,block_height,circle_radius; //图片的宽度、高度,移动条的宽度、高度
  349. var parentWidth = obj.$element.parent().width() || $(window).width();
  350. var parentHeight = obj.$element.parent().height() || $(window).height();
  351. if(obj.options.imgSize.width.indexOf('%')!= -1){
  352. img_width = parseInt(obj.options.imgSize.width)/100 * parentWidth + 'px';
  353.   }else {
  354. img_width = obj.options.imgSize.width;
  355. }
  356. if(obj.options.imgSize.height.indexOf('%')!= -1){
  357. img_height = parseInt(obj.options.imgSize.height)/100 * parentHeight + 'px';
  358.   }else {
  359. img_height = obj.options.imgSize.height;
  360. }
  361. if(obj.options.barSize.width.indexOf('%')!= -1){
  362. bar_width = parseInt(obj.options.barSize.width)/100 * parentWidth + 'px';
  363.   }else {
  364. bar_width = obj.options.barSize.width;
  365. }
  366. if(obj.options.barSize.height.indexOf('%')!= -1){
  367. bar_height = parseInt(obj.options.barSize.height)/100 * parentHeight + 'px';
  368.   }else {
  369. bar_height = obj.options.barSize.height;
  370. }
  371. if(obj.options.blockSize) {
  372. if(obj.options.blockSize.width.indexOf('%')!= -1){
  373. block_width = parseInt(obj.options.blockSize.width)/100 * parentWidth + 'px';
  374.   }else {
  375. block_width = obj.options.blockSize.width;
  376. }
  377. if(obj.options.blockSize.height.indexOf('%')!= -1){
  378. block_height = parseInt(obj.options.blockSize.height)/100 * parentHeight + 'px';
  379.   }else {
  380. block_height = obj.options.blockSize.height;
  381. }
  382. }
  383. if(obj.options.circleRadius) {
  384. if(obj.options.circleRadius.indexOf('%')!= -1){
  385. circle_radius = parseInt(obj.options.circleRadius)/100 * parentHeight + 'px';
  386.   }else {
  387. circle_radius = obj.options.circleRadius;
  388. }
  389. }
  390. return {img_width : img_width, img_height : img_height, bar_width : bar_width, bar_height : bar_height, block_width : block_width, block_height : block_height, circle_radius : circle_radius};
  391. },
  392. //刷新
  393. refresh: function() {
  394. var _this = this;
  395. this.htmlDoms.refresh.show();
  396. this.$element.find('.verify-msg:eq(1)').text('');
  397. this.$element.find('.verify-msg:eq(1)').css('color', '#000');
  398. this.htmlDoms.move_block.animate({'left':'0px'}, 'fast');
  399. this.htmlDoms.left_bar.animate({'width': parseInt(this.setSize.bar_height)}, 'fast');
  400. this.htmlDoms.left_bar.css({'border-color': '#ddd'});
  401. this.htmlDoms.move_block.css('background-color', '#fff');
  402. this.htmlDoms.icon.css('color', '#000');
  403. this.htmlDoms.icon.removeClass('icon-close');
  404. this.htmlDoms.icon.addClass('icon-right');
  405. this.$element.find('.verify-msg:eq(0)').text(this.options.explain);
  406. this.isEnd = false;
  407. getPictrue({captchaType:"blockPuzzle", clientUid: localStorage.getItem('slider'), ts: Date.now()},this.options.baseUrl,function (res) {
  408. if (res.repCode=="0000") {
  409. _this.$element.find(".backImg")[0].src = 'data:image/png;base64,'+res.repData.originalImageBase64
  410. _this.$element.find(".bock-backImg")[0].src = 'data:image/png;base64,'+res.repData.jigsawImageBase64
  411. _this.secretKey = res.repData.secretKey
  412. _this.backToken = res.repData.token
  413. } else {
  414. _this.$element.find(".backImg")[0].src = 'img/default.png'
  415. _this.$element.find(".bock-backImg")[0].src = 'img/itemLeft.png'
  416. _this.htmlDoms.tips.addClass('err-bg').removeClass('suc-bg')
  417. _this.htmlDoms.tips.animate({"bottom":"0px"});
  418. _this.htmlDoms.tips.text(res.repMsg)
  419. setTimeout(function () {
  420. _this.htmlDoms.tips.animate({"bottom":"-35px"});
  421. }, 1000);
  422. }
  423. });
  424. this.htmlDoms.sub_block.css('left', "0px");
  425. },
  426. };
  427. //定义Points的构造函数
  428. var Points = function(ele, opt) {
  429. this.$element = ele,
  430. this.backToken = null,
  431. this.secretKey = '',
  432. this.defaults = {
  433. baseUrl:"https://captcha.anji-plus.com/captcha-api",
  434. captchaType:"clickWord",
  435. containerId:'',
  436. mode : 'fixed', //弹出式pop,固定fixed
  437. checkNum : 3, //校对的文字数量
  438. vSpace : 5, //间隔
  439. imgSize : {
  440. width: '310px',
  441. height: '155px',
  442. },
  443. barSize : {
  444. width : '310px',
  445. height : '50px',
  446. },
  447. beforeCheck: function(){ return true},
  448. ready : function(){},
  449. success : function(){},
  450. error : function(){}
  451. },
  452. this.options = $.extend({}, this.defaults, opt)
  453. };
  454. //定义Points的方法
  455. Points.prototype = {
  456. init : function() {
  457. var _this = this;
  458. //加载页面
  459. _this.loadDom();
  460. _this.refresh();
  461. _this.options.ready();
  462. this.$element[0].onselectstart = document.body.ondrag = function(){
  463. return false;
  464. };
  465. if(this.options.mode == 'pop') {
  466. _this.$element.find('.verifybox-close').on('click', function() {
  467. _this.$element.find(".mask").css("display","none");
  468. });
  469. var clickBtn = document.getElementById(this.options.containerId);
  470. clickBtn && (clickBtn.onclick = function(){
  471. if (_this.options.beforeCheck()) {
  472. _this.$element.find(".mask").css("display","block");
  473. }
  474. })
  475. }
  476. // 注册点击验证事件
  477. _this.$element.find('.back-img').on('click', function(e) {
  478. _this.checkPosArr.push(_this.getMousePos(this, e));
  479. if(_this.num == _this.options.checkNum) {
  480. _this.num = _this.createPoint(_this.getMousePos(this, e));
  481. //按比例转换坐标值
  482. _this.checkPosArr = _this.pointTransfrom(_this.checkPosArr,_this.setSize);
  483. setTimeout(function(){
  484. var data = {
  485. captchaType:_this.options.captchaType,
  486. "pointJson":_this.secretKey ? aesEncrypt(JSON.stringify(_this.checkPosArr),_this.secretKey):JSON.stringify(_this.checkPosArr),
  487. "token":_this.backToken,
  488. clientUid: localStorage.getItem('point'),
  489. ts: Date.now()
  490. }
  491. var captchaVerification = _this.secretKey ? aesEncrypt(_this.backToken+'---'+JSON.stringify(_this.checkPosArr),_this.secretKey):_this.backToken+'---'+JSON.stringify(_this.checkPosArr)
  492. checkPictrue(data, _this.options.baseUrl,function(res){
  493. if (res.repCode=="0000") {
  494. _this.$element.find('.verify-bar-area').css({'color': '#4cae4c', 'border-color': '#5cb85c'});
  495. _this.$element.find('.verify-msg').text('验证成功');
  496. // _this.$element.find('.verify-refresh').hide();
  497. _this.$element.find('.verify-img-panel').unbind('click');
  498. setTimeout(function(){
  499. _this.$element.find(".mask").css("display","none");
  500. _this.refresh();
  501. },1000)
  502. _this.options.success({'captchaVerification':captchaVerification});
  503. }else{
  504. _this.options.error(_this);
  505. _this.$element.find('.verify-bar-area').css({'color': '#d9534f', 'border-color': '#d9534f'});
  506. _this.$element.find('.verify-msg').text('验证失败');
  507. setTimeout(function () {
  508. _this.$element.find('.verify-bar-area').css({'color': '#000','border-color': '#ddd'});
  509. _this.refresh();
  510. }, 400);
  511. }
  512. })
  513. }, 400);
  514. }
  515. if(_this.num < _this.options.checkNum) {
  516. _this.num = _this.createPoint(_this.getMousePos(this, e));
  517. }
  518. });
  519. //刷新
  520. _this.$element.find('.verify-refresh').on('click', function() {
  521. _this.refresh();
  522. });
  523. },
  524. //加载页面
  525. loadDom : function() {
  526. this.fontPos = []; //选中的坐标信息
  527. this.checkPosArr = []; //用户点击的坐标
  528. this.num = 1; //点击的记数
  529. var panelHtml = '';
  530. var wrapStartHtml = '';
  531. this.setSize = Slide.prototype.resetSize(this); //重新设置宽度高度
  532. wrapStartHtml = '<div class="mask">'+
  533. '<div class="verifybox" style="width:'+(parseInt(this.setSize.img_width)+30)+'px">'+
  534. '<div class="verifybox-top">'+
  535. '请完成安全验证'+
  536. '<span class="verifybox-close">'+
  537. '<i class="iconfont icon-close"></i>'+
  538. '</span>'+
  539. '</div>'+
  540. '<div class="verifybox-bottom" style="padding:15px">'+
  541. '<div style="position: relative;">';
  542. if (this.options.mode == 'pop') {
  543. panelHtml = wrapStartHtml
  544. }
  545. panelHtml += '<div class="verify-img-out">'+
  546. '<div class="verify-img-panel">'+
  547. '<div class="verify-refresh" style="z-index:3">'+
  548. '<i class="iconfont icon-refresh"></i>'+
  549. '</div>'+
  550. '<img src="" class="back-img" width="'+this.setSize.img_width+'" height="'+this.setSize.img_height+'">'+
  551. '</div>'+
  552. '</div>'+
  553. '<div class="verify-bar-area" style="width:'+this.setSize.img_width+',height:'+this.setSize.bar_height+',line-height:'+this.setSize.bar_height+'">'+
  554. '<span class="verify-msg"></span>'+
  555. '</div>';
  556. wrapEndHtml = '</div></div></div></div>';
  557. if (this.options.mode == 'pop') {
  558. panelHtml += wrapEndHtml
  559. }
  560. this.$element.append(panelHtml);
  561. this.htmlDoms = {
  562. back_img : this.$element.find('.back-img'),
  563. out_panel : this.$element.find('.verify-img-out'),
  564. img_panel : this.$element.find('.verify-img-panel'),
  565. bar_area : this.$element.find('.verify-bar-area'),
  566. msg : this.$element.find('.verify-msg'),
  567. };
  568. this.$element.css('position', 'relative');
  569. this.htmlDoms.out_panel.css('height', parseInt(this.setSize.img_height) + this.options.vSpace + 'px');
  570. this.htmlDoms.img_panel.css({'width': this.setSize.img_width, 'height': this.setSize.img_height, 'background-size' : this.setSize.img_width + ' '+ this.setSize.img_height, 'margin-bottom': this.options.vSpace + 'px'});
  571. this.htmlDoms.bar_area.css({'width': this.setSize.img_width, 'height': this.setSize.bar_height, 'line-height':this.setSize.bar_height});
  572. },
  573. //获取坐标
  574. getMousePos :function(obj, event) {
  575. var e = event || window.event;
  576. var scrollX = document.documentElement.scrollLeft || document.body.scrollLeft;
  577. var scrollY = document.documentElement.scrollTop || document.body.scrollTop;
  578. var x = e.clientX - ($(obj).offset().left - $(window).scrollLeft());
  579. var y = e.clientY - ($(obj).offset().top - $(window).scrollTop());
  580. return {'x': x, 'y': y};
  581. },
  582. //创建坐标点
  583. createPoint : function (pos) {
  584. this.htmlDoms.img_panel.append('<div class="point-area" style="background-color:#1abd6c;color:#fff;z-index:9999;width:20px;height:20px;text-align:center;line-height:20px;border-radius: 50%;position:absolute;'
  585. +'top:'+parseInt(pos.y-10)+'px;left:'+parseInt(pos.x-10)+'px;">'+this.num+'</div>');
  586. return ++this.num;
  587. },
  588. //刷新
  589. refresh: function() {
  590. var _this = this;
  591. this.$element.find('.point-area').remove();
  592. this.fontPos = [];
  593. this.checkPosArr = [];
  594. this.num = 1;
  595. getPictrue({captchaType:"clickWord", clientUid: localStorage.getItem('point'), ts: Date.now()},_this.options.baseUrl,function(res){
  596. if (res.repCode=="0000") {
  597. _this.htmlDoms.back_img[0].src ='data:image/png;base64,'+ res.repData.originalImageBase64;
  598. _this.backToken = res.repData.token;
  599. _this.secretKey = res.repData.secretKey;
  600. var text = '请依次点击【' + res.repData.wordList.join(",") + '】';
  601. _this.$element.find('.verify-msg').text(text);
  602. } else {
  603. _this.htmlDoms.back_img[0].src = 'images/default.png';
  604. _this.$element.find('.verify-msg').text(res.repMsg);
  605. }
  606. })
  607. },
  608. pointTransfrom:function(pointArr,imgSize){
  609. var newPointArr = pointArr.map(function(p){
  610. var x = Math.round(310 * p.x/parseInt(imgSize.img_width))
  611. var y =Math.round(155 * p.y/parseInt(imgSize.img_height))
  612. return {'x':x,'y':y}
  613. })
  614. return newPointArr
  615. }
  616. };
  617. //在插件中使用slideVerify对象 初始化与是否弹出无关 ,不应该耦合
  618. $.fn.slideVerify = function(options, callbacks) {
  619. var slide = new Slide(this, options);
  620. if (slide.options.mode=="pop") {
  621. slide.init();
  622. }else if (slide.options.mode=="fixed") {
  623. slide.init();
  624. }
  625. };
  626. //在插件中使用clickVerify对象
  627. $.fn.pointsVerify = function(options, callbacks) {
  628. var points = new Points(this, options);
  629. if (points.options.mode=="pop") {
  630. points.init();
  631. }else if (points.options.mode=="fixed") {
  632. points.init();
  633. }
  634. };
  635. })(jQuery, window, document);