Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
M
mall-webview
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
frontend
mall-webview
Commits
1cb56752
Commit
1cb56752
authored
Mar 29, 2021
by
hewenhao
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: paging
parent
d5254d3c
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
179 additions
and
31 deletions
+179
-31
index.ts
src/router/index.ts
+36
-31
use-paging.ts
src/util/use-paging.ts
+93
-0
paging.vue
src/views/test/paging.vue
+50
-0
No files found.
src/router/index.ts
View file @
1cb56752
import
{
createRouter
,
createWebHashHistory
,
RouteRecordRaw
}
from
"vue-router"
;
import
{
createRouter
,
createWebHashHistory
,
RouteRecordRaw
}
from
"vue-router"
;
import
Bonus
from
"../views/bonus/index.vue"
;
import
Bonus
from
"../views/bonus/index.vue"
;
const
routes
:
Array
<
RouteRecordRaw
>
=
[
const
routes
:
Array
<
RouteRecordRaw
>
=
[
{
{
path
:
"/"
,
path
:
"/"
,
name
:
"Bonus"
,
name
:
"Bonus"
,
component
:
Bonus
component
:
Bonus
},
},
{
{
path
:
"/bonusDetail"
,
path
:
"/bonusDetail"
,
name
:
"bonusDetail"
,
name
:
"bonusDetail"
,
component
:
()
=>
import
(
"../views/bonus/bonus-detail/index.vue"
)
component
:
()
=>
import
(
"../views/bonus/bonus-detail/index.vue"
)
},
},
{
{
path
:
"/withdraw"
,
path
:
"/withdraw"
,
name
:
"withdraw"
,
name
:
"withdraw"
,
component
:
()
=>
import
(
"../views/bonus/withdraw/index.vue"
)
component
:
()
=>
import
(
"../views/bonus/withdraw/index.vue"
)
},
},
{
{
path
:
"/test"
,
path
:
"/test"
,
name
:
"test"
,
name
:
"test"
,
component
:
()
=>
import
(
"../views/test/index.vue"
)
component
:
()
=>
import
(
"../views/test/index.vue"
)
}
},
];
{
path
:
"/test/paging"
,
const
router
=
createRouter
({
name
:
"test-paging"
,
history
:
createWebHashHistory
(),
component
:
()
=>
import
(
"../views/test/paging.vue"
)
routes
}
});
];
export
default
router
;
const
router
=
createRouter
({
history
:
createWebHashHistory
(),
routes
});
export
default
router
;
src/util/use-paging.ts
0 → 100644
View file @
1cb56752
/* eslint-disable @typescript-eslint/no-explicit-any */
import
{
reactive
,
UnwrapRef
}
from
"vue"
;
type
PagingFuncRet
<
T
=
object
>
=
{
list
:
T
[]
|
null
;
hasNextPage
:
boolean
;
total
:
number
;
};
type
BasePagingParams
=
{
pageNum
:
number
;
pageSize
:
number
;
};
interface
Paging
<
T
=
object
>
extends
BasePagingParams
{
list
:
T
[];
isLoad
:
boolean
;
loading
:
boolean
;
refreshing
:
boolean
;
hasNextPage
:
boolean
;
error
:
null
|
string
;
total
:
number
;
}
export
type
PagingFunc
<
T
=
object
,
P
=
any
>
=
(
arg
:
Partial
<
P
>
&
BasePagingParams
)
=>
Promise
<
PagingFuncRet
<
T
>>
;
function
usePaging
<
T
=
object
,
P
=
any
>
(
fn
:
PagingFunc
<
T
,
P
>
,
paramsFn
?:
()
=>
Partial
<
P
>
):
[
UnwrapRef
<
Paging
<
T
>>
,
(
arg
?:
Partial
<
P
>
)
=>
void
,
(
arg
?:
Partial
<
P
>
)
=>
void
]
{
const
paging
=
reactive
<
Paging
<
T
>>
({
list
:
[],
isLoad
:
false
,
pageNum
:
1
,
pageSize
:
15
,
loading
:
false
,
refreshing
:
false
,
hasNextPage
:
false
,
error
:
null
,
total
:
0
});
const
request
=
(
isRefresh
:
boolean
)
=>
{
return
async
(
arg
:
Partial
<
P
>
=
{})
=>
{
if
(
paging
.
loading
||
paging
.
refreshing
)
return
;
if
(
!
isRefresh
&&
!
paging
.
hasNextPage
)
return
;
if
(
isRefresh
)
{
paging
.
refreshing
=
true
;
}
else
{
paging
.
loading
=
true
;
}
const
currentPage
=
isRefresh
?
1
:
paging
.
pageNum
+
1
;
try
{
const
params
=
paramsFn
?
paramsFn
()
:
{};
const
ret
=
await
fn
({
...
arg
,
...
params
,
pageNum
:
currentPage
,
pageSize
:
paging
.
pageSize
});
if
(
Array
.
isArray
(
ret
.
list
))
{
if
(
isRefresh
)
{
paging
.
list
=
ret
.
list
as
UnwrapRef
<
T
[]
>
;
}
else
{
paging
.
list
=
paging
.
list
.
concat
(
ret
.
list
as
UnwrapRef
<
T
[]
>
);
}
}
paging
.
total
=
ret
.
total
;
paging
.
hasNextPage
=
ret
.
hasNextPage
;
paging
.
pageNum
=
currentPage
;
}
catch
(
error
)
{
paging
.
error
=
error
.
message
;
}
finally
{
if
(
isRefresh
)
{
paging
.
refreshing
=
false
;
}
else
{
paging
.
loading
=
false
;
}
paging
.
isLoad
=
true
;
}
};
};
const
loadMore
=
request
(
false
);
const
refresh
=
request
(
true
);
return
[
paging
,
loadMore
,
refresh
];
}
export
{
usePaging
};
src/views/test/paging.vue
0 → 100644
View file @
1cb56752
<
template
>
<div>
<div
v-if=
"paging.refreshing"
>
refreshing
</div>
<PullRefresh
:modelValue=
"paging.refreshing"
@
refresh=
"onRefresh"
>
<List
@
load=
"loadMore"
:modelValue=
"paging.loading"
:finished=
"!paging.hasNextPage"
>
<div
v-for=
"item in paging.list"
class=
"item"
:key=
"item.index"
>
{{
item
.
a
}}
</div>
</List>
</PullRefresh>
<div
v-if=
"paging.loading"
>
loading
</div>
<div
v-if=
"paging.isLoad && !paging.hasNextPage"
>
没有更多了
</div>
</div>
</
template
>
<
script
lang=
"ts"
setup
>
import
type
{
PagingFunc
}
from
'@/util/use-paging'
;
import
{
usePaging
}
from
"@/util/use-paging"
;
import
{
onMounted
}
from
"vue"
;
import
{
PullRefresh
,
List
}
from
'vant'
;
type
ListType
=
{
a
:
number
;
index
:
number
;
};
const
fn
:
PagingFunc
<
ListType
,
{
test
:
number
}
>
=
async
(
arg
)
=>
{
await
new
Promise
(
resolve
=>
setTimeout
(
resolve
,
1000
));
console
.
log
(
arg
)
return
{
hasNextPage
:
arg
.
pageNum
<
4
,
total
:
54
,
list
:
new
Array
(
20
).
fill
(
0
).
map
((
_
,
index
)
=>
({
a
:
Math
.
random
(),
index
}))
};
};
const
[
paging
,
loadMore
,
onRefresh
]
=
usePaging
(
fn
,
()
=>
({
test
:
1
}));
onMounted
(()
=>
{
onRefresh
();
});
</
script
>
<
style
lang=
"scss"
>
.item
{
height
:
80px
;
}
</
style
>
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment