1. 程式人生 > >手機端喚醒APP 、js 微信分享

手機端喚醒APP 、js 微信分享

#喚醒APP

var ifr = document.createElement("iframe"); 
            ifr.src = 'xiaosi://caizhidao:8888';
            ifr.style.display = 'none';
            document.body.appendChild(ifr);
            window.setTimeout(function(){
                document.body.removeChild(ifr);
                window.location.href = '/share/download'
            },2000);
        }

最簡單的方法

<a href="javascript:window.location.href='weixin://'">javascript:window.location.href='weixin://' </a>
<a href="weixin://">weixin:// </a>

這個方法只能喚醒微信,之後做什麼都需要使用者自己來

瀏覽器喚醒微信或qq分享只支援微信瀏覽器和uc瀏覽器

  <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script>
  <script src="mshare.js"></script>  //這個js程式碼下面會給出
</head>
<body>
<button data-mshare="0">點選彈出原生分享面板</button>  
<button data-mshare="1">點選觸發朋友圈分享</button>  
<button data-mshare="2">點選觸發傳送給微信朋友</button>  
<a href="weixin://">點選喚醒微信</a>
<a href="javascript:window.location.href='weixin://'">javascript:window.location.href='weixin://' </a>
<a href="weixin://">weixin:// </a>
<script>  
  var mshare = new mShare({  
      title: '齊楓---csdn',  //分享發標題
      url: 'https://blog.csdn.net/qiphon3650',   ///分享的連線
      desc: '一個追求技術的碼農',  /// 分享的 描述  
      img: 'http://1.jpg/150x150'   ///分享的圖片  
  });  
  $('button').click(function () {  
      // 1 ==> 朋友圈  2 ==> 朋友  0 ==> 直接彈出原生  
      mshare.init(+$(this).data('mshare'));  
  });  
  </script> 

mshare.js

/**  
 * 此外掛主要作用是在UC和QQ兩個主流瀏覽器  
 * 上面觸發微信分享到朋友圈或傳送給朋友的功能  
 */
'use strict';
var UA = navigator.appVersion;

/**  
 * 是否是 UC 瀏覽器  
 */
var uc = UA.split('UCBrowser/').length > 1 ? 1 : 0;

/**  
 * 判斷 qq 瀏覽器  
 * 然而qq瀏覽器分高低版本  
 * 2 代表高版本  
 * 1 代表低版本  
 */
var qq = UA.split('MQQBrowser/').length > 1 ? 2 : 0;

/**  
 * 是否是微信  
 */
var wx = /micromessenger/i.test(UA);

/**  
 * 瀏覽器版本  
 */
var qqVs = qq ? parseFloat(UA.split('MQQBrowser/')[1]) : 0;
var ucVs = uc ? parseFloat(UA.split('UCBrowser/')[1]) : 0;

/**  
 * 獲取作業系統資訊  iPhone(1)  Android(2)  
 */
var os = (function () {
  var ua = navigator.userAgent;

  if (/iphone|ipod/i.test(ua)) {
    return 1;
  } else if (/android/i.test(ua)) {
    return 2;
  } else {
    return 0;
  }
}());

/**  
 * qq瀏覽器下面 是否載入好了相應的api檔案  
 */
var qqBridgeLoaded = false;

// 進一步細化版本和平臺判斷  
if ((qq && qqVs < 5.4 && os == 1) || (qq && qqVs < 5.3 && os == 1)) {
  qq = 0;
} else {
  if (qq && qqVs < 5.4 && os == 2) {
    qq = 1;
  } else {
    if (uc && ((ucVs < 10.2 && os == 1) || (ucVs < 9.7 && os == 2))) {
      uc = 0;
    }
  }
}

/**  
 * qq瀏覽器下面 根據不同版本 載入對應的bridge  
 * @method loadqqApi  
 * @param  {Function} cb 回撥函式  
 */
function loadqqApi(cb) {
  // qq == 0   
  if (!qq) {
    return cb && cb();
  }

  var script = document.createElement('script');
  script.src = (+qq === 1) ? '//3gimg.qq.com/html5/js/qb.js' : '//jsapi.qq.com/get?api=app.share';

  /**  
   * 需要等載入過 qq 的 bridge 指令碼之後  
   * 再去初始化分享元件  
   */
  script.onload = function () {
    cb && cb();
  };

  document.body.appendChild(script);
}


/**  
 * UC瀏覽器分享  
 * @method ucShare  
 */
function ucShare(config) {
  // ['title', 'content', 'url', 'platform', 'disablePlatform', 'source', 'htmlID']  
  // 關於platform  
  // ios: kWeixin || kWeixinFriend;  
  // android: WechatFriends || WechatTimeline  
  // uc 分享會直接使用截圖  

  var platform = '';
  var shareInfo = null;

  // 指定了分享型別  
  if (config.type) {
    if (os == 2) {
      platform = config.type == 1 ? 'WechatTimeline' : 'WechatFriends';
    } else if (os == 1) {
      platform = config.type == 1 ? 'kWeixinFriend' : 'kWeixin';
    }
  }

  shareInfo = [config.title, config.desc, config.url, platform, '', '', ''];

  // android   
  if (window.ucweb) {
    ucweb.startRequest && ucweb.startRequest('shell.page_share', shareInfo);
    return;
  }

  if (window.ucbrowser) {
    ucbrowser.web_share && ucbrowser.web_share.apply(null, shareInfo);
    return;
  }
}


/**  
 * qq 瀏覽器分享函式  
 * @method qqShare  
 */
function qqShare(config) {
  var type = config.type;

  //微信好友 1, 微信朋友圈 8  
  type = type ? ((type == 1) ? 8 : 1) : '';

  var share = function () {
    var shareInfo = {
      'url': config.url,
      'title': config.title,
      'description': config.desc,
      'img_url': config.img,
      'img_title': config.title,
      'to_app': type,
      'cus_txt': ''
    };

    if (window.browser) {
      browser.app && browser.app.share(shareInfo);
    } else if (window.qb) {
      qb.share && qb.share(shareInfo);
    }
  };

  if (qqBridgeLoaded) {
    share();
  } else {
    loadqqApi(share);
  }
}

/**  
 * 對外暴露的介面函式  
 * @method mShare  
 * @param  {Object} config 配置物件  
 */
function mShare(config) {
  this.config = config;

  this.init = function (type) {
    if (typeof type != 'undefined') this.config.type = type;

    try {
      if (uc) {
        ucShare(this.config);
      } else if (qq && !wx) {
        qqShare(this.config);
      }
    } catch (e) {}
  }
}

// 預載入 qq bridge  
loadqqApi(function () {
  qqBridgeLoaded = true;
});

if (typeof module === 'object' && module.exports) {
  module.exports = mShare;
} else {
  window.mShare = mShare;
}

有網友給出商家介面

weixin://dl/business/?ticket=  呼叫以下介面

weixin://dl/scan 掃一掃
weixin://dl/feedback 反饋
weixin://dl/moments 朋友圈
weixin://dl/settings 設定
weixin://dl/notifications 訊息通知設定
weixin://dl/chat 聊天設定
weixin://dl/general 通用設定
weixin://dl/officialaccounts 公眾號
weixin://dl/games 遊戲
weixin://dl/help 幫助
weixin://dl/feedback 反饋
weixin://dl/profile 個人資訊
weixin://dl/features 功能外掛