@@ -42,8 +42,7 @@ export default class Vcode extends React.PureComponent<Props, State> {
4242 constructor ( props : Props ) {
4343 super ( props ) ;
4444 this . state = {
45- id :
46- this . props . id || `${ new Date ( ) . getTime ( ) } _${ Math . random ( ) . toFixed ( 4 ) } ` , // 需要一个唯一的ID,因为vcode要直接操作dom
45+ id : this . props . id || `${ Date . now ( ) } _${ Math . random ( ) . toFixed ( 4 ) } ` , // 需要一个唯一的ID,因为vcode要直接操作dom
4746 width : this . props . width || 150 , // vcode宽度
4847 height : this . props . height || 40 , // vcode高度
4948 len : this . props . length || 4 , // 生成几位code
@@ -67,44 +66,7 @@ export default class Vcode extends React.PureComponent<Props, State> {
6766 options : ( ( ) => {
6867 // 初始化参数
6968 const a : Options = {
70- codes : [
71- "a" ,
72- "b" ,
73- "c" ,
74- "d" ,
75- "e" ,
76- "f" ,
77- "g" ,
78- "h" ,
79- "i" ,
80- "j" ,
81- "k" ,
82- "l" ,
83- "m" ,
84- "o" ,
85- "p" ,
86- "q" ,
87- "r" ,
88- "s" ,
89- "t" ,
90- "x" ,
91- "u" ,
92- "v" ,
93- "y" ,
94- "z" ,
95- "w" ,
96- "n" ,
97- "0" ,
98- "1" ,
99- "2" ,
100- "3" ,
101- "4" ,
102- "5" ,
103- "6" ,
104- "7" ,
105- "8" ,
106- "9" ,
107- ] ,
69+ codes : [ "a" , "b" , "c" , "d" , "e" , "f" , "g" , "h" , "i" , "j" , "k" , "l" , "m" , "o" , "p" , "q" , "r" , "s" , "t" , "x" , "u" , "v" , "y" , "z" , "w" , "n" , "0" , "1" , "2" , "3" , "4" , "5" , "6" , "7" , "8" , "9" ] ,
10870 fontSizeMin : 22 , // 字体尺寸最小值
10971 fontSizeMax : 26 , // 字体尺寸最大值
11072 colors : [
@@ -156,11 +118,7 @@ export default class Vcode extends React.PureComponent<Props, State> {
156118 if ( this . props . value !== prevP . value ) {
157119 this . onDraw ( this . props . value ) ;
158120 }
159- if (
160- this . props . width !== prevP . width ||
161- this . props . height !== prevP . height ||
162- this . props . style !== prevP . style
163- ) {
121+ if ( this . props . width !== prevP . width || this . props . height !== prevP . height || this . props . style !== prevP . style ) {
164122 this . setState ( {
165123 width : this . props . width || 150 ,
166124 height : this . props . height || 40 ,
@@ -189,15 +147,8 @@ export default class Vcode extends React.PureComponent<Props, State> {
189147 */
190148 codeCss ( uW : number , i : number ) : string {
191149 return [
192- `font-size:${ this . randint (
193- this . state . options . fontSizeMin ,
194- this . state . options . fontSizeMax
195- ) } px`,
196- `color:${
197- this . state . options . colors [
198- this . randint ( 0 , this . state . options . colors . length - 1 )
199- ]
200- } `,
150+ `font-size:${ this . randint ( this . state . options . fontSizeMin , this . state . options . fontSizeMax ) } px` ,
151+ `color:${ this . state . options . colors [ this . randint ( 0 , this . state . options . colors . length - 1 ) ] } ` ,
201152 "position: absolute" ,
202153 `left:${ this . randint ( uW * i , uW * i + uW - uW / 2 ) } px` ,
203154 "top:50%" ,
@@ -206,11 +157,7 @@ export default class Vcode extends React.PureComponent<Props, State> {
206157 `-ms-transform:rotate(${ this . randint ( - 15 , 15 ) } deg) translateY(-50%)` ,
207158 `-moz-transform:rotate(${ this . randint ( - 15 , 15 ) } deg) translateY(-50%)` ,
208159 `-webkit-transform:rotate(${ this . randint ( - 15 , 15 ) } deg) translateY(-50%)` ,
209- `font-family:${
210- this . state . options . fonts [
211- this . randint ( 0 , this . state . options . fonts . length - 1 )
212- ]
213- } `,
160+ `font-family:${ this . state . options . fonts [ this . randint ( 0 , this . state . options . fonts . length - 1 ) ] } ` ,
214161 "font-weight:bold" ,
215162 "z-index:2" ,
216163 ] . join ( ";" ) ;
@@ -224,34 +171,17 @@ export default class Vcode extends React.PureComponent<Props, State> {
224171 return [
225172 "position: absolute" ,
226173 `opacity:${ this . randint ( 3 , 8 ) / 10 } ` ,
227- `width:${ this . randint (
228- this . state . options . lineWidthMin ,
229- this . state . options . lineWidthMax
230- ) } px`,
231- `height:${ this . randint (
232- this . state . options . lineHeightMin ,
233- this . state . options . lineHeightMax
234- ) } px`,
235- `background:${
236- this . state . options . lineColors [
237- this . randint ( 0 , this . state . options . lineColors . length - 1 )
238- ]
239- } `,
240- `left:${ this . randint (
241- - this . state . options . lineWidthMin / 2 ,
242- this . state . width
243- ) } px`,
174+ `width:${ this . randint ( this . state . options . lineWidthMin , this . state . options . lineWidthMax ) } px` ,
175+ `height:${ this . randint ( this . state . options . lineHeightMin , this . state . options . lineHeightMax ) } px` ,
176+ `background:${ this . state . options . lineColors [ this . randint ( 0 , this . state . options . lineColors . length - 1 ) ] } ` ,
177+ `left:${ this . randint ( - this . state . options . lineWidthMin / 2 , this . state . width ) } px` ,
244178 `top:${ this . randint ( 0 , this . state . height ) } px` ,
245179 `transform:rotate(${ this . randint ( - 30 , 30 ) } deg)` ,
246180 `-o-transform:rotate(${ this . randint ( - 30 , 30 ) } deg)` ,
247181 `-ms-transform:rotate(${ this . randint ( - 30 , 30 ) } deg)` ,
248182 `-moz-transform:rotate(${ this . randint ( - 30 , 30 ) } deg)` ,
249183 `-webkit-transform:rotate(${ this . randint ( - 30 , 30 ) } deg)` ,
250- `font-family:${
251- this . state . options . fonts [
252- this . randint ( 0 , this . state . options . fonts . length - 1 )
253- ]
254- } `,
184+ `font-family:${ this . state . options . fonts [ this . randint ( 0 , this . state . options . fonts . length - 1 ) ] } ` ,
255185 `font-weight:${ this . randint ( 400 , 900 ) } ` ,
256186 ] . join ( ";" ) ;
257187 }
@@ -264,21 +194,15 @@ export default class Vcode extends React.PureComponent<Props, State> {
264194 let c = "" ; // 存储生成的code
265195 const div = document . getElementById ( this . state . id ) ;
266196
267- const isImg : boolean = / ^ h t t p [ s ] * : \/ \/ | \. j p g $ | \. p n g $ | \. j p e g $ | \. g i f $ | \. b m p $ | \. w e b p $ | ^ d a t a : i m a g e / . test (
268- value || ""
269- ) ; // 是否是图片
197+ const isImg : boolean = / ^ h t t p [ s ] * : \/ \/ | \. j p g $ | \. p n g $ | \. j p e g $ | \. g i f $ | \. b m p $ | \. w e b p $ | ^ d a t a : i m a g e / . test ( value || "" ) ; // 是否是图片
270198 if ( div ) {
271199 div . innerHTML = "" ;
272200 }
273201
274202 if ( isImg ) {
275203 // 用户传递了一张图片
276204 const dom = document . createElement ( "img" ) ;
277- dom . style . cssText = [
278- "display: block" ,
279- "max-width:100%" ,
280- "max-height:100%" ,
281- ] . join ( ";" ) ;
205+ dom . style . cssText = [ "display: block" , "max-width:100%" , "max-height:100%" ] . join ( ";" ) ;
282206 dom . src = value as string ;
283207 div && div . appendChild ( dom ) ;
284208 this . props . onChange && this . props . onChange ( null ) ;
@@ -292,11 +216,7 @@ export default class Vcode extends React.PureComponent<Props, State> {
292216 for ( let i = 0 ; i < length ; i ++ ) {
293217 const dom = document . createElement ( "span" ) ;
294218 dom . style . cssText = this . codeCss ( uW , i ) ;
295- const temp = value
296- ? value [ i ]
297- : this . state . options . codes [
298- Math . round ( Math . random ( ) * ( this . state . options . codes . length - 1 ) )
299- ] ;
219+ const temp = value ? value [ i ] : this . state . options . codes [ Math . round ( Math . random ( ) * ( this . state . options . codes . length - 1 ) ) ] ;
300220 dom . innerHTML = String ( temp ) ;
301221 c = `${ c } ${ temp } ` ;
302222 div && div . appendChild ( dom ) ;
@@ -320,13 +240,6 @@ export default class Vcode extends React.PureComponent<Props, State> {
320240 }
321241
322242 render ( ) {
323- return (
324- < div
325- id = { this . state . id }
326- style = { this . state . style }
327- className = { this . props . className }
328- onClick = { ( ) => this . onClick ( ) }
329- />
330- ) ;
243+ return < div id = { this . state . id } style = { this . state . style } className = { this . props . className } onClick = { ( ) => this . onClick ( ) } /> ;
331244 }
332245}
0 commit comments