🎈 perf(购物页面): 添加分页功能]

This commit is contained in:
xuan 2022-11-18 17:05:18 +08:00
parent 0ab2b22be5
commit 8deb8e56f0
5 changed files with 61 additions and 26 deletions

View File

@ -41,7 +41,7 @@ const CustomTabBar: FC = () => {
const handleSelectTabItem = (id: TabBarIndexMap[number]['id']) => { const handleSelectTabItem = (id: TabBarIndexMap[number]['id']) => {
return () => { return () => {
setSelected(id) setSelected(id)
Taro.switchTab({ url: tabItem.find(item=>item.id === id)?.pagePath! }) Taro.switchTab({ url: tabItem?.find(item=>item.id === id)?.pagePath! })
} }
} }
const setSelected = (id: TabBarIndexMap[number]['id']) => { const setSelected = (id: TabBarIndexMap[number]['id']) => {
@ -51,7 +51,7 @@ const CustomTabBar: FC = () => {
return ( return (
<View className={styles.customTabBar}> <View className={styles.customTabBar}>
<View className={styles['customTabBar-line']}></View> <View className={styles['customTabBar-line']}></View>
{tabItem.map((item, index) => { {tabItem?.map((item, index) => {
return ( return (
<View <View
key={index} key={index}

View File

@ -24,7 +24,7 @@ export default () => {
const { fetchData: clitentFetch, state: orderState } = ClientListApi() const { fetchData: clitentFetch, state: orderState } = ClientListApi()
//数据加载状态 //数据加载状态
const statusMore = useMemo(() => { const statusMore = useMemo(() => {
return dataLoadingStatus({ list: clentList.list, total: clentList.total, status: orderState.loading }) return dataLoadingStatus({ list: clentList.list, total: clentList.total, status: orderState.loading! })
}, [clentList, orderState]) }, [clentList, orderState])
const [clientObj, setclientObj] = useState({ const [clientObj, setclientObj] = useState({
@ -152,4 +152,4 @@ export default () => {
</View> </View>
) )
} }

View File

@ -38,9 +38,13 @@ export const Shopping: FC = memo(() => {
) )
}) })
interface InternalContainer {} interface SearchOptions {
short_name_or_phone?: string
page?: number
size?: number
}
const ShoppingCartContainer: FC<InternalContainer> = () => { const ShoppingCartContainer: FC = () => {
let isFirst = useRef(true) let isFirst = useRef(true)
const { isMultipleSelection, isManageStatus, selectedAmount, currentCheckedPurchaserId, currentCheckedSaleMode, colorStore } = useShoppingState() const { isMultipleSelection, isManageStatus, selectedAmount, currentCheckedPurchaserId, currentCheckedSaleMode, colorStore } = useShoppingState()
@ -59,25 +63,31 @@ const ShoppingCartContainer: FC<InternalContainer> = () => {
const { fetchData, state } = ShoppingCartListApi() const { fetchData, state } = ShoppingCartListApi()
const [searchOptions, setSearchOptions] = useState({ const [searchOptions, setSearchOptions] = useState<SearchOptions>({
short_name_or_phone: '',
page: 1,
size: 10,
}) })
useDidShow(() => { useDidShow(() => {
// 第二次进入该页面时触发
if (!isFirst.current) { if (!isFirst.current) {
fetchData(searchOptions) fetchData(getFilterData(searchOptions))
} }
}) })
useEffect(() => { useEffect(() => {
console.log('useEffect fetchData') console.log('useEffect fetchData', getFilterData(searchOptions))
if (!isEmptyObject(getFilterData(searchOptions))) { if (!isFirst.current) {
fetchData(searchOptions) fetchData(getFilterData(searchOptions))
} }
}, [searchOptions]) }, [searchOptions])
// 输入了搜索关键字 // 输入了搜索关键字
const getSearchData = useCallback(e => { const getSearchData = useCallback(e => {
setSearchOptions(prev => ({ ...prev, short_name_or_phone: e })) console.log('getSearchData===>',e);
pageNum.current.page = 1
setSearchOptions(prev => ({ ...prev, short_name_or_phone: e, size: 10 }))
}, []) }, [])
const [shoppingCartData, setShoppingCartData] = useState<{ const [shoppingCartData, setShoppingCartData] = useState<{
@ -85,6 +95,18 @@ const ShoppingCartContainer: FC<InternalContainer> = () => {
total: number total: number
}>({ list: [], total: 0 }) }>({ list: [], total: 0 })
//上拉加载数据
const pageNum = useRef({ size: searchOptions.size, page: searchOptions.page })
// 上拉加载
const getScrollToLower = useCallback(() => {
if (shoppingCartData.list.length < shoppingCartData.total) {
pageNum.current.page!++
const size = pageNum.current.size! * pageNum.current.page!
setSearchOptions((e) => ({ ...e, size }))
console.log(searchOptions, 11111)
}
}, [shoppingCartData])
//数据加载状态 //数据加载状态
const statusMore = useMemo(() => { const statusMore = useMemo(() => {
console.log('shoppingCartData==>', shoppingCartData, state) console.log('shoppingCartData==>', shoppingCartData, state)
@ -101,7 +123,7 @@ const ShoppingCartContainer: FC<InternalContainer> = () => {
useLayoutEffect(() => { useLayoutEffect(() => {
;(async () => { ;(async () => {
console.log('useLayoutEffect') console.log('useLayoutEffect')
await fetchData() await fetchData(getFilterData(searchOptions))
isFirst.current = false isFirst.current = false
let query = Taro.createSelectorQuery() let query = Taro.createSelectorQuery()
console.log('query', query) console.log('query', query)
@ -203,8 +225,9 @@ const ShoppingCartContainer: FC<InternalContainer> = () => {
// 下拉刷新 // 下拉刷新
const handleRefresh = async () => { const handleRefresh = async () => {
pageNum.current.page = 1
setRefreshStatus(true) setRefreshStatus(true)
const res = await fetchData(searchOptions) const res = await fetchData(getFilterData(searchOptions))
if (res.success) { if (res.success) {
setRefreshStatus(false) setRefreshStatus(false)
alert.success('刷新成功') alert.success('刷新成功')
@ -230,15 +253,12 @@ const ShoppingCartContainer: FC<InternalContainer> = () => {
</View> </View>
<View className={classnames('flex-item', 'flex-col', styles['shopping--context'])}> <View className={classnames('flex-item', 'flex-col', styles['shopping--context'])}>
<View id='shoppingListContainer' className={classnames(styles.shopping__list__container, 'flex-item')} style={{ height: listHeightRef.current }}> <View id='shoppingListContainer' className={classnames(styles.shopping__list__container, 'flex-item')} style={{ height: listHeightRef.current }}>
<InfiniteScroll statusMore={statusMore} refresherEnabled={true} selfOnRefresherRefresh={handleRefresh} refresherTriggered={refreshStatus}> <InfiniteScroll statusMore={statusMore} refresherEnabled={true} selfOnRefresherRefresh={handleRefresh} refresherTriggered={refreshStatus} selfonScrollToLower={getScrollToLower}>
{// isPending ? ( {
// null
// ) : (
!!shoppingCartData?.list?.length && !!shoppingCartData?.list?.length &&
shoppingCartData?.list?.map((item, index) => { shoppingCartData?.list?.map((item, index) => {
return <ItemList itemData={item} key={index}></ItemList> return <ItemList itemData={item} key={index}></ItemList>
}) })
// )
} }
</InfiniteScroll> </InfiniteScroll>
</View> </View>

View File

@ -12,6 +12,10 @@ import useUserInfo from '@/use/useUserInfo'
import { goLink } from '@/common/common' import { goLink } from '@/common/common'
import { checkKey } from "@/common/common" import { checkKey } from "@/common/common"
import { useDidShow } from '@tarojs/taro' import { useDidShow } from '@tarojs/taro'
import { useDispatch } from 'react-redux'
import { Dispatch } from 'redux'
import { TabBarAction, TabBarIndex } from '@/reducers/tabBar'
import { TabBarType } from '@/constants/tabbar'
type IconCardType = { type IconCardType = {
jurisdiction?: string //权限key值 jurisdiction?: string //权限key值
iconName: IconNames iconName: IconNames
@ -126,6 +130,9 @@ let statisticAnalysis: IconCardType[] = [
// 用户信息 // 用户信息
const UserInfo: FC = () => { const UserInfo: FC = () => {
const { removeToken, removeUserInfo, userInfo } = useUserInfo() const { removeToken, removeUserInfo, userInfo } = useUserInfo()
const dispatch = useDispatch<Dispatch<TabBarAction>>()
console.log('userInfo==>', userInfo) console.log('userInfo==>', userInfo)
// const [userInfo, setUserInfo] = useState({ // const [userInfo, setUserInfo] = useState({
// avatarUrl: '', // avatarUrl: '',
@ -137,6 +144,7 @@ const UserInfo: FC = () => {
const handleLogout = () => { const handleLogout = () => {
removeToken() removeToken()
removeUserInfo() removeUserInfo()
dispatch({ type: TabBarType.SET_SELECTED, data: { selectedId: TabBarIndex.INDEX } })
goLink('/pages/login/index', {}, 'reLaunch') goLink('/pages/login/index', {}, 'reLaunch')
} }

View File

@ -13,33 +13,40 @@ type TabBarIndexMap = {
} }
} }
export enum TabBarIndex {
INDEX = 1,
SHOPPING = 2,
ORDER = 3,
USER = 4
}
const INITIAL_STATE = { const INITIAL_STATE = {
selectedId: 1, selectedId: TabBarIndex.INDEX,
tabItem: [ tabItem: [
{ {
id: 1, id: TabBarIndex.INDEX,
pagePath: '/pages/index/index', pagePath: '/pages/index/index',
text: '首页', text: '首页',
iconPath: 'icon-shouye1', iconPath: 'icon-shouye1',
selectedIconPath: 'icon-shouye', selectedIconPath: 'icon-shouye',
}, },
{ {
id: 2, id: TabBarIndex.SHOPPING,
pagePath: '/pages/shopping/index', pagePath: '/pages/shopping/index',
text: '购物', text: '购物',
iconPath: 'icon-gouwu1', iconPath: 'icon-gouwu1',
selectedIconPath: 'icon-gouwu', selectedIconPath: 'icon-gouwu',
}, },
{ {
id: 3, id: TabBarIndex.ORDER,
pagePath: '/pages/order/index', pagePath: '/pages/order/index',
text: '订单', text: '订单',
iconPath: 'icon-dingdan1', iconPath: 'icon-dingdan1',
selectedIconPath: 'icon-dingdan', selectedIconPath: 'icon-dingdan',
}, },
{ {
id: 4, id: TabBarIndex.USER,
pagePath: '/pages/user/index', pagePath: '/pages/user/index',
text: '我的', text: '我的',
iconPath: 'icon-gerenzhongxin1', iconPath: 'icon-gerenzhongxin1',
@ -50,7 +57,7 @@ const INITIAL_STATE = {
export type TabBarData = { export type TabBarData = {
selectedId: number selectedId: number
tabItem: TabBarIndexMap[number][] tabItem?: TabBarIndexMap[number][]
} }
export type TabBarAction = { export type TabBarAction = {