TimScreen.tsx 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import { StackScreenProps } from '@react-navigation/stack';
  2. import * as React from 'react';
  3. import { useNavigation } from '@react-navigation/native';
  4. import { WebView } from 'react-native-webview';
  5. import useModel from 'flooks';
  6. import { useMount } from 'ahooks';
  7. import IM from './model';
  8. import { useTranslation } from 'react-i18next';
  9. export default function NoticeScreen({
  10. navigation,
  11. }: StackScreenProps<RootStackParamList, 'Login'>) {
  12. const { t } = useTranslation();
  13. const webRef = React.useRef();
  14. const {
  15. getChat,
  16. userID,
  17. userSig,
  18. tim,
  19. setTim,
  20. initChat,
  21. updateChatInfo,
  22. } = useModel(IM, ['userID', 'userSig', 'tim']);
  23. useMount(() => {
  24. initChat();
  25. });
  26. React.useEffect(() => {
  27. if (userSig && tim)
  28. tim.injectJavaScript(
  29. `window.chatLogin(${JSON.stringify(userID)},${JSON.stringify(userSig)})`
  30. );
  31. }, [userSig, tim]);
  32. return (
  33. <WebView
  34. ref={webRef}
  35. source={{
  36. uri: `http://dingdong.izouma.com/map/tim`,
  37. }}
  38. onMessage={({ nativeEvent }) => {
  39. console.log(nativeEvent);
  40. const info = JSON.parse(nativeEvent.data);
  41. switch (info.name) {
  42. case 'onConversationListUpdated':
  43. getChat();
  44. break;
  45. default:
  46. break;
  47. }
  48. if (info.conversationType === 'C2C') {
  49. updateChatInfo(info);
  50. }
  51. }}
  52. onLoadEnd={() => {
  53. setTim(webRef.current);
  54. }}
  55. />
  56. );
  57. }