react-native

react-native实现微信小程序分享

2019-05-16  本文已影响0人  栀子_f1b9

微信分享一直用的react-native-wechat,但是因为要做微信小程序的分享(看了几个帖子,走了半天弯路),所以在它的基础上做了修改,然后上传到了我本人的github,可以直接拉取调用,有问题欢迎评论。

    调用方法:

1.在自己项目的package.json里面添加: "react-native-wechat":"github:zhizi12/react-native-wechat#1.0.1"

2.按自己的风格yarn或者npm install,总之按照自己的方式下载下来并link即可

3.调用:整个调用都与原项目相同,以下仅做小程序分享举例(配置Key,注册等操作这里不做说明,如有疑问,直接去https://github.com/yorkie/react-native-wechat查看,完全一模一样,如果以前配置过是不需要重新配置的)

在页面做引用:import *as WeChatfrom "react-native-wechat";

调用微信小程序分享:(注意这里的userName是gh_开头的小程序的原生id,不是wx开头的,找小程序开发人员要这个id)

代码,可直接拷贝:

```

/**

* thumbImage - Thumb image of the message, which can be a uri or a resource id.  消息的Thumb图像,可以是uri或资源id

* type - Type of this message. Could be {news|text|imageUrl|imageFile|imageResource|video|audio|file|mini}

* webpageUrl - Required if type equals news or mini. The webpage link to share.  如果是网页或者小程序  网页地址

* userName - 小程序的原生id.

* path - 小程序页面的路径.

* description - 描述

* hdImageData - 小程序节点高清大图,小于128k.

* withShareTicket - 是否使用带 shareTicket 的转发

* miniProgramType - 分享小程序的版本(0-正式,1-开发,2-体验)

*

* imageUrl - Provide a remote image if type equals image. 如果type为image,则使用此分享url

* videoUrl - Provide a remote video if type equals video. 如果type为video,则使用此分享url

* musicUrl - Provide a remote music if type equals audio. 如果type为audio,则使用此分享url

* filePath - 如果type为file,则使用此获取本地文件

* fileExtension - String 如果type为file,则使用此提供文件类型

*/

WeChat.isWXAppInstalled()

.then((isInstalled) => {

if (isInstalled) {

WeChat.shareToSession({

title:'test',

description:'描述',

thumbImage:'',

type:'mini',//mini代表小程序分享

                webpageUrl:'',//必填项,哪怕写空串

                userName :'gh_XXXXXX',//必填,小程序的原生id,一定注意是gh_开头的不是wx开头的那个

                miniProgramType :1,

withShareTicket :false,

path:''  //小程序页面路径,这个因为在sdk里面用到了所以必写,哪怕就如本实例一样写空字符串

            }).then((res)=>{

Toast.show('分享成功')

}).catch((error) => {

console.log("错误信息" +JSON.stringify(error)+"完成")

});

}else {

Toast.show('没有安装微信软件,请您安装微信之后再试')

}

});

```

上一篇 下一篇

猜你喜欢

热点阅读