mirror of
https://github.com/zhigang1992/react-native-wechat-1.git
synced 2026-01-12 17:43:04 +08:00
添加打开小程序的功能
This commit is contained in:
10
README.md
10
README.md
@@ -314,6 +314,16 @@ wechat.getWXAppInstallUrl()
|
||||
wechat.openWXApp()
|
||||
```
|
||||
|
||||
#### launchMiniProgram 打开小程序(安卓需要先注册 api 才可以调用)
|
||||
|
||||
```javascript
|
||||
wechat.launchMiniProgram({
|
||||
username: 'you username', // 原始 id,微信开放平台注册小程序处获取
|
||||
path: '/path1/path2', // 小程序路由地址
|
||||
type: 'test' // 小程序类型 optional('release')
|
||||
})
|
||||
```
|
||||
|
||||
#### sendAuthRequest OAuth2 微信登录
|
||||
|
||||
```javascript
|
||||
|
||||
@@ -17,6 +17,7 @@ import com.rnlib.wechat.helper.SendMessageToWXHelper;
|
||||
import com.rnlib.wechat.helper.WXMediaMessageHelper;
|
||||
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.WXImageObject;
|
||||
@@ -133,6 +134,24 @@ public class RNWechatModule extends ReactContextBaseJavaModule {
|
||||
promise.resolve(api.openWXApp());
|
||||
}
|
||||
|
||||
/**
|
||||
* 打开小程序
|
||||
*/
|
||||
@ReactMethod
|
||||
public void launchMiniProgram(
|
||||
String userName,
|
||||
String path,
|
||||
Integer miniprogramType,
|
||||
Promise promise) {
|
||||
|
||||
final WXLaunchMiniProgram.Req req = new WXLaunchMiniProgram.Req();
|
||||
req.userName = userName;
|
||||
req.path = path;
|
||||
req.miniprogramType = miniprogramType;
|
||||
|
||||
promise.resolve(api.sendReq(req));
|
||||
}
|
||||
|
||||
/**
|
||||
* OAuth2
|
||||
*/
|
||||
@@ -346,6 +365,11 @@ public class RNWechatModule extends ReactContextBaseJavaModule {
|
||||
if (baseResp instanceof SendMessageToWX.Resp) {
|
||||
// 分享
|
||||
body.putString("eventType", "SendMessageToWXResp");
|
||||
} else if (baseResp instanceof WXLaunchMiniProgram.Resp) {
|
||||
// 打开小程序
|
||||
WXLaunchMiniProgram.Resp resp = (WXLaunchMiniProgram.Resp) baseResp;
|
||||
body.putString("eventType", "WXLaunchMiniProgramResp");
|
||||
body.putString("extMsg", resp.extMsg);
|
||||
} else if (baseResp instanceof SendAuth.Resp) {
|
||||
// OAuth2
|
||||
SendAuth.Resp resp = (SendAuth.Resp) baseResp;
|
||||
|
||||
@@ -5,6 +5,10 @@
|
||||
+ (BOOL)sendAuthRequestScope:(NSString *)scope
|
||||
State:(NSString *)state;
|
||||
|
||||
+ (BOOL)launchMiniProgramWithUserName:(NSString *)anUserName
|
||||
path:(NSString *)aPath
|
||||
miniProgramType:(WXMiniProgramType)aMiniProgramType;
|
||||
|
||||
+ (BOOL)sendText:(NSString *)text
|
||||
InScene:(enum WXScene)scene;
|
||||
|
||||
|
||||
@@ -17,6 +17,19 @@
|
||||
return [WXApi sendReq:req];
|
||||
}
|
||||
|
||||
#pragma Other
|
||||
+ (BOOL)launchMiniProgramWithUserName:(NSString *)anUserName
|
||||
path:(NSString *)aPath
|
||||
miniProgramType:(WXMiniProgramType)aMiniProgramType
|
||||
{
|
||||
WXLaunchMiniProgramReq *req = [WXLaunchMiniProgramReq object];
|
||||
req.userName = anUserName;
|
||||
req.path = aPath;
|
||||
req.miniProgramType = aMiniProgramType;
|
||||
|
||||
return [WXApi sendReq:req];
|
||||
}
|
||||
|
||||
#pragma Share
|
||||
+ (BOOL)sendText:(NSString *)text
|
||||
InScene:(enum WXScene)scene
|
||||
|
||||
@@ -118,6 +118,18 @@ RCT_REMAP_METHOD(openWXApp,
|
||||
resolve([self getResolveResFromBool:[WXApi openWXApp]]);
|
||||
}
|
||||
|
||||
RCT_REMAP_METHOD(launchMiniProgram,
|
||||
launchMiniProgramWithUserName:(NSString *)anUserName
|
||||
path:(NSString *)aPath
|
||||
miniProgramType:(NSInteger *)aMiniProgramType
|
||||
resolver:(RCTPromiseResolveBlock)resolve
|
||||
rejecter:(RCTPromiseRejectBlock)reject)
|
||||
{
|
||||
resolve([self getResolveResFromBool:[WXApiRequestHandler launchMiniProgramWithUserName:anUserName
|
||||
path:aPath
|
||||
miniProgramType:(WXMiniProgramType)aMiniProgramType]]);
|
||||
}
|
||||
|
||||
|
||||
#pragma OAuth2
|
||||
|
||||
@@ -272,6 +284,11 @@ RCT_REMAP_METHOD(pay,
|
||||
ASSIGN_EMPTY_STRING(body[@"state"], authResp.state)
|
||||
ASSIGN_EMPTY_STRING(body[@"lang"], authResp.lang)
|
||||
ASSIGN_EMPTY_STRING(body[@"country"], authResp.country)
|
||||
} else if ([resp isKindOfClass:[WXLaunchMiniProgramResp class]]) {
|
||||
// 启动小程序
|
||||
WXLaunchMiniProgramResp *miniProgramResp = (WXLaunchMiniProgramResp *)resp;
|
||||
body[@"eventType"] = @"WXLaunchMiniProgramResp";
|
||||
ASSIGN_EMPTY_STRING(body[@"extMsg"], miniProgramResp.extMsg);
|
||||
} else if ([resp isKindOfClass:[SendMessageToWXResp class]]) {
|
||||
// 发送文字、图片、音乐、视频、链接、小程序
|
||||
SendMessageToWXResp *messageResp = (SendMessageToWXResp *)resp;
|
||||
|
||||
@@ -56,6 +56,7 @@ export const WXMiniProgramTypes = {
|
||||
*/
|
||||
export const WXRespType = {
|
||||
Auth: 'SendAuthResp',
|
||||
LaunchMiniProgram: 'WXLaunchMiniProgramResp',
|
||||
Message: 'SendMessageToWXResp',
|
||||
Pay: 'PayResp'
|
||||
}
|
||||
|
||||
21
src/index.js
21
src/index.js
@@ -40,6 +40,27 @@ export const openWXApp = async () => {
|
||||
return RNWechat.openWXApp()
|
||||
}
|
||||
|
||||
/**
|
||||
* 打开小程序
|
||||
* @param {String} username 小程序原始 id
|
||||
* @param {String} path 小程序页面路径
|
||||
* @param {'test' | 'preview' | 'release'} [type='release'}] 小程序类型
|
||||
* @return {Boolean}
|
||||
*/
|
||||
export const launchMiniProgram = async ({
|
||||
username,
|
||||
path,
|
||||
type = 'release'
|
||||
}) => {
|
||||
if (Platform.OS === 'android') {
|
||||
await checkWechatModuleIsEnable()
|
||||
}
|
||||
if (
|
||||
!await RNWechat.launchMiniProgram(username, path, getWXMiniProgramType(type))
|
||||
) throw new WechatError(Errors.RequestFailed)
|
||||
return listenAndHandleWechatResponse(WXRespType.LaunchMiniProgram)
|
||||
}
|
||||
|
||||
/**
|
||||
* 授权
|
||||
* @desc 只有 snsapi_userinfo scope 有效
|
||||
|
||||
Reference in New Issue
Block a user