xianyu-share-demo.js 4.22 KB
import goldlog from '@ali/universal-goldlog';
import { isWeex } from '@ali/universal-env';
import { isFM, isWechat, isDD, isUC, isWB, isYK } from '../env';
import { obj2qs } from '../index';
import confirm from '../confirm/index';
import Windvane from '@weex-module/windvane';
import Navigator from '@weex-module/navigator';
import showGuideMaskInWechat from './showGuideMaskInWechat';

// h5 详情页定制逻辑,增加 itemPvid
const searchParams = new URLSearchParams(location.search);
let lxTrackParam;

if (searchParams.get('lxTrackParam')) {
  try {
    lxTrackParam = JSON.parse(searchParams.get('lxTrackParam'));
  } catch(e) {
    console.log(e);
  }
}

export default function (url, success, error, cancel, noConfirm) {

  if (typeof url === 'object') {
    let opts = url;
    url = opts.url;
    success = opts.success;
    error = opts.error;
    cancel = opts.cancel;
    noConfirm = opts.noConfirm;
    lxTrackParam = opts.lxTrackParam;
  }

  if (isWeex) {

    if (isFM) {
      Windvane.call2(
        'Base.openWindow', {
          url,
          popBeforeOpen: true
        },
        () => {},
        () => {}
      );

      return false;
    }

    Windvane.call2(
      'Base.isInstall',
      {
          ios: 'fleamarket://',
          android: 'com.taobao.idlefish'
      },
      () => {
        if (noConfirm) {
          callappByWindvaneWhenInstalled(url, success, error);
        } else {
          goldlog.record('/xyzz.5.17', 'CLK', '', 'GET');
          confirm({
            titles: ['即将离开手机淘宝', '打开“闲鱼App”'],
            cancelCallback: () => {
              goldlog.record('/xyzz.5.18', 'CLK', '', 'GET');
              cancel && cancel();
            },
            okCallback: () => {
              goldlog.record('/xyzz.5.19', 'CLK', '', 'GET');
              callappByWindvaneWhenInstalled(url, success, error);
            }
          });
        }
      },
      () => {
        error && error();
      }
    );
  } else {
    if (window.AlipayJSBridge) {

      AlipayJSBridge.call('openInBrowser', {
        url: getCallappUrl(url)
      });

      let startTime = Date.now();

      let timer = setTimeout(() => {
        if (Date.now() - startTime < 3000) {
          error && error();
        }
      }, 2000);

      document.addEventListener('visibilitychange', function () {
        if (timer) {
          clearTimeout(timer);
        }

        success && success();
      });

      return;
    } else if (isWechat) {
      showGuideMaskInWechat();
    } else if (isDD || isUC || isYK) {
      window.location.href = getCallappUrl(url);
      error && error();
    } else if (isWB) {
      url = `fleamarket://home?forward_url=${encodeURIComponent(url)}`;
      new lib.idle.callapp({
        iosNativeUrl: url,
        androidNativeUrl: url,
        lauchFailCallback: function() {
          error && error();
        }
      });
    } else if (typeof lib !== 'undefined' && lib.idle && lib.idle.callapp) {
      new lib.idle.callapp({
        iosNativeUrl: url,
        androidNativeUrl: url,
        lauchFailCallback: function() {
          error && error();
        }
      });
    }
  }
}

function getCallappUrl(url = '') {
  if (/^http/.test(url)) {
    return `fleamarket://weex?url=${encodeURIComponent(url)}`
  } else {
    return url;
  }
}

function callappByWindvaneWhenInstalled(url, success, error) {
  goldlog.record('/widle.tbMiniApp.h5detailinstall', 'CLK', '', 'GET');
  
  const gokey = obj2qs(lxTrackParam);

  if (weex.config.env.appName == 'TM') {
    Navigator.open(
      {
        url: url || 'fleamarket://home'
      },
      () => {
        goldlog.record('/widle.tbMiniApp.h5detailcallappsuccess', 'CLK', gokey, 'GET')

        success && success();
      },
      () => {
        goldlog.record('/widle.tbMiniApp.h5detailcallapperror', 'CLK', '', 'GET')

        error && error();
      }
    );
  } else {
    Windvane.call2(
      'WVClient.open',
      {
        url: getCallappUrl(url) || 'fleamarket://home'
      },
      () => {
        goldlog.record('/widle.tbMiniApp.h5detailcallappsuccess', 'CLK', gokey, 'GET')

        success && success();
      },
      () => {
        goldlog.record('/widle.tbMiniApp.h5detailcallapperror', 'CLK', '', 'GET')

        error && error();
      }
    );
  }
}