//
//  YHLoginViewModel.swift
//  galaxy
//
//  Created by EDY on 2024/1/17.
//  Copyright © 2024 https://www.galaxy-immi.com. All rights reserved.
//

import UIKit

class YHLoginViewModel: YHBaseViewModel {
    var userModel: YHUserModel?

    override init() {
        super.init()
    }
}

//接口
extension YHLoginViewModel {
    //发送验证码
    func getLoginCode(callBackBlock: @escaping (_ success: Bool)->()) {
        let strUrl = YHBaseUrlManager.shared.curURL() + "/user/login-code"
        let _ = YHNetRequest.postRequest(url: strUrl) {[weak self] json, code in
            guard let self = self else { return }
            //1. json字符串 转 对象
            if code == 200 {
                callBackBlock(true)
            }
        } failBlock: { err in
            callBackBlock(false)
        }
    }
    
    func login(callBackBlock: @escaping (_ success: Bool)->()) {
        let strUrl = YHBaseUrlManager.shared.curURL() + "/user/userlogin"
        let _ = YHNetRequest.postRequest(url: strUrl) {[weak self] json, code in
            //1. json字符串 转 对象
            guard let self = self else { return }
            guard let model = NetBaseModel.deserialize(dict: json) else {
                let error : YHErrorModel = YHErrorModel(errorCode:YHErrorCode.dictParseError.rawValue,errorMsg: YHErrorCode.dictParseError.description())
                
                callBackBlock(false)
                return
            }
//            printLog("model 是 ==> \(model)")
            let dic = model.data?.peel
            guard let resultModel = YHUserModel.deserialize(dict: dic as? [AnyHashable : Any]) else {
                return
            }
            self.userModel = resultModel
            callBackBlock(true)
        } failBlock: { err in
            callBackBlock(false)
        }
    }
}
