• 设为首页
  • 收藏本站
  • 积分充值
  • VIP赞助
  • 手机版
  • 微博
  • 微信
    微信公众号 添加方式:
    1:搜索微信号(888888
    2:扫描左侧二维码
  • 快捷导航
    福建二哥 门户 查看主题

    HTML5中外部浏览器唤起微信分享

    发布者: 土豆服务器 | 发布时间: 2025-6-16 12:24| 查看数: 119| 评论数: 0|帖子模式

    最近在做一个手机站,要求点击分享可以直接打开微信分享出去。而不是jiathis,share分享这种的点击出来二维码。在网上看了很多,都说APP能唤起微信,手机网页实现不了。也找了很多都不能直接唤起微信。
    总结出来一个可以直接唤起微信的。适应手机qq浏览器和uc浏览器。
    下面上代码,把这些直接放到要转发的页面里就可以了:
    html部分:
    1. <script src="mshare.js"></script>//引进mshare.js
    2. <button data-mshare="0">点击弹出原生分享面板</button>
    3. <button data-mshare="1">点击触发朋友圈分享</button>
    4. <button data-mshare="2">点击触发发送给微信朋友</button>
    复制代码
    js部分:
    1. <script>
    2. var mshare = new mShare({
    3.     title: 'Lorem ipsum dolor sit.',
    4.     url: 'http://m.ly.com',
    5.     desc: 'Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quaerat inventore minima voluptates.',
    6.     img: 'http://placehold.it/150x150'
    7. });
    8. $('button').click(function () {
    9.     // 1 ==> 朋友圈  2 ==> 朋友  0 ==> 直接弹出原生
    10.     mshare.init(+$(this).data('mshare'));
    11. });
    12. </script>
    复制代码
    下面是mshare.js的代码分享,把这些代码新建一个js文件放进去,然后在页面中引进就ok了。
    1. /**
    2. * 此插件主要作用是在UC和QQ两个主流浏览器
    3. * 上面触发微信分享到朋友圈或发送给朋友的功能
    4. */
    5. 'use strict';
    6. var UA = navigator.appVersion;

    7. /**
    8. * 是否是 UC 浏览器
    9. */
    10. var uc = UA.split('UCBrowser/').length > 1 ? 1 : 0;

    11. /**
    12. * 判断 qq 浏览器
    13. * 然而qq浏览器分高低版本
    14. * 2 代表高版本
    15. * 1 代表低版本
    16. */
    17. var qq = UA.split('MQQBrowser/').length > 1 ? 2 : 0;

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

    22. /**
    23. * 浏览器版本
    24. */
    25. var qqVs = qq ? parseFloat(UA.split('MQQBrowser/')[1]) : 0;
    26. var ucVs = uc ? parseFloat(UA.split('UCBrowser/')[1]) : 0;

    27. /**
    28. * 获取操作系统信息  iPhone(1)  Android(2)
    29. */
    30. var os = (function () {
    31.     var ua = navigator.userAgent;

    32.     if (/iphone|ipod/i.test(ua)) {
    33.         return 1;
    34.     } else if (/android/i.test(ua)) {
    35.         return 2;
    36.     } else {
    37.         return 0;
    38.     }
    39. }());

    40. /**
    41. * qq浏览器下面 是否加载好了相应的api文件
    42. */
    43. var qqBridgeLoaded = false;

    44. // 进一步细化版本和平台判断
    45. if ((qq && qqVs < 5.4 && os == 1) || (qq && qqVs < 5.3 && os == 1)) {
    46.     qq = 0;
    47. } else {
    48.     if (qq && qqVs < 5.4 && os == 2) {
    49.         qq = 1;
    50.     } else {
    51.         if (uc && ((ucVs < 10.2 && os == 1) || (ucVs < 9.7 && os == 2))) {
    52.             uc = 0;
    53.         }
    54.     }
    55. }
    56. /**
    57. * qq浏览器下面 根据不同版本 加载对应的bridge
    58. * @method loadqqApi
    59. * @param  {Function} cb 回调函数
    60. */
    61. function loadqqApi(cb) {
    62.     // qq == 0
    63.     if (!qq) {
    64.         return cb && cb();
    65.     }
    66.     var script = document.createElement('script');
    67.     script.src = (+qq === 1) ? '//3gimg.qq.com/html5/js/qb.js' : '//jsapi.qq.com/get?api=app.share';
    68.     /**
    69.      * 需要等加载过 qq 的 bridge 脚本之后
    70.      * 再去初始化分享组件
    71.      */
    72.     script.onload = function () {
    73.         cb && cb();
    74.     };
    75.     document.body.appendChild(script);
    76. }
    77. /**
    78. * UC浏览器分享
    79. * @method ucShare
    80. */
    81. function ucShare(config) {
    82.     // ['title', 'content', 'url', 'platform', 'disablePlatform', 'source', 'htmlID']
    83.     // 关于platform
    84.     // ios: kWeixin || kWeixinFriend;
    85.     // android: WechatFriends || WechatTimeline
    86.     // uc 分享会直接使用截图
    87.     var platform = '';
    88.     var shareInfo = null;
    89.     // 指定了分享类型
    90.     if (config.type) {
    91.         if (os == 2) {
    92.             platform = config.type == 1 ? 'WechatTimeline' : 'WechatFriends';
    93.         } else if (os == 1) {
    94.             platform = config.type == 1 ? 'kWeixinFriend' : 'kWeixin';
    95.         }
    96.     }
    97.     shareInfo = [config.title, config.desc, config.url, platform, '', '', ''];
    98.     // android
    99.     if (window.ucweb) {
    100.         ucweb.startRequest && ucweb.startRequest('shell.page_share', shareInfo);
    101.         return;
    102.     }
    103.     if (window.ucbrowser) {
    104.         ucbrowser.web_share && ucbrowser.web_share.apply(null, shareInfo);
    105.         return;
    106.     }
    107. }
    108. /**
    109. * qq 浏览器分享函数
    110. * @method qqShare
    111. */
    112. function qqShare(config) {
    113.     var type = config.type;
    114.     //微信好友 1, 微信朋友圈 8
    115.     type = type ? ((type == 1) ? 8 : 1) : '';
    116.     var share = function () {
    117.         var shareInfo = {
    118.             'url': config.url,
    119.             'title': config.title,
    120.             'description': config.desc,
    121.             'img_url': config.img,
    122.             'img_title': config.title,
    123.             'to_app': type,
    124.             'cus_txt': ''
    125.         };
    126.         if (window.browser) {
    127.             browser.app && browser.app.share(shareInfo);
    128.         } else if (window.qb) {
    129.             qb.share && qb.share(shareInfo);
    130.         }
    131.     };
    132.     if (qqBridgeLoaded) {
    133.         share();
    134.     } else {
    135.         loadqqApi(share);
    136.     }
    137. }
    138. /**
    139. * 对外暴露的接口函数
    140. * @method mShare
    141. * @param  {Object} config 配置对象
    142. */
    143. function mShare(config) {
    144.     this.config = config;
    145.     this.init = function (type) {
    146.         if (typeof type != 'undefined') this.config.type = type;
    147.         try {
    148.             if (uc) {
    149.                 ucShare(this.config);
    150.             } else if (qq && !wx) {
    151.                 qqShare(this.config);
    152.             }
    153.         } catch (e) {}
    154.     }
    155. }
    156. // 预加载 qq bridge
    157. loadqqApi(function () {
    158.     qqBridgeLoaded = true;
    159. });
    160. if (typeof module === 'object' && module.exports) {
    161.     module.exports = mShare;
    162. } else {
    163.     window.mShare = mShare;
    164. }
    复制代码
    好了,这样就可以直接唤起微信进行分享啦
    总结
    以上所述是小编给大家介绍的HTML5中外部浏览器唤起微信分享,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!
    如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

    来源:https://www.jb51.net/html5/708579.html
    免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

    最新评论

    浏览过的版块

    QQ Archiver 手机版 小黑屋 福建二哥 ( 闽ICP备2022004717号|闽公网安备35052402000345号 )

    Powered by Discuz! X3.5 © 2001-2023

    快速回复 返回顶部 返回列表