添加打开小程序的功能

This commit is contained in:
dongdayu
2018-07-12 20:38:45 +08:00
parent 91e941f5bc
commit afac00d5aa
7 changed files with 90 additions and 0 deletions

View File

@@ -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

View File

@@ -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;

View File

@@ -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;

View File

@@ -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

View File

@@ -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;

View File

@@ -56,6 +56,7 @@ export const WXMiniProgramTypes = {
*/
export const WXRespType = {
Auth: 'SendAuthResp',
LaunchMiniProgram: 'WXLaunchMiniProgramResp',
Message: 'SendMessageToWXResp',
Pay: 'PayResp'
}

View File

@@ -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 有效