import React,{Component} from 'react';
import SpinContainer from "../../../SpinContainer";
import {Button, Form, Icon, Input, message, Modal, Select, Upload} from "antd";
import {beforeUploadFile,splitUrl} from "@/tools.js";
import Editors from "@/richTextEditors";
import $ from "jquery/src/ajax";
const FormItem = Form.Item;
//上传图片组件
const PicturesWall = React.createClass({
getInitialState() {
return {
previewVisible: false,
previewImage: "",
fileList: this.props.pictureUrl,
type:''
};
},
getDefaultProps() {
return {
changeClick: this.modifyChange
};
},
handleCancel() {
this.setState({ previewVisible: false });
},
handlePreview(file) {
let type = '';
if(file.type){
let str = file.type;
let index = str.indexOf("/");
type =str.substring(0,index) === 'video' ? 1 : str.substring(0,index) === 'audio' ? 2 : 0;
}else{
// let str = file.url || file.thumbUrl;
// let index = str.lastIndexOf(".");
// type = str.substring(index+1);
type = this.props.type;
}
this.setState({
type,
previewImage: file.url || file.thumbUrl,
previewVisible: true
});
},
handleChange(info) {
let fileList = info.fileList;
this.setState({ fileList });
this.props.fileList(fileList);
},
componentWillReceiveProps(nextProps) {
this.state.fileList = nextProps.pictureUrl;
},
render() {
const { previewVisible, previewImage, fileList } = this.state;
const uploadButton = (
);
return (
{fileList && fileList.length >= 1 ? null : uploadButton}
{
this.state.type=== 0 ?
:
this.state.type=== 1 ?
:
this.state.type=== 2 ?
: ''
}
);
}
});
class Add extends Component{
constructor(props) {
super(props);
this.state={
loading: false,
projectType: props.shareType || '',
name:'',
pictureImgUrl: '',
shareImgUrl:'',
shareId:'',
}
this.getOrgCodeUrl = this.getOrgCodeUrl.bind(this);
this.getShareOrgCodeUrl = this.getShareOrgCodeUrl.bind(this);
this.onOk = this.onOk.bind(this);
this.getWeChatShareDetals = this.getWeChatShareDetals.bind(this);
}
componentDidMount() {
if(this.props.id || this.props.shareId){
this.getWeChatShareDetals();
}
}
getOrgCodeUrl(e) {
this.setState({ pictureImgUrl: e });
}
getShareOrgCodeUrl(e) {
this.setState({ shareImgUrl: e });
}
onOk(){
if(!this.state.name){
message.warning('请输入分享标题');
return false;
}
if(isNaN(parseInt(this.state.projectType))){
message.warning('请选择分享位置');
return false;
}
if(this.state.pictureImgUrl.length === 0){
message.warning('请上传朋友分享图');
return false;
}
if(this.state.shareImgUrl.length === 0){
message.warning('请上传朋友圈分享图');
return false;
}
this.setState({
loading: true
});
let url = this.state.shareId ? "/api/admin/weChatShare/update" : "/api/admin/weChatShare/add"
$.ajax({
method: "post",
dataType: "json",
crossDomain: false,
url: globalConfig.context + url,
data: {
pid: this.props.id || undefined, //项目编号
id: this.state.shareId || undefined,
title: this.state.name || undefined,
shareUrl: this.state.pictureImgUrl[0].response.data || undefined,
momentsUrl: this.state.shareImgUrl[0].response.data || undefined,
type: this.state.projectType,
},
success: function(data) {
if (data.error && data.error.length) {
message.warning(data.error[0].message);
} else {
message.success(this.state.shareId ? '修改成功' : '添加成功');
this.props.onOk();
}
}.bind(this)
}).always(
function() {
this.setState({
loading: false
});
}.bind(this)
);
}
getWeChatShareDetals() {
this.setState({
loading: true
});
let data = {};
if(this.props.id){
data.pid = this.props.id
}else if(this.props.shareId){
data.id = this.props.shareId
}
$.ajax({
method: "get",
dataType: "json",
crossDomain: false,
url: globalConfig.context + "/api/admin/weChatShare/details",
data: data
}).done((data1) => {
if (data1.error.length !== 0) {
message.warning(data1.error[0].message);
} else {
if(data1.data){
this.setState({
name: data1.data.title,
projectType: data1.data.type,
pictureImgUrl: data1.data.shareUrl ? splitUrl(data1.data.shareUrl, ',', globalConfig.avatarHost + '/upload') : [],
shareImgUrl: data1.data.momentsUrl ? splitUrl(data1.data.momentsUrl, ',', globalConfig.avatarHost + '/upload') : [],
shareId:data1.data.id,
})
}
}
}).always(function () {
this.setState({
loading: false
});
}.bind(this))
}
render() {
return (
)
}
}
export default Add;