@@ -7,7 +7,7 @@ class Vcode extends React.Component {
77 id : this . props . id || `${ new Date ( ) . getTime ( ) } _${ Math . random ( ) . toFixed ( 4 ) } ` , // 需要一个唯一的ID,因为vcode要直接操作dom
88 width : this . props . width || 150 , // vcode宽度
99 height : this . props . height || 40 , // vcode高度
10- length : this . props . length || 4 , // 生成几位code
10+ len : this . props . length || 4 , // 生成几位code
1111 style : ( ( ) => { // vcode容器样式
1212 const a = {
1313 position : 'relative' ,
@@ -34,8 +34,8 @@ class Vcode extends React.Component {
3434 fontSizeMin : 22 , // 字体尺寸最小值
3535 fontSizeMax : 26 , // 字体尺寸最大值
3636 colors : [ // 字可能的颜色
37- '117cb3' ,
38- 'f47b06' ,
37+ '# 117cb3' ,
38+ '# f47b06' ,
3939 '#202890' ,
4040 '#db1821' ,
4141 '#b812c2' ,
@@ -71,47 +71,86 @@ class Vcode extends React.Component {
7171
7272 /** 组件初始化完毕时触发 **/
7373 componentDidMount ( ) {
74- this . onDraw ( ) ;
74+ this . onDraw ( this . props . value ) ;
75+ }
76+
77+ /** 组件参数改变 **/
78+ componentWillReceiveProps ( nextP ) {
79+ if ( this . props . value !== nextP . value ) {
80+ this . onDraw ( nextP . value ) ;
81+ }
7582 }
7683
7784 /** 用户点击的验证码图片 **/
7885 onClick ( ) {
7986 const div = document . getElementById ( this . state . id ) ;
8087 div . innerHTML = '' ;
81- this . onDraw ( ) ;
88+ this . onDraw ( this . props . value ) ;
8289 }
8390
8491 /** 随机生成验证码 **/
85- onDraw ( ) {
92+ onDraw ( value ) {
8693 let c = '' ; // 存储生成的code
8794 const div = document . getElementById ( this . state . id ) ;
88- const uW = this . state . width / this . state . length ; // 每个字符占的宽度
95+ div . innerHTML = '' ;
96+ /** 生成好看的code **/
97+ console . log ( this . props . value ) ;
8998
90- // 生成好看的code
91- for ( let i = 0 ; i < this . state . length ; i ++ ) {
92- const dom = document . createElement ( 'span' ) ;
93- dom . style . cssText = [
94- `font-size:${ this . randint ( this . state . options . fontSizeMin ,
95- this . state . options . fontSizeMax ) } px`,
96- `color:${ this . state . options . colors [ this . randint ( 0 ,
97- this . state . options . colors . length - 1 ) ] } `,
98- 'position: absolute' ,
99- `left:${ this . randint ( uW * i , ( ( uW * i ) + uW ) - ( uW / 2 ) ) } px` ,
100- 'top:50%' ,
101- `transform:rotate(${ this . randint ( - 15 , 15 ) } deg) translateY(-50%)` ,
102- `-o-transform:rotate(${ this . randint ( - 15 , 15 ) } deg) translateY(-50%)` ,
103- `-ms-transform:rotate(${ this . randint ( - 15 , 15 ) } deg) translateY(-50%)` ,
104- `-moz-transform:rotate(${ this . randint ( - 15 , 15 ) } deg) translateY(-50%)` ,
105- `-webkit-transform:rotate(${ this . randint ( - 15 , 15 ) } deg) translateY(-50%)` ,
106- `font-family:${ this . state . options . fonts [ this . randint ( 0 ,
107- this . state . options . fonts . length - 1 ) ] } `,
108- 'font-weight:bold' ,
109- 'z-index:2' ,
110- ] . join ( ';' ) ;
111- const temp = this . state . options . codes [ ( Math . round ( Math . random ( ) * ( this . state . options . codes . length - 1 ) ) ) ] ;
112- dom . innerHTML = temp ;
113- c = `${ c } ${ temp } ` ;
114- div . appendChild ( dom ) ;
99+ if ( value !== undefined ) { // 如果父级指定了要生成的code
100+ const uW = this . state . width / value . length ; // 每个字符占的宽度
101+ for ( let i = 0 ; i < value . length ; i ++ ) {
102+ const dom = document . createElement ( 'span' ) ;
103+ dom . style . cssText = [
104+ `font-size:${ this . randint ( this . state . options . fontSizeMin ,
105+ this . state . options . fontSizeMax ) } px`,
106+ `color:${ this . state . options . colors [ this . randint ( 0 ,
107+ this . state . options . colors . length - 1 ) ] } `,
108+ 'position: absolute' ,
109+ `left:${ this . randint ( uW * i , ( ( uW * i ) + uW ) - ( uW / 2 ) ) } px` ,
110+ 'top:50%' ,
111+ `transform:rotate(${ this . randint ( - 15 , 15 ) } deg) translateY(-50%)` ,
112+ `-o-transform:rotate(${ this . randint ( - 15 , 15 ) } deg) translateY(-50%)` ,
113+ `-ms-transform:rotate(${ this . randint ( - 15 , 15 ) } deg) translateY(-50%)` ,
114+ `-moz-transform:rotate(${ this . randint ( - 15 , 15 ) } deg) translateY(-50%)` ,
115+ `-webkit-transform:rotate(${ this . randint ( - 15 , 15 ) } deg) translateY(-50%)` ,
116+ `font-family:${ this . state . options . fonts [ this . randint ( 0 ,
117+ this . state . options . fonts . length - 1 ) ] } `,
118+ 'font-weight:bold' ,
119+ 'z-index:2' ,
120+ ] . join ( ';' ) ;
121+ const temp = value [ i ] ;
122+ dom . innerHTML = temp ;
123+ c = `${ c } ${ temp } ` ;
124+ div . appendChild ( dom ) ;
125+ }
126+ } else {
127+ console . log ( '走这里' ) ;
128+ const uW = this . state . width / this . state . len ; // 每个字符占的宽度
129+ for ( let i = 0 ; i < this . state . len ; i ++ ) {
130+ const dom = document . createElement ( 'span' ) ;
131+ dom . style . cssText = [
132+ `font-size:${ this . randint ( this . state . options . fontSizeMin ,
133+ this . state . options . fontSizeMax ) } px`,
134+ `color:${ this . state . options . colors [ this . randint ( 0 ,
135+ this . state . options . colors . length - 1 ) ] } `,
136+ 'position: absolute' ,
137+ `left:${ this . randint ( uW * i , ( ( uW * i ) + uW ) - ( uW / 2 ) ) } px` ,
138+ 'top:50%' ,
139+ `transform:rotate(${ this . randint ( - 15 , 15 ) } deg) translateY(-50%)` ,
140+ `-o-transform:rotate(${ this . randint ( - 15 , 15 ) } deg) translateY(-50%)` ,
141+ `-ms-transform:rotate(${ this . randint ( - 15 , 15 ) } deg) translateY(-50%)` ,
142+ `-moz-transform:rotate(${ this . randint ( - 15 , 15 ) } deg) translateY(-50%)` ,
143+ `-webkit-transform:rotate(${ this . randint ( - 15 , 15 ) } deg) translateY(-50%)` ,
144+ `font-family:${ this . state . options . fonts [ this . randint ( 0 ,
145+ this . state . options . fonts . length - 1 ) ] } `,
146+ 'font-weight:bold' ,
147+ 'z-index:2' ,
148+ ] . join ( ';' ) ;
149+ const temp = this . state . options . codes [ ( Math . round ( Math . random ( ) * ( this . state . options . codes . length - 1 ) ) ) ] ;
150+ dom . innerHTML = temp ;
151+ c = `${ c } ${ temp } ` ;
152+ div . appendChild ( dom ) ;
153+ }
115154 }
116155
117156 // 生成好看的线条
@@ -167,6 +206,7 @@ export default Vcode;
167206/**
168207 id: P.string, // ID
169208 length: P.number, // 生成几位字符串
209+ value: P.string, // 由父级传入指定的字符串生成code
170210 width: P.number, // 多宽 px
171211 height: P.number, // 多高 px
172212 style: P.object, // 自定义style
0 commit comments