| | |
| | | class MkPicture extends Component { |
| | | static propTpyes = { |
| | | style: PropTypes.object, |
| | | lostTip: PropTypes.bool, |
| | | scale: PropTypes.bool, |
| | | url: PropTypes.string, |
| | | urls: PropTypes.array, |
| | |
| | | } |
| | | |
| | | UNSAFE_componentWillMount() { |
| | | const { url } = this.props |
| | | const { url, lostTip } = this.props |
| | | |
| | | if (url) { |
| | | this.setState({url: url, lost: false}) |
| | | this.checkUrl(url) |
| | | } else { |
| | | this.setState({url: LostPng, lost: true}) |
| | | this.setState({url: lostTip ? LostPng: '', lost: true}) |
| | | } |
| | | } |
| | | |
| | | UNSAFE_componentWillReceiveProps (nextProps) { |
| | | const { lostTip } = this.props |
| | | |
| | | if (nextProps.url) { |
| | | if (nextProps.url !== this.state.url) { |
| | | this.setState({url: nextProps.url, lost: false}) |
| | | this.checkUrl(nextProps.url) |
| | | } |
| | | } else { |
| | | this.setState({url: LostPng, lost: true}) |
| | | this.setState({url: lostTip ? LostPng: '', lost: true}) |
| | | } |
| | | } |
| | | |
| | | checkUrl = (url) => { |
| | | let img = new Image() |
| | | img.addEventListener('error', this.loadHandler) |
| | | img.src = url |
| | | |
| | | if (/^https/.test(window.location.protocol)) { // https转换 |
| | | if (/^http:/.test(url)) { |
| | | img.src = url.replace(/^http:/, 'https:') |
| | | } else if (/^\/\//.test(url)) { |
| | | img.src = 'https:' + url |
| | | } else { |
| | | img.src = url |
| | | } |
| | | } else { |
| | | img.src = url |
| | | } |
| | | } |
| | | |
| | | loadHandler = (e) => { |
| | |
| | | } |
| | | |
| | | render() { |
| | | const { style, scale, urls } = this.props |
| | | const { style, scale, urls, lostTip } = this.props |
| | | const { url, lost } = this.state |
| | | |
| | | if (!lostTip && !url) { |
| | | return ( |
| | | <div className="ant-mk-picture empty" style={style}></div> |
| | | ) |
| | | } |
| | | |
| | | return ( |
| | | <div |
| | | className={'ant-mk-picture' + (scale ? ' scale' : '') + (lost ? ' lost' : '')} |