import * as React from 'react'; import './index.scss'; import AddedList from '../Common/AddedList' import UploadImg from './uploadImg' import {taker,getApiUrl2} from '&/helper' interface MetaData{ name:string, url:string, jumpUrl:string } interface Meta { defaultValue:Array } interface Props { data:any, update:Function, _metadata:Meta } const defaultState = { popShow:false, linkUrl: '', errorStatus:false, errorMessage:'', jumpUrl:'', jumpUrlErr:false, editPopShow:false, dfs:'' } export default class BannerSetting extends React.Component { state = defaultState cropImg:{ x:number, y:number, width:number, height:number } refs:any upLinkItem = (index:number) => { const { update, data } = this.props; const temp = data[index]; if (index == 0 && data[1]) { data[0] = data[1] data[1] = temp; update(data) } else if(index == 0 && !data[1]){ } else { data[index] = data[index - 1] data[index - 1] = temp; update(data) } } deleteLinkItem = (index:number) =>{ const { data } = this.props; const { update } = this.props; if(data.length > 1){ //最少1条 data.splice(index, 1); for(let i = 0;i { const { x,y,width,height } = e.detail; const cropImg = { x: Math.round(x), y: Math.round(y), width: Math.round(width), height: Math.round(height) } this.cropImg = cropImg; } addNewLink = () => { this.setState({popShow:true}) } async handleSave() { const {dfs} = this.state; let { x,y,width,height } = this.cropImg const url = `${getApiUrl2('img/cut')}?x=${x}&y=${y}&w=${width}&h=${height}&dfs_url=${dfs}`; const res = await taker(url,'POST',{}); this.setState({linkUrl:res.Data.clientUrl}) } returnState = () => { this.setState(defaultState); } noAddNewLink = () => { this.returnState() } addNewLinkSure = () => { //验证通过再保存(非空,限png/jpg/jpeg格式,大小不超过1M,标准尺寸为750*300px) const { linkUrl,jumpUrl } = this.state; const reg=/[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]/; if( linkUrl == '' ||linkUrl.replace(' ','') == '' ){ //没传jpg,png,jpeg this.setState({errorStatus:true,errorMessage:'请上传图片'}) }else if(!/\.(jpg|jpeg|png|JPG|PNG)$/.test(linkUrl)){ this.setState({errorStatus:true,errorMessage:'请上传规定格式的图片'}) }else if(jumpUrl&&!reg.test(jumpUrl)){ this.setState({ errorStatus:true,errorMessage:'链接格式不正确' }) }else{ let { data=[], update } = this.props; if( data.length > 3 ){ //不超过4条 } else { let tempJumpUrl = jumpUrl; if(jumpUrl.replace(' ','') == ''){ tempJumpUrl = ''; }else{ tempJumpUrl = jumpUrl.indexOf('http://') == 0 || jumpUrl.indexOf('https://') == 0 ? jumpUrl : 'http://'+jumpUrl ; } const linkItem = { name: `banner-${data.length+1}`, url: 'http:'+linkUrl, jumpUrl:tempJumpUrl,height:200 }; data.push(linkItem); this.state.popShow = false; update(data) } this.returnState() } } jumpUrlChange = (e: React.ChangeEvent) => { let val = e.target.value; this.setState({ jumpUrl: val }) } updateImg = (val:string) => { this.setState({ linkUrl: val }) } render(){ let {data} = this.props; // const { popShow,linkUrl, errorStatus, errorMessage, jumpUrl } = this.state; return (
添加图片
限png/jpg/jpeg格式,大小不超过1M,标准尺寸为750*400px
图片链接 {errorMessage}
3 ? 'none' : 'inline-block' }} onClick={ this.addNewLink } href='javaScript:;'> +新增
) } }