File tree 4 files changed +115
-2
lines changed
day-12/react-jike-mobile/src
4 files changed +115
-2
lines changed Original file line number Diff line number Diff line change
1
+ import { http } from '@/utils'
2
+ import type { ResType } from '@/apis/shared.ts'
3
+
4
+ /**
5
+ * 响应数据
6
+ */
7
+ export type DetailDataType = {
8
+ /**
9
+ * 文章id
10
+ */
11
+ art_id : string ;
12
+ /**
13
+ * 文章-是否被点赞,-1无态度, 0未点赞, 1点赞, 是当前登录用户对此文章的态度
14
+ */
15
+ attitude : number ;
16
+ /**
17
+ * 文章作者id
18
+ */
19
+ aut_id : string ;
20
+ /**
21
+ * 文章作者名
22
+ */
23
+ aut_name : string ;
24
+ /**
25
+ * 文章作者头像,无头像, 默认为null
26
+ */
27
+ aut_photo : string ;
28
+ /**
29
+ * 文章_评论总数
30
+ */
31
+ comm_count : number ;
32
+ /**
33
+ * 文章内容
34
+ */
35
+ content : string ;
36
+ /**
37
+ * 文章-是否被收藏,true(已收藏)false(未收藏)是登录的用户对此文章的收藏状态
38
+ */
39
+ is_collected : boolean ;
40
+ /**
41
+ * 文章作者-是否被关注,true(关注)false(未关注), 说的是当前登录用户对这个文章作者的关注状态
42
+ */
43
+ is_followed : boolean ;
44
+ /**
45
+ * 文章_点赞总数
46
+ */
47
+ like_count : number ;
48
+ /**
49
+ * 文章发布时间
50
+ */
51
+ pubdate : string ;
52
+ /**
53
+ * 文章_阅读总数
54
+ */
55
+ read_count : number ;
56
+ /**
57
+ * 文章标题
58
+ */
59
+ title : string ;
60
+ }
61
+
62
+ export function fetchArticleDetailAPI ( id : string ) {
63
+ return http . request < ResType < DetailDataType > > ( {
64
+ url : `/articles/${ id } ` ,
65
+ } )
66
+ }
Original file line number Diff line number Diff line change
1
+ import { useEffect , useState } from 'react'
2
+ import { DetailDataType , fetchArticleDetailAPI } from '@/apis/detail.ts'
3
+ import { useNavigate , useSearchParams } from 'react-router-dom'
4
+ import { NavBar } from 'antd-mobile'
5
+
1
6
const Detail = ( ) => {
2
- return < div > this is Detail</ div >
7
+ const [ detail , setDetail ] = useState < DetailDataType | null > ( null )
8
+
9
+ const [ params ] = useSearchParams ( )
10
+ const id = params . get ( 'id' )
11
+ useEffect ( ( ) => {
12
+ const getDetail = async ( ) => {
13
+ try {
14
+ const res = await fetchArticleDetailAPI ( id ! )
15
+ setDetail ( res . data . data )
16
+ } catch ( error ) {
17
+ throw new Error ( 'fetchArticleDetailAPI error' )
18
+ }
19
+ }
20
+ getDetail ( )
21
+
22
+ } , [ ] )
23
+
24
+ const navigate = useNavigate ( )
25
+ const back = ( ) => {
26
+ navigate ( - 1 )
27
+ }
28
+
29
+ // 数据返回前 loading
30
+ if ( ! detail ) {
31
+ return < div > this is loading</ div >
32
+ }
33
+
34
+ return (
35
+ < div >
36
+ < NavBar onBack = { back } > { detail ?. title } </ NavBar >
37
+ < div dangerouslySetInnerHTML = { {
38
+ __html : detail ?. content ,
39
+ } } > </ div >
40
+ </ div >
41
+ )
3
42
}
4
43
5
44
export default Detail
Original file line number Diff line number Diff line change 1
1
import { Image , InfiniteScroll , List } from 'antd-mobile'
2
2
import { useEffect , useState } from 'react'
3
3
import { ArticleListRes , fetchArticleListAPI } from '@/apis/list.ts'
4
+ import { useNavigate } from 'react-router-dom'
4
5
5
6
type Props = {
6
7
channelId : string
@@ -56,11 +57,18 @@ const HomeList = (props: Props) => {
56
57
}
57
58
}
58
59
60
+ // 路由跳转
61
+ const navigate = useNavigate ( )
62
+ const goToDetail = ( id : string ) => {
63
+ navigate ( `/detail?id=${ id } ` )
64
+ }
65
+
59
66
return (
60
67
< >
61
68
< List >
62
69
{ articleRes . results . map ( ( item ) => (
63
70
< List . Item
71
+ onClick = { ( ) => goToDetail ( item . art_id ) }
64
72
key = { item . art_id }
65
73
prefix = {
66
74
< Image
Original file line number Diff line number Diff line change @@ -10,7 +10,7 @@ const Home = () => {
10
10
return (
11
11
< div className = "tabContainer" >
12
12
{ /* tab 区域 */ }
13
- < Tabs >
13
+ < Tabs defaultActiveKey = { '0' } >
14
14
{ channels . map ( item => (
15
15
< Tabs . Tab title = { item . name } key = { item . id } >
16
16
< div className = "listContainer" >
You can’t perform that action at this time.
0 commit comments