cf-wx-app/src/pages/goodsPublish/goodsPublish.js

437 lines
16 KiB
JavaScript
Raw Normal View History

2018-12-06 17:24:34 +08:00
import Taro, { Component } from '@tarojs/taro'
2019-01-10 17:36:45 +08:00
import { View, Text, Button } from '@tarojs/components'
2019-01-17 17:32:38 +08:00
import { AtInput, AtImagePicker, AtTextarea, } from 'taro-ui'
2019-01-14 17:04:08 +08:00
import CopyrightComponent from '../../component/copyrightComponent/copyrightComponent'
2019-01-08 13:51:26 +08:00
import ShopTypeInteractionComp from '../../component/shopTypeInteractionComp/shopTypeInteractionComp'
import GoodsTypeInteractionComp from '../../component/goodsTypeInteractionComp/goodsTypeInteractionComp'
2018-12-06 17:24:34 +08:00
2019-02-12 17:21:31 +08:00
import LoginService from '../../LoginService'
2018-12-18 17:37:23 +08:00
import URL from '../../serviceAPI.config'
2018-12-06 17:24:34 +08:00
import './goodsPublish.scss'
class GoodsPublish extends Component {
config = {
navigationBarTitleText: '商品发布'
}
constructor() {
super(...arguments)
this.state = {
2019-01-10 17:36:45 +08:00
2019-01-08 13:51:26 +08:00
shopTypeSelected: { name: '选择店铺类型', id: '' },//已选的店铺分类
goodsTypeSelected: { name: '选择商品分类', id: '' },// 已选的商品分类
2018-12-18 17:37:23 +08:00
productName: '',
productPrice: '',
productUnit: '',
productDescript: '',
2018-12-20 17:35:16 +08:00
pickerImageUrl: [], // 上传的图片
ImagesInfo: [],// 后台传回来的图片信息
shopCategoryList: [],
productCategoryList: [],
2019-02-12 17:21:31 +08:00
isPublishAndAdd: false,// 是否点击发布新增按钮
isPublish: false, // 时候点击发布按钮
2018-12-06 17:24:34 +08:00
}
}
2018-12-18 17:37:23 +08:00
// 发布商品api
2019-01-08 13:51:26 +08:00
uploadGoods({ goods_name = "test2",
goods_price = "1.00",
goods_unit = "1", goods_profiles = "test2",
class_id = "10103",
shop_class_id = "1930" }) {
2018-12-24 17:35:51 +08:00
// 上传图片的参数
2018-12-20 17:35:16 +08:00
const uploadProductGoodFileParams = []
if (this.state.ImagesInfo.length) {
for (let i = 0; i < this.state.ImagesInfo.length; i++) {
if (i === 0) {
uploadProductGoodFileParams.push({
2018-12-24 17:35:51 +08:00
file_id: this.state.ImagesInfo[i].file_id,
2018-12-20 17:35:16 +08:00
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,
2019-01-08 13:51:26 +08:00
file_sort: i + 1
2018-12-20 17:35:16 +08:00
})
}
}
} else {
alert('图片为空')
}
2019-01-08 13:51:26 +08:00
console.log('页面图片列表', this.state.pickerImageUrl)
console.log('图片参数列表', uploadProductGoodFileParams)
2018-12-18 17:37:23 +08:00
Taro.request({
url: URL.UploadProduct,
method: 'POST',
dataType: 'json',
data: {
deployType: 1,
action: 1,
2018-12-19 17:40:32 +08:00
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
}),
2018-12-20 17:35:16 +08:00
goodsFiles: JSON.stringify(uploadProductGoodFileParams)
2018-12-18 17:37:23 +08:00
},
header: {
'content-type': 'application/x-www-form-urlencoded',
2018-12-19 17:40:32 +08:00
'Cookie': 'PFWSSS=' + Taro.getStorageSync('session_id'),
'X-Requested-With': 'XMLHttpRequest'
2018-12-18 17:37:23 +08:00
}
})
.then(res => {
2018-12-29 17:15:59 +08:00
Taro.hideLoading()
2019-01-08 13:51:26 +08:00
if (res.data.err_msg === 'success') {
2019-01-03 17:36:59 +08:00
Taro.showToast({
title: '发布成功',
icon: 'success',
duration: 1000
})
setTimeout(() => {
2019-02-12 17:21:31 +08:00
if (this.state.isPublish) {
// 导航到编辑页面
Taro.navigateTo({
url: '/pages/myGoodsEdit/myGoodsEdit?id=' + res.data.goods_id
})
2019-02-12 17:21:31 +08:00
} else if (this.state.isPublishAndAdd) {
// 导航到发布页面
Taro.navigateTo({
url: '/pages/goodsPublish/goodsPublish'
})
}
2019-01-03 17:36:59 +08:00
}, 1000);
}
2019-01-08 13:51:26 +08:00
else {
2019-01-03 17:36:59 +08:00
Taro.showToast({
title: res.data.err_msg,
icon: 'none',
duration: 1000
})
}
2019-01-08 13:51:26 +08:00
2018-12-24 17:35:51 +08:00
console.log('上传商品', res)
2018-12-18 17:37:23 +08:00
}
)
2018-12-19 17:40:32 +08:00
.catch(error => {
2018-12-29 17:15:59 +08:00
Taro.hideLoading()
Taro.showToast({
title: '保存失败',
icon: 'none',
duration: 1000
2018-12-24 17:35:51 +08:00
})
2018-12-19 17:40:32 +08:00
})
2018-12-18 17:37:23 +08:00
}
2018-12-24 17:35:51 +08:00
2018-12-19 17:40:32 +08:00
2019-01-08 13:51:26 +08:00
2019-01-10 17:36:45 +08:00
2018-12-18 17:37:23 +08:00
productNameChange(event) {
2018-12-06 17:24:34 +08:00
this.setState({
2018-12-11 17:34:06 +08:00
productName: event
2018-12-06 17:24:34 +08:00
})
}
2018-12-18 17:37:23 +08:00
productPriceChange(event) {
2018-12-11 17:34:06 +08:00
this.setState({
productPrice: event
})
}
2018-12-18 17:37:23 +08:00
productUnitChange(event) {
2018-12-11 17:34:06 +08:00
this.setState({
productUnit: event
})
}
2018-12-18 17:37:23 +08:00
productDescriptChange(event) {
2018-12-11 17:34:06 +08:00
this.setState({
productDescript: event.target.value
})
}
// 上传图片
2018-12-19 17:40:32 +08:00
onChangeImg(files, operationType, index) {
const that = this
2018-12-11 17:34:06 +08:00
if (operationType === 'add') {
2018-12-19 17:40:32 +08:00
Taro.uploadFile({
url: URL.UploadGoodsPorductImage,
2018-12-19 17:40:32 +08:00
filePath: files[files.length - 1].url,
name: 'file',
formData: {
2019-02-12 17:21:31 +08:00
2018-12-19 17:40:32 +08:00
},
header: {
'content-type': 'multipart/form-data',
'Cookie': 'PFWSSS=' + Taro.getStorageSync('session_id'),
'X-Requested-With': 'XMLHttpRequest'
},
success(response) {
2019-02-12 17:21:31 +08:00
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 {
2018-12-29 17:15:59 +08:00
Taro.showToast({
title: responseMsg,
icon: 'none',
duration: 1500
2018-12-29 17:15:59 +08:00
})
}
2018-12-19 17:40:32 +08:00
}
2018-12-11 17:34:06 +08:00
})
}
if (operationType === 'remove') {
2018-12-20 17:35:16 +08:00
this.state.pickerImageUrl.splice(index, 1);
2019-01-03 17:36:59 +08:00
this.state.ImagesInfo.splice(index, 1);
2019-01-08 13:51:26 +08:00
this.setState({ pickerImageUrl: this.state.pickerImageUrl, ImagesInfo: this.state.ImagesInfo });
2018-12-29 17:15:59 +08:00
Taro.showToast({
title: '删除成功',
icon: 'success',
duration: 2000
2018-12-24 17:35:51 +08:00
})
2018-12-11 17:34:06 +08:00
}
}
2019-01-10 17:36:45 +08:00
publishButtonHandler() {
2019-02-12 17:21:31 +08:00
if (!Taro.getStorageSync('userInfo').user_id) {
LoginService()
return
}
2019-01-08 13:51:26 +08:00
if (this.state.productName &&
this.state.productPrice &&
this.state.productUnit &&
this.state.ImagesInfo.length &&
this.state.goodsTypeSelected.id &&
this.state.shopTypeSelected.id) {
2018-12-29 17:15:59 +08:00
Taro.showLoading({ title: '发布中' }).then(() => {
2019-02-12 17:21:31 +08:00
this.setState({ isPublish: true }, () => {
2018-12-29 17:15:59 +08:00
this.uploadGoods({
goods_name: this.state.productName,
goods_price: this.state.productPrice,
goods_unit: this.state.productUnit,
goods_profiles: this.state.productDescript,
2019-01-08 13:51:26 +08:00
class_id: this.state.goodsTypeSelected.id,
shop_class_id: this.state.shopTypeSelected.id,
2018-12-29 17:15:59 +08:00
})
})
2019-02-12 17:21:31 +08:00
2019-01-08 13:51:26 +08:00
})
2018-12-29 17:15:59 +08:00
} else {
2019-01-08 13:51:26 +08:00
Taro.showToast({ title: '请填写完表格', icon: 'none' })
2018-12-19 17:40:32 +08:00
}
}
publishAndNewButton() {
2019-02-12 17:21:31 +08:00
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(() => {
2019-02-12 17:21:31 +08:00
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,
})
})
2019-02-12 17:21:31 +08:00
})
} else {
Taro.showToast({ title: '请填写完表格', icon: 'none' })
}
}
2019-02-12 17:21:31 +08:00
2019-01-08 13:51:26 +08:00
goToMyGoodListPage() {
2019-02-12 17:21:31 +08:00
console.log('list ', Taro.getStorageSync('userInfo'))
if (!Taro.getStorageSync('userInfo').user_id) {
LoginService()
return
}
Taro.navigateTo({
2019-01-08 13:51:26 +08:00
url: '/pages/myGoodList/myGoodList'
})
}
2018-12-19 17:40:32 +08:00
2019-01-08 13:51:26 +08:00
getDataFromShopChild(value) {
console.log('从子组件店铺分类传回来的值', value)
this.setState({ shopTypeSelected: value })
}
getDataFromGoodsChild(value) {
console.log('从子组件商品分类传回来的值', value)
this.setState({ goodsTypeSelected: value })
}
2019-02-12 17:21:31 +08:00
componentWillMount() {
if (!Taro.getStorageSync('userInfo').user_id) {
LoginService()
}
}
2018-12-19 17:40:32 +08:00
componentDidMount() {
2019-01-08 13:51:26 +08:00
// this.getProductCateList()
// this.getShopCateList()
2018-12-06 17:24:34 +08:00
}
2018-12-18 17:37:23 +08:00
2018-12-06 17:24:34 +08:00
componentWillReceiveProps(nextProps) {
2018-12-20 17:35:16 +08:00
// console.log(this.props, nextProps)
2018-12-06 17:24:34 +08:00
}
componentWillUnmount() { }
2019-02-12 17:21:31 +08:00
componentDidShow() {
}
2018-12-06 17:24:34 +08:00
componentDidHide() { }
2018-12-24 17:35:51 +08:00
2018-12-20 17:35:16 +08:00
2018-12-06 17:24:34 +08:00
render() {
2019-01-08 13:51:26 +08:00
2018-12-06 17:24:34 +08:00
return (
<View className='goods-publish'>
2018-12-06 17:24:34 +08:00
<View className='goods-category'>
2019-01-08 13:51:26 +08:00
{/* 商品分类开始 */}
<GoodsTypeInteractionComp
2019-01-10 17:36:45 +08:00
url={URL.GetProductCategoryList}
onPassDataToChild={this.getDataFromGoodsChild.bind(this)}
selectedValue={this.state.goodsTypeSelected}
></GoodsTypeInteractionComp>
2019-01-08 13:51:26 +08:00
{/* 商品分类结束 */}
<View className='input-box'>
<Text className='require'>*</Text>
2018-12-06 17:24:34 +08:00
<AtInput
2019-01-08 13:51:26 +08:00
name='productName'
title='商品名称:'
type='text'
value={this.state.productName}
2019-01-10 17:36:45 +08:00
border={false}
2019-01-08 13:51:26 +08:00
onChange={this.productNameChange.bind(this)}
2018-12-06 17:24:34 +08:00
/>
</View>
<View className='input-box'>
<Text className='require'>*</Text>
2018-12-18 17:37:23 +08:00
<AtInput
2019-01-08 13:51:26 +08:00
name='productPrice'
title='商品价格:'
type='number'
placeholder='¥'
value={this.state.productPrice}
2019-01-10 17:36:45 +08:00
border={false}
2019-01-08 13:51:26 +08:00
onChange={this.productPriceChange.bind(this)}
2018-12-18 17:37:23 +08:00
/>
</View>
<View className='input-box'>
<Text className='require'>*</Text>
2018-12-18 17:37:23 +08:00
<AtInput
2019-01-08 13:51:26 +08:00
name='productUnit'
title='商品单位:'
2019-01-10 17:36:45 +08:00
type='number'
2019-01-08 13:51:26 +08:00
value={this.state.productUnit}
2019-01-10 17:36:45 +08:00
border={false}
2019-01-08 13:51:26 +08:00
onChange={this.productUnitChange.bind(this)}
2018-12-18 17:37:23 +08:00
/>
</View>
2018-12-11 17:34:06 +08:00
{/* 图片上传 */}
2018-12-06 17:24:34 +08:00
<View className='img-box'>
<View className='title-box'>
<Text className='require'>*</Text>
2018-12-06 17:24:34 +08:00
<Text className='title'>上传图片:</Text>
</View>
<View className='img-container'>
<AtImagePicker
2019-01-08 13:51:26 +08:00
multiple
files={this.state.pickerImageUrl}
onChange={this.onChangeImg.bind(this)}
onFail={this.onFail.bind(this)}
onImageClick={this.onImageClick.bind(this)}
2018-12-06 17:24:34 +08:00
/>
</View>
</View>
2019-01-08 13:51:26 +08:00
{/* 店铺分类 */}
<ShopTypeInteractionComp url={URL.GetShopCategoryList}
2019-02-12 17:21:31 +08:00
shopId={Taro.getStorageSync('shopInfo').shop_id}
selectedValue={this.state.shopTypeSelected}
2019-01-08 13:51:26 +08:00
onPassDataToChild={this.getDataFromShopChild.bind(this)}
></ShopTypeInteractionComp>
{/* 店铺分类结束 */}
2018-12-18 17:37:23 +08:00
2018-12-20 17:35:16 +08:00
2018-12-06 17:24:34 +08:00
<View className='description-box'>
<View className='title-box'>
2018-12-11 17:34:06 +08:00
<Text className='require'></Text>
2018-12-06 17:24:34 +08:00
<Text className='title'>商品简介:</Text>
</View>
<AtTextarea
2019-01-08 13:51:26 +08:00
value={this.state.productDescript}
onChange={this.productDescriptChange.bind(this)}
maxlength='200'
placeholder='你的产品简介'
2018-12-06 17:24:34 +08:00
/>
</View>
</View>
2018-12-24 17:35:51 +08:00
<View className='button-box' >
<View className='button' >
<Button size='mini' className='button-orange' onClick={this.publishButtonHandler.bind(this)}>发布</Button>
2018-12-06 17:24:34 +08:00
</View>
2018-12-24 17:35:51 +08:00
{/* <View className='button'>
2019-01-10 17:36:45 +08:00
<Button type='primary' className='button-a' size='small' >发布并新增</Button>
2018-12-24 17:35:51 +08:00
</View> */}
<View className='button' onClick={this.publishAndNewButton.bind(this)}>
<Button size='mini' className='button-green'>发布并新增</Button>
</View>
<View className='button' onClick={this.goToMyGoodListPage.bind(this)}>
2019-01-10 17:36:45 +08:00
<Button size='mini' className='button-green'>商品列表</Button>
2018-12-06 17:24:34 +08:00
</View>
</View>
2019-01-14 17:04:08 +08:00
<CopyrightComponent></CopyrightComponent>
2018-12-06 17:24:34 +08:00
</View>
)
}
}
export default GoodsPublish