封装Taro的webview组件

import { useState } from 'react';
import { WebView } from '@tarojs/components';
import Taro, { useReady, useShareAppMessage } from '@tarojs/taro';
import { useRouter } from 'taro-hooks';

const WebViewPage = () => {
  const [routerInfo] = useRouter();
  const openId = Taro.getStorageSync('openId');
  const cookies = Taro.getStorageSync(COOKIES);
  const { params, path } = routerInfo;
  const { title } = params;
  const [readyUrl, setReadyUrl] = useState('');

  useReady(() => {
    const realUrl = Taro?.getCurrentInstance()?.router?.params?.url;
    setReadyUrl(realUrl || '');
  });

  useShareAppMessage(() => ({
    title: title || '有来医生·权威健康科普平台',
    path: `/${path}?url=${readyUrl}&title=${title}`,
  }));

  const decodeUrl = decodeURIComponent(readyUrl || '');
  const qMark = decodeUrl.indexOf('?') > -1 ? '' : '?';

  const src = `${decodeUrl}${qMark}&source=yl_mini_program&customersalt=${encodeURIComponent(
    cookies || '',
  )}&cuid=${encodeURIComponent(cookies || '')}&open_id=${openId}`;

  if (readyUrl) {
    return <WebView src={src} />;
  }
};

export default WebViewPage;

具体用法如下:

Taro.redirectTo({
        url: `/pages/webview/webview?url=${encodeURIComponent(combUidToUrl(uid))}`,
      });

你可能感兴趣的:(taro)