ConfigUtils.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. //
  2. // ResourceAllocationUtils.swift
  3. // MTP2_iOS
  4. //
  5. // Created by Handy_Cao on 2018/6/28.
  6. // Copyright © 2018年 Muchinfo. All rights reserved.
  7. //
  8. import Foundation
  9. enum ConfigType: String {
  10. case appVersion = "appVersion"
  11. case url = "url"
  12. }
  13. /// 通用配置工具类
  14. class ConfigUtils {
  15. static func getValue(key: ConfigType) -> Any? {
  16. if let path = Bundle.main.path(forResource: "Config", ofType: "plist"), let dic = NSDictionary(contentsOfFile: path) {
  17. return dic[key.rawValue]
  18. }
  19. return nil
  20. }
  21. static let appDelegate = UIApplication.shared.delegate as! AppDelegate
  22. /// 资源类型
  23. ///
  24. /// - mineFunctionModule: 我的模块功能项
  25. /// - functionModule: 大功能模块
  26. /// - payWay: 支付方式
  27. /// - tradeLink: 链路信息模块
  28. /// - supportOAuth: 是否支持三方登录
  29. /// - isShowTotleVolume: 是否展示成交量
  30. /// - SupportResetPwd 是否支持重置密码
  31. /// - SupportMobileLogin 是否支持手机登录
  32. /// - Search: 查询服务地址
  33. /// - UpdateUrl: App更新地址
  34. /// - RealAuth: 实名认证
  35. /// - OpenApiUrl: OpenApiUrl
  36. enum ResourceType: String {
  37. case mineFunctionModule = "mine's function module"
  38. case functionModule = "function module"
  39. case payWay = "pay way"
  40. case tradeLink = "trade link"
  41. case supportOAuth = "SupportOAuth"
  42. case SupportResetPwd = "SupportResetPwd"
  43. case SupportMobileLogin = "SupportMobileLogin"
  44. case UpdateUrl = "UpdateUrl"
  45. case BankUrl = "BankUrl"
  46. case AreaUrl = "areaUrl"
  47. }
  48. static func getValue(key: ResourceType) -> Any? {
  49. if let appVersion = ConfigUtils.getValue(key: .appVersion) as? String,
  50. let path = Bundle.main.path(forResource: appVersion, ofType: ".plist"),
  51. let dic = NSDictionary(contentsOfFile: path) {
  52. return dic[key.rawValue]
  53. }
  54. return nil
  55. }
  56. /// 获取对应服务的服务地址信息
  57. /// - Parameter type: type
  58. /// - Returns: String
  59. static func getServiceUrl(resourceType type: ResourceType) -> String? {
  60. /// 判断是否第一次运行
  61. guard let array = ConfigUtils.getValue(key: .tradeLink) as? Array<Dictionary<String, Any>> else {
  62. dPrint("【\(#function)】错误的配置文件")
  63. return ""
  64. }
  65. switch type {
  66. case .UpdateUrl:
  67. return (array[0]["updateUrl"] as? String) ?? ""
  68. case .BankUrl:
  69. return (array[0]["bankUrl"] as? String) ?? ""
  70. case .AreaUrl:
  71. return (array[0]["areaUrl"] as? String) ?? ""
  72. default:
  73. return ""
  74. }
  75. }
  76. }
  77. /// 错误码工具类
  78. class ErrorCodeUtils {
  79. static func getValue(key: String) -> Any? {
  80. if let path = Bundle.main.path(forResource: "ErrorCode", ofType: "plist"), let dic = NSDictionary(contentsOfFile: path) {
  81. return dic[key]
  82. }
  83. return nil
  84. }
  85. }
  86. /// 错误信息工具类
  87. class ErrorUtils {
  88. static func getDesc(errorCode: Int) -> String? {
  89. do {
  90. // 先尝试从数据库中获取
  91. let errorEntity = try DatabaseHelper.getError(errorCode: String(errorCode))
  92. if errorEntity != nil { return errorEntity!.remarks! }
  93. } catch {}
  94. // 再深度从自定义错误中获取
  95. if let errorMessage = ErrorCodeUtils.getValue(key: String(errorCode)) as? String { return errorMessage }
  96. return nil
  97. }
  98. }
  99. /// 合规性校验工具
  100. class RegularUtils {
  101. /// 密码校验
  102. ///
  103. /// - Parameter str: 待校验字符串
  104. /// - Returns: 是否验证通过
  105. static func checkAllCharactersPatten(check str: String) -> Bool {
  106. let lowercase = ".*[a-z]+.*"
  107. let captial = ".*[A-Z]+.*"
  108. let number = ".*[0-9]+.*"
  109. let punctuation = ".*[`~!@#$^&*()=|{}':;',\\[\\].<>/?~!@#¥……&*()——|{}【】‘;:”“'。,、?]+.*"
  110. let test1 = NSPredicate(format: "SELF MATCHES %@", lowercase)
  111. let test2 = NSPredicate(format: "SELF MATCHES %@", captial)
  112. let test3 = NSPredicate(format: "SELF MATCHES %@", number)
  113. let test4 = NSPredicate(format: "SELF MATCHES %@", punctuation)
  114. var regularNum = 0
  115. if test1.evaluate(with: str) {
  116. regularNum += 1
  117. }
  118. if test2.evaluate(with: str) {
  119. regularNum += 1
  120. }
  121. if test3.evaluate(with: str) {
  122. regularNum += 1
  123. }
  124. if test4.evaluate(with: str) {
  125. regularNum += 1
  126. }
  127. return regularNum >= 3
  128. }
  129. /// 检查电话号码
  130. ///
  131. /// - Parameter str: 待校验字符串
  132. /// - Returns: 是否验证通过
  133. static func checkPhoneNumber(with str: String) -> Bool {
  134. 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}$"
  135. let test = NSPredicate(format: "SELF MATCHES %@", regex)
  136. return test.evaluate(with: str)
  137. }
  138. /// 校验只包含数字
  139. ///
  140. /// - Parameter str: 待校验字符串
  141. /// - Returns: 是否验证通过
  142. static func checkNumber(with str: String) -> Bool {
  143. let regex = "\\d+"
  144. let test = NSPredicate(format: "SELF MATCHES %@", regex)
  145. return test.evaluate(with: str)
  146. }
  147. /// 校验证件号
  148. ///
  149. /// - Parameter str: 待检验字符串
  150. /// - Returns: 是否验证通过
  151. static func checkCertificate(with str: String) -> Bool {
  152. if str.count > 30 || str.contains("{") || str.contains("}") || str.contains("(") || str.contains(")") || str.contains("*") {
  153. return false
  154. } else {
  155. return true
  156. }
  157. }
  158. }
  159. /// 交易下单控制
  160. class TradeControlUtils {
  161. /// 检验认证状态
  162. ///
  163. /// - Returns: 是否认证
  164. static func checkAuthState(navigationController: UINavigationController?, isSign: Bool = false) -> Bool {
  165. if MTP2BusinessCore.shared.authStatus != 1 {
  166. /// 如果没有认证,跳转到认证界面
  167. if let auth = UIStoryboard(name: "Settings", bundle: nil).instantiateViewController(withIdentifier: "Auth") as? AuthAddressViewController {
  168. auth.url = (MTP2BusinessCore.shared.address?.mobileAuthUrl ?? "")+"/#/auth/information?userid="+"\(MTP2BusinessCore.shared.getUserID())"
  169. auth.title = "实名认证"
  170. navigationController?.pushViewController(auth, animated: true)
  171. }
  172. return false
  173. }
  174. return true
  175. }
  176. }
  177. /// 字符串工具
  178. class StringUtils {
  179. /// 签约状态
  180. static func ESignStatusStringValue(_ Value: ESignStatus? ) -> String {
  181. if let value = Value {
  182. switch value {
  183. /// 未签约
  184. case .SIGNSTATUS_UNSIGN:
  185. return "未签约"
  186. /// 签约待审核
  187. case .SIGNSTATUS_SIGNFORAUDIT:
  188. return "签约待审核"
  189. /// 签约中
  190. case .SIGNSTATUS_SIGNING:
  191. return "签约中"
  192. /// 已签约
  193. case .SIGNSTATUS_SIGNED:
  194. return "已签约"
  195. /// 解约待审核
  196. case .SIGNSTATUS_CANCELFORAUDIT:
  197. return "解约待审核"
  198. /// 解约中
  199. case .SIGNSTATUS_CANCELLING:
  200. return "解约中"
  201. /// 已解约
  202. case .SIGNSTATUS_CANCELLED:
  203. return "已解约"
  204. /// 已解绑
  205. case .SIGNSTATUS_BINDED:
  206. return "已解绑"
  207. /// 绑卡中
  208. case .SIGNSTATUS_BINDING:
  209. return "绑卡中"
  210. /// 审核拒绝
  211. case .SIGNSTATUS_REJECT:
  212. return "审核拒绝"
  213. }
  214. } else {
  215. return "--"
  216. }
  217. }
  218. /// 出入金申请类型
  219. static func ApplyTypeStringValue(_ Value: ApplyType? ) -> String {
  220. if let value = Value {
  221. switch value {
  222. case .APPLYTYPE_OUTMONEY:
  223. return "提现"
  224. case .APPLYTYPE_INMONEY:
  225. return "入金"
  226. case .APPLYTYPE_SINGLEINMONEY:
  227. return "单边账调整:入金"
  228. case .APPLYTYPE_SINGLEIOUTMONEY:
  229. return "单边账调整:提现"
  230. }
  231. } else {
  232. return "--"
  233. }
  234. }
  235. /// 申请状态
  236. static func ApplyStatusStringValue(_ Value: ApplyStatus? ) -> String {
  237. if let value = Value {
  238. switch value {
  239. case .APPLYSTATUS_PENDING:
  240. return "待审核"
  241. case .APPLYSTATUS_PASS:
  242. return "审核通过"
  243. case .APPLYSTATUS_REFUND:
  244. return "审核拒绝"
  245. case .APPLYSTATUS_TRADEFROZEN:
  246. return "交易" + NSLocalizedString("freemargin", comment: "冻结") + "中"
  247. case .APPLYSTATUS_TRADETHAW:
  248. return NSLocalizedString("tradingthaw", comment: "交易解冻中")
  249. case .APPLYSTATUS_TRADETHAWWITHHLODING:
  250. return NSLocalizedString("tradingfree", comment: "交易解冻扣款中")
  251. case .APPLYSTATUS_TRADEINMONEY:
  252. return "交易入金中"
  253. case .APPLYSTATUS_TRADEFROZENTHAWWITHHOLDING:
  254. return "交易" + NSLocalizedString("freemargin", comment: "冻结") + "/解冻/扣款中(银行发起出金时用)"
  255. case .APPLYSTATUS_BANKOUTMONRY:
  256. return "银行出金中"
  257. case .APPLYSTATUS_BANKINMONEY:
  258. return "银行入金中"
  259. case .APPLYSTATUS_SUCCESS:
  260. return "成功"
  261. case .APPLYSTATUS_FAILURE:
  262. return "失败"
  263. case .APPLYSTATUS_BANKAUDIT:
  264. return "银行审核中"
  265. case .APPLYSTATUS_ACCOUNTSERVICEINMONEYFAILURE:
  266. return "账户服务入金失败"
  267. case .APPLYSTATUS_ACCOUNTSERVICETHAWFAILURE:
  268. return "账户服务解冻失败"
  269. case .APPLYSTATUS_ACCOUNTSERVICEFROZENTHAWWITHHOLDINGFAILURE:
  270. return "账户服务解冻扣款失败"
  271. case .APPLYSTATUS_ACCOUNTSERVICEOUTMONEYFAILURE:
  272. return "账户服务出金失败"
  273. }
  274. } else {
  275. return "--"
  276. }
  277. }
  278. /// 获取资源下载Url
  279. /// - Parameter url: url
  280. /// - Returns: URL
  281. static func getImageUrl(_ url: String) -> URL? {
  282. guard let managerUrl = MTP2BusinessCore.shared.address?.openApiUrl,
  283. let urls = NSString(string: url).components(separatedBy: ",").first else { return nil }
  284. return URL(string: managerUrl+NSString(string: urls).replacingOccurrences(of: "./", with: "/"))
  285. }
  286. }
  287. /// 以后用这个方法输出log信息,可以在release下剔除log
  288. ///
  289. /// - Parameter item: 输出内容
  290. func dPrint(_ item: @autoclosure () -> Any) {
  291. #if DEBUG
  292. debugPrint(item())
  293. #endif
  294. }