import Taro, { Component } from '@tarojs/taro'
import { View, Button, Text, Image, Map } from '@tarojs/components'
import { AtTag, AtIcon, } from 'taro-ui'
import URL from '../../serviceAPI.config'
import ScrollToTopComponent from '../../component/scrollToTopComponent/scrollToTopComponent'
import './shop.scss'
import ShopItem from '../../component/shopItemComponent/shopItemComponent'
import BottomNav from '../../component/bottomNav/bottomNav'
import CopyrightComponent from '../../component/copyrightComponent/copyrightComponent'
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,// 请求店铺商品的时候是否添加到旧列表里
loadMorePageIndex:1,//上拉加载页面数
isShowTopNav: 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: [],
longitude: '',
latitude: ''
}
}
// 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 => {
if (data.goods_type[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 => {
if (data.goodsParam[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,
currPage = '', // 不知道为什么筛选的时候要加 加这个参数为1
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,
currPage: currPage
}),
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,
latitude: res.data.data.shop_map.split(',')[0],
longitude: res.data.data.shop_map.split(',')[1]
}, () => {
// 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({
filterOptions: {
all: true,
amount: false,
newProduct: false,
price: false,
popularity: false
}
}
)
this.goodsSearch({ currPage: 1 })
}
if (value == 1) {
this.setState({
filterOptions: {
all: false,
amount: !this.state.filterOptions.amount,
newProduct: false,
price: false,
popularity: false
}
}, () => {
this.state.filterOptions.amount ? this.goodsSearch({ currPage: 1, order: "g.sales_volume desc" }) : this.goodsSearch({ currPage: 1, order: "g.sales_volume" })
}
)
}
if (value == 2) {
this.setState({
filterOptions: {
all: false,
amount: false,
newProduct: !this.state.filterOptions.newProduct,
price: false,
popularity: false
}
}, () => {
this.state.filterOptions.newProduct ? this.goodsSearch({ currPage: 1, order: "g.create_date desc" }) : this.goodsSearch({ currPage: 1, order: "g.create_date" })
}
)
}
if (value == 3) {
this.setState({
filterOptions: {
all: false,
amount: false,
newProduct: false,
price: !this.state.filterOptions.price,
popularity: false
}
}, () => {
this.state.filterOptions.price ? this.goodsSearch({ currPage: 1, order: "g.goods_price desc" }) : this.goodsSearch({ currPage: 1, order: "g.goods_price" })
}
)
}
if (value == 4) {
this.setState({
filterOptions: {
all: false,
amount: false,
newProduct: false,
price: false,
popularity: !this.state.filterOptions.popularity
}
}, () => {
this.state.filterOptions.popularity ? this.goodsSearch({ currPage: 1, order: "g.browse_times desc", }) : this.goodsSearch({ currPage: 1, 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, loadMorePageIndex:1,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: '',
curr_page: 1,
goods_class_id: '',
goodsSpec: [],
goodsParam: [],
goodsParamExt: [],
}, () => {
this.getSearchParams({})
this.goodsSearch({})
})
}
// 关闭背景window
closeBgWindow() {
this.setState({
isShowShopAllCate: false, isBlurWindow: false
})
}
componentWillMount() {
}
componentDidMount() {
Taro.showLoading({ title: '加载中' })
//页面加载之后 得到指定店铺的商品 和 筛选标签
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: '加载中'
})
this.setState({ isAddToList: true,loadMorePageIndex:this.state.loadMorePageIndex }, () => {
this.goodsSearch({ curr_page: this.state.loadMorePageIndex })
})
}
// 页面位置
onPageScroll(location) {
if (location.scrollTop <= 300 && this.state.isShowTopNav) {
this.setState({ isShowTopNav: false })
} else if (location.scrollTop > 300 && !this.state.isShowTopNav) {
this.setState({ isShowTopNav: true })
}
}
render() {
const ShopItemElementsArray = this.state.shopItem.length ? this.state.shopItem.map((item, index) => {
return
}) : null
const filterElementsArray = this.state.filterBar.map((item, index) => {
let isTure = this.state.filterOptions[item]
return
{this.state.filterBarKeys[item]}
{index !== 0 && index !== 5 && (isTure ? : )}
})
// 侧边已选项
const checkedFilterElementsArray = this.state.checkedFilterIdList.map((item, index) => {
return {item.name}
})
// 侧边筛选的分类项
const goodsClassElementsArray = this.state.mainType.map((item, index) => {
return {item.name}
})
// 侧边筛选的商品类型项
const goodsTypeElementsArray = this.state.goodType.map((item, index) => {
return {item.name}
})
// 侧边筛选的其他项
const goodsParamElementsArray = this.state.otherType.map((item, index) => {
let titleKey = Object.keys(item)[0]
let values = item[titleKey]
return {titleKey}
{values.map((subItem, subIndex) => {
let name = subItem.name
let isCheck = subItem.checked
return
{name}
})}
})
// 侧边筛选宽度选项
const widthnessElementsArray = this.state.widthType.map((item, index) => {
return {item.name}
})
// 店铺页面/店铺主页
const shopHomepageElement =
{filterElementsArray}
筛选
{/* 侧边筛选 */}
{this.state.checkedFilterIdList.length ? 已选择 : null}
{checkedFilterElementsArray}
{this.state.mainType.length ? 分类 : null}
{goodsClassElementsArray}
{this.state.goodType.length ? 商品类型 : null}
{goodsTypeElementsArray}
{goodsParamElementsArray}
{this.state.widthType.length ? 宽度 : null}
{widthnessElementsArray}
{/* 店铺的商品列表 */}
{ShopItemElementsArray}
// 店铺详情
const shopDescriptionElement =
{this.state.shopName}
{this.state.shopAddress}
联系人:{this.state.contactName}
联系电话:{this.state.contactNumber}
店铺简介
店铺介绍:
{this.state.shopDescription}
const shopAllCateElementArray = this.state.shopAllInnerCate.map((item, index) => {
return {item.n}
})
return (
{this.state.shopName}
店铺全部分类
{shopAllCateElementArray}
首页
店铺说明
{/* 显示店铺首页或者店铺详情 */}
{this.state.showShopHomePage ? shopHomepageElement : shopDescriptionElement}
{this.state.isShowTopNav ? : null}
)
}
}
export default Shop