diff --git a/android/src/main/java/com/wechatlib/WeChatLibModule.java b/android/src/main/java/com/wechatlib/WeChatLibModule.java index d3e8852..f432416 100644 --- a/android/src/main/java/com/wechatlib/WeChatLibModule.java +++ b/android/src/main/java/com/wechatlib/WeChatLibModule.java @@ -32,6 +32,7 @@ import com.facebook.react.modules.core.DeviceEventManagerModule; import com.tencent.mm.opensdk.modelbase.BaseReq; import com.tencent.mm.opensdk.modelbase.BaseResp; import com.tencent.mm.opensdk.modelbiz.ChooseCardFromWXCardPackage; +import com.tencent.mm.opensdk.modelbiz.WXOpenCustomerServiceChat; import com.tencent.mm.opensdk.modelmsg.SendAuth; import com.tencent.mm.opensdk.modelmsg.SendMessageToWX; import com.tencent.mm.opensdk.modelmsg.ShowMessageFromWX; @@ -672,6 +673,20 @@ public class WeChatLibModule extends ReactContextBaseJavaModule implements IWXAP callback.invoke(api.sendReq(payReq) ? null : INVOKE_FAILED); } + @ReactMethod + public void openCustomerServiceChat(String kfUrl, Callback callback) { + if (api == null) { + callback.invoke(NOT_REGISTERED); + return; + } + // open customer service logic + WXOpenCustomerServiceChat.Req req = new WXOpenCustomerServiceChat.Req(); + + req.corpId = this.appId; + req.url = kfUrl; + callback.invoke(null, api.sendReq(req)); + } + private void _share(final int scene, final ReadableMap data, final Callback callback) { Uri uri = null; if (data.hasKey("thumbImage")) { diff --git a/ios/WechatLib.mm b/ios/WechatLib.mm index 51e6160..68d318b 100644 --- a/ios/WechatLib.mm +++ b/ios/WechatLib.mm @@ -568,6 +568,16 @@ RCT_EXPORT_METHOD(pay:(NSDictionary *)data // callback(@[success ? [NSNull null] : INVOKE_FAILED]); } +// 跳转微信客服 +RCT_EXPORT_METHOD(openCustomerServiceChat:(NSString *)kfUrl + :(RCTResponseSenderBlock)callback) +{ + WXOpenCustomerServiceReq *req = [[WXOpenCustomerServiceReq alloc] init]; + req.corpid = self.appId; //企业ID + req.url = kfUrl; //客服URL + [WXApi sendReq:req completion:nil]; +} + #pragma mark - wx callback -(void) onReq:(BaseReq*)req diff --git a/ios/WechatLib.xcodeproj/project.pbxproj b/ios/WechatLib.xcodeproj/project.pbxproj index e1229f8..54f5697 100644 --- a/ios/WechatLib.xcodeproj/project.pbxproj +++ b/ios/WechatLib.xcodeproj/project.pbxproj @@ -8,7 +8,7 @@ /* Begin PBXBuildFile section */ 86D0366429A4BF3E00A01343 /* libWeChatSDK.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 86D0365E29A4BF2600A01343 /* libWeChatSDK.a */; }; - 86D0366529A4BF5900A01343 /* WechatLib.m in Sources */ = {isa = PBXBuildFile; fileRef = B3E7B5891CC2AC0600A0062D /* WechatLib.m */; }; + 86D0366529A4BF5900A01343 /* WechatLib.mm in Sources */ = {isa = PBXBuildFile; fileRef = B3E7B5891CC2AC0600A0062D /* WechatLib.mm */; }; /* End PBXBuildFile section */ /* Begin PBXCopyFilesBuildPhase section */ @@ -31,7 +31,7 @@ 86D0366029A4BF2700A01343 /* WechatAuthSDK.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WechatAuthSDK.h; sourceTree = ""; }; 86D0366129A4BF2700A01343 /* WXApi.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WXApi.h; sourceTree = ""; }; B3E7B5881CC2AC0600A0062D /* WechatLib.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WechatLib.h; sourceTree = ""; }; - B3E7B5891CC2AC0600A0062D /* WechatLib.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = WechatLib.m; sourceTree = ""; }; + B3E7B5891CC2AC0600A0062D /* WechatLib.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = WechatLib.mm; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -63,7 +63,7 @@ 86D0366129A4BF2700A01343 /* WXApi.h */, 86D0365D29A4BF2600A01343 /* WXApiObject.h */, B3E7B5881CC2AC0600A0062D /* WechatLib.h */, - B3E7B5891CC2AC0600A0062D /* WechatLib.m */, + B3E7B5891CC2AC0600A0062D /* WechatLib.mm */, 134814211AA4EA7D00B7C361 /* Products */, 86D0366329A4BF3E00A01343 /* Frameworks */, ); @@ -133,7 +133,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( - 86D0366529A4BF5900A01343 /* WechatLib.m in Sources */, + 86D0366529A4BF5900A01343 /* WechatLib.mm in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/src/index.d.ts b/src/index.d.ts index 4d07935..164f405 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -9,7 +9,11 @@ enum WXScene { } declare module 'react-native-wechat-lib' { - export function registerApp(appId: string, universalLink?: string): Promise; + export function registerApp( + appId: string, + universalLink?: string + ): Promise; + export function openCustomerServiceChat(kfUrl: string): Promise; export function isWXAppInstalled(): Promise; export function isWXAppSupportApi(): Promise; export function getApiVersion(): Promise; @@ -39,9 +43,20 @@ declare module 'react-native-wechat-lib' { state?: string; returnKey?: string; } - export function sendAuthRequest(scope: string | string[], state?: string): Promise; + export function sendAuthRequest( + scope: string | string[], + state?: string + ): Promise; export interface ShareMetadata { - type: 'news' | 'text' | 'imageUrl' | 'imageFile' | 'imageResource' | 'video' | 'audio' | 'file'; + type: + | 'news' + | 'text' + | 'imageUrl' + | 'imageFile' + | 'imageResource' + | 'video' + | 'audio' + | 'file'; thumbImage?: string; description?: string; webpageUrl?: string; @@ -109,31 +124,31 @@ declare module 'react-native-wechat-lib' { } export function shareText( - message: ShareTextMetadata, + message: ShareTextMetadata ): Promise<{ errCode?: number; errStr?: string }>; export function shareImage( - message: ShareImageMetadata, + message: ShareImageMetadata ): Promise<{ errCode?: number; errStr?: string }>; export function shareLocalImage( - message: ShareImageMetadata, + message: ShareImageMetadata ): Promise<{ errCode?: number; errStr?: string }>; export function shareMusic( - message: ShareMusicMetadata, + message: ShareMusicMetadata ): Promise<{ errCode?: number; errStr?: string }>; export function shareVideo( - message: ShareVideoMetadata, + message: ShareVideoMetadata ): Promise<{ errCode?: number; errStr?: string }>; export function shareWebpage( - message: ShareWebpageMetadata, + message: ShareWebpageMetadata ): Promise<{ errCode?: number; errStr?: string }>; export function shareMiniProgram( - message: ShareMiniProgramMetadata, + message: ShareMiniProgramMetadata ): Promise<{ errCode?: number; errStr?: string }>; export function launchMiniProgram( - message: LaunchMiniProgramMetadata, + message: LaunchMiniProgramMetadata ): Promise<{ errCode?: number; errStr?: string }>; export function subscribeMessage( - message: SubscribeMessageMetadata, + message: SubscribeMessageMetadata ): Promise<{ errCode?: number; errStr?: string }>; export interface PaymentLoad { partnerId: string; @@ -143,7 +158,9 @@ declare module 'react-native-wechat-lib' { package: string; sign: string; } - export function pay(payload: PaymentLoad): Promise<{ errCode?: number; errStr?: string }>; + export function pay( + payload: PaymentLoad + ): Promise<{ errCode?: number; errStr?: string }>; export interface ChooseInvoice { signType?: string; @@ -159,7 +176,7 @@ declare module 'react-native-wechat-lib' { } export function chooseInvoice( - data: ChooseInvoice, + data: ChooseInvoice ): Promise<{ errCode?: number; errStr?: string; cards: Invoice[] }>; export interface ShareFileMetadata { @@ -169,6 +186,6 @@ declare module 'react-native-wechat-lib' { scene?: WXScene; } export function shareFile( - data: ShareFileMetadata, + data: ShareFileMetadata ): Promise<{ errCode?: number; errStr?: string }>; } diff --git a/src/index.js b/src/index.js index 3bb59a6..c1e98fe 100644 --- a/src/index.js +++ b/src/index.js @@ -146,13 +146,19 @@ export const getApiVersion = wrapApi(WeChat.getApiVersion); * @return {Promise} */ export const openWXApp = wrapApi(WeChat.openWXApp); +/** + * Open wechat app + * @method openCustomerServiceChat + * @return {Promise} + */ +export const openCustomerServiceChat = wrapApi(WeChat.openCustomerServiceChat); // wrap the APIs -const nativeShareToTimeline = wrapApi(WeChat.shareToTimeline); +// const nativeShareToTimeline = wrapApi(WeChat.shareToTimeline); const nativeLaunchMiniProgram = wrapApi(WeChat.launchMiniProgram); -const nativeShareToSession = wrapApi(WeChat.shareToSession); +// const nativeShareToSession = wrapApi(WeChat.shareToSession); const nativeShareToFavorite = wrapApi(WeChat.shareToFavorite); -const nativeSendAuthRequest = wrapApi(WeChat.sendAuthRequest); +// const nativeSendAuthRequest = wrapApi(WeChat.sendAuthRequest); const nativeShareText = wrapApi(WeChat.shareText); const nativeShareImage = wrapApi(WeChat.shareImage); const nativeShareLocalImage = wrapApi(WeChat.shareLocalImage); @@ -385,14 +391,22 @@ export function shareMiniProgram(data) { * @param {Integer} miniProgramType - 拉起小程序的类型. 0-正式版 1-开发版 2-体验版 * @param {String} path - 拉起小程序页面的可带参路径,不填默认拉起小程序首页 */ -export function launchMiniProgram({ userName, miniProgramType = 0, path = '' }) { +export function launchMiniProgram({ + userName, + miniProgramType = 0, + path = '', +}) { return new Promise((resolve, reject) => { - if (miniProgramType !== 0 && miniProgramType !== 1 && miniProgramType !== 2) { + if ( + miniProgramType !== 0 && + miniProgramType !== 1 && + miniProgramType !== 2 + ) { reject( new WechatError({ errStr: '拉起小程序的类型不对,0-正式版 1-开发版 2-体验版', errCode: -1, - }), + }) ); return; }