vapor 实践

vapor 根据验证码和邮箱完成用户注册

2018-10-06  本文已影响7人  搞好关系

结合之前的发送验证码,我们接着完成一个简短的用户注册:不考虑安全性
很简短我们定义一个用户的简短Model:

/// SK的用户数据模型
public struct SKUser: SQLiteModel {
    public  var id: Int?
    var name: String
    var createDate: TimeInterval?
    var updateDate: TimeInterval?
    var email: String
    var password:String
    
    /// 默认会发邮件点击d链接完成确认
    var status: Int? = SKUserStatus.unidentified.rawValue
    public   init(name: String, email: String, password: String) {
        self.name = name
        
        self.email = try! MD5.hash(email).base64EncodedString()
        self.password = try! MD5.hash(password).base64EncodedString()
        self.createDate = Date().timeIntervalSince1970
        self.updateDate = self.createDate
    }
}
extension SKUser: Migration&Content&Parameter {
    public static var defaultContentType: MediaType {
        return .json
    }
    
}

然后注册对应的路由
接下来是实现逻辑
1 验证校验码和邮箱是否合法

 SKRegistVerfiy.query(on: req)
            .group(SQLiteBinaryOperator.and, closure: { (and) in
                and.filter(\.verfiyCode, .equal, innerUser.code)
                and.filter(\.email, .equal, innerUser.email)
            }).all()
            .flatMap({ (sks) -> EventLoopFuture<String> in
                
                //首先判断是否存在 邮箱&验证码
                if sks.isEmpty {
                    let result = req.eventLoop.newPromise(String.self)
                    result.succeed(result: "邮箱和验证码不存在")
                    return result.futureResult
                }
                //判断验证码是否在有效期内
                for sk in sks {
                    if !sk.isCodeAvailable {
                        let result = req.eventLoop.newPromise(String.self)
                        
                        result.succeed(result: "验证码过期")
                        
                        return result.futureResult
                    }
                    
                }
...

2.校验用户是否存在当然此处我们是先必须有验证码才能注册可以省去这一步骤, 但是我们是练习就考虑一下用户是否注册过

 let innerUser : InnerUser = try req.query.decode(InnerUser.self)
                
                let skUser: SKUser = SKUser.init(name: innerUser.name
                    , email: innerUser.email
                    , password: innerUser.password)
                
                return  SKUser.query(on: req).group(SQLiteBinaryOperator.or, closure: { (or) in
                    //查询邮箱是否被注册过
                    or.filter(\.email, SQLiteBinaryOperator.equal, skUser.email)
                }).all().flatMap({ (us) -> EventLoopFuture<String> in
                    
                    if us.isEmpty {//没有注册 则开始注册
                        return
                            skUser.save(on: req).flatMap(to: String.self, { (u) -> EventLoopFuture<String> in
                                let result = req.eventLoop.newPromise(String.self)
                                result.succeed(result: "\(u)")
                                return result.futureResult
                            })
                    }else{
                        //已经被注册提示已经注册
                        let e = req.eventLoop.newPromise(String.self)
                        e.succeed(result: "用户已存在")
                        return e.futureResult
                    }
                })

最后附上简单的测试结果:


正常注册返回的用户信息 用户存在提示
上一篇下一篇

猜你喜欢

热点阅读