//
//  LoginViewController+weChat.swift
//  MobilePortal
//
//  Created by caiyang on 2020/7/28.
//  Copyright © 2020 yin chen. All rights reserved.
//

import UIKit

extension LoginViewController {
    
    /// 微信
    @objc func sendAuthRequest(){
//        WXApiManager.shared()?.delegate = self
//        WXApiManager.shared()?.sendAuthRequest(with: self, delegate: self)
//        httpViewModel.access_tokenModel = GetAccessTokenModel()
//        httpViewModel.userinfoModel = UserinfoModel()
//        httpViewModel.access_tokenModel?.successBlock = {[weak self] (value:Any) in
//            self?.getWeChatXinfoHttpPostCmd()
//        }
    }
/// WXAuthDelegate
    func wxAuthSucceed(_ code: String!) {
        AccountInfo.sharedInstance().weChat_code = code
        httpViewModel.getCodeWeChatAccess_token()
        
    }
    func wxAuthDenied() {
        
    }
    
    /// 通过微信ID获取用户
    func getWeChatXinfoHttpPostCmd(){
        showHintView()
        let cmd:HttpCommand = WeChatXinfoHttpPostCmd.init(version: PHttpVersion_v1)
        let block:httpBlock = {[weak self] (result:RequestResult!,useInfo:Any!)->() in
            if result != nil {
                self?.httpWeChatXinfoHttpResultResponse(result: result)
            }else{
                print("空的")
            }
        }
        let dic:NSMutableDictionary = NSMutableDictionary()
        dic[kHttpWeChatXinfoHttpPostCmd_wechatid] = judgmentStrNil(str: AccountInfo.sharedInstance().weChat_openid)
        dic[kHttpWeChatXinfoHttpPostCmd_ticket] = ""
        let completeDelegate = SMBaseHttpComplete.init(block: block , withUserInfo: nil)
        cmd.requestInfo = dic as? [AnyHashable : Any]
        cmd.completeDelegate=completeDelegate
        print("url==%@",cmd.getUrl())
        cmd.execute()
        
    }
    func httpWeChatXinfoHttpResultResponse(result:RequestResult){
        let r:WeChatXinfoHttpResult = result as! WeChatXinfoHttpResult
        if r.isOk() {
            let array:NSMutableArray = r.getWeChatXinfoHttpResult()
            if (array.count == 0) {
                self.getLoginDeviceInformationHttpCmd()
            } else {
                SMToastView.showMessage((array[0] as! String))
            }
        }else{
            SMToastView.showMessage("请查看当前网络状态！")
            self.hidenHintView()
        }
    }
}
