import Taro, { Component } from '@tarojs/taro' import { View, Text } from '@tarojs/components' import { AtInput, AtImagePicker, AtTextarea, AtButton, Picker, AtToast } from 'taro-ui' import InteractionComponent from '../../component/interactionComponent/interactionComponent' import CopyrightComponent from '../../component/copyrightComponent/copyrightComponent' import URL from '../../serviceAPI.config' import './myNeedsEdit.scss' class MyNeedsEdit extends Component { config = { navigationBarTitleText: '需求编辑' } constructor() { super(...arguments) this.state = { industryTypeSelected: '', needsType: [{ name: '业主需求', id: '4' }, { name: '效果图', id: '5' }], needsTypeSelected: { name: '业主需求', id: '4' }, needsState: [ { name: '作废', id: '0' }, { name: '在用', id: '1' }, ], needsStateSelected: { name: '全部', id: '' }, title: '', sd_id: '', //需求id contactName: '', contactNumber: '', contactAddress: '', content: '',//描述 pickerImageUrl: [], ImagesInfo: '', } } //获取需求信息api getMyNeedEditInfo() { Taro.request({ url: URL.EditMyNeeds, method: 'GET', dataType: 'json', data: { demandId: this.$router.params.id, }, header: { 'content-type': 'application/x-www-form-urlencoded', 'Cookie': 'PFWSSS=' + Taro.getStorageSync('session_id'), 'X-Requested-With': 'XMLHttpRequest' } }) .then(res => { console.log('需求详情获取成功', res) // const selectedType = this.state.demandingSupplyCate.filter(item => item.id == res.data.sdInfo.sd_type)[0] // const selectedState = this.state.needsState.filter(item => item.id == res.data.sdInfo.state)[0] let industryType = {} const classId = res.data.sdInfo.class_id for (let outter of res.data.supplyTree) { if (outter.children) { for (let inner of outter.children) { if (inner.class_id === classId) { industryType.name = inner.class_name industryType.id = inner.class_id break } } } if (outter.class_id === classId) { industryType.name = outter.class_name industryType.id = outter.class_id break } } if(!Object.keys(industryType).length){ industryType.name = '全部' industryType.id ='' } const needsType = this.state.needsType.filter(item => { return item.id === res.data.sdInfo.sd_type })[0] const imageFile = res.data.sdInfo.file_path.map(item => { return { url: URL.Base + item.file_path } }) const needsState = this.state.needsState.filter(item => { return item.id === res.data.sdInfo.state })[0] this.setState({ sd_id: res.data.sdInfo.sd_id, industryTypeSelected: industryType, needsTypeSelected: needsType, title: res.data.sdInfo.sd_title, contactName:res.data.sdInfo.user_name, contactNumber:res.data.sdInfo.user_phone, contactAddress: res.data.sdInfo.user_address, content: res.data.sdInfo.sd_desc, needsStateSelected: needsState, pickerImageUrl: imageFile, ImagesInfo: res.data.sdInfo.file_path, }) Taro.hideLoading() } ) .catch(error => { console.log('供求详情获取失败', error) }) } // 上传需求按键 uploadMyNeedsButton() { if (this.state.title && this.state.contactName && this.state.contactNumber && this.state.content && this.state.needsStateSelected) { this.uploadMyNeedsApi({}) } else { Taro.showToast({ title: '请填写完表格', icon: 'none', duration: 1500 }) } } // uploadMyNeedsApi 上传需求 的api uploadMyNeedsApi() { const file_path = []; this.state.ImagesInfo.forEach((item) => { file_path.push({ file_name: item.file_name, file_size: item.file_size, file_path: item.file_path, thumb_path: item.thumb_path }) }) Taro.request({ url: URL.PublishMyNeed, method: 'POST', dataType: 'json', data: { action: 2, sdInfo: JSON.stringify({ class_id: this.state.industryTypeSelected.id, sd_id: this.state.sd_id, sd_type: this.state.needsTypeSelected.id, sd_title: this.state.title, user_name: this.state.contactName, user_phone: this.state.contactNumber, user_address: this.state.contactAddress, sd_desc: this.state.content, state: this.state.needsStateSelected.id, file_path: file_path, }) }, header: { 'content-type': 'application/x-www-form-urlencoded', 'Cookie': 'PFWSSS=' + Taro.getStorageSync('session_id'), 'X-Requested-With': 'XMLHttpRequest' } }) .then(res => { console.log('上传需求', res) if (res.data.err_msg === 'success') { Taro.showToast({ title: '保存成功', icon: 'success', duration: 1500 }).then(() => { // setTimeout(() => { // Taro.navigateTo({ // // url: '/pages/mySupplyDemand/mySupplyDemand' // }) // }, 1500); }) } else { Taro.showToast({ title: res.data.err_msg, icon: 'none', duration: 1500 }) } } ) } // 上传图片 onChangeImg(files, operationType, index) { const that = this if (operationType === 'add') { Taro.uploadFile({ url: URL.UploadDSPorductImage, filePath: files[files.length - 1].url, name: 'file', header: { 'content-type': 'multipart/form-data', 'Cookie': 'PFWSSS=' + Taro.getStorageSync('session_id'), 'X-Requested-With': 'XMLHttpRequest' }, success(response) { const data = JSON.parse(response.data) const imagePath = URL.Base + data.file_path const newPickerImageUrl = that.state.pickerImageUrl.concat({ url: imagePath }) const newImageInfo = that.state.ImagesInfo.concat(data) that.setState({ pickerImageUrl: newPickerImageUrl, ImagesInfo: newImageInfo }) Taro.showToast({ title: '上传成功', icon: 'success', duration: 1500 }) } }) } if (operationType === 'remove') { this.state.ImagesInfo.splice(index, 1) // 删除显示的图片 this.state.pickerImageUrl.splice(index, 1)// 删除图片param that.setState({ pockerImageUrl: this.state.pickerImageUrl, ImagesInfo: this.state.ImagesInfo, }) Taro.showToast({ title: '删除成功', icon: 'success', duration: 1500 }) } } // 修改需求类型 needsTypeChange = e => { this.setState({ needsTypeSelected: this.state.needsType[e.detail.value] }) } // 修改供求状态 needsStateChange = e => { this.setState({ needsStateSelected: this.state.needsState[e.detail.value] }) } //改标题 titleChange(event) { this.setState({ title: event }) } contactNameChange(event) { this.setState({ contactName: event }) } contactNumberChange(event) { this.setState({ contactNumber: event }) } contactAddressChange(event) { this.setState({ contactAddress: event.target.value }) } contentChange(event) { this.setState({ content: event.target.value }) } goToMyNeedsPage() { Taro.navigateTo({ url: '/pages/myNeeds/myNeeds' }) } getDataFromChild(value) { console.log('从子组件传回来的值', value) this.setState({ industryTypeSelected: value }) } componentDidMount() { Taro.showLoading({ title: '加载中' }) this.getMyNeedEditInfo() } componentWillReceiveProps(nextProps) { console.log(this.props, nextProps) } componentWillUnmount() { } componentDidShow() { } componentDidHide() { } render() { return ( {/* 行业分类 */} {/* 需求类型 */} *需求类型: {this.state.needsTypeSelected.name} * * * 联系地址: *需求内容: 需求图片: (最多4张) *状态: {this.state.needsStateSelected.name} 保存 {/* 保存并新增 */} 我的需求 ) } } export default MyNeedsEdit