该文档为平时开发中遇到的关于微信转发的一些小问题,故将此记录下来,如有遇到一样问题的猿友,愿能够解决你的问题。可能有些地方写的不是很好,请轻喷。若还有其余的看法,也敬请提出,互相学习。api
登陆apiCloud官网模块商店,寻找到微信模块,并添加到对应的app项目中 【该模块为无偿使用】bash
添加好以后,需进入微信开发平台申请对应项目的key,须要app包名、 app icon,这个过程大概须要一天左右的时间。 具体能够参考微信模块文档微信
一、微信只能分享本地的图片,不能分享网络图片,因此须要先下载后再去进行转发
二、微信对于照片的大小存在,不能超过10M【后面会提到一种参考的解决方法】
三、config.xml中需配置正确的微信开发平台申请的项目KEY
复制代码
let wx = api.require('wx');
wx.isInstalled(function(ret, err) { // 先判断手机端是否存在微信客户端
if (ret.installed) {
// 下载,并判断图片是否过大于10M,则不能分享
api.download({
url: 'http://xxxxx', //网络图片下载 url
savePath: `fs://image/shareImg${new Date().getTime()}_${parseInt(Math.random() * 1000)}.png`, //图片存储路径,随机命名图片
report: true,
cache: true,
allowResume: true
}, function(ret, err) {
that.isLoading = true
if (ret.state == 1) {
that.isLoading = false
if (ret.fileSize && ret.fileSize >= 10 * 1000 * 100) {
api.toast({
msg: '图片大于10M,没法分享',
duration: 1500,
location: 'middle'
})
return
}
wx.shareImage({
scene : 'session',
contentUrl : ret.savePath // 此处使用savePath便可,无需使用fs
}, function(ret, err) {
if (ret.status) {
api.toast({
msg: '分享成功',
duration: 1500,
location: 'middle'
})
} else {
api.toast({
msg: '分享失败,错误代码为' + err.code,
duration: 1500,
location: 'middle'
})
}
});
} else if (ret.state == 0){
// api.toast({
// msg: '图片下载中',
// duration: 1000,
// location: 'middle'
// })
} else {
that.isLoading = false
api.toast({
msg: '图片下载失败,没法分享',
duration: 1500,
location: 'middle'
})
}
});
} else {
api.toast({
msg: '当前设备未安装微信客户端',
duration: 1500,
location: 'middle'
})
}
})
}
复制代码
能够使用apiCloud中api对象的imageCache,缺点就是分享的图片会出现模糊。网络
重复处理压缩图片质量大小,使其质量小于10Msession