下面是实现代码。
var sdhimg = Math.ceil(Math.random()*1000); //获取0~1000的随机数
api.download({
url: goods_img,
savePath: ‘fs://ceshi’+sdhimg+’.jpg’, //这里要兼容安卓不获取重复图片,必须带上这个随机数参数。ios随意。
report: true,
cache: false,
allowResume: true
}, function(ret, err) {
// console.log(JSON.stringify(ret.savePath));
if (ret.state == 1) {
aurl = ret.savePath
}
});
下面是微信和其他方式分享。
var dialogBox = api.require(‘dialogBox’);
dialogBox.actionMenu({
rect: {
h: 180
},
texts: {
cancel: ‘取消’
},
items: [{
text: ‘微信好友’,
icon: ‘widget://image/home/share_friends.png’
}, {
text: ‘朋友圈’,
icon: ‘widget://image/home/share_pyq.png’
}, {
text: ‘其他’,
icon: ‘widget://image/home/share_more.png’
}],
styles: {
bg: ‘#FFF’,
column: 3,
itemText: {
color: ‘#000’,
size: 11,
marginT: 10
},
itemIcon: {
size: 35
},
cancel: {
bg: ‘fs://icon.png’,
color: ‘#000’,
h: 44,
size: 13
}
}
}, function(ret) {
//console.log(JSON.stringify(ret));
if (ret.index == 0) { //分享到微信
var data = {
product: obj.commodity_id,
sharepath: “/about_shoppings.html”
};
ajaxRequest('/v2/ecapi.product.get', 'post', data, true, function(ret) {
if (ret.error_code == 0) {
var fx_url = ret.product.share_url; //生成专属分享链接
//console.log(fx_url);
var wx = api.require('wx');
wx.shareWebpage({
apiKey: '',
scene: 'session',
title: sharename,
description: sharename,
thumb:"fs://"+aurl,
contentUrl: fx_url
}, function(ret, err) {
if (ret.status) {
} else {
// console.log(JSON.stringify(err));
}
});
} else {
toast(ret.error_desc);
}
});
dialogBox.close({
dialogName: 'actionMenu'
});
} else if (ret.index == 1) { //分享到其他
var data = {
product: obj.commodity_id,
sharepath: "/about_shoppings.html"
};
ajaxRequest('/v2/ecapi.product.get', 'post', data, true, function(ret) {
if (ret.error_code == 0) {
var fx_url = ret.product.share_url; //生成专属分享链接
//console.log(aimg);
var wx = api.require('wx');
wx.shareWebpage({
apiKey: '',
scene: 'timeline',
title: sharename,
description: sharename,
thumb:"fs://"+aurl,
contentUrl: fx_url
}, function(ret, err) {
if (ret.status) {} else {
//console.log(JSON.stringify(err))
}
});
} else {
toast(ret.error_desc);
}
});
dialogBox.close({
dialogName: 'actionMenu'
});
} else if (ret.index == 2) { //分享到其他
var data = {
product: obj.commodity_id,
sharepath: "/about_shoppings.html"
};
ajaxRequest('/v2/ecapi.product.get', 'post', data, true, function(ret) {
if (ret.error_code == 0) {
var fx_url = ret.product.share_url; //生成专属分享链接
//console.log(fx_url);
sharedModule.share({
path: fx_url,
type: 'url'
});
} else {
toast(ret.error_desc);
return;
}
});
dialogBox.close({
dialogName: 'actionMenu'
});
} else if (ret.eventType == "cancel") {
dialogBox.close({
dialogName: 'actionMenu'
});
}
});