import Taro, { Component } from '@tarojs/taro' import { View, Text, Button } from '@tarojs/components' import { AtInput, AtImagePicker, AtTextarea, } from 'taro-ui' import CopyrightComponent from '../../component/copyrightComponent/copyrightComponent' import ShopTypeInteractionComp from '../../component/shopTypeInteractionComp/shopTypeInteractionComp' import GoodsTypeInteractionComp from '../../component/goodsTypeInteractionComp/goodsTypeInteractionComp' import LoginService from '../../LoginService' import URL from '../../serviceAPI.config' import './goodsPublish.scss' class GoodsPublish extends Component { config = { navigationBarTitleText: '商品发布' } constructor() { super(...arguments) this.state = { shopTypeSelected: { name: '选择店铺类型', id: '' },//已选的店铺分类 goodsTypeSelected: { name: '选择商品分类', id: '' },// 已选的商品分类 productName: '', productPrice: '', productUnit: '', productDescript: '', pickerImageUrl: [], // 上传的图片 ImagesInfo: [],// 后台传回来的图片信息 shopCategoryList: [], productCategoryList: [], isPublishAndAdd: false,// 是否点击发布新增按钮 isPublish: false, // 时候点击发布按钮 } } // 发布商品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(() => { if (this.state.isPublish) { // 导航到编辑页面 Taro.navigateTo({ url: '/pages/myGoodsEdit/myGoodsEdit?id=' + res.data.goods_id }) } else if (this.state.isPublishAndAdd) { // 导航到发布页面 Taro.navigateTo({ url: '/pages/goodsPublish/goodsPublish' }) } }, 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 }) }) } 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: { }, header: { 'content-type': 'multipart/form-data', 'Cookie': 'PFWSSS=' + Taro.getStorageSync('session_id'), 'X-Requested-With': 'XMLHttpRequest' }, success(response) { const responseMsg = JSON.parse(response.data).err_msg if (responseMsg === 'success' || responseMsg === 'duplicate') { 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: 2000 }) }) } else { Taro.showToast({ title: responseMsg, icon: 'none', duration: 1500 }) } } }) } 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 }) } } publishButtonHandler() { if (!Taro.getStorageSync('userInfo').user_id) { LoginService() return } 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(() => { this.setState({ isPublish: true }, () => { 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, }) }) }) } else { Taro.showToast({ title: '请填写完表格', icon: 'none' }) } } publishAndNewButton() { if (!Taro.getStorageSync('userInfo').user_id) { LoginService() return } 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(() => { this.setState({ isPublishAndAdd: true }, () => { 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, }) }) }) } else { Taro.showToast({ title: '请填写完表格', icon: 'none' }) } } goToMyGoodListPage() { console.log('list ', Taro.getStorageSync('userInfo')) if (!Taro.getStorageSync('userInfo').user_id) { LoginService() return } Taro.navigateTo({ url: '/pages/myGoodList/myGoodList' }) } getDataFromShopChild(value) { console.log('从子组件店铺分类传回来的值', value) this.setState({ shopTypeSelected: value }) } getDataFromGoodsChild(value) { console.log('从子组件商品分类传回来的值', value) this.setState({ goodsTypeSelected: value }) } componentWillMount() { if (!Taro.getStorageSync('userInfo').user_id) { LoginService() } } componentDidMount() { // this.getProductCateList() // this.getShopCateList() } componentWillReceiveProps(nextProps) { // console.log(this.props, nextProps) } componentWillUnmount() { } componentDidShow() { } componentDidHide() { } render() { return ( {/* 商品分类开始 */} {/* 商品分类结束 */} * * * {/* 图片上传 */} * 上传图片: {/* 店铺分类 */} {/* 店铺分类结束 */} 商品简介: {/* */} ) } } export default GoodsPublish