message.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import { useState, useRef, useEffect } from 'react'
  2. import { View, MovableArea, MovableView, Image, Text, ScrollView } from '@tarojs/components'
  3. import { createSelectorQuery, chooseImage, showToast } from '@tarojs/taro'
  4. import './message.scss'
  5. import { followUser, getMessages } from '@/services/friend'
  6. import { MainColorType } from '@/context/themes/color'
  7. import { jumpPage } from '@/features/trackTimeDuration/hooks/Common'
  8. import dayjs from 'dayjs'
  9. import { rpxToPx } from '@/utils/tools'
  10. import NewButton, { NewButtonType } from '@/_health/base/new_button'
  11. import Taro from '@tarojs/taro'
  12. import NoRecord from '@/_health/components/no_record'
  13. export default function Message() {
  14. const [list, setList] = useState<any>([])
  15. const [loaded, setLoaded] = useState(false)
  16. const [page, setPage] = useState(1)
  17. const [isPulling, setIsPulling] = useState(false)
  18. const [isLoading, setIsLoading] = useState(false)
  19. const [total, setTotal] = useState(-1)
  20. useEffect(() => {
  21. Taro.setNavigationBarTitle({
  22. title: '消息'
  23. })
  24. setPage(1)
  25. getMessageData(1)
  26. Taro.eventCenter.on('followUser', listenFollowUser)
  27. Taro.eventCenter.on('unfollowUser', listenUnfollowUser)
  28. }, [])
  29. function listenFollowUser(e) {
  30. getMessageData(1)
  31. }
  32. function listenUnfollowUser(e) {
  33. getMessageData(1)
  34. }
  35. function onRefresh() {
  36. setIsPulling(true)
  37. getMessageData(1)
  38. }
  39. function more() {
  40. if (isLoading) return
  41. if (total == list.length) return
  42. var index = page + 1
  43. getMessageData(index)
  44. }
  45. function onScroll() {
  46. }
  47. function getMessageData(index) {
  48. setPage(index)
  49. setIsLoading(true)
  50. getMessages({ page: index, limit: 20 }).then(res => {
  51. if (index == 1) {
  52. setList((res as any).data)
  53. setTotal((res as any).total)
  54. }
  55. else {
  56. setList([...list, ...(res as any).data])
  57. }
  58. setLoaded(true)
  59. setIsPulling(false)
  60. setIsLoading(false)
  61. }).catch(e => {
  62. setIsPulling(false)
  63. setIsLoading(false)
  64. })
  65. }
  66. function tapFollow(item) {
  67. var params: any = {
  68. follow_origin: 'WECHAT_PRIVATE_CHAT',
  69. user_id: item.user.id,
  70. }
  71. followUser(params).then(res => {
  72. item.relation = 'FRIEND'
  73. setList(JSON.parse(JSON.stringify(list)))
  74. Taro.eventCenter.trigger('followUser', item.user.id)
  75. })
  76. }
  77. function messageItem(item, index) {
  78. return <View key={index} className='message_item' >
  79. <Image className='message_item_avatar' src={item.user.avatar} />
  80. <View style={{ display: 'flex', flexDirection: 'column', flex: 1 }}>
  81. <Text className='h28 g02'>
  82. <Text onClick={() => {
  83. jumpPage('./home?uid=' + item.user.id)
  84. }} className='h28 bold link' style={{ color: MainColorType.link }}>{item.user.nickname} </Text>
  85. 关注了我
  86. </Text>
  87. <Text className='h22 g02' style={{ marginTop: rpxToPx(12) }}>{dayjs(item.timestamp).format('MM-DD HH:mm')}</Text>
  88. </View>
  89. {
  90. item.relation == 'FOLLOWER' ? <NewButton
  91. type={NewButtonType.fill}
  92. height={rpxToPx(72)}
  93. color={MainColorType.blue}
  94. title='Follow back'
  95. onClick={() => tapFollow(item)}
  96. /> :
  97. item.relation == 'FRIEND' ? <Text className="h30 g02">Friend</Text> :
  98. item.relation == 'FOLLOWING' ? <Text className="h30 g02">Following</Text> : null
  99. }
  100. </View>
  101. }
  102. if (loaded && list.length == 0)
  103. return <NoRecord />
  104. return <ScrollView style='height:100vh'
  105. enableBackToTop
  106. scrollY={true}
  107. refresherEnabled={true}
  108. upperThreshold={70}
  109. lowerThreshold={140}
  110. refresherBackground={MainColorType.g05}
  111. onRefresherRefresh={onRefresh}
  112. refresherTriggered={isPulling}
  113. onScroll={onScroll}
  114. onScrollToUpper={() => {
  115. }}
  116. onScrollToLower={more}
  117. ><View>
  118. {
  119. list.map((item, index) => {
  120. return messageItem(item, index)
  121. })
  122. }
  123. </View>
  124. </ScrollView>
  125. }