home.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. import { View, Text, Image, ScrollView } from "@tarojs/components";
  2. import './home.scss'
  3. import Taro, { useRouter } from "@tarojs/taro";
  4. import { useEffect, useState } from "react";
  5. import { followUser, getFriendMoments, getUserMoments, getUserProfile, unfollowUser } from "@/services/friend";
  6. import { useSelector } from "react-redux";
  7. import NewButton, { NewButtonType } from "@/_health/base/new_button";
  8. import { rpxToPx } from "@/utils/tools";
  9. import { MainColorType } from "@/context/themes/color";
  10. import showActionSheet from "@/components/basic/ActionSheet";
  11. import ListFooter from "@/_health/components/list_footer";
  12. import RecentItem from "../components/recent_item";
  13. import { useTranslation } from "react-i18next";
  14. import NoRecord from "@/_health/components/no_record";
  15. let useRoute;
  16. let useNavigation;
  17. let scenario = '';
  18. if (process.env.TARO_ENV == 'rn') {
  19. useRoute = require("@react-navigation/native").useRoute
  20. useNavigation = require("@react-navigation/native").useNavigation
  21. }
  22. let timer
  23. export default function UserHome() {
  24. const user = useSelector((state: any) => state.user);
  25. const [profile, setProfile] = useState<any>(null)
  26. const [loaded, setLoaded] = useState(false)
  27. const [loadedList, setLoadedList] = useState(false)
  28. const systemInfo: any = Taro.getWindowInfo ? Taro.getWindowInfo() : Taro.getSystemInfoSync();
  29. const navigationBarHeight = systemInfo.statusBarHeight + 44;
  30. const observerObjBottom = Taro.createIntersectionObserver().relativeToViewport({ bottom: 100 })
  31. const [loading, setLoading] = useState(false)
  32. const [noMore, setNoMore] = useState(false)
  33. const [itemLayouts, setItemLayouts] = useState<any>([])
  34. const [itemHeights, setItemHeights] = useState<any>([])
  35. const [pageTop, setPageTop] = useState(0)
  36. const [page, setPage] = useState(1)
  37. const [endSignal, setEndSignal] = useState(0)
  38. const [list, setList] = useState<any>([])
  39. const query = Taro.createSelectorQuery()
  40. const { t } = useTranslation()
  41. const [myScrollTop, setMyScrollTop] = useState(0)
  42. const [count, setCount] = useState(0)
  43. let router
  44. let navigation;
  45. if (useNavigation) {
  46. navigation = useNavigation()
  47. }
  48. if (process.env.TARO_ENV == 'rn') {
  49. router = useRoute()
  50. }
  51. else {
  52. router = useRouter()
  53. }
  54. useEffect(() => {
  55. getProfile()
  56. moments(1)
  57. }, [])
  58. useEffect(() => {
  59. timer = setInterval(() => {
  60. setCount(count => count + 1)
  61. }, 1000)
  62. return () => {
  63. clearInterval(timer)
  64. }
  65. }, [])
  66. useEffect(() => {
  67. if (list.length == 0) return
  68. setTimeout(() => {
  69. measureItemLayouts()
  70. }, 300)
  71. observerObjBottom.observe('#footer', (res) => {
  72. setEndSignal(endSignal => endSignal + 1)
  73. // console.log(moments.length)
  74. // if (moments.length==0) return
  75. // loadMore()
  76. })
  77. }, [list])
  78. useEffect(() => {
  79. if (moments.length == 0) return
  80. // loadMore()
  81. }, [endSignal])
  82. function getProfile() {
  83. getUserProfile({ user_id: router.params.uid }).then(res => {
  84. setProfile(res)
  85. setLoaded(true)
  86. })
  87. }
  88. function loadMore() {
  89. if (loading) return;
  90. if (noMore) return;
  91. setLoading(true)
  92. var index = page;
  93. index++;
  94. setPage(index)
  95. moments(index)
  96. }
  97. function moments(index) {
  98. setPage(index)
  99. var params: any = {
  100. user_id: router.params.uid,
  101. page: index,
  102. limit: 10
  103. }
  104. if (index != 1 && list.length > 0) {
  105. params.last_timestamp = list[list.length - 1].time.timestamp
  106. }
  107. getUserMoments(params).then(res => {
  108. if (index == 1) {
  109. setList((res as any).data)
  110. }
  111. else {
  112. setList([...list, ...(res as any).data])
  113. }
  114. setLoading(false)
  115. setLoadedList(true)
  116. if ((res as any).data.length == 0) {
  117. setNoMore(true)
  118. }
  119. else {
  120. setNoMore(false)
  121. }
  122. })
  123. }
  124. function measureItemLayouts() {
  125. if (list.length <= 10) {
  126. list.forEach((item, index) => {
  127. query.select(`#history3-${index}`).boundingClientRect()
  128. });
  129. query.exec((res) => {
  130. var layouts: any = []
  131. var heights: any = []
  132. res.forEach((rect, index) => {
  133. if (rect) {
  134. layouts[index] = rect.top + myScrollTop
  135. heights[index] = rect.height
  136. }
  137. });
  138. setItemLayouts(layouts)
  139. setItemHeights(heights)
  140. })
  141. }
  142. else {
  143. list.forEach((item, index) => {
  144. if (index >= itemLayouts.length) {
  145. query.select(`#history3-${index}`).boundingClientRect()
  146. }
  147. });
  148. query.exec((res) => {
  149. var layouts: any = []
  150. var heights: any = []
  151. res.forEach((rect, index) => {
  152. if (rect) {
  153. layouts[index] = rect.top + myScrollTop
  154. heights[index] = rect.height
  155. }
  156. });
  157. setItemLayouts([...itemLayouts, ...layouts])
  158. setItemHeights([...itemHeights, ...heights])
  159. })
  160. }
  161. }
  162. function onScroll(e) {
  163. // var top = e.detail.scrollTop
  164. // myScrollTop = top
  165. var top = e.detail.scrollTop - e.detail.deltaY
  166. // myScrollTop = e.detail.scrollTop
  167. setPageTop(top)
  168. setMyScrollTop(e.detail.scrollTop)
  169. }
  170. function tapFollow() {
  171. var params: any = {
  172. follow_origin: 'WECHAT_PRIVATE_CHAT',
  173. user_id: router.params.uid,
  174. }
  175. followUser(params).then(res => {
  176. getProfile()
  177. Taro.eventCenter.trigger('followUser', router.params.uid)
  178. })
  179. }
  180. function tapFollowing() {
  181. showActionSheet({
  182. title: `不再关注“${profile.nickname}”,他的动态将不出现在搭子圈`,
  183. itemList: ['不再关注'],
  184. success: (res) => {
  185. if (res == 0) {
  186. unfollowUser(profile.id).then(res => {
  187. Taro.eventCenter.trigger('unfollowUser', router.params.uid)
  188. getProfile()
  189. })
  190. }
  191. }
  192. })
  193. }
  194. if (!loaded) return <View />
  195. // console.log(list.length,pageTop,itemLayouts,)
  196. return <ScrollView style='height:100vh'
  197. enableBackToTop
  198. scrollY={true}
  199. // refresherEnabled={props.onRefresherRefresh}
  200. // upperThreshold={70}
  201. lowerThreshold={140}
  202. // refresherBackground={MainColorType.g05}
  203. // onRefresherRefresh={props.onRefresherRefresh}
  204. // refresherTriggered={props.isPulling}
  205. onScroll={onScroll}
  206. onScrollToUpper={() => {
  207. setPage(1)
  208. setPageTop(0)
  209. if (moments.length > 10) {
  210. setList(list.slice(0, 10))
  211. setItemHeights(itemHeights.slice(0, 10))
  212. setItemLayouts(itemLayouts.slice(0, 10))
  213. }
  214. }}
  215. onScrollToLower={loadMore}
  216. ><View>
  217. <View className="home_top">
  218. <Image className="cover" mode="aspectFill" src={profile.cover} />
  219. <Image className="home_avatar" src={profile.avatar} />
  220. <View className="home_nickname bold">{profile.nickname}</View>
  221. <View style={{
  222. position: 'fixed',
  223. top: 0,
  224. left: 0,
  225. zIndex: 10,
  226. height: navigationBarHeight, width: rpxToPx(750), backgroundColor: pageTop >= rpxToPx(380) ? '#f5f5f5' : 'transparent', display: 'flex',
  227. flexDirection: 'column', justifyContent: 'flex-end'
  228. }}>
  229. <View style={{ height: 44, width: rpxToPx(750), position: 'relative', display: 'flex', flexDirection: 'row', alignItems: 'center', justifyContent: 'center' }}>
  230. {/* <View onClick={() => {
  231. Taro.navigateBack()
  232. }} style={{ position: 'absolute', left: 0, top: 0, bottom: 0, width: 80, display: 'flex', alignItems: 'center', justifyContent: 'center' }}>Back</View> */}
  233. <Image src={require('@assets/_health/navi_back.png')} style={{
  234. position: 'absolute',
  235. width: rpxToPx(92),
  236. height: rpxToPx(64),
  237. left: 0,
  238. top: 22 - rpxToPx(32)
  239. }}
  240. onClick={() => {
  241. Taro.navigateBack()
  242. }}
  243. />
  244. <Text style={{
  245. color: pageTop >= rpxToPx(380) ? '#000' : 'transparent'
  246. }} className="bold">{profile.nickname}</Text>
  247. </View>
  248. </View>
  249. </View>
  250. <View style={{ height: rpxToPx(62) }}></View>
  251. {
  252. router.params.uid != user.id && <View style={{ marginLeft: rpxToPx(45) }}>
  253. {
  254. profile.relation == 'FRIEND' || profile.relation == 'FOLLOWING' ?
  255. // <NewButton type={NewButtonType.fill}
  256. // width={rpxToPx(665)}
  257. // height={rpxToPx(72)}
  258. // title="following"
  259. // color={MainColorType.g02}
  260. // onClick={tapFollowing}
  261. // />
  262. <View onClick={tapFollowing} className="following h30 bold">{t('health.following2')}</View>
  263. : <NewButton type={NewButtonType.fill}
  264. width={rpxToPx(665)}
  265. height={rpxToPx(72)}
  266. title={t('health.follow')}
  267. color={MainColorType.blue}
  268. onClick={tapFollow}
  269. />
  270. }
  271. </View>
  272. }
  273. {/* <View style={{height:rpxToPx(60)}}/> */}
  274. {
  275. list.map((item, index) => {
  276. if (itemLayouts.length >= index + 1 && pageTop > 0 && index > 5) {
  277. if (Math.abs(itemLayouts[index] - pageTop) > 2500) {
  278. return <View style={{ height: itemHeights[index] }} id={`history—temp-${index}`}>
  279. </View>
  280. }
  281. }
  282. return <View key={index} id={`history3-${index}`}>
  283. <RecentItem data={item} index={index} />
  284. </View>
  285. })
  286. }
  287. {
  288. loadedList && list.length == 0 && <NoRecord />
  289. }
  290. <ListFooter noMore={noMore} loading={loading} />
  291. <View id="footer" style={{ width: 1, height: 1 }}></View>
  292. </View>
  293. </ScrollView>
  294. }