import Taro, { Component } from '@tarojs/taro' import { View, Text } from '@tarojs/components' import { AtInput, AtImagePicker, AtTextarea, AtButton, } from 'taro-ui' import ShopTypeInteractionComp from '../../component/shopTypeInteractionComp/shopTypeInteractionComp' import GoodsTypeInteractionComp from '../../component/goodsTypeInteractionComp/goodsTypeInteractionComp' import URL from '../../serviceAPI.config' import './goodsPublish.scss' class GoodsPublish extends Component { config = { navigationBarTitleText: '商品发布' } constructor() { super(...arguments) this.state = { objectMultiArray: [ ], shopTypeSelected: { name: '选择店铺类型', id: '' },//已选的店铺分类 goodsTypeSelected: { name: '选择商品分类', id: '' },// 已选的商品分类 productName: '', productPrice: '', productUnit: '', productDescript: '', pickerImageUrl: [], // 上传的图片 ImagesInfo: [],// 后台传回来的图片信息 shopCategoryList: [], productCategoryList: [], } } //商品目录请求api GetShopCategoryList getProductCateList() { Taro.request({ url: URL.GetProductCategoryList, method: 'POST', dataType: 'json', header: { 'content-type': 'application/x-www-form-urlencoded', 'Cookie': 'PFWSSS=' + Taro.getStorageSync('session_id'), 'X-Requested-With': 'XMLHttpRequest' } }) .then(res => { if (res.data.err_msg === 'success') { this.setState({ productCategoryList: res.data.data }) console.log('商品分类目录', res) this.sortShopCateToState(res.data.data) } } ) .catch(error => { console.log('商品分类请求错误', error) }) } // 发布商品api uploadGoods({ goods_name = "test2", goods_price = "1.00", goods_unit = "1", goods_profiles = "test2", class_id = "10103", shop_class_id = "1930" }) { // 上传图片的参数 const uploadProductGoodFileParams = [] if (this.state.ImagesInfo.length) { for (let i = 0; i < this.state.ImagesInfo.length; i++) { if (i === 0) { uploadProductGoodFileParams.push({ file_id: this.state.ImagesInfo[i].file_id, file_type: 1, if_cover: 1, file_sort: 1 }) } else { uploadProductGoodFileParams.push({ file_id: this.state.ImagesInfo[i].file_id, file_type: 1, if_cover: 0, file_sort: i + 1 }) } } } else { alert('图片为空') } console.log('页面图片列表', this.state.pickerImageUrl) console.log('图片参数列表', uploadProductGoodFileParams) Taro.request({ url: URL.UploadProduct, method: 'POST', dataType: 'json', data: { deployType: 1, action: 1, goods: JSON.stringify({ goods_name: goods_name, goods_price: goods_price, goods_unit: goods_unit, goods_profiles: goods_profiles, class_id: class_id, shop_class_id: shop_class_id }), goodsFiles: JSON.stringify(uploadProductGoodFileParams) }, header: { 'content-type': 'application/x-www-form-urlencoded', 'Cookie': 'PFWSSS=' + Taro.getStorageSync('session_id'), 'X-Requested-With': 'XMLHttpRequest' } }) .then(res => { Taro.hideLoading() if (res.data.err_msg === 'success') { Taro.showToast({ title: '发布成功', icon: 'success', duration: 1000 }) setTimeout(() => { Taro.navigateTo({ url: '/pages/goods/goods?id=' + res.data.goods_id }) }, 1000); } else { Taro.showToast({ title: res.data.err_msg, icon: 'none', duration: 1000 }) } console.log('上传商品', res) } ) .catch(error => { Taro.hideLoading() Taro.showToast({ title: '保存失败', icon: 'none', duration: 1000 }) }) } // 整理后台传出来的店铺分类目录 sortShopCateToState(shopData) { const firstColumn = [] const secondColumn = [] const thirdColumn = [] for (let outterItem of shopData) { firstColumn.push({ id: outterItem.class_id, class_name: outterItem.class_name }) if (outterItem.children.length) { for (let middleItem of outterItem.children) { secondColumn.push({ id: middleItem.class_id, class_name: middleItem.class_name }) if (middleItem.children.length) { for (let innerItem of middleItem.children) { thirdColumn.push({ id: innerItem.class_id, class_name: innerItem.class_name }) } } } } } //------- 把新指传给objectMultiArray之后就不可以用了 to be continue this.setState({ objectMultiArray: [firstColumn, secondColumn, thirdColumn] }, () => { console.log('new', this.state.objectMultiArray) }) } // 改变商品分类状态 onChangeProductType(e) { this.setState({ selectorChecked: this.state.selector[e.detail.value] }) } productNameChange(event) { this.setState({ productName: event }) } productPriceChange(event) { this.setState({ productPrice: event }) } productUnitChange(event) { this.setState({ productUnit: event }) } productDescriptChange(event) { this.setState({ productDescript: event.target.value }) } // 上传图片 onChangeImg(files, operationType, index) { const that = this if (operationType === 'add') { Taro.uploadFile({ url: URL.UploadGoodsPorductImage, filePath: files[files.length - 1].url, name: 'file', formData: { user: 'test' }, 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) console.log('第一', newImageInfo) that.setState({ pickerImageUrl: newPickerImageUrl, ImagesInfo: newImageInfo }, () => { Taro.showToast({ title: '上传成功', icon: 'success', duration: 2000 }) }) } }) } if (operationType === 'remove') { this.state.pickerImageUrl.splice(index, 1); this.state.ImagesInfo.splice(index, 1); this.setState({ pickerImageUrl: this.state.pickerImageUrl, ImagesInfo: this.state.ImagesInfo }); Taro.showToast({ title: '删除成功', icon: 'success', duration: 2000 }) } } // 图片上传失败 onFail(mes) { console.log(mes) } // 删除图片 onImageClick(index) { } onClickUploadGoods() { if (this.state.productName && this.state.productPrice && this.state.productUnit && this.state.ImagesInfo.length && this.state.goodsTypeSelected.id && this.state.shopTypeSelected.id) { Taro.showLoading({ title: '发布中' }).then(() => { setTimeout(() => { this.uploadGoods({ goods_name: this.state.productName, goods_price: this.state.productPrice, goods_unit: this.state.productUnit, goods_profiles: this.state.productDescript, class_id: this.state.goodsTypeSelected.id, shop_class_id: this.state.shopTypeSelected.id, }) }, 1000) }) } else { Taro.showToast({ title: '请填写完表格', icon: 'none' }) } } goToMyGoodListPage() { Taro.navigateTo({ url: '/pages/myGoodList/myGoodList' }) } getDataFromShopChild(value) { console.log('从子组件店铺分类传回来的值', value) this.setState({ shopTypeSelected: value }) } getDataFromGoodsChild(value) { console.log('从子组件商品分类传回来的值', value) this.setState({ goodsTypeSelected: value }) } componentDidMount() { // this.getProductCateList() // this.getShopCateList() } componentWillReceiveProps(nextProps) { // console.log(this.props, nextProps) } componentWillUnmount() { } componentDidShow() { } componentDidHide() { } render() { console.log("this.state.productCategoryList", this.state.productCategoryList) return ( {/* 商品分类开始 */} {/* 商品分类结束 */} * * * {/* 图片上传 */} * 上传图片: {/* 店铺分类 */} {/* 店铺分类结束 */} 商品简介: 发布 {/* 发布并新增 */} 商品列表 ) } } export default GoodsPublish