├── .gitignore ├── README.md ├── android ├── build.gradle ├── libs │ └── libammsdk.jar └── src │ └── main │ ├── AndroidManifest.xml │ └── java │ └── com │ └── heng │ └── wechat │ ├── WeChatModule.java │ └── WeChatPackage.java ├── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | .DS_Store 3 | proguard-rules.pro 4 | wechat.iml -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # react-native-wechat-android [![npm version](https://img.shields.io/npm/v/react-native-wechat-android.svg?style=flat-square)](https://www.npmjs.com/package/react-native-wechat-android) ![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg) 2 | react-native 的微信SDK辅助包,支持微信登录、微信分享、微信支付(本模块仅支持Android)。 3 | 4 | - [iOS模块](https://github.com/beefe/react-native-wechat-ios)地址 5 | - [兼容iOS](https://github.com/beefe/react-native-wechat-android/wiki/%E5%85%BC%E5%AE%B9iOS)示例 6 | 7 | ## [安装及使用方法](https://github.com/beefe/react-native-wechat-android/wiki) 8 | 9 | ## 提供以下方法 / Method 10 | 11 | * [registerApp](#registerappappidcallback) 12 | * [openWXApp](#openwxappcallback) 13 | * [isWXAppInstalled](#iswxappinstalledcallback) 14 | * [isWXAppSupportAPI](#iswxappsupportapicallback) 15 | * [getWXAppSupportAPI](#iswxappsupportapicallback) 16 | * [sendAuthReq](#sendauthreqscopestatecallback) 17 | * [sendReq](#sendreqoptionscallback) 18 | * [weChatPay](#wechatpayoptionscallback) 19 | 20 | ### registerApp(appId,callback) 21 | 22 | appId : 在微信开放平台申请的AppID 23 | 24 | callback : 回调(err,res) 25 | 26 | 使用示例: 27 | 28 | ```javascript 29 | WeChatAndroid.registerApp(appId,(err,registerOK) => { 30 | ... 31 | }); 32 | ``` 33 | 34 | ### openWXApp(callback) 35 | 36 | callback : 回调(err,res) 37 | 38 | 使用示例: 39 | 40 | ```javascript 41 | WeChatAndroid.openWXApp((err,res) => { 42 | ... 43 | }); 44 | ``` 45 | 46 | ### isWXAppInstalled(callback) 47 | 48 | callback : 回调(err,res) 49 | 50 | 使用示例: 51 | 52 | ```javascript 53 | WeChatAndroid.isWXAppInstalled( 54 | (err,isInstalled) => { 55 | ... 56 | } 57 | ); 58 | ``` 59 | 60 | ### isWXAppSupportAPI(callback) 61 | 62 | callback : 回调(err,res) 63 | 64 | 使用示例: 65 | 66 | ```javascript 67 | WeChatAndroid.isWXAppSupportAPI( 68 | (err,isSupport) => { 69 | ... 70 | } 71 | ); 72 | ``` 73 | 74 | ### getWXAppSupportAPI(callback) 75 | 76 | callback : 回调(err,res) 77 | 78 | 使用示例: 79 | 80 | ```javascript 81 | WeChatAndroid.getWXAppSupportAPI( 82 | (err,supportAPI) => { 83 | ... 84 | } 85 | ); 86 | ``` 87 | 88 | ### sendAuthReq(scope,state,callback) 89 | 90 | scope : 微信登录需要的参数(可空) 91 | 92 | state : 微信登录需要的参数(可空) 93 | 94 | callback : 回调(err,res) 95 | 96 | 使用示例: 97 | 98 | ```javascript 99 | WeChatAndroid.sendAuthReq('snsapi_userinfo','SECRET',(err,authReqOK) => { 100 | ... 101 | }); 102 | ``` 103 | or 104 | ```javascript 105 | WeChatAndroid.sendAuthReq(null,null,(err,authReqOK) => { 106 | ... 107 | }); 108 | ``` 109 | ```javascript 110 | // 处理登录回调结果 111 | DeviceEventEmitter.addListener('finishedAuth',function(event){ 112 | if(event.success){ 113 | ToastAndroid.show( 114 | ' code = ' + JSON.stringify(event.code) 115 | + ' state = ' + JSON.stringify(event.state), 116 | ToastAndroid.LONG 117 | ); 118 | }else{ 119 | ToastAndroid.show('授权失败',ToastAndroid.SHORT); 120 | } 121 | }); 122 | ``` 123 | 124 | ### sendReq(options,callback) 125 | 126 | options : 分享到微信需要的参数 127 | * type (必传,用来区分分享的内容) 128 | * 1:文字 129 | * 2:图片 130 | * 3:网页 131 | * 4:音乐 132 | * 5:视频 133 | * title (分享的标题) 134 | * desc (分享的描述) 135 | * thumbSize (分享的缩略图大小,不传默认150) 136 | * tagName 137 | * transaction 138 | * scene (分享方式,传错或者不传默认为0) 139 | * 0:聊天界面/好友 140 | * 1:朋友圈 141 | * 2:收藏 142 | * text (当分享类型为文本时使用) 143 | * imageUrl (当分享类型为网络照片时使用) 144 | * imagePath (当分享类型为本地照片时使用) 145 | * webpageUrl (当分享类型为网页时使用) 146 | * musicUrl (当分享类型为音乐时使用) 147 | * musicLowBandUrl (当分享类型为音乐时使用) 148 | * thumbImage (仅当分享类型为网页、音乐、视频时使用) 149 | 150 | callback : 回调(err,res) 151 | 152 | 使用示例: 153 | 154 | ```javascript 155 | 156 | //分享文本 157 | var textOptions = { 158 | title: '分享一段内容给你', 159 | transaction: 'text', 160 | scene: 0, 161 | type: 1, 162 | text: '这里是分享的文本内容', 163 | } 164 | 165 | //分享网络图片 166 | var networkImageOptions = { 167 | title: '分享一张图片给你', 168 | thumbSize: 150, 169 | scene: 0, 170 | type: 2, 171 | 172 | imageUrl: 'https://avatars3.githubusercontent.com/u/3015681?v=3&s=460', 173 | } 174 | 175 | //分享本地图片 176 | var localImageOptions = { 177 | title: '分享一张图片给你', 178 | thumbSize: 150, 179 | scene: 0, 180 | type: 2, 181 | 182 | imagePath: '/mnt/sdcard/temp.png', 183 | } 184 | 185 | //分享网页 186 | var webpageOptions = { 187 | title: '分享这个网页给你', 188 | desc: '我发现这个网页很有趣,特意分享给你', 189 | thumbSize: 150, 190 | scene: 0, 191 | type: 3, 192 | 193 | webpageUrl: 'https://github.com/beefe/react-native-wechat-android', 194 | thumbImage: 'http://img1.imgtn.bdimg.com/it/u=3924416677,403957246&fm=21&gp=0.jpg', 195 | } 196 | 197 | //分享音乐 198 | var musicOptions = { 199 | title: '这里是分享的标题', 200 | desc: '发现一首好听的音乐,分享给你', 201 | transaction: 'music', 202 | scene: 1, 203 | type: 4, 204 | 205 | musicUrl: 'http://staff2.ustc.edu.cn/~wdw/softdown/index.asp/0042515_05.ANDY.mp3', 206 | thumbImage: 'http://img1.imgtn.bdimg.com/it/u=3924416677,403957246&fm=21&gp=0.jpg', 207 | } 208 | 209 | //分享视频 210 | var videoOptions = { 211 | title: '这里是分享的标题', 212 | desc: '这个视频好有趣,一起来看看', 213 | transaction: 'video', 214 | scene: 1, 215 | type: 5, 216 | 217 | videoUrl: 'http://www.iqiyi.com/v_19rrnlidhk.html?src=sharemodclk131212', 218 | thumbImage: 'http://img1.imgtn.bdimg.com/it/u=3924416677,403957246&fm=21&gp=0.jpg', 219 | } 220 | 221 | WeChatAndroid.sendReq(videoOptions,(err,sendOK) => { 222 | ... 223 | }); 224 | 225 | // 分享回调 226 | DeviceEventEmitter.addListener('finishedShare',function(event){ 227 | if(event.success){ 228 | ToastAndroid.show('分享成功',ToastAndroid.SHORT); 229 | }else{ 230 | ToastAndroid.show('分享失败',ToastAndroid.SHORT); 231 | } 232 | }); 233 | ``` 234 | 235 | ### weChatPay(options,callback) 236 | 237 | options : [微信支付需要的参数](https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=9_12&index=2) 238 | 239 | callback : 回调(err,res) 240 | 241 | 使用示例: 242 | 243 | ```javascript 244 | 245 | var payOptions = { 246 | appId: 'wx8888888888888888', 247 | nonceStr: '5K8264ILTKCH16CQ2502SI8ZNMTM67VS', 248 | packageValue: 'Sign=WXPay', 249 | partnerId: '1900000109', 250 | prepayId: 'WX1217752501201407033233368018', 251 | timeStamp: '1412000000', 252 | sign: 'C380BEC2BFD727A4B6845133519F3AD6', 253 | }; 254 | 255 | WeChatAndroid.weChatPay(payOptions,(err,sendReqOK) => { 256 | ... 257 | }); 258 | 259 | // 处理支付回调结果 260 | DeviceEventEmitter.addListener('finishedPay',function(event){ 261 | if(event.success){ 262 | // 在此发起网络请求由服务器验证是否真正支付成功,然后做出相应的处理 263 | }else{ 264 | ToastAndroid.show('支付失败',ToastAndroid.SHORT); 265 | } 266 | }); 267 | ``` 268 | 269 | ## Notes 270 | * 打包apk请参考[Generating Signed APK](http://facebook.github.io/react-native/docs/signed-apk-android.html#content) 271 | * 如需要混淆,需要在混淆文件里加上以下代码: 272 | ```text 273 | -keep class com.tencent.mm.sdk.** { 274 | *; 275 | } 276 | ``` 277 | -------------------------------------------------------------------------------- /android/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | 3 | android { 4 | compileSdkVersion 23 5 | buildToolsVersion "23.0.1" 6 | 7 | defaultConfig { 8 | minSdkVersion 16 9 | targetSdkVersion 23 10 | } 11 | } 12 | 13 | dependencies { 14 | compile fileTree(include: ['*.jar'], dir: 'libs') 15 | compile 'com.facebook.react:react-native:0.16.1' 16 | } 17 | -------------------------------------------------------------------------------- /android/libs/libammsdk.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/beefe/react-native-wechat-android/692e6e05331a6acb59f811c3dc5b0c9da1248dc7/android/libs/libammsdk.jar -------------------------------------------------------------------------------- /android/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /android/src/main/java/com/heng/wechat/WeChatModule.java: -------------------------------------------------------------------------------- 1 | package com.heng.wechat; 2 | 3 | import android.graphics.Bitmap; 4 | import android.graphics.BitmapFactory; 5 | import android.text.TextUtils; 6 | 7 | import com.facebook.react.bridge.Callback; 8 | import com.facebook.react.bridge.ReactApplicationContext; 9 | import com.facebook.react.bridge.ReactContextBaseJavaModule; 10 | import com.facebook.react.bridge.ReactMethod; 11 | import com.facebook.react.bridge.ReadableMap; 12 | import com.tencent.mm.sdk.modelmsg.SendAuth; 13 | import com.tencent.mm.sdk.modelmsg.SendMessageToWX; 14 | import com.tencent.mm.sdk.modelmsg.WXImageObject; 15 | import com.tencent.mm.sdk.modelmsg.WXMediaMessage; 16 | import com.tencent.mm.sdk.modelmsg.WXMusicObject; 17 | import com.tencent.mm.sdk.modelmsg.WXTextObject; 18 | import com.tencent.mm.sdk.modelmsg.WXVideoObject; 19 | import com.tencent.mm.sdk.modelmsg.WXWebpageObject; 20 | import com.tencent.mm.sdk.modelpay.PayReq; 21 | import com.tencent.mm.sdk.openapi.IWXAPI; 22 | import com.tencent.mm.sdk.openapi.WXAPIFactory; 23 | 24 | import java.io.ByteArrayOutputStream; 25 | import java.io.File; 26 | import java.io.IOException; 27 | import java.net.URL; 28 | 29 | /** 30 | * Created by heng on 2015/12/10. 31 | *

