verify.js 27 KB

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