@@ -42,8 +42,7 @@ export default class Vcode extends React.PureComponent<Props, State> {
42
42
constructor ( props : Props ) {
43
43
super ( props ) ;
44
44
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
47
46
width : this . props . width || 150 , // vcode宽度
48
47
height : this . props . height || 40 , // vcode高度
49
48
len : this . props . length || 4 , // 生成几位code
@@ -67,44 +66,7 @@ export default class Vcode extends React.PureComponent<Props, State> {
67
66
options : ( ( ) => {
68
67
// 初始化参数
69
68
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" ] ,
108
70
fontSizeMin : 22 , // 字体尺寸最小值
109
71
fontSizeMax : 26 , // 字体尺寸最大值
110
72
colors : [
@@ -156,11 +118,7 @@ export default class Vcode extends React.PureComponent<Props, State> {
156
118
if ( this . props . value !== prevP . value ) {
157
119
this . onDraw ( this . props . value ) ;
158
120
}
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 ) {
164
122
this . setState ( {
165
123
width : this . props . width || 150 ,
166
124
height : this . props . height || 40 ,
@@ -189,15 +147,8 @@ export default class Vcode extends React.PureComponent<Props, State> {
189
147
*/
190
148
codeCss ( uW : number , i : number ) : string {
191
149
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 ) ] } ` ,
201
152
"position: absolute" ,
202
153
`left:${ this . randint ( uW * i , uW * i + uW - uW / 2 ) } px` ,
203
154
"top:50%" ,
@@ -206,11 +157,7 @@ export default class Vcode extends React.PureComponent<Props, State> {
206
157
`-ms-transform:rotate(${ this . randint ( - 15 , 15 ) } deg) translateY(-50%)` ,
207
158
`-moz-transform:rotate(${ this . randint ( - 15 , 15 ) } deg) translateY(-50%)` ,
208
159
`-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 ) ] } ` ,
214
161
"font-weight:bold" ,
215
162
"z-index:2" ,
216
163
] . join ( ";" ) ;
@@ -224,34 +171,17 @@ export default class Vcode extends React.PureComponent<Props, State> {
224
171
return [
225
172
"position: absolute" ,
226
173
`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` ,
244
178
`top:${ this . randint ( 0 , this . state . height ) } px` ,
245
179
`transform:rotate(${ this . randint ( - 30 , 30 ) } deg)` ,
246
180
`-o-transform:rotate(${ this . randint ( - 30 , 30 ) } deg)` ,
247
181
`-ms-transform:rotate(${ this . randint ( - 30 , 30 ) } deg)` ,
248
182
`-moz-transform:rotate(${ this . randint ( - 30 , 30 ) } deg)` ,
249
183
`-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 ) ] } ` ,
255
185
`font-weight:${ this . randint ( 400 , 900 ) } ` ,
256
186
] . join ( ";" ) ;
257
187
}
@@ -264,21 +194,15 @@ export default class Vcode extends React.PureComponent<Props, State> {
264
194
let c = "" ; // 存储生成的code
265
195
const div = document . getElementById ( this . state . id ) ;
266
196
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 || "" ) ; // 是否是图片
270
198
if ( div ) {
271
199
div . innerHTML = "" ;
272
200
}
273
201
274
202
if ( isImg ) {
275
203
// 用户传递了一张图片
276
204
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 ( ";" ) ;
282
206
dom . src = value as string ;
283
207
div && div . appendChild ( dom ) ;
284
208
this . props . onChange && this . props . onChange ( null ) ;
@@ -292,11 +216,7 @@ export default class Vcode extends React.PureComponent<Props, State> {
292
216
for ( let i = 0 ; i < length ; i ++ ) {
293
217
const dom = document . createElement ( "span" ) ;
294
218
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 ) ) ] ;
300
220
dom . innerHTML = String ( temp ) ;
301
221
c = `${ c } ${ temp } ` ;
302
222
div && div . appendChild ( dom ) ;
@@ -320,13 +240,6 @@ export default class Vcode extends React.PureComponent<Props, State> {
320
240
}
321
241
322
242
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 ( ) } /> ;
331
244
}
332
245
}
0 commit comments