import * as WeChat from 'react-native-wechat'; import request from '../utils/request'; import AndroidUtil from '../../util/AndroidUtil'; /** public static String WX_APPID = "wx51acc19c8f7a0f6f"; public static String WX_SECRET = "e830d45f497025041269ef6221140c3d"; */ WeChat.registerApp('wx51acc19c8f7a0f6f'); export default class wechat { //登录方法 static wechatLogin(callback) { WeChat.isWXAppInstalled().then((isInstalled) => { if (isInstalled) { WeChat.sendAuthRequest('snsapi_userinfo').then((result) => { var object = JSON.parse(JSON.stringify(result)); var loginUrl = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx51acc19c8f7a0f6f&secret=e830d45f497025041269ef6221140c3d&code=' + object['code'] + '&grant_type=authorization_code'; request(loginUrl).then((res) => { // console.log('code请求下一步返回结果:' + res['openid']); // callback(res); var getUserUrl = 'https://api.weixin.qq.com/sns/userinfo?access_token=' + res['access_token'] + '&openid=' + res['openid']; request(getUserUrl).then((user) => { console.log('微信返回信息'); console.log('user', user); console.log('===================================='); callback(user); }); }); }); } else { alert('请安装微信'); } }); } /** * 好友文字分享 * @param {string} descriptiontext 测试微信好友分享的文本内容 */ static shareToSessionText(descriptiontext) { WeChat.isWXAppInstalled().then((isInstalled) => { if (isInstalled) { WeChat.shareToSession({ type: 'text', description: descriptiontext }) .then((result) => { console.log(result); }) .catch((error) => { error(error.message); }); } else { alert('请安装微信'); } }); } /** * 微信好友分享链接 * @param {string} title_text 分享的标题 * @param {string} description_text 分享的标题内容 * @param {string} thumbImage_url 分享的标题图片 * @param {string} webpageUrl_url 分享的链接 */ static shareToSessionNews(title_text, description_text, thumbImage_url, webpageUrl_url) { WeChat.isWXAppInstalled().then((isInstalled) => { if (isInstalled) { WeChat.shareToSession({ title: title_text, description: description_text, thumbImage: thumbImage_url, type: 'news', webpageUrl: webpageUrl_url }).catch((error) => { alert(error.message); }); } else { alert('请安装微信'); } }); } /** * 微信朋友圈分享的文本 * @param {string} description_text 测试微信朋友圈分享的文本内容 */ static shareToTimelineText(description_text) { WeChat.isWXAppInstalled().then((isInstalled) => { if (isInstalled) { WeChat.shareToTimeline({ type: 'text', description: description_text }).catch((error) => { console.log(error.message); }); } else { alert('请安装微信'); } }); } /** * 微信朋友圈分享链接 * @param {string} title_text 分享的标题 * @param {string} description_text 分享的标题内容 * @param {string} thumbImage_url 分享的标题图片 * @param {string} webpageUrl_url 分享的链接 */ static shareToTimelineNews(title_text, description_text, thumbImage_url, webpageUrl_url) { WeChat.isWXAppInstalled().then((isInstalled) => { if (isInstalled) { WeChat.shareToTimeline({ title: title_text, description: description_text, thumbImage: thumbImage_url, type: 'news', webpageUrl: webpageUrl_url }).catch((error) => { console.log(error.message); }); } else { alert('请安装微信'); } }); } /** * 微信支付 * @param {string} partnerId 商家向财付通申请的商家id * @param {string} prepayId 预支付订单 * @param {string} nonceStr 随机串,防重发 * @param {string} timeStamp 时间戳,防重发. * @param {string} package 商家根据财付通文档填写的数据和签名 * @param {string} sign 商家根据微信开放平台文档对数据做的签名 */ static pay(partnerId, prepayId, nonceStr, timeStamp, packages, sign, callback) { WeChat.isWXAppInstalled().then((isInstalled) => { if (isInstalled) { WeChat.pay({ partnerId: partnerId, // 商家向财付通申请的商家id prepayId: prepayId, // 预支付订单 nonceStr: nonceStr, // 随机串,防重发 timeStamp: timeStamp, // 时间戳,防重发. package: packages, // "Sign=WXPay", // 商家根据财付通文档填写的数据和签名 sign: sign // 商家根据微信开放平台文档对数据做的签名 }) .then((requestJson) => { // //支付成功回调 // if (requestJson.errCode == '0') { // //回调成功处理 // } callback(requestJson); }) .catch((err) => { alert('支付失败:' + err); }); } else { alert('请安装微信'); } }); } /** 跳转小程序(需要改 react-native-wechat插件 1.删除react-native-android默认的jar包,直接引用微信官网的包,或者下载官方最新包wechat-sdk-android-with-mta-x.x.x.jar 2.在react-native-wechat插件的index.js中,增加toMiniProgram方法) 3.在react-native-wechat插件WeChatModule中,删除旧的引用,增加新的引用 import com.tencent.mm.opensdk.constants.ConstantsAPI; import com.tencent.mm.opensdk.modelbase.BaseReq; import com.tencent.mm.opensdk.modelbase.BaseResp; import com.tencent.mm.opensdk.modelbiz.WXLaunchMiniProgram; import com.tencent.mm.opensdk.modelmsg.SendAuth; import com.tencent.mm.opensdk.modelmsg.SendMessageToWX; import com.tencent.mm.opensdk.modelmsg.WXFileObject; import com.tencent.mm.opensdk.modelmsg.WXImageObject; import com.tencent.mm.opensdk.modelmsg.WXMediaMessage; import com.tencent.mm.opensdk.modelmsg.WXMusicObject; import com.tencent.mm.opensdk.modelmsg.WXTextObject; import com.tencent.mm.opensdk.modelmsg.WXVideoObject; import com.tencent.mm.opensdk.modelmsg.WXWebpageObject; import com.tencent.mm.opensdk.modelpay.PayReq; import com.tencent.mm.opensdk.modelpay.PayResp; import com.tencent.mm.opensdk.openapi.IWXAPI; import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler; import com.tencent.mm.opensdk.openapi.WXAPIFactory; 4.在react-native-wechat插件WeChatModule中,增加跳转小程序方法 @ReactMethod public void toMiniProgram(String programId, String path) { WXLaunchMiniProgram.Req req = new WXLaunchMiniProgram.Req(); req.userName = programId; // 填小程序原始id req.path = path; //拉起小程序页面的可带参路径,不填默认拉起小程序首页 req.miniprogramType = WXLaunchMiniProgram.Req.MINIPTOGRAM_TYPE_RELEASE;// 可选打开 开发版,体验版和正式版 api.sendReq(req); } */ static toMiniProgram(programId, path) { WeChat.toMiniProgram(programId, path); } } /*** //github:https://github.com/yorkie/react-native-wechat 使用方法: 1.登录使用方法,返回object wechat.wechatLogin((user) => { console.log('openid:' + user['openid']); console.log('unionid:' + user['unionid']); console.log('nickname:' + user['nickname']); console.log('sex:' + user['sex']); console.log('avatar:' + user['province'] + '--' + user['city']); }); 2.跳转小程序方法 wechat.toMiniProgram('gh_d81480f7a2fd', ''); //微信各种分享方法传参定义 { type {Number} type of this message. Can be {news|text|imageUrl|imageFile|imageResource|video|audio|file} thumbImage {String} Thumb image of the message, which can be a uri or a resource id. description {String} The description about the sharing. webpageUrl {String} Required if type equals news. The webpage link to share. imageUrl {String} Provide a remote image if type equals image. videoUrl {String} Provide a remote video if type equals video. usicUrl {String} Provide a remote music if type equals audio. filePath {String} Provide a local file if type equals file. fileExtension {String} Provide the file type if type equals file. } */