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

763 lines
29 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import Taro, { Component } from '@tarojs/taro'
import { View, Button, Text, Image } from '@tarojs/components'
import { AtTag, AtIcon, } from 'taro-ui'
import URL from '../../serviceAPI.config'
import './shop.scss'
import ShopItem from '../../component/shopItemComponent/shopItemComponent'
import BottomNav from '../../component/bottomNav/bottomNav'
import CopyrightComponent from '../../component/copyrightComponent/copyrightComponent'
let loadMorePageIndex = 1 // 底部加载接口请求参数
class Shop extends Component {
// 项目配置
config = {
navigationBarTitleText: '店铺'
}
constructor() {
super(...arguments)
this.state = {
isShopDetailsOn: false, // 是否显示店铺说明页面
shopItem: [], // 所有商品
value: '', // 搜索框的值
shopId: '', // 店铺的id
shopName: '', // 店铺名
FilterText: '', // 筛选的可选项
filterBar: ['all', 'amount', 'newProduct', 'price', 'popularity'], //筛选选项
filterBarKeys: { all: '综合排序', amount: '销量', newProduct: '新品', price: '价格', popularity: '人气' }, // 筛选选项对应值
selectedFilterValue: 0, //筛选项
isShowFilter: false, //是否显示侧边筛选
showShopHomePage: true,// 是否显示首页页面
shopDescriptionData: '',// 店铺详情信息
shopName: '',//店铺名称
shopAddress: '',//店铺地址
contactName: '',//联系人
contactNumber: '',//联系电话
shopDescription: '',//店铺简介
filterOptions: {
all: true,
amount: false,
newProduct: false,
price: false,
popularity: false,
},
filterCondition: '',// 筛选条件
isShowShopAllCate: false,
shopAllInnerCate: '',// 店铺内部分类
isBlurWindow: false,
mainType: [],// 侧边筛选分类
goodType: [],//侧边商品类型
otherType: [],//侧边其他类型
widthType: [],// 侧边宽度类型
checkedFilterIdList: [],//已选的筛选id
isAddToList: false,// 请求店铺商品的时候是否添加到旧列表里
// 下面是函数的默认参数
curr_page: 1,
page_count: 10,
shop_name: false,
shop_id: this.$router.params.id,
config_id: 4,
shop_class_id: '',
order: '',
goods_class_id: '',
goodsSpec: [],
goodsParam: [],
goodsParamExt: [],
}
}
// api 得到筛选的标签请求
getSearchParams({
shop_name = this.state.shop_name,
shop_id = this.state.shop_id,
shop_class_id = this.state.shop_class_id,
goods_class_id = this.state.goods_class_id,
class_filter = this.state.class_filter,
goods_type = this.state.goods_type,
goodsSpec = this.state.goodsSpec,
goodsParam = this.state.goodsParam,
goodsParamExt = this.state.goodsParamExt }) {
Taro.request({
url: URL.GetSearchParam,
method: 'POST',
dataType: 'json',
data: {
goods: JSON.stringify({
shop_name: shop_name,
shop_id: shop_id,
shop_class_id: shop_class_id,
goods_class_id: goods_class_id,
class_filter: class_filter,
goods_type: goods_type,
}),
goodsSpec: JSON.stringify(goodsSpec),
goodsParam: JSON.stringify(goodsParam),
goodsParamExt: JSON.stringify(goodsParamExt),
},
header: {
'content-type': 'application/x-www-form-urlencoded',
'X-Requested-With': 'XMLHttpRequest'
}
})
.then(res => {
if (res.statusCode === 200) {
console.log('筛选项目成功', res)
Taro.hideLoading()
this.formatFilterData(res.data).then(data => {
this.setState({
sideFilterdata: data,
mainType: data.mainType || [],
goodType: data.goodType || [],
otherType: data.otherType || [],
widthType: data.widthType || [],
})
console.log('formated data', data)
})
} else {
console.log('筛选项目获取失败')
}
})
}
async formatFilterData(data) {
const formatedFilterOptions = {}
//侧边筛选分类
if (data.goods_class) {
let typeArray = []
data.goods_class.forEach(item => {
typeArray.push({ id: item.class_id, name: item.class_name, checked: false })
});
formatedFilterOptions.mainType = typeArray
}
//侧边筛选商品类型
if (data.goods_type) {
// console.log('数据',data.goods_type)
let goodTypeArray = []
Object.keys(data.goods_type).forEach(key => {
goodTypeArray.push({ id: data.goods_type[key].goods_type_id, name: data.goods_type[key].goods_type_ch_name, checked: false })
});
formatedFilterOptions.goodType = goodTypeArray
}
//侧边筛选其他类型
if (data.goodsParam) {
let goodsParamArray = []
Object.keys(data.goodsParam).forEach(key => {
const value = data.goodsParam[key]
const subArray = value.param_value.map(item => {
return { id: value.param_id, name: item, checked: false }
})
goodsParamArray.push({ [value.param_name]: subArray })
});
formatedFilterOptions.otherType = goodsParamArray
}
//侧边筛选宽度类型
if (data.goodsParamExt) {
let goodsParamExtArray = []
Object.keys(data.goodsParamExt).forEach(key => {
const value = data.goodsParamExt[key]
const keyId = value.param_id
Object.keys(value.param_value).forEach(item => {
const name = value.param_value[item].value_desc
const value1 = value.param_value[item]
goodsParamExtArray.push({ name: name, value: value1, checked: false, id: keyId })
});
});
formatedFilterOptions.widthType = goodsParamExtArray
}
return formatedFilterOptions
}
// api 得到所有的产品请求
goodsSearch({
curr_page = this.state.curr_page,
page_count = this.state.page_count,
shop_name = this.state.shop_name,
shop_id = this.state.shop_id,
config_id = this.state.config_id,
shop_class_id = this.state.shop_class_id,
order = this.state.order,
goods_class_id = this.state.goods_class_id,
goodsSpec = this.state.goodsSpec,
goodsParam = this.state.goodsParam,
goodsParamExt = this.state.goodsParamExt }) {
Taro.request({
url: URL.GoodsSearch,
method: 'POST',
dataType: 'json',
data: {
goods: JSON.stringify({
curr_page: curr_page,
page_count: page_count,
shop_name: shop_name,
shop_id: shop_id,
config_id: config_id,
shop_class_id: shop_class_id,
order: order,
goods_class_id: goods_class_id,
}),
goodsRegion: JSON.stringify({}),
goodsSpec: JSON.stringify(goodsSpec),
goodsParam: JSON.stringify(goodsParam),
goodsParamExt: JSON.stringify(goodsParamExt),
},
header: {
'content-type': 'application/x-www-form-urlencoded',
'X-Requested-With': 'XMLHttpRequest'
}
})
.then(res => {
console.log('searchGood', res)
Taro.hideLoading()
if (res.data.goods.length) {
if (this.state.isAddToList) {
this.setState({
shopItem: this.state.shopItem.concat(res.data.goods)
}, () => {
this.setState({ isAddToList: false })
})
} else {
this.setState({ shopItem: res.data.goods, filterCondition: order })
}
} else {
Taro.showToast({
title: '没有更多了',
icon: 'none'
})
}
})
}
// api 得到店铺详情请求
getShopDescription({ shopID = 808 }) {
Taro.request({
url: URL.ShopDescription,
method: 'POST',
dataType: 'json',
data: {
shopID: shopID,
},
header: {
'content-type': 'application/x-www-form-urlencoded',
}
})
.then(res => {
Taro.hideLoading()
this.setState({
shopDescriptionData: res.data,
shopName: res.data.data.shop_name,
shopAddress: res.data.data.shop_address,
contactName: res.data.userRes.name,
contactNumber: res.data.userRes.phone,
shopDescription: res.data.data.shop_desc
}, () => {
// console.log(this.state.shopDescriptionData)
})
}
)
}
// api 获取店铺内的店铺分类请求 GetShopCategoryList
getShopInnerCate({ id = 808 }) {
Taro.request({
url: URL.GetShopCategoryList,
method: 'POST',
dataType: 'json',
data: {
id: id,
},
header: {
'content-type': 'application/x-www-form-urlencoded',
'X-Requested-With': 'XMLHttpRequest'
}
})
.then(res => {
if (res.statusCode === 200) {
Taro.hideLoading()
const originalData = res.data.data
const sortedData = []
for (let item in originalData) {
let each = originalData[item]
each['id'] = item
sortedData.push(each)
}
// console.log('sorteddata', sortedData)
this.setState({ shopAllInnerCate: sortedData })
} else {
console.log('获取店铺内部分类失败')
}
}
)
}
// 产品排序
accendingDescending(value) {
Taro.showLoading({ title: '加载中' })
this.setState({ selectedFilterValue: value })
if (value == 0) {
this.setState({
currentPage: 1,
filterOptions: {
all: true,
amount: false,
newProduct: false,
price: false,
popularity: false
}
}
)
this.goodsSearch({})
}
if (value == 1) {
this.setState({
currentPage: 1,
filterOptions: {
all: false,
amount: !this.state.filterOptions.amount,
newProduct: false,
price: false,
popularity: false
}
}, () => {
this.state.filterOptions.amount ? this.goodsSearch({ order: "g.sales_volume desc" }) : this.goodsSearch({ order: "g.sales_volume" })
}
)
}
if (value == 2) {
this.setState({
currentPage: 1,
filterOptions: {
all: false,
amount: false,
newProduct: !this.state.filterOptions.newProduct,
price: false,
popularity: false
}
}, () => {
this.state.filterOptions.newProduct ? this.goodsSearch({ order: "g.create_date desc" }) : this.goodsSearch({ order: "g.create_date" })
}
)
}
if (value == 3) {
this.setState({
currentPage: 1,
filterOptions: {
all: false,
amount: false,
newProduct: false,
price: !this.state.filterOptions.price,
popularity: false
}
}, () => {
this.state.filterOptions.price ? this.goodsSearch({ order: "g.goods_price desc" }) : this.goodsSearch({ order: "g.goods_price" })
}
)
}
if (value == 4) {
this.setState({
currentPage: 1,
filterOptions: {
all: false,
amount: false,
newProduct: false,
price: false,
popularity: !this.state.filterOptions.popularity
}
}, () => {
this.state.filterOptions.popularity ? this.goodsSearch({ order: "g.browse_times desc", }) : this.goodsSearch({ order: "g.browse_times" })
}
)
}
}
// 是否显示侧边筛选
showAndHideFilter() {
this.setState({ isShowFilter: !this.state.isShowFilter })
}
// 选择侧边筛选的标签
selectTag(name) {
const id = name.name
Taro.showLoading({ title: '加载中' })
// 处理分类筛选项
const newMainType = this.state.mainType.map((item) => {
if (item.id === id) {
item.checked = !item.checked
setTimeout(() => {
this.setState({ goods_class_id: id, checkedFilterIdList: this.state.checkedFilterIdList.concat([item]) }, () => {
this.getSearchParams({ goods_class_id: this.state.goods_class_id })
this.goodsSearch({ goods_class_id: this.state.goods_class_id })
})
}, 1000);
}
return item
})
// 处理商品类型筛选项
const newGoodType = this.state.goodType.map((item) => {
if (item.id === id) {
item.checked = !item.checked
setTimeout(() => {
this.setState({ goods_type: id, checkedFilterIdList: this.state.checkedFilterIdList.concat([item]) }, () => {
this.getSearchParams({ goods_type: this.state.goods_type })
this.goodsSearch({ goods_type: this.state.goods_type })
})
}, 1000);
}
return item
})
// 处理其他筛选项
const newOtherType = this.state.otherType.map(item => {
const value = Object.values(item)[0]
for (let each in value) {
if (value[each].name === id) {
value[each].checked = !value[each].checked
setTimeout(() => {
this.setState({ checkedFilterIdList: this.state.checkedFilterIdList.concat(value[each]), goodsParam: [{ param_id: value[each].name.id, param_value: value[each].name }] }, () => {
this.getSearchParams({ goodsParam: this.state.goodsParam })
this.goodsSearch({ goodsParam: this.state.goodsParam })
})
}, 1000);
}
}
return item
})
// 处理宽度筛选项
const newWidthType = this.state.widthType.map((item) => {
if (item.name === id) {
item.checked = !item.checked
setTimeout(() => {
this.setState({ checkedFilterIdList: this.state.checkedFilterIdList.concat(item), goodsParamExt: [{ param_id: item.id, param_ext: [item.value] }] }, () => {
this.getSearchParams({ goodsParamExt: this.state.goodsParamExt })
this.goodsSearch({ goodsParamExt: this.state.goodsParamExt })
})
}, 1000);
}
return item
})
this.setState({ mainType: newMainType, goodType: newGoodType, widthType: newWidthType, otherType: newOtherType }, () => {
// console.log(this.state.mainType)
})
}
handleOnPageChange(value) {
Taro.showLoading({ title: '加载中' })
this.goodsSearch({ curr_page: value.current, order: this.state.filterCondition })
}
showHomePage() {
this.setState({ showShopHomePage: true })
}
showDescriptionPage() {
this.setState({ showShopHomePage: false })
}
isShowShopAllCate() {
this.setState({ isShowShopAllCate: !this.state.isShowShopAllCate, isBlurWindow: true })
}
handleShopInnerCate(id) {
Taro.showLoading({ title: '加载中' })
this.setState({
isShowShopAllCate: false, isBlurWindow: false
})
this.goodsSearch({ shop_class_id: id, shop_id: this.$router.params.id })
}
// 确认筛选
submitFilter() {
this.setState({ isShowFilter: false })
}
//重置按键筛选
resetFilterList() {
Taro.showLoading({ title: '加载中' })
this.setState({
checkedFilterIdList: [], curr_page: 1,
page_count: 10,
shop_name: false,
shop_id: 1305,
config_id: 4,
shop_class_id: '',
order: '',
currPage: 1,
goods_class_id: '',
goodsSpec: [],
goodsParam: [],
goodsParamExt: [],
}, () => {
this.getSearchParams({})
this.goodsSearch({})
})
}
// 关闭背景window
closeBgWindow() {
this.setState({
isShowShopAllCate: false, isBlurWindow: false
})
}
componentWillMount() {
}
componentDidMount() {
Taro.showLoading({ title: '加载中' })
console.log('this.$router.params.id', this.$router.params.id)
//页面加载之后 得到指定店铺的商品 和 筛选标签
this.goodsSearch({ shop_id: this.$router.params.id }) // 加载店铺商品
this.getSearchParams({})// 加载筛选项
this.getShopDescription({ shopID: this.$router.params.id }) // 加载店铺说明
// this.getSearchBarkeyWords()
this.getShopInnerCate({ id: this.$router.params.id })
}
componentDidShow() { }
componentDidHide() { }
// 底部加载
onReachBottom() {
Taro.showLoading({
title: '加载中'
})
loadMorePageIndex += 1
this.setState({ isAddToList: true }, () => {
this.goodsSearch({ curr_page: loadMorePageIndex })
})
}
render() {
const ShopItemElementsArray = this.state.shopItem.length ? this.state.shopItem.map((item, index) => {
return <View key={index} className='shop-item' >
<ShopItem item={item}></ShopItem>
</View>
}) : null
const filterElementsArray = this.state.filterBar.map((item, index) => {
let isTure = this.state.filterOptions[item]
return <View key={index} onClick={this.accendingDescending.bind(this, index)} className={index === this.state.selectedFilterValue ? 'filter-title actived' : 'filter-title'}>
<Text className='text'>
{this.state.filterBarKeys[item]}
</Text>
{index !== 0 && index !== 5 && (isTure ? <AtIcon value='chevron-down' size='10' color='#F00'></AtIcon> : <AtIcon value='chevron-up' size='10' color='#F00'></AtIcon>)}
</View>
})
// 侧边已选项
const checkedFilterElementsArray = this.state.checkedFilterIdList.map((item, index) => {
return <AtTag style='margin-left:5px' key={index}
name={item.id}
type='primary'
active={item.checked}
//onClick={this.selectTag.bind(this)}
>{item.name}</AtTag>
})
// 侧边筛选的分类项
const goodsClassElementsArray = this.state.mainType.map((item, index) => {
return <AtTag style='margin-left:5px' key={index}
name={item.id}
type='primary'
active={item.checked}
onClick={this.selectTag.bind(this)}
>{item.name}</AtTag>
})
// 侧边筛选的商品类型项
const goodsTypeElementsArray = this.state.goodType.map((item, index) => {
return <AtTag style='margin-left:5px' key={index}
name={item.id}
type='primary'
active={item.checked}
onClick={this.selectTag.bind(this)}
>{item.name}</AtTag>
})
// 侧边筛选的其他项
const goodsParamElementsArray = this.state.otherType.map((item, index) => {
let titleKey = Object.keys(item)[0]
let values = item[titleKey]
return <View key={index}><View className='title' >{titleKey}</View>
{values.map((subItem, subIndex) => {
let name = subItem.name
let isCheck = subItem.checked
return <View className='button-box' key={subIndex}>
<AtTag style='margin-left:5px'
name={name}
type='primary'
active={isCheck}
onClick={this.selectTag.bind(this)}
>{name}</AtTag>
</View>
})}
</View>
})
// 侧边筛选宽度选项
const widthnessElementsArray = this.state.widthType.map((item, index) => {
return <AtTag style='margin-left:5px' key={index}
name={item.name}
type='primary'
active={item.checked}
onClick={this.selectTag.bind(this)}
>{item.name}</AtTag>
})
// 店铺页面/店铺主页
const shopHomepageElement = <View className='shop-home-page'>
<View className='filter-box'>
{filterElementsArray}
<View onClick={this.showAndHideFilter.bind(this)} className='filter-title' >
<Text className='text'>
筛选
</Text>
</View>
</View>
<View className={this.state.isShowFilter ? 'side-filter show' : 'side-filter'}>
<View className='left' onClick={this.showAndHideFilter.bind(this)}></View>
{/* 侧边筛选 */}
<View className='right'>
<View className='filter-box'>
{this.state.checkedFilterIdList.length ? <View className='title'>已选择</View> : null}
<View className='button-box'>{checkedFilterElementsArray}</View>
{this.state.mainType.length ? <View className='title'>分类</View> : null}
<View className='button-box'>{goodsClassElementsArray}</View>
{this.state.goodType.length ? <View className='title'>商品类型</View> : null}
<View className='button-box'>{goodsTypeElementsArray}</View>
{goodsParamElementsArray}
{this.state.widthType.length ? <View className='title'>宽度</View> : null}
<View className='button-box'>{widthnessElementsArray}</View>
<View className='confirm-button'>
<Button className='button' type='primary' size='mini' style='background-color:#FF7142' onClick={this.submitFilter.bind(this)} >确认</Button>
<Button className='button' type='primary' size='mini' style='background-color:#5cb85c' onClick={this.resetFilterList.bind(this)}>重置</Button>
<View className='gap'></View>
</View>
</View>
</View>
</View>
{/* 店铺的商品列表 */}
<View className='container'>
{ShopItemElementsArray}
</View>
</View>
// 店铺详情
const shopDescriptionElement = <View className='shop-description'>
<View className='img-box'>
<Image src={URL.Base + 'Public/images/shop/bg_banner.png'} mode='scaleToFill' style='width: 100%;height:150px;' />
<View className='content'>
<View className='shop-name'>
{this.state.shopName}
</View>
<View className='shop-address'>
{this.state.shopAddress}
</View>
<View className='contact'>
<Text className='contact-name'>联系人:{this.state.contactName}</Text>
<Text className='contact-number'>联系电话{this.state.contactNumber}</Text>
</View>
</View>
</View>
<View className='shop-description2'>
<View className='title'>
店铺简介
</View>
<View className='description'>
<Text className='title-sub'>店铺介绍</Text>
<Text className='detail'>{this.state.shopDescription}</Text>
</View>
</View>
<View className='description-img'>
<View className='img-box'> <Image mode='aspectFit' src={URL.Base + 'Public/images/shop/bg_banner.png'} style='max-width: 100%; max-height:100%;' /></View>
<View className='img-box'> <Image mode='aspectFit' src={URL.Base + 'Public/images/shop/bg_banner.png'} style='max-width: 100%;max-height:100%;' /></View>
<View className='img-box'> <Image mode='aspectFit' src={URL.Base + 'Public/images/shop/bg_banner.png'} style='max-width: 100%;max-height:100%;' /></View>
</View>
</View>
const shopAllCateElementArray = this.state.shopAllInnerCate.map((item, index) => {
return <View key={index} className='item' onClick={this.handleShopInnerCate.bind(this, item.id)}> <Text>{item.n}</Text> </View>
})
return (
<View className='shop' >
<View onClick={this.state.closeBgWindow.bind(this)} className={this.state.isBlurWindow ? 'show-blur' : ''}></View>
<View className='banner-box'>
<Image src={URL.Base + 'Public/visual_editing/img/ksh_bg.jpg'} mode='scaleToFill' style='width: 100%;height:120px; ' />
<View className='shop-name'>{this.state.shopName}</View>
</View>
<View className='nav-box'>
<View className='nav'>
<View className='shop-cate' onClick={this.isShowShopAllCate.bind(this)}>
<Text className='text'>
店铺全部分类
</Text>
<AtIcon value='menu' size='10' color='white'></AtIcon>
</View>
<View className={this.state.isShowShopAllCate ? 'shopcate-selection show' : 'shopcate-selection'}>
{shopAllCateElementArray}</View>
<View className='homepage-link' onClick={this.showHomePage.bind()}>
<Text className='text'>
首页
</Text>
</View>
<View className='shoppage-link' onClick={this.showDescriptionPage.bind()}>
<Text className='text'>
店铺说明
</Text>
</View>
</View>
</View>
{/* 显示店铺首页或者店铺详情 */}
{this.state.showShopHomePage ? shopHomepageElement : shopDescriptionElement}
<CopyrightComponent></CopyrightComponent>
<View className='bottom-nav-box'>
<BottomNav otherData={{ menu: [{ name: '首页' }, { name: '分类' }, { name: '购物车' }, { name: '教程软件' }, { name: '更多' }] }} />
</View>
</View>
)
}
}
export default Shop