mui微信分享shareApp

//微信分享js包(可以直接新建一个js文件把代码复制进去)
plusshare.js

(function() {
    var plusReady = function(callback) {
        if (window.plus) {
            callback();
        } else {
            document.addEventListener('plusready', callback);
        }
    }
    var shareServices = {};
    var init = function() {
        plus.share.getServices(function(services) {
            for (var i = 0, len = services.length; i < len; i++) {
                shareServices[services[i].id] = services[i];
            }
        });
    };
    var isWechatInstalled = function() {
        return plus.runtime.isApplicationExist && plus.runtime.isApplicationExist({
            pname: 'com.tencent.mm',
            action: 'weixin://'
        });
    };
	var plusReady = function(callback) {
		if(window.plus) {
			callback();
		} else {
			document.addEventListener('plusready', callback);
		}
	}
	var shareServices = {};
	var init = function() {
		plus.share.getServices(function(services) {
			for(var i = 0, len = services.length; i < len; i++) {
				shareServices[services[i].id] = services[i];
			}
		});
	};
	var isWechatInstalled = function() {
		return plus.runtime.isApplicationExist && plus.runtime.isApplicationExist({
			pname: 'com.tencent.mm',
			action: 'weixin://'
		});
	};

    function share(id, msg, callback) {
        var service = shareServices[id];
        if (!service) {
            plus.nativeUI.alert('无效的分享服务[' + id + ']');
            callback && callback(false);
            return;
        }
        var _share = function() {
            service.send(msg, function() {
                plus.nativeUI.toast("分享到\"" + service.description + "\"成功!");
                callback && callback(true);
            }, function(e) {
                plus.nativeUI.toast("分享到\"" + service.description + "\"失败!");
                callback && callback(false);
            })
        };
        if (service.authenticated) {
            _share(service, msg, callback);
        } else {
            service.authorize(function() {
                _share(service, msg, callback);
            }, function(e) {
                plus.nativeUI.alert("认证授权失败");
                callback && callback(false);
            })
        }
    };
	function share(id, msg, callback) {
		var service = shareServices[id];
		if(!service) {
			callback && callback(false);
			return;
		}
		var _share = function() {
			service.send(msg, function() {
				plus.nativeUI.toast("分享到\"" + service.description + "\"成功!");
				callback && callback(true);
			}, function(e) {
				plus.nativeUI.toast("分享到\"" + service.description + "\"失败!");
				callback && callback(false);
			})
		};
		if(service.authenticated) {
			_share(service, msg, callback);
		} else {
			service.authorize(function() {
				_share(service, msg, callback);
			}, function(e) {
				console.log("认证授权失败");
				callback && callback(false);
			})
		}
	};

    function openSystem(msg, callback) {
        if (plus.share.sendWithSystem) {
            plus.share.sendWithSystem(msg, function() {
                //TODO 系统分享暂不支持回调
                //callback && callback(true);
            }, function() {
                //TODO 系统分享暂不支持回调
                //callback && callback(false);
            });
        } else {
            callback && callback(false);
        }
    }
    var open = function(msg, callback) {
        if (shareServices.weixin && isWechatInstalled()) {
            plus.nativeUI.actionSheet({
                title: '分享到',
                cancel: "取消",
                buttons: [{
                    title: "微信消息"
                }, {
                    title: "微信朋友圈"
                }]
            }, function(e) {
                var index = e.index;
                switch (index) {
                    case 1: //分享到微信好友
                        msg.extra = {
                            scene: 'WXSceneSession'
                        };
                        share('weixin', msg, callback);
                        break;
                    case 2: //分享到微信朋友圈
                        msg.title = msg.content;
                        msg.extra = {
                            scene: 'WXSceneTimeline'
                        };
                        share('weixin', msg, callback);
                        break;
                    case 3: //更多分享
                        var url = msg.href ? ('( ' + msg.href + ' )') : '';
                        msg.title = msg.title + url;
                        msg.content = msg.content + url;
                        openSystem(msg, callback);
                        break;
                }
            })
        } else {
            //系统分享
            var url = msg.href ? ('( ' + msg.href + ' )') : '';
            msg.title = msg.title + url;
            msg.content = msg.content + url;
            openSystem(msg, callback);
        }
    };
    plusReady(init);
    window.plusShare = open;
	function openSystem(msg, callback) {
		if(plus.share.sendWithSystem) {
			plus.share.sendWithSystem(msg, function() {
				//TODO 系统分享暂不支持回调
				//callback && callback(true);
			}, function() {
				//TODO 系统分享暂不支持回调
				//callback && callback(false);
			});
		} else {
			callback && callback(false);
		}
	}
	var open = function(msg, callback) {
		/**
		 *如下情况直接打开系统分享
		 * 1、未配置微信分享通道
		 * 2、用户手机未安装威胁你
		 * 3、360浏览器下
		 */

		if(shareServices.weixin && isWechatInstalled() && !/360\sAphone/.test(navigator.userAgent)) {
			plus.nativeUI.actionSheet({
				title: '分享到',
				cancel: "取消",
				buttons: [{
					title: "微信消息"
				}, {
					title: "微信朋友圈"
				}]
			}, function(e) {
				var index = e.index;
				switch(index) {
					case 1: //分享到微信好友
						msg.extra = {
							scene: 'WXSceneSession'
						};
						share('weixin', msg, callback);
						break;
					case 2: //分享到微信朋友圈
						msg.title = msg.content;
						msg.extra = {
							scene: 'WXSceneTimeline'
						};
						share('weixin', msg, callback);
						break;
					case 3: //更多分享
						var url = msg.href ? ('( ' + msg.href + ' )') : '';
						msg.title = msg.title + url;
						msg.content = msg.content + url;
						openSystem(msg, callback);
						break;
				}
			})
		} else {
			//系统分享
			var url = msg.href ? ('( ' + msg.href + ' )') : '';
			msg.title = msg.title + url;
			msg.content = msg.content + url;
			openSystem(msg, callback);
		}
	};
	plusReady(init);
	window.plusShare = open;
})();

