mop-flutter-sdk/ios/Classes/Api/MOPAppletDelegate.m

248 lines
10 KiB
Dart
Raw Normal View History

2020-04-26 14:57:08 +08:00
//
// MOPAppletDelegate.m
// mop
//
// Created by 康旭耀 on 2020/4/20.
//
#import "MOPAppletDelegate.h"
#import "MopPlugin.h"
#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];
[channel invokeMethod:@"extensionApi:getUserInfo" arguments:nil result:^(id _Nullable result) {
CFRunLoopStop(CFRunLoopGetMain());
userInfo = result;
2020-04-26 14:57:08 +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-10-18 14:00:38 +08:00
__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
}
- (NSArray<id<FATAppletMenuProtocol>> *)customMenusInApplet:(FATAppletInfo *)appletInfo atPath:(NSString *)path {
2023-08-14 11:55:29 +08:00
NSLog(@"customMenusInApplet:%@,appletInfo=%@",path,appletInfo);
2023-10-18 14:00:38 +08:00
__block NSArray *list;
FlutterMethodChannel *channel = [[MopPlugin instance] methodChannel];
[channel invokeMethod:@"extensionApi:getCustomMenus" arguments:@{@"appId": appletInfo.appId} result:^(id _Nullable result) {
2023-10-18 15:08:53 +08:00
CFRunLoopStop(CFRunLoopGetMain());
2023-10-18 14:00:38 +08:00
if ([result isKindOfClass:[NSArray class]]) {
list = result;
2023-10-18 14:52:28 +08:00
NSLog(@"customMenusInApplet2222:list=%@",list);
2023-10-18 14:00:38 +08:00
}
}];
2023-10-18 15:08:53 +08:00
CFRunLoopRun();
2023-08-14 11:55:29 +08:00
NSLog(@"customMenusInApplet:%@,list=%@",path,list);
2023-08-22 18:02:39 +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];
}
NSString *typeString = data[@"type"];
if (typeString) {
FATAppletMenuStyle style = [typeString isEqualToString:@"onMiniProgram"] ? FATAppletMenuStyleOnMiniProgram : FATAppletMenuStyleCommon;
model.menuType = style;
}
[models addObject:model];
}
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-09-18 10:48:49 +08:00
NSLog(@"HJH1,clickCustomItemMenuWithInfo");
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];
NSDictionary *arguments = @{
@"appId": contentInfo[@"appId"],
@"path": contentInfo[@"path"],
@"menuId": contentInfo[@"menuId"],
2023-01-11 16:31:12 +08:00
@"appInfo": jsonString
};
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-09-18 10:48:49 +08:00
NSLog(@"HJH2,clickCustomItemMenuWithInfo");
2021-12-22 21:03:38 +08:00
}
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;
}
- (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];
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