xianyu.js 2.53 KB
/**
 * Xianyu-SDK
 *
 * 与原生 APP 交互的代码
 *
 */
import queryString from 'query-string';

const xianyu = {
  /**
   * 判断是否是 APP
   */
  isAliApp: /AliApp/i.test(navigator.userAgent || ''),
  isiOS: /\(i[^;]+;( U;)? CPU.+Mac OS X/i.test(navigator.userAgent || ''),
  isAndroid: /Android/i.test(navigator.userAgent || ''),

  setXianyuWebview() {
    if (this.isAliApp && window.WindVane) {
      window.WindVane.call('WVIdleFishApi', 'setNavigationBarVisible', { visible: false }, () => {
        console.log('set ok');
      }, () => {
      });
    }
  },

  finishXianyuPage() {
    if (this.isAliApp && window.WindVane) {
      window.WindVane.call('WVIdleFishApi', 'finish', {}, () => {
        console.log('set ok');
      }, () => {
      });
    }
  },

  backXianyuPage() {
    if (this.isAliApp && window.WindVane) {
      window.WindVane.call('WVIdleFishApi', 'nativeBack', {}, () => {
        console.log('set ok');
      }, () => {
      });
    }
  },

  setXianyuTitle(args) {
    if (this.isAliApp && window.WindVane) {
      window.WindVane.call('WVIdleFishApi', 'setTitle', args, () => {
        console.log('set ok');
      }, () => {
      });
    }
  },

  setXianyuNav() {
    if (this.isAliApp && window.WindVane) {
      window.WindVane.call('WVIdleFishApi', 'setHideNavigatorRightItem', {}, () => {
        console.log('set ok');
      }, () => {
      });
    }
  },

  goXianyuNewPage(args) {
    console.log(args);
    if (!args.url) {
      return;
    }

    let urlSplit = args.url.split('?');

    if (urlSplit[1]) {
      let pageName = '';
      let pageParams = {};

      try {
        pageParams = queryString.parse(urlSplit[1]);

        if (pageParams.pagename) {
          switch (pageParams.pagename) {
            case 'productList':
              pageName = 'List';
              break;
            case 'productDetail':
              pageName = 'ProductDetail';
              break;
            default:
              break;
          }
          delete pageParams.pagename;
        }
      } catch (error) {
        console.log(error);
      }

      if (pageName) {
        return this.$router.push({
          name: pageName,
          params: pageParams
        });
      }
    }

    if (this.isAliApp && window.WindVane) {
      window.WindVane.call('Base', 'openWindow', args, () => {
        console.log('open new window success');
      }, () => {
        window.open(args.url, '_blank');
      });
    } else {
      if (args.url) {
        window.open(args.url, '_blank');
      }
    }
  },
};

export default xianyu;