verify.js 26 KB

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