App在用戶(hù)執(zhí)行需要登錄權(quán)限的操作時(shí)候,往往需要先進(jìn)行登錄判斷,如果未登錄,則直接跳轉(zhuǎn)到登錄頁(yè)面,否則直接進(jìn)入業(yè)務(wù)邏輯,代碼大致如下:
- (void)actionGift {
if ([NGCommonAPI isLogin]) {
//已登錄,進(jìn)入禮包頁(yè)面
NGMyGiftVController *viewController = [NGMyGiftVController new];
[self.navigationController pushViewController:viewController animated:YES];
}else {
//未登錄,跳轉(zhuǎn)到登錄頁(yè)面
NGLoginViewController *login = [[NGLoginViewController alloc] init];
[self presentViewController:[[UINavigationController alloc] initWithRootViewController:login] animated:YES completion:nil];
}
}
其他地方需要登錄邏輯判斷的,也都要添加同樣的邏輯代碼,這樣存在幾個(gè)問(wèn)題
- 跳轉(zhuǎn)登錄的一套相同代碼存在程序的多處地方,違反了封裝的原則
- if/else的寫(xiě)法具有迷惑性,因?yàn)榉堑卿浨闆r下,用戶(hù)除了跳轉(zhuǎn)到登錄頁(yè)面外,還有可能是其他的操作,不利于閱讀
- presentViewController函數(shù)依賴(lài)于當(dāng)前頁(yè)面,在非viewController地方使用不具有通用性
Python Flask 路由實(shí)現(xiàn)
由于用Flask開(kāi)發(fā)過(guò)后臺(tái)接口,發(fā)現(xiàn)其對(duì)需要登錄的處理十分優(yōu)雅,大致如下:
@admin.route('/h5_activity_prizes', methods=['GET'])
@login_required
def h5_activity_prizes():
ur"""h5活動(dòng)獎(jiǎng)品設(shè)置"""
g.uri_path = request.path
....
這里的login_required是python里的裝飾器,如果該接口是需要登錄權(quán)限的,則用該裝飾器修飾,否則不寫(xiě)。登錄的判斷邏輯,以及未登錄的處理都封裝在里面,使用者無(wú)需知道內(nèi)部的實(shí)現(xiàn)邏輯。是不是很簡(jiǎn)單?但是iOS并沒(méi)有裝飾器這樣的神器,試試用宏來(lái)實(shí)現(xiàn)看看
iOS 宏封裝登錄邏輯
- 新建一個(gè)GFCommonFun的通用類(lèi),封裝登錄校驗(yàn)函數(shù),這里登錄頁(yè)面的彈出依賴(lài)于rootViewController
+ (UIViewController *)rootViewController{
return [UIApplication sharedApplication].keyWindow.rootViewController;
}
+ (BOOL)checkLogin {
if ([GFCommonFun isLogin]) {
return NO;
}
UIViewController* root = [GFCommonFun rootViewController];
GFLoginVController *viewController = [[GFLoginVController alloc] init];
[root presentViewController:[[UINavigationController alloc] initWithRootViewController:viewController] animated:YES completion:^{
}];
return YES;
}
- 編寫(xiě)宏,注意這里使用return控制跳轉(zhuǎn),在宏展開(kāi)后,會(huì)跳出宏當(dāng)前所在的函數(shù)體,這樣實(shí)現(xiàn)了跳轉(zhuǎn)到登錄頁(yè)面后而無(wú)需執(zhí)行剩余代碼的功能
#define GF_Check_Login if([GFCommonFun checkLogin]) {return;};
3.最后在任意需要登錄權(quán)限的函數(shù)里第一行加上GF_Check_Login即可
#import "GFCommonFun.h"
//評(píng)論點(diǎn)贊
- (void)lightenComment:(NSString *)args {
GF_Check_Login
//具體點(diǎn)贊邏輯
...
}
基于同樣的原理,app中需要在函數(shù)執(zhí)行前的操作均可以封裝成宏的方式
備注
上述方案并非最佳方案,有更好的方案,請(qǐng)指教