├── .gitignore ├── README-zh.md ├── README.md ├── android ├── WXEntryActivity.java └── WXPayEntryActivity.java ├── package.json └── plugin.xml /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .weexpack.cache -------------------------------------------------------------------------------- /README-zh.md: -------------------------------------------------------------------------------- 1 | # weex-wechat 2 | 3 | > Wechat(auth, pay, share) plugin for weex. 4 | 5 | ## 准备工作 6 | 7 | ```bash 8 | weexpack plugin install weex-wechat 9 | ``` 10 | 11 | ## 使用方法 12 | 13 | ```javascript 14 | var wechat = weex.requireModule('wechat'); 15 | var stream = weex.requireModule('stream'); 16 | 17 | wechat.registerApp("appid", function(data) { 18 | console.log(data, "wx register") 19 | }) 20 | 21 | wechat.login({}, function(data) { 22 | console.log(data) 23 | }) 24 | 25 | // share text to timeline (share to session use shareToSession) 26 | wechat.shareToTimeLine({ 27 | type: "text", 28 | content: "text content" 29 | }, function(data) { 30 | console.log("text shared", data) 31 | }) 32 | 33 | // share image to timeline 34 | wechat.shareToTimeLine({ 35 | type: "image", 36 | image: imageUrl 37 | }, function(data) { 38 | console.log("image shared", data) 39 | }) 40 | 41 | // share video to timeline 42 | wechat.shareToTimeLine({ 43 | type: "video", 44 | title: 'video title', 45 | content: "video content", 46 | image: imageUrl, 47 | url: 'https://v.qq.com/x/cover/m4cz4v1n0av4a8k/x00223sb1nm.html?new=1' 48 | }, function(data) { 49 | console.log("video shared", data) 50 | }) 51 | 52 | // share webpage to timeline 53 | wechat.shareToTimeLine({ 54 | type: "webpage", 55 | title: 'vebpage title', 56 | content: "webpage content", 57 | image: imageUrl, 58 | url: 'http://github.com/doabit' 59 | }, function(data) { 60 | console.log("web page shared", data) 61 | }) 62 | 63 | // wxpay 64 | stream.fetch({ 65 | method: 'POST', 66 | url: 'http://192.168.1.102:3000/wx_app_pay', //change to your wepay api 67 | type: "json" 68 | }, function(resData){ 69 | if (resData.ok) { 70 | var data = resData.data; 71 | wechat.pay({ 72 | appid: data.appid, 73 | sign: data.sign, 74 | timestamp: data.timestamp, 75 | noncestr: data.noncestr, 76 | partnerid: data.partnerid, 77 | prepayid: data.prepayid, 78 | packageValue:data.package 79 | }, function(resData){ 80 | console.log(resData) 81 | }) 82 | } else { 83 | console.log(resData) 84 | } 85 | }) 86 | ``` 87 | 88 | ## android 配置 89 | 90 | ### 添加 `jitpack.io` 到 android/build.gradle 91 | 92 | ```gradle 93 | allprojects { 94 | repositories { 95 | mavenCentral() 96 | jcenter() 97 | maven { url 'https://jitpack.io' } 98 | } 99 | } 100 | ``` 101 | 102 | ### 集成微信登录和分享 103 | 104 | 集成微信登录和分享,首先创建一个包名为 'wxapi',并且添加一个名为 [WXEntryActivity](https://github.com/doabit/weex-wechat/blob/master/android/WXEntryActivity.java) 的类。 105 | 106 | 添加下面代码到 `AndroidManifest.xml`: 107 | 108 | ```xml 109 | 110 | 111 | 116 | 117 | 118 | ``` 119 | 120 | ### 集成微信支付 121 | 122 | 在 `wxapi`包中添加文件名为 [WXPayEntryActivity](https://github.com/doabit/weex-wechat/blob/master/android/WXPayEntryActivity.java) 的类 123 | 124 | 添加下面代码到 `AndroidManifest.xml`: 125 | 126 | ```xml 127 | 128 | 129 | 134 | 135 | 136 | ``` 137 | 138 | ## IOS 配置 139 | 140 | 在Xcode中,选择你的工程设置项,选中“TARGETS”一栏,在“info”标签栏的“URL type“添加“URL scheme”为你所注册的应用程序id(如下图所示)。 141 | 142 | ![URL_TYPE](https://user-images.githubusercontent.com/330368/32264784-3cbe0b8a-beae-11e7-9c75-aab901b4cdab.jpg) 143 | 144 | iOS 9 往后, 在 Targets > info > Custom iOS Target Properties 中 添加 wechat 和 weixin 到 LSApplicationQueriesSchemes. 或者 直接编辑 Info.plist: 145 | 146 | ```plist 147 | LSApplicationQueriesSchemes 148 | 149 | weixin 150 | wechat 151 | 152 | ``` 153 | 154 | 添加如下代码到 AppDelegate.m: 155 | 156 | ```objc 157 | - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url { 158 | return [WXApi handleOpenURL:url delegate:[WeChatManager shareInstance]]; 159 | } 160 | 161 | - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation { 162 | return [WXApi handleOpenURL:url delegate:[WeChatManager shareInstance]]; 163 | } 164 | ``` 165 | 166 | ## 参考资料 167 | 168 | [react-native-wechat](https://github.com/yorkie/react-native-wechat) 169 | 170 | [WeexErosFramework](https://github.com/aa453509345/WeexErosFramework) 171 | 172 | [微信开放平台](https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=1417674108&token=&lang=zh_CN) 173 | 174 | ## Author 175 | 176 | [@doabit](https://github.com/doabit) 177 | 178 | ## license 179 | 180 | [MIT](http://opensource.org/licenses/MIT) -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # weex-wechat [中文版](https://github.com/doabit/weex-wechat/blob/master/README-zh.md) 2 | 3 | > Wechat(auth, pay, share) plugin for weex. 4 | 5 | ## getting start 6 | 7 | ```bash 8 | weexpack plugin install weex-wechat 9 | ``` 10 | 11 | ## Usage 12 | 13 | ```javascript 14 | var wechat = weex.requireModule('wechat'); 15 | var stream = weex.requireModule('stream'); 16 | 17 | wechat.registerApp("appid", function(data) { 18 | console.log(data, "wx register") 19 | }) 20 | 21 | wechat.login({}, function(data) { 22 | console.log(data) 23 | }) 24 | 25 | // share text to timeline (share to session use shareToSession) 26 | wechat.shareToTimeLine({ 27 | type: "text", 28 | content: "text content" 29 | }, function(data) { 30 | console.log("text shared", data) 31 | }) 32 | 33 | // share image to timeline 34 | wechat.shareToTimeLine({ 35 | type: "image", 36 | image: imageUrl 37 | }, function(data) { 38 | console.log("image shared", data) 39 | }) 40 | 41 | // share video to timeline 42 | wechat.shareToTimeLine({ 43 | type: "video", 44 | title: 'video title', 45 | content: "video content", 46 | image: imageUrl, 47 | url: 'https://v.qq.com/x/cover/m4cz4v1n0av4a8k/x00223sb1nm.html?new=1' 48 | }, function(data) { 49 | console.log("video shared", data) 50 | }) 51 | 52 | // share webpage to timeline 53 | wechat.shareToTimeLine({ 54 | type: "webpage", 55 | title: 'vebpage title', 56 | content: "webpage content", 57 | image: imageUrl, 58 | url: 'http://github.com/doabit' 59 | }, function(data) { 60 | console.log("web page shared", data) 61 | }) 62 | 63 | // wxpay 64 | stream.fetch({ 65 | method: 'POST', 66 | url: 'http://192.168.1.102:3000/wx_app_pay', //change to your wepay api 67 | type: "json" 68 | }, function(resData){ 69 | if (resData.ok) { 70 | var data = resData.data; 71 | wechat.pay({ 72 | appid: data.appid, 73 | sign: data.sign, 74 | timestamp: data.timestamp, 75 | noncestr: data.noncestr, 76 | partnerid: data.partnerid, 77 | prepayid: data.prepayid, 78 | packageValue:data.package 79 | }, function(resData){ 80 | console.log(resData) 81 | }) 82 | } else { 83 | console.log(resData) 84 | } 85 | }) 86 | ``` 87 | 88 | ## setup for android 89 | 90 | ### add `jitpack.io` to android/build.gradle 91 | 92 | ```gradle 93 | allprojects { 94 | repositories { 95 | mavenCentral() 96 | jcenter() 97 | maven { url 'https://jitpack.io' } 98 | } 99 | } 100 | ``` 101 | 102 | ### Integrating with login and share 103 | 104 | If you are going to integrate login or share functions, you need to create a package named 'wxapi' in your application package and a class named [WXEntryActivity](https://github.com/doabit/weex-wechat/blob/master/android/WXEntryActivity.java) 105 | 106 | Then add the following node to `AndroidManifest.xml`: 107 | 108 | ```xml 109 | 110 | 111 | 116 | 117 | 118 | ``` 119 | 120 | ### Integrating the WeChat Payment 121 | 122 | If you are going to integrate payment functionality by using this library, then 123 | create a package named also `wxapi` in your application package and a class named 124 | [WXPayEntryActivity](https://github.com/doabit/weex-wechat/blob/master/android/WXPayEntryActivity.java) 125 | 126 | Then add the following node to `AndroidManifest.xml`: 127 | 128 | ```xml 129 | 130 | 131 | 136 | 137 | 138 | ``` 139 | 140 | ## setup for ios 141 | 142 | Add "URL Schema" as your app id for "URL type" in Targets > info, See the following screenshot for the view on your XCode. 143 | 144 | ![URL_TYPE](https://user-images.githubusercontent.com/330368/32264784-3cbe0b8a-beae-11e7-9c75-aab901b4cdab.jpg) 145 | 146 | On iOS 9, add wechat and weixin into LSApplicationQueriesSchemes in Targets > info > Custom iOS Target Properties. Or edit Info.plist then add: 147 | 148 | ```plist 149 | LSApplicationQueriesSchemes 150 | 151 | weixin 152 | wechat 153 | 154 | ``` 155 | 156 | Then copy the following in AppDelegate.m: 157 | 158 | ```objc 159 | - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url { 160 | return [WXApi handleOpenURL:url delegate:[WeChatManager shareInstance]]; 161 | } 162 | 163 | - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation { 164 | return [WXApi handleOpenURL:url delegate:[WeChatManager shareInstance]]; 165 | } 166 | ``` 167 | 168 | ## reference resources 169 | 170 | [react-native-wechat](https://github.com/yorkie/react-native-wechat) 171 | 172 | [WeexErosFramework](https://github.com/aa453509345/WeexErosFramework) 173 | 174 | [WeChat Open Platform](https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=1417674108&token=&lang=zh_CN) 175 | 176 | ## Author 177 | 178 | [@doabit](https://github.com/doabit) 179 | 180 | ## license 181 | 182 | [MIT](http://opensource.org/licenses/MIT) -------------------------------------------------------------------------------- /android/WXEntryActivity.java: -------------------------------------------------------------------------------- 1 | package com.doabit.weex.wxapi; 2 | 3 | import android.app.Activity; 4 | import android.content.Intent; 5 | import android.os.Bundle; 6 | 7 | import com.doabit.weex.extend.model.WeChatAuthResultModel; 8 | import com.doabit.weex.extend.model.WeChatShareResultModel; 9 | import com.doabit.weex.extend.module.WeChatModule; 10 | import com.taobao.weex.utils.WXLogUtils; 11 | import com.tencent.mm.opensdk.modelbase.BaseReq; 12 | import com.tencent.mm.opensdk.modelbase.BaseResp; 13 | import com.tencent.mm.opensdk.modelmsg.SendAuth; 14 | import com.tencent.mm.opensdk.modelmsg.SendMessageToWX; 15 | import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler; 16 | 17 | //import android.content.Intent; 18 | /** 19 | * Created by doabit on 2017/10/20. 20 | */ 21 | 22 | public class WXEntryActivity extends Activity implements IWXAPIEventHandler { 23 | private static final String TAG = "WXEntryActivity"; 24 | 25 | @Override 26 | public void onCreate(Bundle savedInstanceState) { 27 | super.onCreate(savedInstanceState); 28 | //注册API 29 | WeChatModule.wxapi.handleIntent(getIntent(), this); 30 | } 31 | 32 | @Override 33 | protected void onNewIntent(Intent intent) { 34 | super.onNewIntent(intent); 35 | setIntent(intent); 36 | if (WeChatModule.wxapi!= null) { 37 | WeChatModule.wxapi.handleIntent(intent, this); 38 | } 39 | } 40 | 41 | @Override 42 | public void onReq(BaseReq baseReq) { 43 | 44 | } 45 | 46 | 47 | @Override 48 | public void onResp(BaseResp resp) { 49 | 50 | WXLogUtils.e(TAG, "error_code:---->" + resp.errCode); 51 | 52 | if (resp instanceof SendAuth.Resp) { 53 | SendAuth.Resp newResp = (SendAuth.Resp) resp; 54 | 55 | WXLogUtils.e(newResp.code, newResp.url); 56 | 57 | //获取微信传回的code 58 | String code = newResp.code; 59 | WeChatAuthResultModel result = new WeChatAuthResultModel(); 60 | result.resCode = newResp.errCode; 61 | result.msg = newResp.errStr; 62 | result.code = newResp.code; 63 | 64 | WXLogUtils.e(TAG, code); 65 | 66 | WeChatModule.getInstance().reciverResult(result); 67 | 68 | // Map params=new HashMap<>(); 69 | // params.put("key", code); 70 | // WeChatModule.getInstance().mWXSDKInstance.fireGlobalEventCallback("wx_login",params); 71 | 72 | 73 | finish(); 74 | } else if (resp instanceof SendMessageToWX.Resp) { 75 | SendMessageToWX.Resp newResp = (SendMessageToWX.Resp) (resp); 76 | WeChatShareResultModel result = new WeChatShareResultModel(); 77 | result.resCode = newResp.errCode; 78 | result.msg = newResp.errStr; 79 | result.openid = newResp.openId; 80 | 81 | WeChatModule.getInstance().reciverResult(result); 82 | finish(); 83 | } 84 | finish(); 85 | } 86 | 87 | } 88 | 89 | -------------------------------------------------------------------------------- /android/WXPayEntryActivity.java: -------------------------------------------------------------------------------- 1 | package com.doabit.weex.wxapi; 2 | 3 | import android.app.Activity; 4 | import android.content.Intent; 5 | import android.os.Bundle; 6 | import android.util.Log; 7 | 8 | import com.doabit.weex.extend.model.BaseResultModel; 9 | import com.doabit.weex.extend.module.WeChatModule; 10 | import com.tencent.mm.opensdk.constants.ConstantsAPI; 11 | import com.tencent.mm.opensdk.modelbase.BaseReq; 12 | import com.tencent.mm.opensdk.modelbase.BaseResp; 13 | import com.tencent.mm.opensdk.openapi.IWXAPI; 14 | import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler; 15 | 16 | public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler { 17 | 18 | private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity"; 19 | 20 | private IWXAPI api; 21 | 22 | @Override 23 | public void onCreate(Bundle savedInstanceState) { 24 | super.onCreate(savedInstanceState); 25 | //注册API 26 | WeChatModule.wxapi.handleIntent(getIntent(), this); 27 | } 28 | 29 | @Override 30 | protected void onNewIntent(Intent intent) { 31 | super.onNewIntent(intent); 32 | setIntent(intent); 33 | if (api != null) { 34 | api.handleIntent(intent, this); 35 | } 36 | } 37 | 38 | @Override 39 | public void onReq(BaseReq req) { 40 | 41 | } 42 | 43 | /** 44 | * int ERR_OK = 0; int ERR_COMM = -1; int ERR_USER_CANCEL = -2; int ERR_SENT_FAILED = -3; int 45 | * ERR_AUTH_DENIED = -4; int ERR_UNSUPPORT = -5; 46 | */ 47 | @Override 48 | public void onResp(BaseResp resp) { 49 | Log.i(TAG, "PAY_ACTIVE"); 50 | if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) { 51 | BaseResultModel result = new BaseResultModel(); 52 | result.msg = resp.errStr; 53 | result.resCode = resp.errCode; 54 | WeChatModule.getInstance().reciverResult(result); 55 | 56 | finish(); 57 | } 58 | } 59 | } -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "0.2.1", 3 | "platform": [ 4 | "ios", 5 | "android" 6 | ], 7 | "description": "Wechat(auth, pay, share) module for weex.", 8 | "keywords": [ 9 | "weex", 10 | "plugin", 11 | "wechat", 12 | "auth", 13 | "wxpay", 14 | "share" 15 | ], 16 | "license": "MIT", 17 | "name": "weex-wechat" 18 | } -------------------------------------------------------------------------------- /plugin.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | weex-wechat 7 | Wechat(auth, pay, share) module for weex. 8 | MIT 9 | weex,plugin,wechat,auth,wxpay,share 10 | https://github.com/doabit/weex-wechat.git 11 | https://github.com/doabit/weex-wechat/issues 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 36 | 37 | 38 | 39 | --------------------------------------------------------------------------------