@@ -62,7 +62,7 @@ class Vcode extends React.Component {
6262 }
6363
6464 /** 组件参数改变 **/
65- componentWillReceiveProps ( nextP , nextS ) {
65+ UNSAFE_componentWillReceiveProps ( nextP , nextS ) {
6666 if ( this . props . value !== nextP . value ) {
6767 this . onDraw ( nextP . value ) ;
6868 }
@@ -77,12 +77,16 @@ class Vcode extends React.Component {
7777 /** 用户点击的验证码图片 **/
7878 onClick ( ) {
7979 const div = document . getElementById ( this . state . id ) ;
80- div . innerHTML = '' ;
81- this . onDraw ( this . props . value ) ;
80+ // 如果this.props.value有值,表明值是外部受控,这个地方不需要重新渲染
81+ let code = null ;
82+ if ( ! this . props . value ) {
83+ code = this . onDraw ( this . props . value ) ;
84+ }
85+ this . props . onClick && this . props . onClick ( ) ; // 触发外部的onClick,什么都不返回
8286 }
8387
8488 /** 随机生成一个Code的CSS样式 **/
85- codeCss ( ) {
89+ codeCss ( uW , i ) {
8690 return [
8791 `font-size:${ this . randint ( this . state . options . fontSizeMin ,
8892 this . state . options . fontSizeMax ) } px`,
@@ -129,42 +133,39 @@ class Vcode extends React.Component {
129133 onDraw ( value ) {
130134 let c = '' ; // 存储生成的code
131135 const div = document . getElementById ( this . state . id ) ;
136+ const isImg = / ^ 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 ) ; // 是否是图片
132137 div . innerHTML = '' ;
133138
134- /** 生成好看的code **/
135- const codeCss = this . codeCss ( ) ;
136-
137- if ( value !== undefined ) { // 如果父级指定了要生成的code
138- const uW = this . state . width / value . length ; // 每个字符占的宽度
139- for ( let i = 0 ; i < value . length ; i ++ ) {
140- const dom = document . createElement ( 'span' ) ;
141- dom . style . cssText = codeCss ;
142- const temp = value [ i ] ;
143- dom . innerHTML = temp ;
144- c = `${ c } ${ temp } ` ;
145- div . appendChild ( dom ) ;
146- }
147- } else {
148- const uW = this . state . width / this . state . len ; // 每个字符占的宽度
149- for ( let i = 0 ; i < this . state . len ; i ++ ) {
150- const dom = document . createElement ( 'span' ) ;
151- dom . style . cssText = codeCss ;
152- const temp = this . state . options . codes [ ( Math . round ( Math . random ( ) * ( this . state . options . codes . length - 1 ) ) ) ] ;
153- dom . innerHTML = temp ;
154- c = `${ c } ${ temp } ` ;
139+ if ( isImg ) { // 用户传递了一张图片
140+ const dom = document . createElement ( "img" ) ;
141+ dom . style . cssText = [ "display: block" , "max-width:100%" , "max-height:100%" ] . join ( ";" ) ;
142+ dom . src = value ;
155143 div . appendChild ( dom ) ;
156- }
144+ this . props . onChange && this . props . onChange ( null ) ;
145+ return null ;
157146 }
158147
148+ // 不是图片而是普通字符串, 如果value存在说明是用户自定义的字符串
149+ let length = value . length || this . state . len ; // 字符的长度
150+
151+ const uW = this . state . width / length / 1.01 ; // 每个字符占的宽度
152+ for ( let i = 0 ; i < length ; i ++ ) {
153+ const dom = document . createElement ( 'span' ) ;
154+ dom . style . cssText = this . codeCss ( uW , i ) ;
155+ const temp = value ? value [ i ] : this . state . options . codes [ ( Math . round ( Math . random ( ) * ( this . state . options . codes . length - 1 ) ) ) ] ;
156+ dom . innerHTML = temp ;
157+ c = `${ c } ${ temp } ` ;
158+ div . appendChild ( dom ) ;
159+ }
160+
159161 // 生成好看的线条
160162 for ( let i = 0 ; i < this . state . options . lines ; i ++ ) {
161163 const dom = document . createElement ( 'div' ) ;
162164 dom . style . cssText = this . lineCss ( ) ;
163165 div . appendChild ( dom ) ;
164166 }
165- if ( this . props . onChange ) {
166- this . props . onChange ( c ) ;
167- }
167+ this . props . onChange && this . props . onChange ( c ) ; // 触发回调
168+ return c ;
168169 }
169170
170171 /** 生成范围随机数 **/
0 commit comments