diff --git a/Classes_cocos/JcWallet.h b/Classes_cocos/JcWallet.h index f39b4ea..f0d8fbd 100644 --- a/Classes_cocos/JcWallet.h +++ b/Classes_cocos/JcWallet.h @@ -17,6 +17,7 @@ NS_CC_BEGIN se::ValueArray args; }; bool addToArgArray(se::ValueArray *args, const char *valChar); + int nativeCallBack(const char *funId, const char *methodName, const char *cparams); class CC_DLL JcWallet { public: diff --git a/Classes_cocos/JcWallet.mm b/Classes_cocos/JcWallet.mm index 5f5b4f6..f3be893 100644 --- a/Classes_cocos/JcWallet.mm +++ b/Classes_cocos/JcWallet.mm @@ -30,6 +30,7 @@ NS_CC_BEGIN bool _isStarted = false; uv_async_t gasync = {0}; + struct AsyncTaskData { std::mutex mtx; std::list > tasks; @@ -183,6 +184,21 @@ NS_CC_BEGIN } } +int nativeCallBack(const char *funId, const char *methodName, const char *cparams) { + JSMethodParam *data = new JSMethodParam(); + std::string methodNameStr(methodName); + data->methodName = methodNameStr; + data->funId = funId; + data->paramCount = 1; + addToArgArray(&data->args, funId); + addToArgArray(&data->args, cparams); + std::shared_ptr params(data); + int result = schedule_task_into_server_thread_task_queue(&gasync, [=](){ + JcWallet::getInstance()->runJsMethod(params); + }); + return result == 0 ? 1 : 0; +} + NS_CC_END static bool getOrCreatePlainObject_r(const char* name, se::Object* parent, se::Object** outObj) { @@ -242,6 +258,62 @@ bool jsb_scanQRCode(se::State& s) { } SE_BIND_FUNC(jsb_scanQRCode) +static bool JSB_showQRCode(se::State& s) +{ + const auto& args = s.args(); + size_t argc = args.size(); + CC_UNUSED bool ok = true; + if (argc > 4) { + std::string funid; + ok = seval_to_std_string(args[0], &funid); + SE_PRECONDITION2(ok, false, "funid is invalid!"); + std::string text; + ok = seval_to_std_string(args[1], &text); + SE_PRECONDITION2(ok, false, "content is invalid!"); + std::string title; + ok = seval_to_std_string(args[2], &title); + SE_PRECONDITION2(ok, false, "title is invalid!"); + std::string oid; + ok = seval_to_std_string(args[3], &oid); + SE_PRECONDITION2(ok, false, "data id is invalid!"); + NSString *ntitle = [NSString stringWithCString:title.c_str() encoding:NSUTF8StringEncoding]; + NSString *nfunid = [NSString stringWithCString:funid.c_str() encoding:NSUTF8StringEncoding]; + NSString *ncontent = [NSString stringWithCString:text.c_str() encoding:NSUTF8StringEncoding]; + NSString *noid = [NSString stringWithCString:oid.c_str() encoding:NSUTF8StringEncoding]; + UIWindow* window = [[[UIApplication sharedApplication] delegate] window]; + [window.rootViewController showQRCode:nfunid content:ncontent title:ntitle oid: noid]; + return true; + } + + SE_REPORT_ERROR("wrong number of arguments: %d, was expecting %d", (int)argc, 4); + return false; +} +SE_BIND_FUNC(JSB_showQRCode) + +static bool JSB_restoreKey(se::State& s) +{ + const auto& args = s.args(); + size_t argc = args.size(); + CC_UNUSED bool ok = true; + if (argc > 1) { + std::string funid; + ok = seval_to_std_string(args[0], &funid); + SE_PRECONDITION2(ok, false, "funid is invalid!"); + std::string oid; + ok = seval_to_std_string(args[1], &oid); + SE_PRECONDITION2(ok, false, "oid is invalid!"); + NSString *nfunid = [NSString stringWithCString:funid.c_str() encoding:NSUTF8StringEncoding]; + NSString *noid = [NSString stringWithCString:oid.c_str() encoding:NSUTF8StringEncoding]; + UIWindow* window = [[[UIApplication sharedApplication] delegate] window]; + [window.rootViewController loadRestoreKey:nfunid oid: noid]; + return true; + } + + SE_REPORT_ERROR("wrong number of arguments: %d, was expecting %d", (int)argc, 2); + return false; +} +SE_BIND_FUNC(JSB_restoreKey) + bool jsb_signWithGoogle(se::State& s) { const auto& args = s.args(); size_t argc = args.size(); @@ -276,10 +348,15 @@ bool jsb_signOutGoogle(se::State& s) { } SE_BIND_FUNC(jsb_signOutGoogle) + + + bool jsb_register_walletevent_modules(se::Object* global) { getOrCreatePlainObject_r("jsb", global, &__jsbObj); __jsbObj->defineFunction("jcCallback", _SE(jsb_wallet_callback)); __jsbObj->defineFunction("scanQRCode", _SE(jsb_scanQRCode)); + __jsbObj->defineFunction("restoreKey", _SE(JSB_restoreKey)); + __jsbObj->defineFunction("showQRCode", _SE(JSB_showQRCode)); __jsbObj->defineFunction("signWithGoogle", _SE(jsb_signWithGoogle)); __jsbObj->defineFunction("signOutGoogle", _SE(jsb_signOutGoogle)); return true; diff --git a/Classes_cocos/UIViewController+Wallet.h b/Classes_cocos/UIViewController+Wallet.h index 0e64ba9..8912dcb 100644 --- a/Classes_cocos/UIViewController+Wallet.h +++ b/Classes_cocos/UIViewController+Wallet.h @@ -14,6 +14,8 @@ +(void)toWallet:(NSString *)url; -(void)scanQRCode:(NSString *)funid title:(NSString *) title; +-(void)showQRCode:(NSString *)funid content:(NSString *) content title:(NSString *) title oid:(NSString *) oid; +-(void)loadRestoreKey:(NSString *)funid oid:(NSString *) oid; -(void)signWithGoogle:(NSString *)funid; -(void)signOutGoogle:(NSString *)funid; @end diff --git a/Classes_cocos/UIViewController+Wallet.mm b/Classes_cocos/UIViewController+Wallet.mm index af9b2a5..5d2146a 100644 --- a/Classes_cocos/UIViewController+Wallet.mm +++ b/Classes_cocos/UIViewController+Wallet.mm @@ -11,11 +11,12 @@ #import "QRCodeReaderDelegate.h" #include #include "WalletEvent.h" +#include "JcWallet.h" #import @import GoogleSignIn; static NSString * const kClientID = -@"165555585193-alshovj5m91vlmj8uuhb11e6msvq32gm.apps.googleusercontent.com"; +@"53206975661-qan0rnefniegjv53ohild375pv0p7ekd.apps.googleusercontent.com"; @implementation UIViewController (Wallet) @@ -24,6 +25,13 @@ static NSString * const kClientID = [app openURL:[NSURL URLWithString:url]]; } +-(void)showQRCode:(NSString *)funid content:(NSString *) content title:(NSString *) title oid:(NSString *) oid { + +} +-(void)loadRestoreKey:(NSString *)funid oid:(NSString *) oid{ + +} + -(void)scanQRCode:(NSString *)funid title:(NSString *) title{ NSLog(@"scanQRCode:: funId: %@ title: %@", funid, title); std::string sfunid = std::string([funid UTF8String], [funid lengthOfBytesUsingEncoding:NSUTF8StringEncoding]); @@ -87,7 +95,7 @@ static NSString * const kClientID = } } --(void)signToGoogle { +-(void)signToGoogle:(NSString *) funid { GIDConfiguration *_configuration = [[GIDConfiguration alloc] initWithClientID:kClientID]; [GIDSignIn.sharedInstance signInWithConfiguration:_configuration presentingViewController:self @@ -96,10 +104,10 @@ static NSString * const kClientID = NSLog(@"Status: Authentication error: %@", error); } [self refreshUserInfo]; - [self refreshTokenID: user]; + [self refreshTokenID: user funid:funid]; }]; } --(void) refreshTokenID:(GIDGoogleUser *)user { +-(void) refreshTokenID:(GIDGoogleUser *)user funid:(NSString*) funid{ [user.authentication doWithFreshTokens:^(GIDAuthentication * _Nullable authentication, NSError * _Nullable error) { if (error) { return; } @@ -108,6 +116,11 @@ static NSString * const kClientID = NSString *idToken = authentication.idToken; // Send ID token to backend (example below). NSLog(@"idToken: %@", idToken); + std::string methodName = "nativeCallBack"; + NSString *paramStr = [NSString stringWithFormat:@"{\"errcode\": 0, \"data\": \"%@\"}", idToken]; + std::string sfunid = std::string([funid UTF8String], [funid lengthOfBytesUsingEncoding:NSUTF8StringEncoding]); + std::string sparam = std::string([paramStr UTF8String], [paramStr lengthOfBytesUsingEncoding:NSUTF8StringEncoding]); + cocos2d::nativeCallBack(sfunid.c_str(), methodName.c_str(), sparam.c_str()); }]; } @@ -116,10 +129,10 @@ static NSString * const kClientID = NSError * _Nullable error) { if (error) { // Show the app's signed-out state. - [self signToGoogle]; + [self signToGoogle: funid]; } else { // Show the app's signed-in state. - [self refreshTokenID: user]; + [self refreshTokenID: user funid:funid]; } }]; @@ -153,5 +166,26 @@ static NSString * const kClientID = NSLog(@"hostedDomain: %@", GIDSignIn.sharedInstance.currentUser.hostedDomain); } +-(void)checkOnSvr:(NSString *) idToken { +// NSString *signinEndpoint = @"https://yourbackend.example.com/tokensignin"; +// NSDictionary *params = @{@"idtoken": idToken}; +// +// NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:signinEndpoint]; +// [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; +// [request setHTTPMethod:@"POST"]; +// [request setHTTPBody:[self httpBodyForParamsDictionary:params]]; +// +// NSOperationQueue *queue = [[NSOperationQueue alloc] init]; +// [NSURLConnection sendAsynchronousRequest:request +// queue:queue +// completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { +// if (error) { +// NSLog(@"Error: %@", error.localizedDescription); +// } else { +// NSLog(@"Signed in as %@", data.bytes); +// } +// }]; +} + @end