xianyu.js 6.94 KB
/* eslint-disable */
/**
 * Xianyu-SDK
 *
 * 与原生 APP 交互的代码
 *
 */
import queryString from 'querystring';

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('setNavigationBarVisible ok');
      }, () => {
      });
    }
  },

  /*
   * @ description: 导航栏右侧自定义按钮 设为关闭
   * @ author: huzhiming
   * @ date: 2019-11-14 16:15:07
   * @ version: v1.0.0
  */
  closeNavRightItem() {

    if (this.isAliApp && window.WindVane) {
      window.WindVane.call('WVIdleFishApi', 'setHideNavigatorRightItem', {},()=>{
      },()=>{
      })
    }
  },

  /*
   * @ description: 导航栏右侧自定义按钮 设为可见
   * @ author: huzhiming
   * @ date: 2019-11-05 11:54:24
   * @ version: v1.0.0
  */
  setNavRightItem (shareParam = null, handler) {
    window._xianyuShare = () => {
      this.setXianyuShare(shareParam, handler);
    }
    // xianyu android bug: title必传切不能为空字符串,icon才能展示
    // xianyu ios bug: 图片按钮无法隐藏 TOTO ali修复
    const param =  {
      title: '∙∙∙', // 按钮名称
      icon: this.isAndroid ? ' http://cdn.yoho.cn/xianyu/share-btn.png': '',
      func: '_xianyuShare' // func: 'test' //点击调用函数 注意调用的函数必须挂载在window上
    }
    // console.log('this.isAliApp && window.WindVane', this.isAliApp , window.WindVane);
    if (this.isAliApp && window.WindVane) {
      window.WindVane.call('WVIdleFishApi', 'setNavigatorRightItem',param, function(data) {
          console.log('setNavigatorRightItem success:',data);
      }, function(e) {
          console.log('setNavigatorRightItem error:',e);
      });
    }
  },

  /*
   * @ description: 配置分享参数进行分享操作
   * @ author: huzhiming
   * @ date: 2019-11-05 10:38:24
   * @ version: v1.0.0
  */
  setXianyuShare (param = {
    shareType: 'activity', // 类型,默认activity
    image: '//img11.static.yhbimg.com/goodsimg/2018/12/24/17/01070adae9791c70ed02593550437cf30e.jpg?imageMogr2/thumbnail/600x600/background/d2hpdGU=/position/center/quality/80',
    url: '//xianyu.yohobuy.com/xianyu/index/channel?wxIsAvailable', // 分享链接
    link: '//xianyu.yohobuy.com/xianyu/index/channel?wxIsAvailable', // 和url保持
    title: '闲鱼潮品首页', // 分享标题
    text: '' // 分享描述
  }, { XY_SHARE_PRODUCT, XY_SHARE_TYPE }) {
    if (this.isAliApp && window.WindVane) {
      XY_SHARE_PRODUCT.call(this);
      window.WindVane.call('WVIdleFishApi', 'showShareMenu', param, (data)=>{
        console.log('success:', JSON.stringify(data),typeof data.isCancel);
        if (data.isCancel==='false') {
          XY_SHARE_TYPE.call(this, data);
        }
      }, function(e) {
        console.log('fail:', JSON.stringify(e));
      });
    }
  },

  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');
      }, () => {
      });
    }
  },

  /**
   *
   * @param args payURL
   * @param okAction
   * @param errorAction
   */
  setXianyuPay(args, okAction, errorAction) {
    if (this.isAliApp && window.WindVane) {
      window.WindVane.call('WVIdleFishApi', 'payAlipay', args, (data) => {
        okAction && okAction(data);
      }, (error) => {
        errorAction && errorAction(error);
      });
    }
  },

  goXianyuNewPage(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.hasOwnProperty('isNeedRefresh')) {
          pageParams.isNeedRefresh = false; // 添加禁止刷新参数
          args.url = urlSplit[0] + '?' + queryString.stringify(pageParams);
        }

        let arr = [];

        if (Object.keys(pageParams)[0] === 'openby:yohobuy') {
          for (let key in pageParams) {
            arr.push(pageParams[key]);
          }
          pageParams = JSON.parse(arr[0]).params;
          pageParams.owner = pageParams.logisticsType;
          pageParams.code = pageParams.ordercode;
        }
        if (pageParams.pagename) {
          switch (pageParams.pagename) {
            case 'productList':
              pageName = 'List';
              break;
            case 'productDetail':
              pageName = 'ProductDetail';
              break;
            case 'logisticsInfo':
              pageName = 'orderLogisticsInfo';
              break;
            case 'productListNew':
              pageName = 'List';
              break;
            default:
              break;
          }
          delete pageParams.pagename;
        }
      } catch (error) {
        console.log(error);
      }

      if (pageName) {

        let paramsV = {};

        if (pageName === 'List') {
          paramsV.query = pageParams;
        } else {
          paramsV.params = pageParams;
        }
        console.log(paramsV);
        return this.$router.push({
          name: pageName,
          ...paramsV,
        });
      }
    }

    if (this.isAliApp && window.WindVane) {
      args.url = encodeURI(decodeURI(args.url));

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

  copyToClipboard(args, okAction, errorAction) {
    if (this.isAliApp && window.WindVane) {
      window.WindVane.call('Base', 'copyToClipboard', args, data => {
        okAction && okAction(data);
      }, error => {
        errorAction && errorAction(error);
      });
    }
  },
  getClientInfo(okAction, errorAction) {
    if (this.isAliApp && window.WindVane) {
      window.WindVane.call('WVNativeDetector', 'getModelInfo', {}, data => {
        okAction && okAction(data);
      }, error => {
        errorAction && errorAction(error);
      });
    } else {
      errorAction && errorAction();
    }
  }
};

export default xianyu;