// // ResourceAllocationUtils.swift // MTP2_iOS // // Created by Handy_Cao on 2018/6/28. // Copyright © 2018年 Muchinfo. All rights reserved. // import Foundation enum ConfigType: String { case appVersion = "appVersion" case url = "url" } /// 通用配置工具类 class ConfigUtils { static func getValue(key: ConfigType) -> Any? { if let path = Bundle.main.path(forResource: "Config", ofType: "plist"), let dic = NSDictionary(contentsOfFile: path) { return dic[key.rawValue] } return nil } static let appDelegate = UIApplication.shared.delegate as! AppDelegate /// 资源类型 /// /// - mineFunctionModule: 我的模块功能项 /// - functionModule: 大功能模块 /// - payWay: 支付方式 /// - tradeLink: 链路信息模块 /// - supportOAuth: 是否支持三方登录 /// - isShowTotleVolume: 是否展示成交量 /// - SupportResetPwd 是否支持重置密码 /// - SupportMobileLogin 是否支持手机登录 /// - Search: 查询服务地址 /// - UpdateUrl: App更新地址 /// - RealAuth: 实名认证 /// - OpenApiUrl: OpenApiUrl enum ResourceType: String { case mineFunctionModule = "mine's function module" case functionModule = "function module" case payWay = "pay way" case tradeLink = "trade link" case supportOAuth = "SupportOAuth" case SupportResetPwd = "SupportResetPwd" case SupportMobileLogin = "SupportMobileLogin" case UpdateUrl = "UpdateUrl" case BankUrl = "BankUrl" case AreaUrl = "areaUrl" } static func getValue(key: ResourceType) -> Any? { if let appVersion = ConfigUtils.getValue(key: .appVersion) as? String, let path = Bundle.main.path(forResource: appVersion, ofType: ".plist"), let dic = NSDictionary(contentsOfFile: path) { return dic[key.rawValue] } return nil } /// 获取对应服务的服务地址信息 /// - Parameter type: type /// - Returns: String static func getServiceUrl(resourceType type: ResourceType) -> String? { /// 判断是否第一次运行 guard let array = ConfigUtils.getValue(key: .tradeLink) as? Array> else { dPrint("【\(#function)】错误的配置文件") return "" } switch type { case .UpdateUrl: return (array[0]["updateUrl"] as? String) ?? "" case .BankUrl: return (array[0]["bankUrl"] as? String) ?? "" case .AreaUrl: return (array[0]["areaUrl"] as? String) ?? "" default: return "" } } } /// 错误码工具类 class ErrorCodeUtils { static func getValue(key: String) -> Any? { if let path = Bundle.main.path(forResource: "ErrorCode", ofType: "plist"), let dic = NSDictionary(contentsOfFile: path) { return dic[key] } return nil } } /// 错误信息工具类 class ErrorUtils { static func getDesc(errorCode: Int) -> String? { do { // 先尝试从数据库中获取 let errorEntity = try DatabaseHelper.getError(errorCode: String(errorCode)) if errorEntity != nil { return errorEntity!.remarks! } } catch {} // 再深度从自定义错误中获取 if let errorMessage = ErrorCodeUtils.getValue(key: String(errorCode)) as? String { return errorMessage } return nil } } /// 合规性校验工具 class RegularUtils { /// 密码校验 /// /// - Parameter str: 待校验字符串 /// - Returns: 是否验证通过 static func checkAllCharactersPatten(check str: String) -> Bool { let lowercase = ".*[a-z]+.*" let captial = ".*[A-Z]+.*" let number = ".*[0-9]+.*" let punctuation = ".*[`~!@#$^&*()=|{}':;',\\[\\].<>/?~!@#¥……&*()——|{}【】‘;:”“'。,、?]+.*" let test1 = NSPredicate(format: "SELF MATCHES %@", lowercase) let test2 = NSPredicate(format: "SELF MATCHES %@", captial) let test3 = NSPredicate(format: "SELF MATCHES %@", number) let test4 = NSPredicate(format: "SELF MATCHES %@", punctuation) var regularNum = 0 if test1.evaluate(with: str) { regularNum += 1 } if test2.evaluate(with: str) { regularNum += 1 } if test3.evaluate(with: str) { regularNum += 1 } if test4.evaluate(with: str) { regularNum += 1 } return regularNum >= 3 } /// 检查电话号码 /// /// - Parameter str: 待校验字符串 /// - Returns: 是否验证通过 static func checkPhoneNumber(with str: String) -> Bool { let regex = "^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|166|198|199|(147))\\d{8}$" let test = NSPredicate(format: "SELF MATCHES %@", regex) return test.evaluate(with: str) } /// 校验只包含数字 /// /// - Parameter str: 待校验字符串 /// - Returns: 是否验证通过 static func checkNumber(with str: String) -> Bool { let regex = "\\d+" let test = NSPredicate(format: "SELF MATCHES %@", regex) return test.evaluate(with: str) } /// 校验证件号 /// /// - Parameter str: 待检验字符串 /// - Returns: 是否验证通过 static func checkCertificate(with str: String) -> Bool { if str.count > 30 || str.contains("{") || str.contains("}") || str.contains("(") || str.contains(")") || str.contains("*") { return false } else { return true } } } /// 交易下单控制 class TradeControlUtils { /// 检验认证状态 /// /// - Returns: 是否认证 static func checkAuthState(navigationController: UINavigationController?, isSign: Bool = false) -> Bool { if MTP2BusinessCore.shared.authStatus != 1 { /// 如果没有认证,跳转到认证界面 if let auth = UIStoryboard(name: "Settings", bundle: nil).instantiateViewController(withIdentifier: "Auth") as? AuthAddressViewController { auth.url = (MTP2BusinessCore.shared.address?.mobileAuthUrl ?? "")+"/#/auth/information?userid="+"\(MTP2BusinessCore.shared.getUserID())" auth.title = "实名认证" navigationController?.pushViewController(auth, animated: true) } return false } return true } } /// 字符串工具 class StringUtils { /// 签约状态 static func ESignStatusStringValue(_ Value: ESignStatus? ) -> String { if let value = Value { switch value { /// 未签约 case .SIGNSTATUS_UNSIGN: return "未签约" /// 签约待审核 case .SIGNSTATUS_SIGNFORAUDIT: return "签约待审核" /// 签约中 case .SIGNSTATUS_SIGNING: return "签约中" /// 已签约 case .SIGNSTATUS_SIGNED: return "已签约" /// 解约待审核 case .SIGNSTATUS_CANCELFORAUDIT: return "解约待审核" /// 解约中 case .SIGNSTATUS_CANCELLING: return "解约中" /// 已解约 case .SIGNSTATUS_CANCELLED: return "已解约" /// 已解绑 case .SIGNSTATUS_BINDED: return "已解绑" /// 绑卡中 case .SIGNSTATUS_BINDING: return "绑卡中" /// 审核拒绝 case .SIGNSTATUS_REJECT: return "审核拒绝" } } else { return "--" } } /// 出入金申请类型 static func ApplyTypeStringValue(_ Value: ApplyType? ) -> String { if let value = Value { switch value { case .APPLYTYPE_OUTMONEY: return "提现" case .APPLYTYPE_INMONEY: return "入金" case .APPLYTYPE_SINGLEINMONEY: return "单边账调整:入金" case .APPLYTYPE_SINGLEIOUTMONEY: return "单边账调整:提现" } } else { return "--" } } /// 申请状态 static func ApplyStatusStringValue(_ Value: ApplyStatus? ) -> String { if let value = Value { switch value { case .APPLYSTATUS_PENDING: return "待审核" case .APPLYSTATUS_PASS: return "审核通过" case .APPLYSTATUS_REFUND: return "审核拒绝" case .APPLYSTATUS_TRADEFROZEN: return "交易" + NSLocalizedString("freemargin", comment: "冻结") + "中" case .APPLYSTATUS_TRADETHAW: return NSLocalizedString("tradingthaw", comment: "交易解冻中") case .APPLYSTATUS_TRADETHAWWITHHLODING: return NSLocalizedString("tradingfree", comment: "交易解冻扣款中") case .APPLYSTATUS_TRADEINMONEY: return "交易入金中" case .APPLYSTATUS_TRADEFROZENTHAWWITHHOLDING: return "交易" + NSLocalizedString("freemargin", comment: "冻结") + "/解冻/扣款中(银行发起出金时用)" case .APPLYSTATUS_BANKOUTMONRY: return "银行出金中" case .APPLYSTATUS_BANKINMONEY: return "银行入金中" case .APPLYSTATUS_SUCCESS: return "成功" case .APPLYSTATUS_FAILURE: return "失败" case .APPLYSTATUS_BANKAUDIT: return "银行审核中" case .APPLYSTATUS_ACCOUNTSERVICEINMONEYFAILURE: return "账户服务入金失败" case .APPLYSTATUS_ACCOUNTSERVICETHAWFAILURE: return "账户服务解冻失败" case .APPLYSTATUS_ACCOUNTSERVICEFROZENTHAWWITHHOLDINGFAILURE: return "账户服务解冻扣款失败" case .APPLYSTATUS_ACCOUNTSERVICEOUTMONEYFAILURE: return "账户服务出金失败" } } else { return "--" } } /// 获取资源下载Url /// - Parameter url: url /// - Returns: URL static func getImageUrl(_ url: String) -> URL? { guard let managerUrl = MTP2BusinessCore.shared.address?.openApiUrl, let urls = NSString(string: url).components(separatedBy: ",").first else { return nil } return URL(string: managerUrl+NSString(string: urls).replacingOccurrences(of: "./", with: "/")) } } /// 以后用这个方法输出log信息,可以在release下剔除log /// /// - Parameter item: 输出内容 func dPrint(_ item: @autoclosure () -> Any) { #if DEBUG debugPrint(item()) #endif }