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

592 lines
23 KiB
JavaScript
Raw Normal View History

2018-12-06 17:24:34 +08:00
import Taro, { Component } from '@tarojs/taro'
2018-12-18 17:37:23 +08:00
import { View, Text, Radio, RadioGroup, Label } from '@tarojs/components'
import { AtInput, AtImagePicker, AtTextarea, AtButton, Picker, AtToast } from 'taro-ui'
2018-12-06 17:24:34 +08:00
2018-12-18 17:37:23 +08:00
import URL from '../../serviceAPI.config'
2018-12-06 17:24:34 +08:00
import './goodsPublish.scss'
2018-12-20 17:35:16 +08:00
import { throws } from 'assert';
2018-12-06 17:24:34 +08:00
class GoodsPublish extends Component {
config = {
navigationBarTitleText: '商品发布'
}
constructor() {
super(...arguments)
this.state = {
2018-12-24 17:35:51 +08:00
2018-12-20 17:35:16 +08:00
objectMultiArray: [
2018-12-24 17:35:51 +08:00
2018-12-20 17:35:16 +08:00
],
multiIndex: [0, 0, 0],
shopCategoryPicker: [], // 店铺分类选项
2018-12-24 17:35:51 +08:00
shopCategoryCheckedPicker: { name: '选择店铺类型' },
2018-12-18 17:37:23 +08:00
productName: '',
productPrice: '',
productUnit: '',
productDescript: '',
2018-12-20 17:35:16 +08:00
pickerImageUrl: [], // 上传的图片
ImagesInfo: [],// 后台传回来的图片信息
2018-12-19 17:40:32 +08:00
isUploadImageSuccess: false,
uploadImageTextTip: '',
isUploadProductSuccess: false,
uploadProductTextTip: '',
2018-12-20 17:35:16 +08:00
shopCategoryList: [],
productCategoryList: [],
2018-12-18 17:37:23 +08:00
2018-12-06 17:24:34 +08:00
}
}
2018-12-18 17:37:23 +08:00
2018-12-20 17:35:16 +08:00
//商品目录请求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
getShopCateList() {
Taro.request({
url: URL.GetShopCategoryList,
method: 'POST',
dataType: 'json',
data: {
id: Taro.getStorageSync('shopInfo').shop_id
},
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({ shopCategoryList: res.data.data })
const shopCategoryInfo = res.data.data
const shopCategory = []
// 处理后台返回的店铺分类信息
for (let item in shopCategoryInfo) {
const children = shopCategoryInfo[item].c
for (let child in children) {
shopCategory.push({ id: children[child].id, name: children[child].n })
}
}
this.setState({ shopCategoryPicker: shopCategory })
console.log('店铺分类目录', res)
}
}
)
.catch(error => {
console.log('店铺分类请求错误', error)
})
}
2018-12-18 17:37:23 +08:00
// 发布商品api
2018-12-29 17:15:59 +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,
file_sort: 1
})
}
}
} else {
alert('图片为空')
}
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()
Taro.showToast({
title: '发布成功',
icon: 'success',
duration: 1000
2018-12-24 17:35:51 +08:00
})
2018-12-29 17:15:59 +08:00
setTimeout(() => {
2019-01-02 17:31:07 +08:00
Taro.navigateTo({
url: '/pages/goods/goods?id='+res.data.goods_id
})
2018-12-29 17:15:59 +08:00
}, 1000);
2019-01-02 17:31:07 +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-20 17:35:16 +08:00
// 整理后台传出来的店铺分类目录
sortShopCateToState(shopData) {
const firstColumn = []
const secondColumn = []
const thirdColumn = []
for (let outterItem of shopData) {
2018-12-24 17:35:51 +08:00
firstColumn.push({ id: outterItem.class_id, class_name: outterItem.class_name })
2018-12-20 17:35:16 +08:00
if (outterItem.children.length) {
for (let middleItem of outterItem.children) {
2018-12-24 17:35:51 +08:00
secondColumn.push({ id: middleItem.class_id, class_name: middleItem.class_name })
2018-12-20 17:35:16 +08:00
if (middleItem.children.length) {
for (let innerItem of middleItem.children) {
2018-12-24 17:35:51 +08:00
thirdColumn.push({ id: innerItem.class_id, class_name: innerItem.class_name })
2018-12-20 17:35:16 +08:00
}
}
}
}
}
//------- 把新指传给objectMultiArray之后就不可以用了 to be continue
2018-12-24 17:35:51 +08:00
this.setState({ objectMultiArray: [firstColumn, secondColumn, thirdColumn] }, () => {
console.log('new', this.state.objectMultiArray)
})
2018-12-20 17:35:16 +08:00
}
2018-12-19 17:40:32 +08:00
2018-12-11 17:34:06 +08:00
// 改变商品分类状态
2018-12-19 17:40:32 +08:00
onChangeProductType(e) {
this.setState({
selectorChecked: this.state.selector[e.detail.value]
})
}
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: {
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
2018-12-20 17:35:16 +08:00
const newPickerImageUrl = that.state.pickerImageUrl.concat({ url: imagePath })
const newImageInfo = that.state.ImagesInfo.concat(data)
2018-12-29 17:15:59 +08:00
that.setState({ pickerImageUrl: newPickerImageUrl, ImagesInfo: newImageInfo }, () => {
Taro.showToast({
title: '上传成功',
icon: 'success',
duration: 2000
})
2018-12-20 17:35:16 +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);
this.setState({ files: this.state.pickerImageUrl });
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
}
}
// 图片上传失败
2018-12-06 17:24:34 +08:00
onFail(mes) {
console.log(mes)
}
2018-12-11 17:34:06 +08:00
// 删除图片
onImageClick(index) {
2018-12-18 17:37:23 +08:00
}
2018-12-19 17:40:32 +08:00
onClickUploadGoods() {
2018-12-24 17:35:51 +08:00
if (this.state.productName && this.state.productPrice && this.state.productUnit && this.state.ImagesInfo.length && this.state.shopCategoryCheckedPicker.id) {
2018-12-29 17:15:59 +08:00
Taro.showLoading({ title: '发布中' }).then(() => {
2018-12-24 17:35:51 +08:00
setTimeout(() => {
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-02 17:31:07 +08:00
class_id: "10103",// to be continue //商品分类的选项
2018-12-29 17:15:59 +08:00
shop_class_id: this.state.shopCategoryCheckedPicker.id,
})
},1000)})
} else {
Taro.showToast({title:'请填写完表格',icon:'none'})
2018-12-19 17:40:32 +08:00
}
}
2018-12-20 17:35:16 +08:00
shopCategoryChanged(e) {
this.setState({
shopCategoryCheckedPicker: this.state.shopCategoryPicker[e.detail.value]
}, () => {
console.log(this.state.shopCategoryCheckedPicker)
})
}
goToMyGoodListPage(){
Taro.navigateTo({
url: '/pages/myGoodList/myGoodList'
})
}
2018-12-19 17:40:32 +08:00
componentDidMount() {
2018-12-20 17:35:16 +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() { }
componentDidShow() { }
componentDidHide() { }
2018-12-20 17:35:16 +08:00
//--- 三级联动--------------
bindMultiPickerChange(e) {
console.log('picker发送选择改变携带值为', e.detail.value)
this.setState({
multiIndex: e.detail.value
})
}
bindMultiPickerCol(e) {
console.log('修改的列为', e.detail.column, ',值为', e.detail.value)
const data = {
multiArray: this.state.objectMultiArray,
multiIndex: this.state.multiIndex
}
data.multiIndex[e.detail.column] = e.detail.value
switch (e.detail.column) {
case 0:
switch (data.multiIndex[0]) {
case 0:
2018-12-24 17:35:51 +08:00
data.multiArray[1] = this.state.productCategoryList[0].children ? this.state.productCategoryList[0].children : [{ class_name: '' }]
data.multiArray[2] = this.state.productCategoryList[0].children ? this.state.productCategoryList[0].children : [{ class_name: '' }]
2018-12-20 17:35:16 +08:00
break
case 1:
2018-12-24 17:35:51 +08:00
data.multiArray[1] = this.state.productCategoryList[1].children
break
case 2:
data.multiArray[1] = this.state.productCategoryList[2].children
break
case 3:
data.multiArray[1] = this.state.productCategoryList[3].children
break
case 4:
data.multiArray[1] = this.state.productCategoryList[4].children
2018-12-20 17:35:16 +08:00
break
}
data.multiIndex[1] = 0
data.multiIndex[2] = 0
break
case 1:
switch (data.multiIndex[0]) {
case 0:
2018-12-24 17:35:51 +08:00
break
case 1:
2018-12-20 17:35:16 +08:00
switch (data.multiIndex[1]) {
case 0:
2018-12-24 17:35:51 +08:00
data.multiArray[2] = this.state.productCategoryList[1].children[0].children
2018-12-20 17:35:16 +08:00
break
case 1:
2018-12-24 17:35:51 +08:00
data.multiArray[2] = this.state.productCategoryList[1].children[1].children
2018-12-20 17:35:16 +08:00
break
case 2:
2018-12-24 17:35:51 +08:00
data.multiArray[2] = this.state.productCategoryList[1].children[2].children
2018-12-20 17:35:16 +08:00
break
case 3:
2018-12-24 17:35:51 +08:00
data.multiArray[2] = this.state.productCategoryList[1].children[3].children ? this.state.productCategoryList[1].children[3].children : [{ class_name: '' }]
break
}
break
case 2:
switch (data.multiIndex[1]) {
case 0:
data.multiArray[2] = this.state.productCategoryList[2].children[0].children
break
case 1:
data.multiArray[2] = this.state.productCategoryList[2].children[1].children
break
case 2:
data.multiArray[2] = this.state.productCategoryList[2].children[2].children
break
case 3:
data.multiArray[2] = this.state.productCategoryList[2].children[3].children
2018-12-20 17:35:16 +08:00
break
case 4:
2018-12-24 17:35:51 +08:00
data.multiArray[2] = this.state.productCategoryList[2].children[4].children
break
case 5:
data.multiArray[2] = this.state.productCategoryList[2].children[5].children
2018-12-20 17:35:16 +08:00
break
}
break
2018-12-24 17:35:51 +08:00
case 3:
switch (data.multiIndex[1]) {
case 0:
data.multiArray[2] = this.state.productCategoryList[3].children[0].children
break
case 1:
data.multiArray[2] = this.state.productCategoryList[3].children[1].children
break
case 2:
data.multiArray[2] = this.state.productCategoryList[3].children[2].children
break
case 3:
data.multiArray[2] = this.state.productCategoryList[3].children[3].children ? this.state.productCategoryList[3].children[3].children : [{ class_name: '' }]
break
case 3:
data.multiArray[2] = this.state.productCategoryList[3].children[3].children ? this.state.productCategoryList[3].children[3].children : [{ class_name: '' }]
break
}
break
case 4:
2018-12-20 17:35:16 +08:00
switch (data.multiIndex[1]) {
case 0:
2018-12-24 17:35:51 +08:00
data.multiArray[2] = this.state.productCategoryList[4].children[0].children
2018-12-20 17:35:16 +08:00
break
case 1:
2018-12-24 17:35:51 +08:00
data.multiArray[2] = this.state.productCategoryList[4].children[1].children
2018-12-20 17:35:16 +08:00
break
case 2:
2018-12-24 17:35:51 +08:00
data.multiArray[2] = this.state.productCategoryList[4].children[2].children
2018-12-20 17:35:16 +08:00
break
2018-12-24 17:35:51 +08:00
2018-12-20 17:35:16 +08:00
}
break
2018-12-24 17:35:51 +08:00
2018-12-20 17:35:16 +08:00
}
data.multiIndex[2] = 0
break
}
this.setState({ multiIndex: data.multiIndex })
}
2018-12-06 17:24:34 +08:00
render() {
2018-12-29 17:15:59 +08:00
2018-12-06 17:24:34 +08:00
return (
<View className='goods-publish'>
2018-12-29 17:15:59 +08:00
2018-12-06 17:24:34 +08:00
<View className='goods-category'>
<View className='page-section'>
<View>
2018-12-24 17:35:51 +08:00
<Picker
range={this.state.objectMultiArray}
onChange={this.bindMultiPickerChange.bind(this)}
rangeKey='class_name'
mode='multiSelector'
onColumnchange={this.bindMultiPickerCol.bind(this)}
value={this.state.multiIndex}
2018-12-20 17:35:16 +08:00
>
<View className='picker'>
<View className='title-box'>
2018-12-20 17:35:16 +08:00
<Text className='title'><Text className='require'>*</Text>:</Text> <Text className='selected'>
{this.state.objectMultiArray[0]?this.state.objectMultiArray[0][this.state.multiIndex[0]].class_name:''}
&{this.state.objectMultiArray[1]?this.state.objectMultiArray[1][this.state.multiIndex[1]].class_name:''}
&{this.state.objectMultiArray[2]?this.state.objectMultiArray[2][this.state.multiIndex[2]].class_name:''}
2018-12-20 17:35:16 +08:00
</Text>
</View>
</View>
</Picker>
</View>
</View>
<View className='input-box'>
<Text className='require'>*</Text>
2018-12-06 17:24:34 +08:00
<AtInput
name='productName'
title='商品名称:'
type='text'
value={this.state.productName}
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
name='productPrice'
title='商品价格:'
type='number'
placeholder='¥'
value={this.state.productPrice}
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
name='productUnit'
title='商品单位:'
type='text'
value={this.state.productUnit}
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
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>
<View className='shoptype-box'>
2018-12-18 17:37:23 +08:00
{/* 店铺分类 */}
2018-12-20 17:35:16 +08:00
<View className='title-box page-section'>
<Picker mode='selector' rangeKey='name' range={this.state.shopCategoryPicker} onChange={this.shopCategoryChanged.bind(this)}>
<View className='picker'>
<Text className='require'>*</Text>
<Text className='title'> 店铺分类</Text>
<Text className='selected'>{this.state.shopCategoryCheckedPicker.name}</Text>
2018-12-06 17:24:34 +08:00
2018-12-20 17:35:16 +08:00
</View>
</Picker>
2018-12-06 17:24:34 +08:00
</View>
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>
2018-12-18 17:37:23 +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>
2018-12-18 17:37:23 +08:00
2018-12-06 17:24:34 +08:00
<AtTextarea
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' >
2018-12-19 17:40:32 +08:00
<AtButton type='primary' size='small' onClick={this.onClickUploadGoods.bind(this)}>发布</AtButton>
2018-12-06 17:24:34 +08:00
</View>
2018-12-24 17:35:51 +08:00
{/* <View className='button'>
<AtButton type='primary' className='button-a' size='small' >发布并新增</AtButton>
</View> */}
<View className='button' onClick={this.goToMyGoodListPage.bind(this)}>
2018-12-18 17:37:23 +08:00
<AtButton type='primary' className='button-a' size='small'>商品列表</AtButton>
2018-12-06 17:24:34 +08:00
</View>
2018-12-18 17:37:23 +08:00
2018-12-06 17:24:34 +08:00
</View>
<copyrightComponent></copyrightComponent>
2018-12-06 17:24:34 +08:00
</View>
)
}
}
export default GoodsPublish