verify.js 26 KB

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