32 | * Edited by heng on 15/12/18 33 | * Added share webPage and weChat Pay 34 | *

35 | * Edited by heng on 2015/12/22 36 | * Add remote image async download 37 | *

38 | * Edited by heng on 2015/12/29 39 | * 1.Removed Handler and Thread 40 | * 2.Modify options param 41 | * 3.Added share local 42 | * 4.Added and remote image(分享远程图片到朋友圈和收藏都会失败,具体原因待查,建议把远程图片下载到本地来分享) 43 | *

44 | * Edited by heng on 2016/02/02 45 | * 1.Added method openWXApp 46 | * 2.Edited callback(err,res) 47 | * 3.Reconstruction code 48 | */ 49 | public class WeChatModule extends ReactContextBaseJavaModule { 50 | 51 | public static final String REACT_MODULE_NAME = "WeChatAndroid"; 52 | 53 | public static IWXAPI wxApi = null; 54 | public static String appId; 55 | public static ReactApplicationContext reactApplicationContext; 56 | 57 | 58 | /*============ WeChat share options key ==============*/ 59 | public static final String OPTIONS_TITLE = "title"; 60 | public static final String OPTIONS_DESC = "desc"; 61 | public static final String OPTIONS_TAG_NAME = "tagName"; 62 | public static final String OPTIONS_THUMB_SIZE = "thumbSize"; 63 | public static final String OPTIONS_TRANSACTION = "transaction"; 64 | public static final String OPTIONS_SCENE = "scene"; 65 | public static final String OPTIONS_TYPE = "type"; 66 | 67 | public static final String OPTIONS_TEXT = "text"; 68 | 69 | public static final String OPTIONS_IMAGE_URL = "imageUrl"; 70 | public static final String OPTIONS_IMAGE_PATH = "imagePath"; 71 | 72 | public static final String OPTIONS_THUMB_IMAGE = "thumbImage"; 73 | 74 | public static final String OPTIONS_WEBPAGE_URL = "webpageUrl"; 75 | 76 | public static final String OPTIONS_MUSIC_URL = "musicUrl"; 77 | public static final String OPTIONS_MUSIC_LOW_BAND_URL = "musicLowBandUrl"; 78 | 79 | public static final String OPTIONS_VIDEO_URL = "videoUrl"; 80 | public static final String OPTIONS_VIDEO_LOW_BAND_URL = "videoLowBandUrl"; 81 | /*============ WeChat share options key ==============*/ 82 | 83 | /*============ WeChat pay options key ==============*/ 84 | public static final String OPTIONS_APP_ID = "appId"; 85 | public static final String OPTIONS_NONCE_STR = "nonceStr"; 86 | public static final String OPTIONS_PACKAGE_VALUE = "packageValue"; 87 | public static final String OPTIONS_PARTNER_ID = "partnerId"; 88 | public static final String OPTIONS_PREPAY_ID = "prepayId"; 89 | public static final String OPTIONS_TIME_STAMP = "timeStamp"; 90 | public static final String OPTIONS_SIGN = "sign"; 91 | /*============ WeChat pay options key ==============*/ 92 | 93 | 94 | public static final int TYPE_TEXT = 1; //文字 95 | public static final int TYPE_IMAGE = 2; //图片 96 | public static final int TYPE_WEB_PAGE = 3; //网页 97 | public static final int TYPE_MUSIC = 4; //音乐 98 | public static final int TYPE_VIDEO = 5; //视频 99 | 100 | String tagName = null; 101 | String title = null; 102 | String desc = null; 103 | String transaction = null; 104 | Bitmap bitmap = null; //分享的缩略图 105 | int thumbSize = 150; //分享的缩略图大小 106 | int scene; //分享的方式(0:聊天界面,1:朋友圈,2:收藏) 107 | 108 | 109 | public WeChatModule(ReactApplicationContext reactContext) { 110 | super(reactContext); 111 | reactApplicationContext = reactContext; 112 | } 113 | 114 | @Override 115 | public String getName() { 116 | return REACT_MODULE_NAME; 117 | } 118 | 119 | /** 120 | * 注册AppID到微信(使用微信SDK必须先调用此方法) 121 | */ 122 | @ReactMethod 123 | public void registerApp(String appId, Callback callback) { 124 | if (TextUtils.isEmpty(appId)) { 125 | if (callback != null) { 126 | callback.invoke("appId must be not null !"); 127 | } 128 | } else { 129 | WeChatModule.appId = appId; 130 | WeChatModule.wxApi = WXAPIFactory.createWXAPI(getReactApplicationContext(), appId, true); 131 | boolean registered = WeChatModule.wxApi.registerApp(appId); 132 | if (callback != null) { 133 | callback.invoke(null, registered); 134 | } 135 | } 136 | } 137 | 138 | private void commonCallback(Callback callback, boolean res) { 139 | if (callback != null) { 140 | if (WeChatModule.wxApi == null) { 141 | callback.invoke("please registerApp before this !"); 142 | return; 143 | } 144 | callback.invoke(null, res); 145 | } 146 | } 147 | 148 | /** 149 | * 打开微信客户端 150 | */ 151 | @ReactMethod 152 | public void openWXApp(Callback callback) { 153 | commonCallback(callback, WeChatModule.wxApi.openWXApp()); 154 | } 155 | 156 | /** 157 | * 判断是否安装微信 158 | */ 159 | @ReactMethod 160 | public void isWXAppInstalled(Callback callback) { 161 | commonCallback(callback, WeChatModule.wxApi.isWXAppInstalled()); 162 | } 163 | 164 | /** 165 | * 判断安装的版本是否为微信支持的API 166 | */ 167 | @ReactMethod 168 | public void isWXAppSupportAPI(Callback callback) { 169 | commonCallback(callback, WeChatModule.wxApi.isWXAppSupportAPI()); 170 | } 171 | 172 | /** 173 | * 获取微信支持的API版本 174 | */ 175 | @ReactMethod 176 | public void getWXAppSupportAPI(Callback callback) { 177 | if (callback != null) { 178 | if (WeChatModule.wxApi == null) { 179 | callback.invoke("please registerApp before this !"); 180 | return; 181 | } 182 | int supportAPI = WeChatModule.wxApi.getWXAppSupportAPI(); 183 | callback.invoke(null, supportAPI); 184 | } 185 | } 186 | 187 | /** 188 | * 微信授权登录 189 | */ 190 | @ReactMethod 191 | public void sendAuthReq(String scope, String state, Callback callback) { 192 | if (WeChatModule.wxApi == null) { 193 | if (callback != null) { 194 | callback.invoke("please registerApp before this !"); 195 | } 196 | } else { 197 | if (TextUtils.isEmpty(scope)) { 198 | scope = "snsapi_userinfo"; 199 | } 200 | if (TextUtils.isEmpty(state)) { 201 | state = "SECRET"; 202 | } 203 | SendAuth.Req req = new SendAuth.Req(); 204 | req.scope = scope; 205 | req.state = state; 206 | boolean sendReqOK = WeChatModule.wxApi.sendReq(req); 207 | if (callback != null) { 208 | callback.invoke(null, sendReqOK); 209 | } 210 | } 211 | } 212 | 213 | /** 214 | * 分享到微信 215 | */ 216 | @ReactMethod 217 | public void sendReq(ReadableMap options, Callback callback) { 218 | if (WeChatModule.wxApi == null) { 219 | if (callback != null) { 220 | callback.invoke("please registerApp before this !"); 221 | } 222 | } else { 223 | if (options == null) { 224 | if (callback != null) { 225 | callback.invoke("please setting options !"); 226 | } 227 | } else { 228 | if (options.hasKey(OPTIONS_TYPE)) { 229 | WXMediaMessage msg = new WXMediaMessage(); 230 | 231 | int type = options.getInt(OPTIONS_TYPE); 232 | switch (type) { 233 | case TYPE_TEXT: 234 | msg.mediaObject = getTextObj(options); 235 | break; 236 | case TYPE_IMAGE: 237 | msg.mediaObject = getImageObj(options); 238 | break; 239 | case TYPE_WEB_PAGE: 240 | msg.mediaObject = getWebpageObj(options); 241 | break; 242 | case TYPE_MUSIC: 243 | msg.mediaObject = getMusicObj(options); 244 | break; 245 | case TYPE_VIDEO: 246 | msg.mediaObject = getVideoObj(options); 247 | break; 248 | default: 249 | if (callback != null) { 250 | callback.invoke("please check correct media type !"); 251 | } 252 | break; 253 | } 254 | 255 | if (options.hasKey(OPTIONS_TITLE)) { 256 | title = options.getString(OPTIONS_TITLE); 257 | } 258 | if (options.hasKey(OPTIONS_DESC)) { 259 | desc = options.getString(OPTIONS_DESC); 260 | } 261 | if (options.hasKey(OPTIONS_TAG_NAME)) { 262 | tagName = options.getString(OPTIONS_TAG_NAME); 263 | } 264 | if (options.hasKey(OPTIONS_THUMB_SIZE)) { 265 | thumbSize = options.getInt(OPTIONS_THUMB_SIZE); 266 | } 267 | if (options.hasKey(OPTIONS_TRANSACTION)) { 268 | transaction = options.getString(OPTIONS_TRANSACTION); 269 | } 270 | if (options.hasKey(OPTIONS_SCENE)) { 271 | scene = options.getInt(OPTIONS_SCENE); 272 | } 273 | 274 | if (!TextUtils.isEmpty(title)) { 275 | msg.title = title; 276 | } 277 | if (!TextUtils.isEmpty(desc)) { 278 | msg.description = desc; 279 | } 280 | if (!TextUtils.isEmpty(tagName)) { 281 | msg.mediaTagName = tagName; 282 | } 283 | if (bitmap != null) { 284 | Bitmap thumbBmp = Bitmap.createScaledBitmap(bitmap, thumbSize, thumbSize, true); 285 | bitmap.recycle(); 286 | msg.thumbData = bmpToByteArray(thumbBmp, true); 287 | } 288 | 289 | SendMessageToWX.Req req = new SendMessageToWX.Req(); 290 | req.message = msg; 291 | if (!TextUtils.isEmpty(transaction)) { 292 | req.transaction = transaction; 293 | } else { 294 | req.transaction = String.valueOf(System.currentTimeMillis()); 295 | } 296 | if (scene == 0 || scene == 1 || scene == 2) { 297 | req.scene = scene; 298 | } else { 299 | req.scene = 0; 300 | } 301 | boolean sendReqOK = WeChatModule.wxApi.sendReq(req); 302 | if (callback != null) { 303 | callback.invoke(null, sendReqOK); 304 | } 305 | } else { 306 | if (callback != null) { 307 | callback.invoke("please setting share type !"); 308 | } 309 | } 310 | } 311 | } 312 | } 313 | 314 | 315 | /** 316 | * 微信支付 317 | */ 318 | @ReactMethod 319 | public void weChatPay(ReadableMap options, Callback callback) { 320 | if (WeChatModule.wxApi == null) { 321 | if (callback != null) { 322 | callback.invoke("please registerApp before this !"); 323 | } 324 | return; 325 | } 326 | String appId = WeChatModule.appId; 327 | String nonceStr = ""; 328 | String packageValue = ""; 329 | String partnerId = ""; 330 | String prepayId = ""; 331 | String timeStamp = ""; 332 | String sign = ""; 333 | if (options != null) { 334 | if (options.hasKey(OPTIONS_APP_ID)) { 335 | appId = options.getString(OPTIONS_APP_ID); 336 | } 337 | if (options.hasKey(OPTIONS_NONCE_STR)) { 338 | nonceStr = options.getString(OPTIONS_NONCE_STR); 339 | } 340 | if (options.hasKey(OPTIONS_PACKAGE_VALUE)) { 341 | packageValue = options.getString(OPTIONS_PACKAGE_VALUE); 342 | } 343 | if (options.hasKey(OPTIONS_PARTNER_ID)) { 344 | partnerId = options.getString(OPTIONS_PARTNER_ID); 345 | } 346 | if (options.hasKey(OPTIONS_PREPAY_ID)) { 347 | prepayId = options.getString(OPTIONS_PREPAY_ID); 348 | } 349 | if (options.hasKey(OPTIONS_TIME_STAMP)) { 350 | timeStamp = options.getString(OPTIONS_TIME_STAMP); 351 | } 352 | if (options.hasKey(OPTIONS_SIGN)) { 353 | sign = options.getString(OPTIONS_SIGN); 354 | } 355 | } 356 | 357 | PayReq request = new PayReq(); 358 | request.appId = appId; 359 | request.nonceStr = nonceStr; 360 | request.packageValue = packageValue; 361 | request.partnerId = partnerId; 362 | request.prepayId = prepayId; 363 | request.timeStamp = timeStamp; 364 | request.sign = sign; 365 | boolean sendReqOK = WeChatModule.wxApi.sendReq(request); 366 | if (callback != null) { 367 | callback.invoke(null, sendReqOK); 368 | } 369 | } 370 | 371 | 372 | /** 373 | * Added by heng on 2015/12/29 374 | *

375 | * Bitmap to byte array 376 | */ 377 | private byte[] bmpToByteArray(Bitmap bmp, boolean needRecycle) { 378 | ByteArrayOutputStream output = new ByteArrayOutputStream(); 379 | bmp.compress(Bitmap.CompressFormat.PNG, 100, output); 380 | if (needRecycle) { 381 | bmp.recycle(); 382 | } 383 | byte[] result = output.toByteArray(); 384 | try { 385 | output.close(); 386 | } catch (Exception e) { 387 | e.printStackTrace(); 388 | } 389 | return result; 390 | } 391 | 392 | /** 393 | * 获取文本对象 394 | * */ 395 | private WXTextObject getTextObj(ReadableMap options) { 396 | WXTextObject textObject = new WXTextObject(); 397 | if (options.hasKey(OPTIONS_TEXT)) { 398 | textObject.text = options.getString(OPTIONS_TEXT); 399 | } 400 | return textObject; 401 | } 402 | 403 | /** 404 | * 获取图片对象 405 | * */ 406 | private WXImageObject getImageObj(ReadableMap options) { 407 | WXImageObject imageObject = new WXImageObject(); 408 | if (options.hasKey(OPTIONS_IMAGE_URL)) { 409 | String remoteUrl = options.getString(OPTIONS_IMAGE_URL); 410 | imageObject.imageUrl = remoteUrl; 411 | try { 412 | bitmap = BitmapFactory.decodeStream(new URL(remoteUrl).openStream()); 413 | } catch (IOException e) { 414 | bitmap = null; 415 | e.printStackTrace(); 416 | } 417 | } 418 | if (options.hasKey(OPTIONS_IMAGE_PATH)) { 419 | String localPath = options.getString(OPTIONS_IMAGE_PATH); 420 | File file = new File(localPath); 421 | if (file.exists()) { 422 | imageObject.setImagePath(localPath); 423 | bitmap = BitmapFactory.decodeFile(localPath); 424 | } else { 425 | bitmap = null; 426 | } 427 | } 428 | return imageObject; 429 | } 430 | 431 | /** 432 | * 获取网页对象 433 | * */ 434 | private WXWebpageObject getWebpageObj(ReadableMap options) { 435 | WXWebpageObject webpageObject = new WXWebpageObject(); 436 | if (options.hasKey(OPTIONS_WEBPAGE_URL)) { 437 | webpageObject.webpageUrl = options.getString(OPTIONS_WEBPAGE_URL); 438 | } 439 | if(options.hasKey(OPTIONS_THUMB_IMAGE)){ 440 | String thumbImage = options.getString(OPTIONS_THUMB_IMAGE); 441 | try { 442 | bitmap = BitmapFactory.decodeStream(new URL(thumbImage).openStream()); 443 | } catch (IOException e) { 444 | bitmap = null; 445 | e.printStackTrace(); 446 | } 447 | } 448 | return webpageObject; 449 | } 450 | 451 | /** 452 | * 获取音乐对象 453 | * */ 454 | private WXMusicObject getMusicObj(ReadableMap options) { 455 | WXMusicObject musicObject = new WXMusicObject(); 456 | if (options.hasKey(OPTIONS_MUSIC_URL)) { 457 | musicObject.musicUrl = options.getString(OPTIONS_MUSIC_URL); 458 | } 459 | if (options.hasKey(OPTIONS_MUSIC_LOW_BAND_URL)) { 460 | musicObject.musicLowBandUrl = options.getString(OPTIONS_MUSIC_LOW_BAND_URL); 461 | } 462 | 463 | if(options.hasKey(OPTIONS_THUMB_IMAGE)){ 464 | String thumbImage = options.getString(OPTIONS_THUMB_IMAGE); 465 | try { 466 | bitmap = BitmapFactory.decodeStream(new URL(thumbImage).openStream()); 467 | } catch (IOException e) { 468 | bitmap = null; 469 | e.printStackTrace(); 470 | } 471 | } 472 | return musicObject; 473 | } 474 | 475 | /** 476 | * 获取视频对象 477 | * */ 478 | private WXVideoObject getVideoObj(ReadableMap options){ 479 | WXVideoObject videoObject = new WXVideoObject(); 480 | if(options.hasKey(OPTIONS_VIDEO_URL)){ 481 | videoObject.videoUrl = options.getString(OPTIONS_VIDEO_URL); 482 | } 483 | if(options.hasKey(OPTIONS_VIDEO_LOW_BAND_URL)){ 484 | videoObject.videoLowBandUrl = options.getString(OPTIONS_VIDEO_LOW_BAND_URL); 485 | } 486 | if(options.hasKey(OPTIONS_THUMB_IMAGE)){ 487 | String thumbImage = options.getString(OPTIONS_THUMB_IMAGE); 488 | try { 489 | bitmap = BitmapFactory.decodeStream(new URL(thumbImage).openStream()); 490 | } catch (IOException e) { 491 | bitmap = null; 492 | e.printStackTrace(); 493 | } 494 | } 495 | return videoObject; 496 | } 497 | 498 | } 499 | -------------------------------------------------------------------------------- /android/src/main/java/com/heng/wechat/WeChatPackage.java: -------------------------------------------------------------------------------- 1 | package com.heng.wechat; 2 | 3 | import com.facebook.react.ReactPackage; 4 | import com.facebook.react.bridge.JavaScriptModule; 5 | import com.facebook.react.bridge.NativeModule; 6 | import com.facebook.react.bridge.ReactApplicationContext; 7 | import com.facebook.react.uimanager.ViewManager; 8 | 9 | import java.util.ArrayList; 10 | import java.util.Collections; 11 | import java.util.List; 12 | 13 | /** 14 | * Created by heng on 15/12/10. 15 | */ 16 | public class WeChatPackage implements ReactPackage { 17 | 18 | @Override 19 | public List createNativeModules(ReactApplicationContext reactContext) { 20 | List list = new ArrayList(); 21 | list.add(new WeChatModule(reactContext)); 22 | return list; 23 | } 24 | 25 | @Override 26 | public List> createJSModules() { 27 | return Collections.emptyList(); 28 | } 29 | 30 | @Override 31 | public List createViewManagers(ReactApplicationContext reactContext) { 32 | return Collections.emptyList(); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var React = require('react-native'); 2 | 3 | var { NativeModules } = React; 4 | 5 | var WeChatAndroid = NativeModules.WeChatAndroid; 6 | 7 | module.exports = WeChatAndroid; 8 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-native-wechat-android", 3 | "version": "2.0.1", 4 | "description": "the wechat util for android", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/beefe/react-native-wechat-android.git" 12 | }, 13 | "keywords": [ 14 | "react-native-wechat", 15 | "react-native-wechat-android" 16 | ], 17 | "author": "heng", 18 | "license": "MIT", 19 | "bugs": { 20 | "url": "https://github.com/beefe/react-native-wechat-android/issues" 21 | }, 22 | "homepage": "https://github.com/beefe/react-native-wechat-android#readme" 23 | } 24 | --------------------------------------------------------------------------------