2020-04-26 14:57:08 +08:00
//
// MOPAppletDelegate.m
// mop
//
// Created by 康旭耀 on 2020/4/20.
//
# import " MOPAppletDelegate.h "
# import " MopPlugin.h "
2021-02-26 13:41:45 +08:00
# import " MopCustomMenuModel.h "
2022-08-25 21:40:52 +08:00
# import < mop / MOPTools . h >
2023-01-03 22:13:59 +08:00
# import < objc / runtime . h >
2020-04-26 14:57:08 +08:00
2021-12-22 21:03:38 +08:00
@ interface NSString ( FATEncode )
- ( NSString * ) fat_encodeString ;
@ end
2020-04-26 14:57:08 +08:00
@ implementation MOPAppletDelegate
2020-04-27 18:55:36 +08:00
+ ( instancetype ) instance
{
static MOPAppletDelegate * _instance ;
static dispatch_once_t once ;
dispatch_once ( & once , ^ {
_instance = [ [ self alloc ] init ] ;
} ) ;
return _instance ;
}
2020-04-26 14:57:08 +08:00
- ( void ) forwardAppletWithInfo: ( NSDictionary * ) contentInfo completion: ( void ( ^ ) ( FATExtensionCode , NSDictionary * ) ) completion
{
2020-12-10 17:53:10 +08:00
NSLog ( @ " forwardAppletWithInfo1:%@ " , contentInfo ) ;
2020-04-26 14:57:08 +08:00
FlutterMethodChannel * channel = [ [ MopPlugin instance ] methodChannel ] ;
2020-04-27 18:55:36 +08:00
[ channel invokeMethod: @ " extensionApi:forwardApplet " arguments: @ { @ " appletInfo " : contentInfo } result: ^ ( id _Nullable result ) {
2020-04-26 14:57:08 +08:00
if ( [ result isKindOfClass: [ FlutterError class ] ] | | [ result isKindOfClass: [ FlutterMethodNotImplemented class ] ] )
{
completion ( FATExtensionCodeFailure , nil ) ;
} else
{
completion ( FATExtensionCodeSuccess , result ) ;
}
} ] ;
}
2023-01-11 14:55:08 +08:00
- ( NSDictionary * ) getUserInfoWithAppletInfo: ( FATAppletInfo * ) appletInfo {
2020-04-26 14:57:08 +08:00
NSLog ( @ " getUserInfoWithAppletInfo " ) ;
__block NSDictionary * userInfo ;
FlutterMethodChannel * channel = [ [ MopPlugin instance ] methodChannel ] ;
2021-03-03 22:59:09 +08:00
[ channel invokeMethod: @ " extensionApi:getUserInfo " arguments: nil result: ^ ( id _Nullable result ) {
CFRunLoopStop ( CFRunLoopGetMain ( ) ) ;
userInfo = result ;
2020-04-26 14:57:08 +08:00
} ] ;
2021-03-03 22:59:09 +08:00
CFRunLoopRun ( ) ;
2020-04-26 14:57:08 +08:00
return userInfo ;
}
2023-04-07 21:40:02 +08:00
- ( BOOL ) appletInfo: ( FATAppletInfo * ) appletInfo didClickMoreBtnAtPath: ( NSString * ) path {
2023-08-14 11:55:29 +08:00
NSLog ( @ " didClickMoreBtnAtPath:%@,appletInfo=%@ " , path , appletInfo ) ;
2023-08-14 09:24:21 +08:00
return NO ;
// __block BOOL flag;
// FlutterMethodChannel *channel = [[MopPlugin instance] methodChannel];
// NSLog(@"appletInfo:didClickMoreBtnAtPath,appId=%@,path=%@,channel=%@",appletInfo.appId,path,channel);
// [channel invokeMethod:@"extensionApi:customCapsuleMoreButtonClick" arguments:@{@"appId": appletInfo.appId} result:^(id _Nullable result) {
// CFRunLoopStop(CFRunLoopGetMain());
// if ([result isKindOfClass:[NSNumber class]]) {
// flag = [result boolValue];
// }
// }];
// CFRunLoopRun();
//
// return flag;
2023-04-07 21:40:02 +08:00
}
2021-02-26 13:41:45 +08:00
- ( NSArray < id < FATAppletMenuProtocol > > * ) customMenusInApplet: ( FATAppletInfo * ) appletInfo atPath: ( NSString * ) path {
2023-08-14 11:55:29 +08:00
NSLog ( @ " customMenusInApplet:%@,appletInfo=%@ " , path , appletInfo ) ;
2023-08-22 18:02:39 +08:00
NSDictionary < NSString * , NSString * > * item1 = @ { @ " menuId " : @ " shareToChatPicId " , @ " title " : @ " Share to friends " , @ " image " : @ " miniChat " , @ " darkImage " : @ " miniChat " , @ " type " : @ " common " } ;
NSDictionary < NSString * , NSString * > * item2 = @ { @ " menuId " : @ " shareToOutsidePicId " , @ " title " : @ " Share to others " , @ " image " : @ " miniSend " , @ " darkImage " : @ " miniSend " , @ " type " : @ " common " } ;
NSDictionary < NSString * , NSString * > * item3 = @ { @ " menuId " : @ " shareQrcodePicId " , @ " title " : @ " Phiz code " , @ " image " : @ " qrCode " , @ " darkImage " : @ " qrCode " , @ " type " : @ " common " } ;
NSDictionary < NSString * , NSString * > * item4 = @ { @ " menuId " : @ " 555 " , @ " title " : @ " 小程序支付测试4 " , @ " image " : @ " qrCode " , @ " darkImage " : @ " qrCode " , @ " type " : @ " common " } ;
2023-08-14 11:55:29 +08:00
2023-08-22 18:02:39 +08:00
NSArray < NSDictionary < NSString * , NSString * > * > * list = @ [ item1 , item2 , item3 , item4 ] ;
2023-08-14 11:55:29 +08:00
NSLog ( @ " customMenusInApplet:%@,list=%@ " , path , list ) ;
2023-08-22 18:02:39 +08:00
2021-03-03 22:59:09 +08:00
NSMutableArray * models = [ NSMutableArray array ] ;
for ( NSDictionary < NSString * , NSString * > * data in list ) {
MopCustomMenuModel * model = [ [ MopCustomMenuModel alloc ] init ] ;
model . menuId = data [ @ " menuId " ] ;
model . menuTitle = data [ @ " title " ] ;
2023-01-03 23:19:33 +08:00
NSString * imageUrl = data [ @ " image " ] ;
if ( [ imageUrl hasPrefix: @ " http " ] ) {
// 需要异步加载,待优化!
2023-06-09 16:43:26 +08:00
model . menuIconUrl = imageUrl ;
2023-01-03 23:19:33 +08:00
} else {
model . menuIconImage = [ UIImage imageNamed: imageUrl ] ;
}
2023-06-14 19:06:10 +08:00
NSString * darkImageUrl = data [ @ " darkImage " ] ;
if ( [ darkImageUrl hasPrefix: @ " http " ] ) {
model . menuDarkIconUrl = darkImageUrl ;
} else {
model . menuIconDarkImage = [ UIImage imageNamed: darkImageUrl ] ;
}
2021-03-03 22:59:09 +08:00
NSString * typeString = data [ @ " type " ] ;
if ( typeString ) {
FATAppletMenuStyle style = [ typeString isEqualToString: @ " onMiniProgram " ] ? FATAppletMenuStyleOnMiniProgram : FATAppletMenuStyleCommon ;
model . menuType = style ;
}
[ models addObject: model ] ;
}
2021-02-26 13:41:45 +08:00
2021-03-03 22:59:09 +08:00
return models ;
}
2023-08-22 18:02:39 +08:00
2022-03-31 15:50:34 +08:00
- ( void ) clickCustomItemMenuWithInfo: ( NSDictionary * ) contentInfo inApplet: ( FATAppletInfo * ) appletInfo completion: ( void ( ^ ) ( FATExtensionCode code , NSDictionary * result ) ) completion {
2023-01-03 22:13:59 +08:00
NSError * parseError = nil ;
NSMutableDictionary * shareDic = [ [ NSMutableDictionary alloc ] initWithDictionary: [ self dictionaryRepresentation: appletInfo ] ] ;
[ shareDic setValue: @ { @ " desc " : shareDic [ @ " originalInfo " ] [ @ " customData " ] [ @ " detailDescription " ] } forKey: @ " params " ] ;
2023-01-11 16:31:12 +08:00
[ shareDic setValue: contentInfo [ @ " query " ] forKey: @ " query " ] ;
2023-01-03 22:13:59 +08:00
NSData * jsonData = [ NSJSONSerialization dataWithJSONObject: shareDic options: NSJSONWritingPrettyPrinted error: & parseError ] ;
NSString * jsonString = [ [ NSString alloc ] initWithData: jsonData encoding: NSUTF8StringEncoding ] ;
2021-03-03 22:59:09 +08:00
NSDictionary * arguments = @ {
@ " appId " : contentInfo [ @ " appId " ] ,
@ " path " : contentInfo [ @ " path " ] ,
@ " menuId " : contentInfo [ @ " menuId " ] ,
2023-01-11 16:31:12 +08:00
@ " appInfo " : jsonString
2021-03-03 22:59:09 +08:00
} ;
FlutterMethodChannel * channel = [ [ MopPlugin instance ] methodChannel ] ;
[ channel invokeMethod: @ " extensionApi:onCustomMenuClick " arguments: arguments result: ^ ( id _Nullable result ) {
} ] ;
2022-03-31 15:50:34 +08:00
2021-12-22 21:03:38 +08:00
if ( [ @ " Desktop " isEqualToString: contentInfo [ @ " menuId " ] ] ) {
[ self addToDesktopItemClick: appletInfo path: contentInfo [ @ " path " ] ] ;
}
}
2023-01-03 22:13:59 +08:00
- ( NSDictionary * ) dictionaryRepresentation: ( FATAppletInfo * ) object {
unsigned int count ;
objc_property_t * properties = class_copyPropertyList ( [ object class ] , & count ) ;
NSMutableDictionary * dict = [ NSMutableDictionary dictionaryWithCapacity: count ] ;
for ( int i = 0 ; i < count ; i + + ) {
NSString * key = [ NSString stringWithUTF8String: property_getName ( properties [ i ] ) ] ;
id value = [ object valueForKey: key ] ;
if ( key & & value ) {
[ dict setObject: value forKey: key ] ;
}
}
free ( properties ) ;
return dict ;
}
2021-10-19 21:26:05 +08:00
- ( void ) applet: ( NSString * ) appletId didOpenCompletion: ( NSError * ) error {
if ( ! appletId ) {
return ;
}
NSDictionary * params = @ { @ " appId " : appletId } ;
FlutterMethodChannel * channel = [ [ MopPlugin instance ] methodChannel ] ;
[ channel invokeMethod: @ " extensionApi:appletDidOpen " arguments: params result: ^ ( id _Nullable result ) {
} ] ;
}
2021-12-22 21:03:38 +08:00
static NSString * scheme = @ " fatae55433be2f62915 " ; //App对应的scheme
- ( void ) addToDesktopItemClick: ( FATAppletInfo * ) appInfo path: ( NSString * ) path {
NSMutableString * herf = [ NSString stringWithFormat: @ " %@://applet/appid/%@? " , scheme , appInfo . appId ] . mutableCopy ;
NSString * query = [ NSString stringWithFormat: @ " apiServer=%@&path=%@ " , appInfo . apiServer , path ] ;
if ( [ appInfo . startParams [ @ " query " ] length ] ) {
query = [ NSString stringWithFormat: @ " %@&query=%@ " , query , appInfo . startParams [ @ " query " ] ] ;
}
[ herf appendString: query . fat_encodeString ] ;
NSMutableString * url = [ NSMutableString stringWithFormat: @ " %@/mop/scattered-page/#/desktopicon " , appInfo . apiServer ] ;
[ url appendFormat: @ " ?iconpath=%@ " , appInfo . appAvatar ] ;
[ url appendFormat: @ " &apptitle=%@ " , appInfo . appTitle . fat_encodeString ] ;
[ url appendFormat: @ " &linkhref=%@ " , herf ] ;
NSLog ( @ " 跳转到中间页面:%@ " , url ) ;
[ [ UIApplication sharedApplication ] openURL: [ NSURL URLWithString: url ] ] ;
}
2022-05-17 17:58:19 +08:00
- ( void ) getPhoneNumberWithAppletInfo: ( FATAppletInfo * ) appletInfo bindGetPhoneNumber: ( void ( ^ ) ( NSDictionary * ) ) bindGetPhoneNumber {
NSDictionary * params = @ { @ " name " : @ " getPhoneNumber " } ;
2021-12-22 21:03:38 +08:00
2022-05-17 17:58:19 +08:00
FlutterMethodChannel * channel = [ [ MopPlugin instance ] methodChannel ] ;
2023-06-08 23:29:36 +08:00
self . bindGetPhoneNumbers = bindGetPhoneNumber ;
NSLog ( @ " getPhoneNumberWithAppletInfo " ) ;
2022-05-17 17:58:19 +08:00
[ channel invokeMethod: @ " extensionApi:getPhoneNumber " arguments: params result: ^ ( id _Nullable result ) {
2022-10-11 12:54:57 +08:00
// !self.bindGetPhoneNumbers?: bindGetPhoneNumber(result);
2022-05-17 17:58:19 +08:00
} ] ;
}
2022-08-25 00:27:14 +08:00
- ( void ) chooseAvatarWithAppletInfo: ( FATAppletInfo * ) appletInfo bindChooseAvatar: ( void ( ^ ) ( NSDictionary * result ) ) bindChooseAvatar {
2022-08-25 21:40:52 +08:00
UIAlertController * alertController = [ UIAlertController alertControllerWithTitle: nil
message: nil
preferredStyle: UIAlertControllerStyleActionSheet ] ;
2022-11-25 10:14:14 +08:00
NSString * album = [ MOPTools fat_currentLanguageIsEn ] ? @ " Album " : @ " 从相册选择 " ;
UIAlertAction * chooseAlbumAction = [ UIAlertAction actionWithTitle: album style: UIAlertActionStyleDefault handler: ^ ( UIAlertAction * _Nonnull action ) {
2022-08-25 21:40:52 +08:00
NSDictionary * params = @ { @ " name " : @ " chooseAvatarAlbum " } ;
FlutterMethodChannel * channel = [ [ MopPlugin instance ] methodChannel ] ;
[ channel invokeMethod: @ " extensionApi:chooseAvatarAlbum " arguments: params result: ^ ( id _Nullable result ) {
! bindChooseAvatar ? : bindChooseAvatar ( result ) ;
} ] ;
} ] ;
2022-11-25 10:14:14 +08:00
NSString * camera = [ MOPTools fat_currentLanguageIsEn ] ? @ " Camera " : @ " 拍照 " ;
UIAlertAction * photoAction = [ UIAlertAction actionWithTitle: camera style: UIAlertActionStyleDefault handler: ^ ( UIAlertAction * _Nonnull action ) {
2022-08-25 21:40:52 +08:00
NSDictionary * params = @ { @ " name " : @ " chooseAvatarPhoto " } ;
FlutterMethodChannel * channel = [ [ MopPlugin instance ] methodChannel ] ;
[ channel invokeMethod: @ " extensionApi:chooseAvatarPhoto " arguments: params result: ^ ( id _Nullable result ) {
! bindChooseAvatar ? : bindChooseAvatar ( result ) ;
} ] ;
2022-08-25 00:27:14 +08:00
} ] ;
2022-11-25 10:14:14 +08:00
NSString * cancel = [ MOPTools fat_currentLanguageIsEn ] ? @ " Cancel " : @ " 取消 " ;
UIAlertAction * cancelAction = [ UIAlertAction actionWithTitle: cancel style: UIAlertActionStyleCancel handler: ^ ( UIAlertAction * _Nonnull action ) {
2022-08-25 21:40:52 +08:00
bindChooseAvatar ( @ { } ) ;
} ] ;
[ alertController addAction: chooseAlbumAction ] ;
[ alertController addAction: photoAction ] ;
[ alertController addAction: cancelAction ] ;
UIViewController * topVC = [ MOPTools topViewController ] ;
[ topVC presentViewController: alertController animated: YES completion: nil ] ;
2022-08-25 00:27:14 +08:00
}
2022-08-25 21:40:52 +08:00
2021-12-22 21:03:38 +08:00
@ end
@ implementation NSString ( FATEncode )
- ( NSString * ) fat_encodeString {
return ( __bridge NSString * ) CFURLCreateStringByAddingPercentEscapes ( NULL , ( __bridge CFStringRef ) self , NULL , ( CFStringRef ) @ " !*'();:@&=+ $ ,/?%#[] " , kCFStringEncodingUTF8 ) ;
}
2020-04-26 14:57:08 +08:00
@ end