【cordova ionic2 微信分享】使用 cordova-plugin-wechat 做微信分享的注意点

来源:互联网 发布:韩家炜 数据挖掘 pdf 编辑:程序博客网 时间:2024/05/17 23:24

cordova-plugin-wechat在github的地址:

https://github.com/xu-li/cordova-plugin-wechat


主要常用功能:

检查是否安装微信

(<any>window).Wechat.isInstalled(installed => {  if (installed) {    // todo when installed  } else {    // todo when uninstalled  }}, reason => {  // todo when uninstalled});


检查是否安装微信+微信授权

(<any>window).Wechat.isInstalled(function (installed) {  if(installed){    var scope = "snsapi_userinfo",    state = "_" + (+new Date());    (<any>window).Wechat.auth(scope, state, function (response) {      // auth success with response    }, function (reason) {      // auth failed with reason    });  }else{    // todo when uninstalled    alert("您还没有安装微信,请先安装微信。");  }}, function (reason) {  // todo when uninstalled});


分享图片到朋友圈

(<any>window).Wechat.share({  message: {    title: shareTitle,    description: shareDesc,    thumb: shareThumb,    media: {      type: (<any>window).Wechat.Type.IMAGE,      image: shareImg    }  },  scene: (<any>window).Wechat.Scene.TIMELINE   // share to Timeline}, () => {  // todo when success}, reason => {  // todo when error or cancel});


分享链接到朋友圈

(<any>window).Wechat.share({  message: {    title: shareTitle,    description: shareDesc,    thumb: shareThumb,    media: {      type: (<any>window).Wechat.Type.WEBPAGE,      webpageUrl: shareUrl    }  },  scene: (<any>window).Wechat.Scene.TIMELINE   // share to Timeline}, function () {  // todo when success}, function (reason) {  // todo when error or cancel});


分享图片给朋友

(<any>window).Wechat.share({  message: {    title: shareTitle,    description: shareDesc,    thumb: shareThumb,    media: {      type: (<any>window).Wechat.Type.IMAGE,      image: shareImg    }  },  scene: (<any>window).Wechat.Scene.SESSION   // share to Friend}, () => {  // todo when success}, reason => {  // todo when error or cancel});

分享链接给朋友

(<any>window).Wechat.share({  message: {    title: shareTitle,    description: shareDesc,    thumb: shareThumb,    media: {      type: (<any>window).Wechat.Type.WEBPAGE,      webpageUrl: shareUrl    }  },  scene: (<any>window).Wechat.Scene.SESSION   // share to friend}, function () {  // todo when success}, function (reason) {  // todo when error or cancel});


使用时需要申请微信开放平台的应用,安装插件时需要把申请来的WECHATAPPID填写在对应位置,这里就不赘述了。



其中上述方法中,缩略图和图片的地址有以下一些限制需要注意:


1、可以使用远程的图片地址(Android和iOS均可以),但是图片地址长度必须小于等于293个字符(超出长度分享的图片将会无法获取到)。像是ucloud缩略图的地址要加签名、缩略图指令,如果还要加上水印的指令,url的长度就会超出,这里的解决方案是用自己的服务的短一点的链接地址接一下,并不需要返回图片流,而是直接在自己链接中跳转到ucloud的原始地址即可。


2、Android可以使用保存到本地的图片地址(file://XXX/XXX/123.jpg这种)(把远程图片用fileTransfer下载到本地这种),但是需要把file://替换为/(即地址需填写为/XXX/XXX/123.jpg),此方法iOS不适用


3、可以使用图片的 base64数据作为图片地址(Android和iOS均可以)(把远程图片下载到本地,然后用canvas转一下图片为base64的内容(因为canvas不能跨域,所以需要先下载到本地)),对于需要保存到本地之后分享的iOS,需要使用此方法,因为2那个方法iOS不适用


4、可以使用打在包里的图片作为图片地址(www/assets/imgs/share-img.png这种)(Android和iOS均可以)



另外,针对2、3的图片,如果每次分享都会使用新下载一次的图片的话,建议离开页面时,删除下载到本地的图片

阅读全文
0 0
原创粉丝点击