import {FC, useCallback, useEffect, useMemo, useRef, useState} from "react"; import {Image, PageContainer, ScrollView, Text, View} from "@tarojs/components"; import Taro, {useRouter} from "@tarojs/taro"; import {ArticleRecord, brandApi, HomeApi} from "@/api"; import styles from "./article.module.scss"; import down from "@/static/img/doubleDown.png"; import {Profile} from "@/store"; import {parse} from "@/utils/marked/marked"; import Empty from "@/components/empty/empty"; import Spin from "@/components/spinner"; import {beforeTime} from "@/utils/time"; import Img from "@/components/image/image"; import Collect from "@/components/collect/collect"; import catalogue from "@/static/img/catalogue-inactive.png"; import Icon from "@/components/icon"; import articlesEvent from "@/hooks/articlesEvent"; const article: FC = () => { const {token} = Profile.useContainer() const [enable, setEnable] = useState(true) const {id} = useRouter().params as unknown as { id: number } const [articleInfo, setArticleInfo] = useState() const [ultra, setUltra] = useState(true) const [show, setShow] = useState(false) const globalData = Taro.getApp().globalData const {children, headings} = useMemo(() => parse(articleInfo?.content || ''), [articleInfo]) const query = Taro.createSelectorQuery() const [maoId, setMaoId] = useState('') const [timing, setTiming] = useState(undefined) const headingTop = useRef<{ id: string; top: number }[]>([]) useEffect(() => { if(headings.length){ if(headingTop.current?.[0]?.top){ return } headings.forEach(item => { query.select(`#${item.id}`).boundingClientRect().exec((res) => { if(res.length === headings.length && res?.[0]?.top){ headingTop.current = res } }) }) } },[headings]) useEffect(() => { setTimeout(() => { const query = Taro.createSelectorQuery() query.select('#childrenNode').boundingClientRect((res) => { if (!Array.isArray(res)) { console.log({childrenNode: res}) setUltra(globalData.pageHeight * .45 <= res.height) } }).exec() }, 300) }, [children]) useEffect(() => { if (!maoId && headings.length) { setMaoId(headings[0].id) } }, [headings]) function mao(id: string) { setMaoId(id) setShow(false) Taro.nextTick(() => { console.log(headingTop.current,'headingTop.cur') query.select(`#${id}`).boundingClientRect() query.exec((res) => { if (res.length) { Taro.pageScrollTo({ scrollTop: headingTop.current.find(d => d.id === id)?.top ?? 0, duration: 300 } ) } }) }) } const onCollect = (v: boolean | undefined) => { setArticleInfo((s) => ({...s, collect: v} as ArticleRecord)) } Taro.useReady(() => { getData().then() }) Taro.useUnload(() => { clearTimeout(timing) }) const getData = useCallback(async () => { try { const data = await brandApi.articleInfo(id) setTiming(setTimeout(() => { HomeApi.articleViews(id).then(() => { setArticleInfo({ ...data, page_view: data.page_view + 1 }) articlesEvent.recordsAdd({id: Number(id), view: (data?.page_view || 0) + 1}) }) }, 2000)) Taro.setNavigationBarTitle({title: data.title}) if (data.content.length < 200) { setUltra(false) } setArticleInfo(data) } catch (e) { } setEnable(false) }, [id]) function helloWorld() { return ( <> { headings.length > 0 && setShow(true)}> 目录 } {articleInfo?.title} { articleInfo?.owner_type === 1 && articleInfo?.brands?.map(d => ( {d?.name} {beforeTime(articleInfo?.created_at)} . 阅读 {articleInfo.page_view || 0} )) } { articleInfo?.owner_type === 2 && articleInfo?.illness?.map(d => ( {d.name} {beforeTime(articleInfo?.created_at)} . 阅读 {articleInfo.page_view || 0} )) } { children.length > 0 ? {children} : } { !token && ultra && children.length > 0 && Taro.navigateTo({url: '/pages/login/login'})}> 登录查看更多内容 } setShow(false)} onAfterLeave={() => setShow(false)} show={show} round overlay overlayStyle={'background:rgba(0,0,0,0.3)'} > setShow(false)}> 目录 { headings.length > 0 && headings.map((d) => mao(d.id)}> {d.text} ) } ) } return helloWorld() } export default article