relation.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. import { MainColorType } from "@/context/themes/color";
  2. import { followUser, getMyFollowers, getMyFollowings, getMyFriends } from "@/services/friend";
  3. import { View, Image, Text, ScrollView } from "@tarojs/components";
  4. import { useEffect, useState } from "react";
  5. import './relation.scss'
  6. import NewButton, { NewButtonType } from "@/_health/base/new_button";
  7. import { rpxToPx } from "@/utils/tools";
  8. import { jumpPage } from "@/features/trackTimeDuration/hooks/Common";
  9. import { IconArrow } from "@/components/basic/Icons";
  10. import Taro from "@tarojs/taro";
  11. import NoRecord from "@/_health/components/no_record";
  12. export default function Relation() {
  13. const [count, setCount] = useState(0)
  14. const [friends, setFriends] = useState<any>([])
  15. const [followers, setFollowers] = useState<any>([])
  16. const [followings, setFollowings] = useState<any>([])
  17. const [index, setIndex] = useState(0)
  18. const [list, setList] = useState<any>([])
  19. const [loaded, setLoaded] = useState(false)
  20. const [page, setPage] = useState(1)
  21. const [isPulling, setIsPulling] = useState(false)
  22. const [isLoading, setIsLoading] = useState(false)
  23. const [total, setTotal] = useState(-1)
  24. const systemInfo: any = Taro.getWindowInfo ? Taro.getWindowInfo() : Taro.getSystemInfoSync();
  25. const navigationBarHeight = systemInfo.statusBarHeight + 44;
  26. useEffect(() => {
  27. // getRelation()
  28. // getFollowers()
  29. // getFollowings()
  30. Taro.eventCenter.on('followUser', listenFollowUser)
  31. Taro.eventCenter.on('unfollowUser', listenUnfollowUser)
  32. setPage(1)
  33. getList(1)
  34. return () => {
  35. // Taro.eventCenter.off('followUser')
  36. // Taro.eventCenter.off('unfollowUser')
  37. }
  38. }, [])
  39. useEffect(() => {
  40. setPage(1)
  41. getList(1)
  42. }, [index])
  43. function getList(i) {
  44. setPage(i)
  45. if (index == 0) {
  46. getRelation(i)
  47. }
  48. else if (index == 1) {
  49. getFollowings(i)
  50. }
  51. else {
  52. getFollowers(i)
  53. }
  54. }
  55. function onRefresh() {
  56. setIsPulling(true)
  57. setPage(1)
  58. getList(1)
  59. }
  60. function more() {
  61. if (isLoading) return
  62. if (total == list.length) return
  63. var i = page + 1
  64. getList(i)
  65. }
  66. function listenFollowUser(e) {
  67. console.log('follow user', e)
  68. // getRelation()
  69. // getFollowers()
  70. // getFollowings()
  71. getList(1)
  72. }
  73. function listenUnfollowUser(e) {
  74. console.log('unfollow user', e)
  75. // getRelation()
  76. // getFollowers()
  77. // getFollowings()
  78. getList(1)
  79. }
  80. function getRelation(i) {
  81. setPage(i)
  82. setIsLoading(true)
  83. getMyFriends({
  84. page: i,
  85. limit: 20
  86. }).then(res => {
  87. setIsLoading(false)
  88. setLoaded(true)
  89. setIsPulling(false)
  90. setCount((res as any).total)
  91. // setFriends((res as any).data)
  92. if (i == 1) {
  93. setList((res as any).data)
  94. }
  95. else {
  96. setList([...list, ...(res as any).data])
  97. }
  98. }).catch(e => {
  99. setIsLoading(false)
  100. setLoaded(true)
  101. setIsPulling(false)
  102. })
  103. }
  104. function getFollowers(i) {
  105. setPage(i)
  106. setIsLoading(true)
  107. getMyFollowers({
  108. page: i,
  109. limit: 20
  110. }).then(res => {
  111. setIsLoading(false)
  112. setLoaded(true)
  113. setIsPulling(false)
  114. setCount((res as any).total)
  115. // setFollowers((res as any).data)
  116. if (i == 1) {
  117. setList((res as any).data)
  118. }
  119. else {
  120. setList([...list, ...(res as any).data])
  121. }
  122. }).catch(e => {
  123. setIsLoading(false)
  124. setLoaded(true)
  125. setIsPulling(false)
  126. })
  127. }
  128. function getFollowings(i) {
  129. setPage(i)
  130. setIsLoading(true)
  131. getMyFollowings({
  132. page: i,
  133. limit: 20
  134. }).then(res => {
  135. setIsLoading(false)
  136. setLoaded(true)
  137. setIsPulling(false)
  138. setCount((res as any).total)
  139. // setFollowings((res as any).data)
  140. if (i == 1) {
  141. setList((res as any).data)
  142. }
  143. else {
  144. setList([...list, ...(res as any).data])
  145. }
  146. }).catch(e => {
  147. setIsLoading(false)
  148. setLoaded(true)
  149. setIsPulling(false)
  150. })
  151. }
  152. function friendsList() {
  153. return <View>
  154. {/* <View>好友数量{count}</View> */}
  155. {
  156. friends.map((item, index) => {
  157. return followItem(item, index * 1000)
  158. })
  159. }
  160. </View>
  161. }
  162. function followerList() {
  163. return <View>
  164. {/* <View>我的关注{count2}</View> */}
  165. {
  166. followers.map((item, index) => {
  167. return followItem(item, index * 1000)
  168. })
  169. }
  170. </View>
  171. }
  172. function followingList() {
  173. return <View>
  174. {/* <View>我的粉丝{count3}</View> */}
  175. {
  176. followings.map((item, index) => {
  177. return followItem(item, index)
  178. })
  179. }
  180. </View>
  181. }
  182. function userList() {
  183. return <View>
  184. {/* <View>我的粉丝{count3}</View> */}
  185. {
  186. list.map((item, index) => {
  187. return followItem(item, index)
  188. })
  189. }
  190. </View>
  191. }
  192. function tapFollow(item) {
  193. var params: any = {
  194. follow_origin: 'WECHAT_PRIVATE_CHAT',
  195. user_id: item.id,
  196. }
  197. followUser(params).then(res => {
  198. // getProfile()
  199. Taro.eventCenter.trigger('followUser', item.id)
  200. debugger
  201. })
  202. }
  203. function followItem(item, index) {
  204. return <View key={index} className="relation_item" onClick={() => {
  205. jumpPage('./home?uid=' + item.id)
  206. }}>
  207. <Image src={item.avatar} className="relation_avatar" />
  208. <Text className="h34" style={{ color: MainColorType.link, flex: 1 }}>{item.nickname}</Text>
  209. {/* <Text>{item.relation}</Text> */}
  210. {
  211. item.relation == 'FRIEND' && <Text className="h30 g02">Friend</Text>
  212. }
  213. {
  214. item.relation == 'FOLLOWING' && <Text className="h30 g02">Following</Text>
  215. }
  216. {
  217. item.relation == 'FOLLOWER' && <NewButton
  218. title="Follow"
  219. type={NewButtonType.alpha}
  220. color={MainColorType.blue}
  221. width={rpxToPx(136)}
  222. height={rpxToPx(72)}
  223. onClick={() => {
  224. tapFollow(item)
  225. }}
  226. />
  227. }
  228. <IconArrow width={rpxToPx(34)} color={MainColorType.g02} />
  229. <View className="border_footer_line" style={{ left: rpxToPx(150) }} />
  230. </View>
  231. }
  232. function onScroll() {
  233. }
  234. // if (loaded && list.length == 0)
  235. // return <NoRecord />
  236. return <View >
  237. <View style={{
  238. position: 'fixed',
  239. top: 0,
  240. left: 0,
  241. zIndex: 10,
  242. height: navigationBarHeight, width: rpxToPx(750), backgroundColor: '#f5f5f5', display: 'flex',
  243. flexDirection: 'column', justifyContent: 'flex-end'
  244. }}>
  245. <View style={{ height: 44, width: rpxToPx(750), position: 'relative', display: 'flex', flexDirection: 'row', alignItems: 'center', justifyContent: 'center' }}>
  246. {/* <View onClick={() => {
  247. Taro.navigateBack()
  248. }} style={{ position: 'absolute', left: 0, top: 0, bottom: 0, width: 80, display: 'flex', alignItems: 'center', justifyContent: 'center' }}>Back</View> */}
  249. <Image src={require('@assets/_health/navi_back.png')} style={{
  250. position: 'absolute',
  251. width: rpxToPx(92),
  252. height: rpxToPx(64),
  253. left: 0,
  254. top: 22 - rpxToPx(32)
  255. }}
  256. onClick={() => {
  257. Taro.navigateBack()
  258. }}
  259. />
  260. <View style={{ display: 'flex', flexDirection: 'row', height: 44, alignItems: 'center', justifyContent: 'center' }}>
  261. <View className="relation_tab_item" onClick={() => { setIndex(0) }}>
  262. <View style={{ color: index == 0 ? '#000' : MainColorType.g02, fontWeight: 'bold' }}>搭子</View>
  263. <View className={index == 0 ? 'sel_line1' : 'line'}></View>
  264. </View>
  265. <View className="relation_tab_item" onClick={() => { setIndex(1) }}>
  266. <View style={{ color: index == 1 ? '#000' : MainColorType.g02, fontWeight: 'bold' }}>关注</View>
  267. <View className={index == 1 ? 'sel_line1' : 'line'}></View>
  268. </View>
  269. <View className="relation_tab_item" onClick={() => { setIndex(2) }}>
  270. <View style={{ color: index == 2 ? '#000' : MainColorType.g02, fontWeight: 'bold' }}>粉丝</View>
  271. <View className={index == 2 ? 'sel_line1' : 'line'}></View>
  272. </View>
  273. {/* <View style={{ flex: 1, color: index == 0 ? '#000' : MainColorType.g02, textAlign: 'center' }} onClick={() => { setIndex(0) }}>搭子</View>
  274. <View style={{ flex: 1, color: index == 1 ? '#000' : MainColorType.g02, textAlign: 'center' }} onClick={() => { setIndex(1) }}>关注</View>
  275. <View style={{ flex: 1, color: index == 2 ? '#000' : MainColorType.g02, textAlign: 'center' }} onClick={() => { setIndex(2) }}>粉丝</View> */}
  276. </View>
  277. </View>
  278. </View><ScrollView style={{
  279. marginTop: navigationBarHeight,
  280. height: '95vh'
  281. }}
  282. enableBackToTop
  283. scrollY={true}
  284. refresherEnabled={true}
  285. upperThreshold={70}
  286. lowerThreshold={140}
  287. refresherBackground={MainColorType.g05}
  288. onRefresherRefresh={onRefresh}
  289. refresherTriggered={isPulling}
  290. onScroll={onScroll}
  291. onScrollToUpper={() => {
  292. }}
  293. onScrollToLower={more}
  294. ><View >
  295. {
  296. userList()
  297. }
  298. {
  299. loaded && list.length == 0 && <NoRecord />
  300. }
  301. {/* {
  302. index == 0 && friendsList()
  303. }
  304. {
  305. index == 1 && followingList()
  306. }
  307. {
  308. index == 2 && followerList()
  309. } */}
  310. </View>
  311. </ScrollView>
  312. </View>
  313. }