clicaptcha.js 5.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /**
  2. * js-cookie v2.1.0
  3. * https://github.com/js-cookie/js-cookie
  4. */
  5. (function(factory){if(typeof define==='function'&&define.amd){define(factory)}else if(typeof exports==='object'){module.exports=factory()}else{var _OldCookies=window.Cookies;var api=window.Cookies=factory();api.noConflict=function(){window.Cookies=_OldCookies;return api}}}(function(){function extend(){var i=0;var result={};for(;i<arguments.length;i++){var attributes=arguments[i];for(var key in attributes){result[key]=attributes[key]}}return result}function init(converter){function api(key,value,attributes){var result;if(arguments.length>1){attributes=extend({path:'/'},api.defaults,attributes);if(typeof attributes.expires==='number'){var expires=new Date();expires.setMilliseconds(expires.getMilliseconds()+attributes.expires*864e+5);attributes.expires=expires}try{result=JSON.stringify(value);if(/^[\{\[]/.test(result)){value=result}}catch(e){}if(!converter.write){value=encodeURIComponent(String(value)).replace(/%(23|24|26|2B|3A|3C|3E|3D|2F|3F|40|5B|5D|5E|60|7B|7D|7C)/g,decodeURIComponent)}else{value=converter.write(value,key)}key=encodeURIComponent(String(key));key=key.replace(/%(23|24|26|2B|5E|60|7C)/g,decodeURIComponent);key=key.replace(/[\(\)]/g,escape);return(document.cookie=[key,'=',value,attributes.expires&&'; expires='+attributes.expires.toUTCString(),attributes.path&&'; path='+attributes.path,attributes.domain&&'; domain='+attributes.domain,attributes.secure?'; secure':''].join(''))}if(!key){result={}}var cookies=document.cookie?document.cookie.split('; '):[];var rdecode=/(%[0-9A-Z]{2})+/g;var i=0;for(;i<cookies.length;i++){var parts=cookies[i].split('=');var name=parts[0].replace(rdecode,decodeURIComponent);var cookie=parts.slice(1).join('=');if(cookie.charAt(0)==='"'){cookie=cookie.slice(1,-1)}try{cookie=converter.read?converter.read(cookie,name):converter(cookie,name)||cookie.replace(rdecode,decodeURIComponent);if(this.json){try{cookie=JSON.parse(cookie)}catch(e){}}if(key===name){result=cookie;break}if(!key){result[name]=cookie}}catch(e){}}return result}api.get=api.set=api;api.getJSON=function(){return api.apply({json:true},[].slice.call(arguments))};api.defaults={};api.remove=function(key,attributes){api(key,'',extend(attributes,{expires:-1}))};api.withConverter=init;return api}return init(function(){})}));
  6. (function($){
  7. $.fn.extend({
  8. 'clicaptcha': function(options){
  9. var opts = $.extend({}, defaluts, options); //使用jQuery.extend覆盖插件默认参数
  10. var $this = this;
  11. if(!$('#clicaptcha-box').length){
  12. $('body').append('<div id="clicaptcha-box">'+
  13. '<img class="clicaptcha-img" src="" alt="易网验证码加载失败,请点击刷新按钮">'+
  14. '<div class="clicaptcha-title"></div>'+
  15. '<div class="clicaptcha-refresh-box">'+
  16. '<div class="clicaptcha-refresh-line clicaptcha-refresh-line-left"></div>'+
  17. '<a href="javascript:;" class="clicaptcha-refresh-btn" title="刷新"></a>'+
  18. '<div class="clicaptcha-refresh-line clicaptcha-refresh-line-right"></div>'+
  19. '</div>'+
  20. '</div>');
  21. $('body').append('<div id="clicaptcha-mask"></div>');
  22. $('#clicaptcha-mask').click(function(){
  23. $('#clicaptcha-box').hide();
  24. $(this).hide();
  25. });
  26. $('#clicaptcha-box .clicaptcha-refresh-btn').click(function(){
  27. $this.clicaptcha(opts);
  28. });
  29. }
  30. $('#clicaptcha-box, #clicaptcha-mask').show();
  31. $('#clicaptcha-box .clicaptcha-img').attr('src', opts.src + '?' + new Date().getTime()).load(function(){
  32. var thisObj = $(this);
  33. var text = Cookies.get('clicaptcha_text').split(',');
  34. var title = '请依次点击';
  35. var t = [];
  36. for(var i = 0; i < text.length; i++){
  37. t.push('“<span>'+text[i]+'</span>”');
  38. }
  39. title += t.join('、');
  40. $('#clicaptcha-box .clicaptcha-title').html(title);
  41. var xyArr = [];
  42. thisObj.off('mousedown').on('mousedown', function(e){
  43. e.preventDefault();
  44. thisObj.off('mouseup').on('mouseup', function(e){
  45. $('#clicaptcha-box .clicaptcha-title span:eq('+xyArr.length+')').addClass('clicaptcha-clicked');
  46. xyArr.push(($(document).scrollLeft() + e.clientX - $(this).offset().left) + ',' + ($(document).scrollTop() + e.clientY - $(this).offset().top));
  47. if(xyArr.length == text.length){
  48. var captchainfo = [xyArr.join('-'), thisObj.width(), thisObj.height()].join(';');
  49. $.ajax({
  50. type: 'POST',
  51. url: opts.src,
  52. data: {
  53. do : 'check',
  54. info : captchainfo
  55. }
  56. }).done(function(cb){
  57. if(cb == 1){
  58. $this.val(captchainfo).data('ischeck', true);
  59. $('#clicaptcha-box .clicaptcha-title').html(opts.success_tip);
  60. setTimeout(function(){
  61. $('#clicaptcha-box, #clicaptcha-mask').hide();
  62. opts.callback();
  63. }, 1500);
  64. }else{
  65. $('#clicaptcha-box .clicaptcha-title').html(opts.error_tip);
  66. setTimeout(function(){
  67. $this.clicaptcha(opts);
  68. }, 1500);
  69. }
  70. });
  71. }
  72. });
  73. });
  74. });
  75. return this;
  76. },
  77. 'clicaptchaCheck': function(){
  78. var ischeck = false;
  79. if(this.data('ischeck') == true){
  80. ischeck = true;
  81. }
  82. return ischeck;
  83. },
  84. 'clicaptchaReset': function(){
  85. this.val('').removeData('ischeck');
  86. return this;
  87. }
  88. });
  89. //默认参数
  90. var defaluts = {
  91. src: 'clicaptcha/clicaptcha.php',
  92. success_tip: '验证成功!',
  93. error_tip: '未点中正确区域,请重试!',
  94. callback: function(){}
  95. };
  96. })(window.jQuery);