Skip to content

syk83528/GetRouter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GetRouter

CI Status Version License Platform

Installation

// Podfile:
pod 'GetRouter', '1.0.5'

// 终端中:
pod install --repo-update

很纯粹的路由组件

提供一个无侵入的路由解析组件,本组件不涉及跳转操作,只用来解析路由,匹配路由,权限拦截. 文件查看顺序: GetRouter->GetRouterName->GetRouterHandler->GetRouterMiddleware

prepare

// 注册
func prepareRouter() {
    GetRouter.config(nativeScheme: "native", routeNameKey: "ios_router")
    GetRouter.registerHandler(GetRouterCommonHandler())
    GetRouter.registerHandler(GetRouterTestHandler())
}
// 路由名
extension GetRouterName {
    static let tabSelect: GetRouterName = "/common/tabSelect"
    static let test: GetRouterName = "/common/test"
    static let home1: GetRouterName = "/common/home1"
    static let home2: GetRouterName = "/common/home2"
    static let home3: GetRouterName = "/common/home3"
    static let home4: GetRouterName = "/common/home4"
}

/// 公共
struct GetRouterCommonHandler: GetRouterHandlerSource {
    var pages: [GetPage] = [
        GetPage(name: .test,
                middlewares: [
                    GetRouterMiddlewareVip(priority: 1),
                ],
                action: { params in
                    TestHomePage().push()
                }
        ),
    ]
}

中间件拦截(权限校验)

// 同步
class GetRouterMiddlewareLogin: GetRouterMiddleware {
    
    override func handler(routeName: GetRouterName, params: GetDict?) -> Bool {
        let isLogin = Bool.random()
        if !isLogin {
            print("未登录")
        }
        return isLogin
    }
}

// 异步
class GetRouterMiddlewareVip: GetRouterMiddleware {
    override func handlerAsync(routeName: GetRouterName, params: GetDict?) async throws -> Bool {
        try await withCheckedThrowingContinuation({ continuation in
            DispatchQueue.global().asyncAfter(deadline: .now() + 2, execute: {
                continuation.resume(returning: true)
            })
        })
    }
}

调用

GetRouter.to(name: .home1, params: ["userId": 2222])
GetRouter.to(url: "native://?ios_router=/common/home1&userId=2222")

Author

suyikun, [email protected]

License

GetRouter is available under the MIT license. See the LICENSE file for more info.

About

swift路由组件

Resources

License

Stars

Watchers

Forks

Packages

No packages published