verify.js 26 KB

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