UNPKG

tt-mp

Version:

一套组件化、可复用、易扩展的头条小程序 UI 组件库

1 lines 3.42 kB
"use strict";var pluginName=null,EAN13=function(){function t(t,i,e,s){var n;if(this.id=t,this.number=i,this.ctx=s,this.settings={width:200,height:100,number:!0,prefix:!0,color:"black",debug:!1,onValid:function(){},onInvalid:function(){},onSuccess:function(){},onError:function(){}},e)for(n in e)this.settings[n]=e[n];this._name=pluginName,this.init()}return t.prototype.settings={},t.prototype.init=function(){var t,i;return 12===this.number.length&&(t=this.generateCheckDigit(this.number),this.number+=t),13===this.number.length?(this.validate()?this.settings.onValid.call():this.settings.onInvalid.call(),i=this.getCode(),this.draw(i)):this.settings.onError.call()},t.prototype.getCode=function(){for(var t=["0001101","0011001","0010011","0111101","0100011","0110001","0101111","0111011","0110111","0001011"],i=["0100111","0110011","0011011","0100001","0011101","0111001","0000101","0010001","0001001","0010111"],e=["1110010","1100110","1101100","1000010","1011100","1001110","1010000","1000100","1001000","1110100"],s="",n=["xxxxxx","xxyxyy","xxyyxy","xxyyyx","xyxxyy","xyyxxy","xyyyxx","xyxyxy","xyxyyx","xyyxyx"][parseInt(this.number.substr(0,1),10)].split(""),r=this.number.substr(1).split(""),h=0;h<6;)"x"===n[h]?s+=t[r[h]]:s+=i[r[h]],h++;for(h=6;h<12;)s+=e[r[h]],h++;return s},t.prototype.clear=function(t){return t.clearRect(0,0,this.settings.width,this.settings.height)},t.prototype.draw=function(t){var e,s,i,n,r,h,o,f,l,u,x={prefix_offset:.06,font_stretch:.073,border_line_height_number:.9,border_line_height:1,line_height:.9,font_size:.15,font_y:1.03,text_offset:4.5},c=this.settings.prefix?this.settings.width-this.settings.width*x.prefix_offset:this.settings.width,g=this.settings.number?(e=x.border_line_height_number*this.settings.height,x.line_height*e):e=x.border_line_height*this.settings.height,a=c/95;if(this.id){for(s=tt.createCanvasContext(this.id,this.ctx),this.clear(s),s.setFillStyle(this.settings.color),n=this.settings.number&&this.settings.prefix?this.settings.width*x.prefix_offset:0,r=t.split(""),s.fillRect(n,0,a,e),n+=2*a,s.fillRect(n,0,a,e),n+=a,i=0;i<42;)"1"===r[i]&&s.fillRect(n,0,Math.floor(a)+1,g),n+=a,i++;for(n+=a,s.fillRect(n,0,a,e),n+=2*a,s.fillRect(n,0,a,e),n+=2*a,i=42;i<84;)"1"===r[i]&&s.fillRect(n,0,Math.floor(a)+1,g),n+=a,i++;if(s.fillRect(n,0,a,e),n+=2*a,s.fillRect(n,0,a,e),this.settings.number&&(s.setFontSize(x.font_size*g+"px monospace"),o=this.number.substr(0,1),this.settings.prefix&&s.fillText(o,0,e*x.font_y),h=a*x.text_offset+(this.settings.prefix?x.prefix_offset*this.settings.width:0),this.number.substr(1,6).split("").forEach(function(t,i){return s.fillText(t,h,e*x.font_y),h+=x.font_stretch*c}),h=49*a+(this.settings.prefix?x.prefix_offset*this.settings.width:0)+x.text_offset,this.number.substr(7).split("").forEach(function(t,i){return s.fillText(t,h,e*x.font_y),h+=x.font_stretch*c})),this.settings.debug)for(f=l=0,u=2*a;0<u?l<=c:c<=l;f=l+=u)s.beginPath(),s.rect(f,.4*g,a,.1*g),s.setFillStyle("red"),s.fill();return s.draw(),this.settings.onSuccess.call()}return this.settings.onError.call()},t.prototype.generateCheckDigit=function(t){var e=0;return t.split("").forEach(function(t,i){return e+=i%2==0?parseInt(t,10):3*parseInt(t,10)}),10-e%10%10},t.prototype.validate=function(){return parseInt(this.number.slice(-1),10)===this.generateCheckDigit(this.number.slice(0,-1))},t}();"undefined"!=typeof module&&void 0!==module.exports&&(module.exports=EAN13);