web123456

Mobile browser calls WeChat to realize sharing

I am recently building a mobile site and asked to click on sharing to open WeChat to share directly. Instead of jiathis, share this kind of click to get the QR code. I have read a lot on the Internet, and they all say that APP can evoke WeChat, but mobile web pages cannot be realized. I also found a lot of them but couldn't call WeChat directly.

Summarize one that can directly evoke WeChat. Adapt to mobile QQ browsers and uc browsers.

The following code is listed and just put these directly on the page to be forwarded:

html part:

<script src=""></script>//Introduction
 <button data-mshare="0">Click to pop up the native sharing panel</button>
 <button data-mshare="1">Click to trigger sharing in the circle of friends</button>
 <button data-mshare="2">Click to trigger send to WeChat friends</button>


js part:

<script>
 var mshare = new mShare({
     title: 'Lorem ipsum dolor sit.',
     url: '',
     desc: 'Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quaerat inventore minima voluptates.',
     img: '/150x150'
 });
 $('button').click(function () {
     // 1 ==> Moments 2 ==> Friends 0 ==> Pop up the native directly
     (+$(this).data('mshare'));
 });
 </script>


The following is the code sharing. Create a new js file and put it in, and then introduce it on the page.

/**
  * This plugin is mainly used in two mainstream browsers: UC and QQ
  * The function above triggers WeChat to share to friends or send to friends
  */
 'use strict';
 var UA = ;

 /**
  * Is it a UC browser?
  */
 var uc = ('UCBrowser/').length > 1 ? 1 : 0;

 /**
  * Judge qq browser
  * However, the QQ browser is divided into high and low versions
  * 2 represents a higher version
  * 1 represents the lower version
  */
 var qq = ('MQQBrowser/').length > 1 ? 2 : 0;

 /**
  * Is it WeChat?
  */
 var wx = /micromessenger/(UA);

 /**
  * Browser version
  */
 var qqVs = qq ? parseFloat(('MQQBrowser/')[1]) : 0;
 var ucVs = uc ? parseFloat(('UCBrowser/')[1]) : 0;

 /**
  * Get operating system information iPhone(1) Android(2)
  */
 var os = (function () {
     var ua = ;

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

 /**
  * Under the qq browser, is the corresponding API file loaded?
  */
 var qqBridgeLoaded = false;

 // Further refine version and platform judgment
 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;
         }
     }
 }

 /**
  * Load the corresponding bridge according to different versions below the qq browser
  * @method loadqqApi
  * @param {Function} cb callback function
  */
 function loadqqApi(cb) {
     // qq == 0
     if (!qq) {
         return cb && cb();
     }

     var script = ('script');
      = (+qq === 1) ? '///html5/js/' : '///get?api=';

     /**
      * You need to wait for the qq bridge script to load
      * Then, go to initialize the sharing component
      */
      = function () {
         cb && cb();
     };

     (script);
 }


 /**
  * UC browser sharing
  * @method ucShare
  */
 function ucShare(config) {
     // ['title', 'content', 'url', 'platform', 'disablePlatform', 'source', 'htmlID']
     // About platform
     // ios: kWeixin || kWeixinFriend;
     // android: WeChatFriends || WeChatTimeline
     //UC Sharing will use screenshots directly

     var platform = '';
     var shareInfo = null;

     // Specified sharing type
     if () {
         if (os == 2) {
             platform = == 1 ? 'WechatTimeline' : 'WechatFriends';
         } else if (os == 1) {
             platform = == 1 ? 'kWeixinFriend' : 'kWeixin';
         }
     }

     shareInfo = [, , , platform, '', '', ''];

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

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


 /**
  * qq browser sharing function
  * @method qqShare
  */
 function qqShare(config) {
     var type = ;

     //WeChat friends 1, WeChat Moments 8
     type = type ? ((type == 1) ? 8 : 1) : '';

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

         if () {
              && (shareInfo);
         } else if () {
              && (shareInfo);
         }
     };

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

 /**
  * Exposed interface functions
  * @method mShare
  * @param {Object} config configuration object
  */
 function mShare(config) {
      = config;

      = function (type) {
         if (typeof type != 'undefined') = type;

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

 // Preload qq bridge
 loadqqApi(function () {
     qqBridgeLoaded = true;
 });

 if (typeof module === 'object' && ) {
      = mShare;
 } else {
      = mShare;
 }
OK, so you can directly call WeChat to share