首先感谢@DCloud_Android_磊子,test_weixin_share_mul_pic这个是他写给我的,我只做了一点小改动
用法,先setShareMsg把当前页面的商品名称、相册存到全局变量msg,然后调用shareToWechat弹出actionSheet选择分享到朋友圈或者发送给好友。
函数代码如下:
function setShareMsg(goodsdata){
msg.content = msg.title = goodsdata.goods_name;
/* 分享图片 */
mui.each(goodsdata.pictures, function(i, n){
msg.pictures.push( n.url );
});
/* 分享链接 */
msg.href = mui.constMap.SERVER_HOST + "/mobile/goods.php?id=" + goodsdata.id ;
msg.thumbs.push("_www/72.png");
var userdata = getCachedUser();
if(userdata ){
msg.href += "&father=" + userdata.user.wx.wxid;
}
msg.brief = goodsdata.goods_brief;
getShortUrl(msg.href, function(result){
if(! result.error_code){
msg.url_short = result[0].url_short;
}
});
}
/**
* 发送分享消息
* @param {plus.share.ShareService} s
*/
function shareMessage(ex) {
var pictures = [];
var pic_unloaded = msg.pictures.length ;
function onStateChanged( d, status ) {
switch(d.state){
case 4:
if(status == 200){
console.log("图片下载完成:" + d.filename);
pictures.push( plus.io.convertLocalFileSystemURL(d.filename) );
pic_unloaded --;
if(pic_unloaded == 0){
copyToClip( msg.title + (msg.url_short || msg.href) + "\r\n" + msg.brief );
mui.toast("标题已复制,请长按黏贴");
console.log(JSON.stringify(pictures));
test_weixin_share_mul_pic(ex, pictures, msg.title + (msg.url_short || msg.href) + "\r\n" + msg.brief);
}
} else {
mui.toast("下载图片失败");
}
break;
case 1:
console.log("下载开始");
break;
case 2:
console.log("请求已响应");
break;
case 3:
console.log("下载进行中");
break;
default:
console.log("state: " + d.state);
break;
}
}
mui.toast("下载图片中,即将打开微信分享...");
mui.each(msg.pictures, function(i, n){
// 创建下载任务
var dtask = plus.downloader.createDownload( n );
dtask.addEventListener( "statechanged", onStateChanged, false );
dtask.start();
});
}
function shareToWechat(){
var shareto = ['com.tencent.mm.ui.tools.ShareToTimeLineUI', 'com.tencent.mm.ui.tools.ShareImgUI'];
plus.nativeUI.actionSheet( {title:"分享到微信",cancel:"取消",buttons:[{title:"分享到微信朋友圈"},{title:"发送给微信好友"}]}, function(e){
console.log( "User pressed: "+ e.index);
shareMessage(shareto[e.index - 1]);
} );
}
function test_weixin_share_mul_pic(ex, pics, description){
var Intent = plus.android.importClass('android.content.Intent');
var ComponentName = plus.android.importClass('android.content.ComponentName');
var ArrayList = plus.android.importClass('java.util.ArrayList');
var Uri = plus.android.importClass('android.net.Uri');
var Environment = plus.android.importClass('android.os.Environment');
var File = plus.android.importClass('java.io.File');
//var sdcardDir = plus.android.invoke(Environment.getExternalStorageDirectory(),'getAbsolutePath');
var intent = new Intent();
var localComponentName = new ComponentName("com.tencent.mm", ex);
intent.setComponent(localComponentName);
intent.setAction("android.intent.action.SEND_MULTIPLE");
intent.setType("image/*");
var localArrayList = new ArrayList();
for(var i =0;i< pics.length;i++){
var filePath = pics[i]; //sdcardDir +
//console.log('filePath=' + filePath);
localArrayList.add(Uri.fromFile(new File(filePath)));
}
intent.putParcelableArrayListExtra("android.intent.extra.STREAM", localArrayList);
intent.putExtra(Intent.EXTRA_TEXT, description);
var act = plus.android.runtimeMainActivity();
act.startActivity(intent);
}
28 个评论
要回复文章请先登录或注册
johnsm
王伟锋
fsyang
2***@qq.com
QuinJet
gangder
QuinJet
7***@qq.com
慢慢成长
深海智行