moment_main.tsx 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. import TabBar from "@/components/navigation/TabBar";
  2. import { rpxToPx } from "@/utils/tools";
  3. import { View, Text, Image, ScrollView, Button } from "@tarojs/components";
  4. import Taro, { useDidShow, useRouter, useShareAppMessage } from "@tarojs/taro";
  5. import { useDispatch, useSelector } from "react-redux";
  6. import { useEffect, useState } from "react";
  7. import { followUser, getFriendMoments, getMyFriends, getUserDashBoard } from "@/services/friend";
  8. import FriendGuide from "./guide";
  9. import EmptyContent from "./empty_content";
  10. import MomentItem from "./moment_item";
  11. import { useTranslation } from "react-i18next";
  12. import { windows } from "@/services/health";
  13. import { setFastWithSleep, setFinishSetup, setLongFast, setRefreshs, setWindows } from "@/store/health";
  14. import { getInfoSuccess } from "@/store/user";
  15. import showActionSheet from "@/components/basic/ActionSheet";
  16. import { jumpPage } from "@/features/trackTimeDuration/hooks/Common";
  17. import './moment.scss'
  18. import ListFooter from "@/_health/components/list_footer";
  19. import { MainColorType } from "@/context/themes/color";
  20. import dayjs from "dayjs";
  21. import { IconClose, IconMenu } from "@/components/basic/Icons";
  22. import MomentShare from "./moment_share";
  23. let useRoute;
  24. let useNavigation;
  25. if (process.env.TARO_ENV == 'rn') {
  26. useRoute = require("@react-navigation/native").useRoute
  27. useNavigation = require("@react-navigation/native").useNavigation
  28. }
  29. let timer;
  30. let myScrollTop = 0
  31. export default function MomentMain() {
  32. const user = useSelector((state: any) => state.user);
  33. const observerObjBottom = Taro.createIntersectionObserver().relativeToViewport({ bottom: 100 })
  34. const [loading, setLoading] = useState(false)
  35. const [noMore, setNoMore] = useState(false)
  36. const [friends, setFriends] = useState<any>([])
  37. const [count, setCount] = useState(0)
  38. const systemInfo: any = Taro.getWindowInfo ? Taro.getWindowInfo() : Taro.getSystemInfoSync();
  39. const navigationBarHeight = systemInfo.statusBarHeight + 44;
  40. const [itemLayouts, setItemLayouts] = useState<any>([])
  41. const [itemHeights, setItemHeights] = useState<any>([])
  42. const [isPulling, setIsPulling] = useState(false)
  43. const [pageTop, setPageTop] = useState(0)
  44. let router
  45. let navigation;
  46. if (useNavigation) {
  47. navigation = useNavigation()
  48. }
  49. if (process.env.TARO_ENV == 'rn') {
  50. router = useRoute()
  51. }
  52. else {
  53. router = useRouter()
  54. }
  55. const [loaded, setLoaded] = useState(false)
  56. const [homeType, setHomeType] = useState('NO_FRIEND')
  57. const [dashBoard, setDashBoard] = useState<any>(null)
  58. const [moments, setMoments] = useState<any>([])
  59. const [page, setPage] = useState(1)
  60. const [endSignal, setEndSignal] = useState(0)
  61. const [closeGuide, setCloseGuide] = useState(false)
  62. const query = Taro.createSelectorQuery()
  63. const dispatch = useDispatch()
  64. const { t } = useTranslation()
  65. useEffect(() => {
  66. dayjs.locale(global.language == 'en' ? 'en' : 'zh-cn');
  67. require('moment/locale/en-gb')
  68. require('moment/locale/zh-cn')
  69. timer = setInterval(() => {
  70. setCount(count => count + 1)
  71. }, 1000)
  72. Taro.eventCenter.on('followUser', listenFollowUser)
  73. Taro.eventCenter.on('unfollowUser', listenUnfollowUser)
  74. return () => {
  75. Taro.eventCenter.off('followUser')
  76. Taro.eventCenter.off('unfollowUser')
  77. clearInterval(timer)
  78. }
  79. }, [])
  80. function listenFollowUser(e) {
  81. myFriends()
  82. }
  83. function listenUnfollowUser(e) {
  84. myFriends()
  85. }
  86. useEffect(() => {
  87. myFriends()
  88. if (router.params.type == 'share') {
  89. if (global.shareTicket) {
  90. Taro.getShareInfo({
  91. shareTicket: global.shareTicket,
  92. success(result) {
  93. updateRelation(result)
  94. },
  95. })
  96. }
  97. else {
  98. updateRelation(null)
  99. }
  100. }
  101. }, [user.isLogin])
  102. useEffect(() => {
  103. if (moments.length == 0) return
  104. setTimeout(() => {
  105. measureItemLayouts()
  106. }, 300)
  107. observerObjBottom.observe('#footer', (res) => {
  108. setEndSignal(endSignal => endSignal + 1)
  109. // if (moments.length==0) return
  110. // loadMore()
  111. })
  112. }, [moments])
  113. useEffect(() => {
  114. if (moments.length == 0) return
  115. loadMore()
  116. }, [endSignal])
  117. function measureItemLayouts() {
  118. if (moments.length <= 10) {
  119. moments.forEach((item, index) => {
  120. query.select(`#history2-${index}`).boundingClientRect()
  121. });
  122. query.exec((res) => {
  123. var layouts: any = []
  124. var heights: any = []
  125. res.forEach((rect, index) => {
  126. if (rect) {
  127. layouts[index] = rect.top + myScrollTop
  128. heights[index] = rect.height
  129. }
  130. });
  131. setItemLayouts(layouts)
  132. setItemHeights(heights)
  133. })
  134. }
  135. else {
  136. moments.forEach((item, index) => {
  137. if (index >= itemLayouts.length) {
  138. query.select(`#history2-${index}`).boundingClientRect()
  139. }
  140. });
  141. query.exec((res) => {
  142. var layouts: any = []
  143. var heights: any = []
  144. res.forEach((rect, index) => {
  145. if (rect) {
  146. layouts[index] = rect.top + myScrollTop
  147. heights[index] = rect.height
  148. }
  149. });
  150. setItemLayouts([...itemLayouts, ...layouts])
  151. setItemHeights([...itemHeights, ...heights])
  152. })
  153. }
  154. }
  155. function onScroll(e) {
  156. // var top = e.detail.scrollTop
  157. // myScrollTop = top
  158. var top = e.detail.scrollTop - e.detail.deltaY
  159. myScrollTop = e.detail.scrollTop
  160. setPageTop(top)
  161. }
  162. useDidShow(() => {
  163. getDashBoard()
  164. })
  165. function myFriends() {
  166. if (!user.isLogin) {
  167. setLoaded(true)
  168. return
  169. }
  170. setItemHeights([])
  171. setItemLayouts([])
  172. getDashBoard()
  173. getMoments(1)
  174. }
  175. function onRefresh() {
  176. setIsPulling(true)
  177. getMoments(1)
  178. getDashBoard()
  179. }
  180. function getMoments(index) {
  181. setPage(index)
  182. getFriendMoments({
  183. page: index,
  184. limit: 10
  185. }).then(res => {
  186. setIsPulling(false)
  187. if (index == 1) {
  188. setMoments((res as any).data)
  189. }
  190. else {
  191. setMoments([...moments, ...(res as any).data])
  192. }
  193. setLoading(false)
  194. if ((res as any).data.length == 0) {
  195. setNoMore(true)
  196. }
  197. else {
  198. setNoMore(false)
  199. }
  200. }).catch(e => {
  201. setIsPulling(false)
  202. setLoading(false)
  203. })
  204. }
  205. function getDashBoard() {
  206. getUserDashBoard().then(res => {
  207. setLoaded(true)
  208. setHomeType((res as any).homepage_type)
  209. setDashBoard(res)
  210. })
  211. }
  212. function updateRelation(obj) {
  213. if (!user.isLogin) {
  214. Taro.setStorageSync('share_uid', router.params.uid)
  215. Taro.setStorageSync('share_info', JSON.stringify(obj))
  216. return
  217. }
  218. if (user.id != router.params.uid) {
  219. var params: any = {
  220. follow_origin: obj ? 'WECHAT_GROUP_CHAT' : 'WECHAT_PRIVATE_CHAT',
  221. user_id: router.params.uid,
  222. }
  223. if (obj) {
  224. params.wechat = obj
  225. }
  226. followUser(params).then(res => {
  227. myFriends()
  228. })
  229. }
  230. }
  231. function loadMore() {
  232. if (loading) return;
  233. if (noMore) return;
  234. setLoading(true)
  235. var index = page;
  236. index++;
  237. setPage(index)
  238. getMoments(index)
  239. }
  240. function more() {
  241. showActionSheet({
  242. title: '',
  243. itemList: ['个人主页', '我的搭子', '消息通知'],
  244. success: (index) => {
  245. switch (index) {
  246. case 0:
  247. jumpPage('/_moment/pages/home?uid=' + user.id)
  248. break;
  249. case 1:
  250. jumpPage('/_moment/pages/relation')
  251. break
  252. case 2:
  253. jumpPage('/_moment/pages/message')
  254. break
  255. }
  256. }
  257. })
  258. }
  259. function friendGuide() {
  260. return <View style={{ position: 'fixed', left: 0, top: 0, width: rpxToPx(750), height: '100vh', zIndex: 100 }}>
  261. <View style={{ height: navigationBarHeight, width: rpxToPx(750), backgroundColor: '#fff' }} />
  262. {/* <View onClick={() => {
  263. setCloseGuide(true)
  264. }}>关闭</View> */}
  265. {
  266. user.isLogin && <View style={{
  267. position: 'absolute', left: 10, top: navigationBarHeight-44, width: 44, height: 44,
  268. display: 'flex', alignItems: 'center', justifyContent: 'center'
  269. }}
  270. onClick={() => {
  271. setCloseGuide(true)
  272. }}
  273. >
  274. <IconClose color="#000" width={30} height={30} />
  275. </View>
  276. }
  277. <FriendGuide />
  278. {
  279. process.env.TARO_ENV == 'weapp' && <TabBar index={4} />
  280. }</View>
  281. }
  282. function empty() {
  283. return <View style={{ position: 'fixed', left: 0, top: 0, width: rpxToPx(750), height: '100vh', zIndex: 100 }}>
  284. <View style={{ height: navigationBarHeight, width: rpxToPx(750), backgroundColor: '#fff' }} />
  285. {
  286. user.isLogin && <View style={{
  287. position: 'absolute', left: 10, top: navigationBarHeight-44, width: 44, height: 44,
  288. display: 'flex', alignItems: 'center', justifyContent: 'center'
  289. }}
  290. onClick={() => {
  291. setCloseGuide(true)
  292. }}
  293. >
  294. <IconClose color="#000" width={30} height={30} />
  295. </View>
  296. }
  297. <EmptyContent friends={dashBoard.friends} />
  298. {
  299. process.env.TARO_ENV == 'weapp' && <TabBar index={4} />
  300. }
  301. </View>
  302. }
  303. function listDetail() {
  304. return <View >
  305. <View style={{
  306. position: 'fixed',
  307. top: 0,
  308. left: 0,
  309. zIndex: 10,
  310. height: navigationBarHeight, width: rpxToPx(750), backgroundColor: '#f5f5f5', display: 'flex',
  311. flexDirection: 'column', justifyContent: 'flex-end'
  312. }}>
  313. <View style={{ height: 44, width: rpxToPx(750), position: 'relative', display: 'flex', flexDirection: 'row', alignItems: 'center', justifyContent: 'center' }}>
  314. <View style={{ fontWeight: 'bold', }}>{t('health.moments')}</View>
  315. <View onClick={more} style={{ position: 'absolute', left: 10, top: 0, bottom: 0, width: 44, display: 'flex', alignItems: 'center', justifyContent: 'center' }}>
  316. <IconMenu color="#000" width={rpxToPx(40)} />
  317. </View>
  318. <View style={{ position: 'absolute', left: 54, top: 0, bottom: 0, width: 44, display: 'flex', alignItems: 'center', justifyContent: 'center' }}>
  319. <Image src={require('@assets/_health/wechat.png')} style={{ width: rpxToPx(48), height: rpxToPx(48) }} />
  320. <Button openType="share" style={{ position: 'absolute', left: 0, right: 0, top: 0, bottom: 0, opacity: 0 }} />
  321. </View>
  322. </View>
  323. </View>
  324. <ScrollView style={{
  325. marginTop: navigationBarHeight,
  326. height: '90vh'
  327. }}
  328. enableBackToTop
  329. scrollY={true}
  330. refresherEnabled={true}
  331. upperThreshold={70}
  332. // lowerThreshold={140}
  333. refresherBackground={MainColorType.g05}
  334. onRefresherRefresh={onRefresh}
  335. refresherTriggered={isPulling}
  336. onScroll={onScroll}
  337. onScrollToUpper={() => {
  338. setPage(1)
  339. if (moments.length > 10) {
  340. setMoments(moments.slice(0, 10))
  341. setItemHeights(itemHeights.slice(0, 10))
  342. setItemLayouts(itemLayouts.slice(0, 10))
  343. }
  344. }}
  345. // onScrollToLower={props.loadMore}
  346. >
  347. <View style={{ backgroundColor: '#fff', minHeight: '100vh', paddingTop: rpxToPx(30), paddingBottom: 100 }}>
  348. {
  349. dashBoard && dashBoard.new_message && <View className="new_message_bg">
  350. <View className="new_message" onClick={() => {
  351. jumpPage('/_moment/pages/message')
  352. }}>
  353. {
  354. dashBoard.new_message.avatars.map((item, index) => {
  355. return <Image className="message_avatar" src={item} key={index} style={{ zIndex: 9 - index, marginLeft: index == 0 ? rpxToPx(8) : -15 }} />
  356. })
  357. }
  358. <View className="h26 bold" style={{ color: '#fff', minWidth: rpxToPx(260), textAlign: 'center' }}>{dashBoard.new_message.message_tip}</View>
  359. </View>
  360. </View>
  361. }
  362. {
  363. moments.map((item, index) => {
  364. if (itemLayouts.length >= index + 1 && pageTop > 0 && index > 5) {
  365. if (Math.abs(itemLayouts[index] - pageTop) > 2500) {
  366. return <View style={{ height: itemHeights[index] }} id={`history—temp-${index}`}>
  367. </View>
  368. }
  369. }
  370. return <View key={index} id={`history2-${index}`}>
  371. <MomentItem data={item} />
  372. </View>
  373. })
  374. }
  375. <ListFooter noMore={noMore} loading={loading} />
  376. <View id="footer" style={{ width: 1, height: 1 }}></View>
  377. </View>
  378. </ScrollView>
  379. {
  380. process.env.TARO_ENV == 'weapp' && <TabBar index={4} />
  381. }
  382. {
  383. homeType == 'NO_FRIEND' && !closeGuide && friendGuide()
  384. }
  385. {
  386. homeType == 'NO_MOMENT' && !closeGuide && empty()
  387. }
  388. </View>
  389. }
  390. function content() {
  391. if (!loaded) {
  392. return <View >
  393. {
  394. process.env.TARO_ENV == 'weapp' && <TabBar index={4} />
  395. }
  396. </View>
  397. }
  398. if (!user.isLogin) {
  399. return friendGuide()
  400. }
  401. return listDetail()
  402. return <View >
  403. <View style={{
  404. position: 'fixed',
  405. top: 0,
  406. left: 0,
  407. zIndex: 10,
  408. height: navigationBarHeight, width: rpxToPx(750), backgroundColor: '#fff', display: 'flex',
  409. flexDirection: 'column', justifyContent: 'flex-end'
  410. }}>
  411. <View style={{ height: 44, width: rpxToPx(750), position: 'relative', display: 'flex', flexDirection: 'row', alignItems: 'center', justifyContent: 'center' }}>
  412. <View>{t('health.moments')}</View>
  413. <View onClick={more} style={{ position: 'absolute', left: 0, top: 0, bottom: 0, width: 80, display: 'flex', alignItems: 'center', justifyContent: 'center' }}>More</View>
  414. </View>
  415. </View>
  416. <View style={{ backgroundColor: '#fff', minHeight: '100vh', paddingTop: navigationBarHeight, marginTop: rpxToPx(60), paddingBottom: 100 }}>
  417. {
  418. dashBoard && dashBoard.new_message && <View className="new_message_bg">
  419. <View className="new_message" onClick={() => {
  420. jumpPage('/_moment/pages/message')
  421. }}>
  422. {
  423. dashBoard.new_message.avatars.map((item, index) => {
  424. return <Image className="message_avatar" src={item} key={index} style={{ zIndex: 9 - index, marginLeft: index == 0 ? rpxToPx(8) : -15 }} />
  425. })
  426. }
  427. <View className="h26 bold" style={{ color: '#fff', minWidth: rpxToPx(260), textAlign: 'center' }}>{dashBoard.new_message.message_tip}</View>
  428. </View>
  429. </View>
  430. }
  431. {
  432. moments.map((item, index) => {
  433. if (itemLayouts.length >= index + 1 && pageTop > 0) {
  434. if (Math.abs(itemLayouts[index] - pageTop) > 2000) {
  435. return <View style={{ height: itemHeights[index] }} id={`history-${index}`}>
  436. </View>
  437. }
  438. // if (Math.abs(itemLayouts[index] - pageTop) > 1500) {
  439. // return <View style={{
  440. // height: itemHeights[index], display: 'flex',
  441. // paddingLeft: rpxToPx(40),
  442. // paddingRight: rpxToPx(40),
  443. // boxSizing: 'border-box',
  444. // flexDirection: 'row'
  445. // }} id={`history-${index}`}>
  446. // <TimelineDate timestamp={item.window_range.start_timestamp}
  447. // pre_timestamp={index > 0 ? list[index - 1].window_range.start_timestamp : null}
  448. // />
  449. // <View style={{
  450. // display: 'flex', flexDirection: 'column', flex: 1,
  451. // width: rpxToPx(552), height: itemHeights[index] - rpxToPx(60), backgroundColor: '#fafafa'
  452. // }}>
  453. // </View>
  454. // </View>
  455. // }
  456. }
  457. return <View key={index} id={`history-{index}`}>
  458. <MomentItem data={item} />
  459. </View>
  460. })
  461. }
  462. <ListFooter noMore={noMore} loading={loading} />
  463. </View>
  464. {
  465. process.env.TARO_ENV == 'weapp' && <TabBar index={4} />
  466. }
  467. </View>
  468. }
  469. return <View>
  470. {
  471. content()
  472. }
  473. <MomentShare />
  474. </View>
  475. return <View>
  476. <ScrollView scrollY style={{ height: '100vh' }}>
  477. <Button openType="share">分享</Button>
  478. <View>好友数量{count}</View>
  479. {
  480. friends.map((item, index) => {
  481. return <View key={index}>
  482. <Image src={item.avatar} style={{ width: 70, height: 70 }} />
  483. <Text>{item.nickname}</Text>
  484. <Text>{item.relation}</Text>
  485. </View>
  486. })
  487. }
  488. <View style={{ height: '100vh', backgroundColor: 'pink', width: rpxToPx(750) }} id="a"></View>
  489. <View style={{ height: '100vh', backgroundColor: 'blue', width: rpxToPx(750) }} id="b"></View>
  490. <View style={{ height: '100vh', backgroundColor: 'yellow', width: rpxToPx(750) }} id="c"></View>
  491. <View style={{ height: '100vh', backgroundColor: 'green', width: rpxToPx(750) }} id="d"></View>
  492. <View style={{ height: '100vh', backgroundColor: 'red', width: rpxToPx(750) }} id="e"></View>
  493. </ScrollView>
  494. {
  495. process.env.TARO_ENV == 'weapp' && <TabBar index={4} />
  496. }
  497. </View>
  498. }