加入收藏 | 设为首页 | 会员中心 | 我要投稿 湖南网 (https://www.hunanwang.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程 > 正文

HTML5中外部赏识器唤起微信分享成果的代码

发布时间:2020-09-23 18:32:32 所属栏目:编程 来源:网络整理
导读:这篇文章首要先容了HTML5中外部赏识器唤起微信分享成果,本文通过实例代码给各人先容的很是具体,对各人的进修或事变具有必然的参考小心代价,必要的伴侣可以参考

最近在做一个手机站,要求点击分享可以直接打开微信分享出去。而不是jiathis,share分享这种的点击出来二维码。在网上看了许多,都说APP能唤起微信,手机网页实现不了。也找了许多都不能直接唤起微信。

总结出来一个可以直接唤起微信的。顺应手机qq赏识器和uc赏识器。

下面上代码,把这些直接放到要转发的页面里就可以了:

html部门:

<script src=http://www.jb51.net/html5/"mshare.js"></script>/引进mshare.js <button data-mshare="0">点击弹出原生分享面板</button> <button data-mshare="1">点击触发伴侣圈分享</button> <button data-mshare="2">点击触发发送给微信伴侣</button>

js部门:

<script> var mshare = new mShare({ title: 'Lorem ipsum dolor sit.', url: 'http://m.ly.com', desc: 'Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quaerat inventore minima voluptates.', img: 'http://placehold.it/150x150' }); $('button').click(function () { // 1 ==> 伴侣圈 2 ==> 伴侣 0 ==> 直接弹出原生 mshare.init(+$(this).data('mshare')); }); </script>

下面是mshare.js的代码分享,把这些代码新建一个js文件放进去,然后在页面中引进就ok了。

/** * 此插件首要浸染是在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; }

好了,这样就可以直接唤起微信举办分享啦

(编辑:湖南网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读