引入分享js

<script type="text/javascript" src="../../js/plusshare.js"></script>
function shareApp(id) {
				mui.ajax(site_url + 'personal/generateQRCode', {
					data: {
						token: plus.storage.getItem('token')
					},
					dataType: 'json',
					type: 'post',
					timeout: 10000,
					headers: {
						'content-type': 'application/x-www-form-urlencoded'
					},
					success: function(data) {
						if(data.status == 1) {
							invideCode = data.invideCode;
						} else if(data.status == 5){
							invideCode = data.invideCode;
							mui.toast('抱歉不是会员不得推荐积分');
						}
					},
					error: function(xhr, type, errorThrown) {
						mui.toast('网络错误');
					}
				});
				var message = {
					//title: appName, //应用名字
					//content: shareTitle,
					//href: "***********************/regWeb/bounds?invideCode=" + invideCode, //分享出去后,点击跳转地址
					//thumbs: ["**********************/zzg.png"] //分享缩略图
					pictures:["******************/static/img/"+id+".png"]
				}
				//调起分享
				plusShare(message, function(res) {
					//分享回调函数
					if(res) {
						plus.nativeUI.toast("分享成功");
						mui.ajax(site_url + 'personal/shareAddRestPoint', {
							data: {
								token: plus.storage.getItem('token')
							},
							dataType: 'json',
							type: 'post',
							timeout: 10000,
							headers: {
								'content-type': 'application/x-www-form-urlencoded'
							},
							success: function(data) {
								if(data.status == 1) {
									mui.toast('谢谢分享');
									
								}else{
									mui.toast('谢谢分享');
								}
								
							},
							error: function(xhr, type, errorThrown) {
								mui.toast('网络错误');
							}
						});	
					} else {
						plus.nativeUI.toast("分享失败");
					}
				})
			}
			

最后HBuilder勾选
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值