Common.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. //
  2. // Common.swift
  3. // o2app
  4. //
  5. // Created by 刘振兴 on 2017/8/18.
  6. // Copyright © 2017年 zone. All rights reserved.
  7. //
  8. import UIKit
  9. import SwiftValidator
  10. import SwiftyUserDefaults
  11. import CocoaLumberjack
  12. import CoreTelephony
  13. // 屏幕宽度
  14. let kScreenH = UIScreen.main.bounds.height
  15. // 屏幕高度
  16. let kScreenW = UIScreen.main.bounds.width
  17. public class DeviceUtil {
  18. static let shared: DeviceUtil = {
  19. return DeviceUtil()
  20. }()
  21. private init() {}
  22. //当前设备名称
  23. func getDeviceName() -> String {
  24. return UIDevice.current.name
  25. }
  26. //设备uuid
  27. func getDeviceUUId() -> String {
  28. return UIDevice.current.identifierForVendor?.uuidString ?? ""
  29. }
  30. //设备Model
  31. func getDeviceModel() -> String {
  32. return UIDevice.current.model
  33. }
  34. // 当前系统版本
  35. func getSystemVersion() -> String {
  36. return UIDevice.current.systemVersion
  37. }
  38. //运营商信息
  39. func getCarrier() -> String {
  40. let net = CTTelephonyNetworkInfo()
  41. return net.subscriberCellularProvider?.carrierName ?? ""
  42. }
  43. //获取当前网络情况
  44. // func getNetInfo(callback: @escaping (String)->Void ) {
  45. // let reachability = Reachability.init(hostName: "www.o2oa.net")
  46. // let status = reachability?.currentReachabilityStatus()
  47. // if status != nil {
  48. // switch status! {
  49. // case ReachableViaWiFi:
  50. // callback("wifi")
  51. // case ReachableViaWWAN:
  52. // callback(self.getNetType())
  53. // default:
  54. // callback("none")
  55. // }
  56. // }else {
  57. // callback("none")
  58. // }
  59. // }
  60. func getDeviceInfoForJsApi(callback: @escaping (O2UtilPhoneInfo)->Void ) {
  61. var info = O2UtilPhoneInfo()
  62. info.screenWidth = "\(kScreenW)"
  63. info.screenHeight = "\(kScreenH)"
  64. info.brand = self.getDeviceName()
  65. info.model = self.getDeviceModel()
  66. info.version = self.getSystemVersion()
  67. info.operatorType = self.getCarrier()
  68. // getNetInfo { (type) in
  69. // info.netInfo = type
  70. // callback(info)
  71. // }
  72. //TODO 网络情况
  73. callback(info)
  74. }
  75. private func getNetType() -> String {
  76. var netconnType = ""
  77. let info = CTTelephonyNetworkInfo()
  78. let status = info.currentRadioAccessTechnology
  79. if (status == "CTRadioAccessTechnologyGPRS") {
  80. netconnType = "GPRS"
  81. }else if (status == "CTRadioAccessTechnologyEdge" ) {
  82. netconnType = "2.75G EDGE";
  83. }else if (status == "CTRadioAccessTechnologyWCDMA"){
  84. netconnType = "3G"
  85. }else if (status == "CTRadioAccessTechnologyHSDPA"){
  86. netconnType = "3.5G HSDPA"
  87. }else if (status == "CTRadioAccessTechnologyHSUPA"){
  88. netconnType = "3.5G HSUPA"
  89. }else if (status == "CTRadioAccessTechnologyCDMA1x"){
  90. netconnType = "2G"
  91. }else if (status == "CTRadioAccessTechnologyCDMAEVDORev0"){
  92. netconnType = "3G"
  93. }else if (status == "CTRadioAccessTechnologyCDMAEVDORevA"){
  94. netconnType = "3G"
  95. }else if (status == "CTRadioAccessTechnologyCDMAEVDORevB"){
  96. netconnType = "3G"
  97. }else if (status == "CTRadioAccessTechnologyeHRPD"){
  98. netconnType = "HRPD"
  99. }else if (status == "CTRadioAccessTechnologyLTE"){
  100. netconnType = "4G"
  101. }
  102. return netconnType
  103. }
  104. }
  105. // MARK:- 自定义手机号码检校规则
  106. public class MobileNumberRule: RegexRule {
  107. static let regex = "^\\d{11}$"
  108. convenience init(message : String = "Not a valid Mobile Number"){
  109. self.init(regex: MobileNumberRule.regex, message : message)
  110. }
  111. }
  112. // MARK:- 所有配置定义,使用SwiftyUserDefaults实现存储
  113. protocol AppConfigEnable {
  114. //是否第一次使用应用,显示引导页使用
  115. var isFirstTime:Bool? { get set }
  116. // IM是否已经登录
  117. var openIMLoginStatus:Bool? { get set }
  118. // 跳转用的 从那个页面过来的
  119. var taskIndex: Int {get set}
  120. var appBackType: Int {get set}
  121. var notificationGranted: Bool {get set}
  122. var firstGranted: Bool {get set}
  123. // 生物识别登录用户
  124. var bioAuthUser: String {get set}
  125. var firstLoad: Bool {get set}
  126. //服务器 移动端配置hash值 判断是否更新了
  127. var customStyleHashLocal: String {get set}
  128. // 主题名称
  129. var themeName: String {get set}
  130. //demo服务器提示公告
  131. var demoAlertTag: Bool {get set}
  132. }
  133. // MARK:- 扩展定义的键
  134. extension DefaultsKeys {
  135. static let isFirstTime = DefaultsKey<Bool?>("isFirstTime")
  136. static let openIMLoginStatus = DefaultsKey<Bool?>("openIMLoginStatus")
  137. static let taskIndex = DefaultsKey<Int>("taskIndex")
  138. static let appBackType = DefaultsKey<Int>("appBackType")
  139. static let notificationGranted = DefaultsKey<Bool>("notificationGranted")
  140. static let firstGranted = DefaultsKey<Bool>("fristGranted")
  141. static let bioAuthUser = DefaultsKey<String>("bioAuthUser")
  142. static let firstLoad = DefaultsKey<Bool>("firstLoad")
  143. static let customStyleHashLocal = DefaultsKey<String>("customStyleHashLocal")
  144. static let themeName = DefaultsKey<String>("themeName")
  145. static let demoAlertTag = DefaultsKey<Bool>("demoAlertTag")
  146. }
  147. // MARK:- Default App Config
  148. public class AppConfigSettings: AppConfigEnable {
  149. var customStyleHashLocal: String {
  150. get {
  151. return Defaults[.customStyleHashLocal]
  152. }
  153. set {
  154. Defaults[.customStyleHashLocal] = newValue
  155. }
  156. }
  157. var themeName: String {
  158. get {
  159. return Defaults[.themeName]
  160. }
  161. set {
  162. Defaults[.themeName] = newValue
  163. }
  164. }
  165. var openIMLoginStatus: Bool?{
  166. get {
  167. guard let openIMLoginStatus = Defaults[.openIMLoginStatus] else {
  168. return false
  169. }
  170. return openIMLoginStatus
  171. }
  172. set {
  173. Defaults[.openIMLoginStatus] = newValue
  174. }
  175. }
  176. var isFirstTime: Bool? {
  177. get {
  178. guard let firstTime = Defaults[.isFirstTime] else {
  179. return true
  180. }
  181. return firstTime
  182. }
  183. set {
  184. Defaults[.isFirstTime] = newValue
  185. }
  186. }
  187. var notificationGranted: Bool {
  188. get {
  189. return Defaults[.notificationGranted]
  190. }
  191. set {
  192. Defaults[.notificationGranted] = newValue
  193. }
  194. }
  195. var firstGranted: Bool {
  196. get {
  197. return Defaults[.firstGranted]
  198. }
  199. set {
  200. Defaults[.firstGranted] = newValue
  201. }
  202. }
  203. var bioAuthUser: String {
  204. get {
  205. return Defaults[.bioAuthUser]
  206. }
  207. set {
  208. Defaults[.bioAuthUser] = newValue
  209. }
  210. }
  211. var firstLoad: Bool {
  212. get {
  213. return Defaults[.firstLoad]
  214. }
  215. set {
  216. Defaults[.firstLoad] = newValue
  217. }
  218. }
  219. var taskIndex: Int {
  220. get {
  221. return Defaults[.taskIndex]
  222. }
  223. set {
  224. Defaults[.taskIndex] = newValue
  225. }
  226. }
  227. var appBackType: Int {
  228. get {
  229. return Defaults[.appBackType]
  230. }
  231. set {
  232. Defaults[.appBackType] = newValue
  233. }
  234. }
  235. var demoAlertTag: Bool {
  236. get {
  237. return Defaults[.demoAlertTag]
  238. }
  239. set {
  240. Defaults[.demoAlertTag] = newValue
  241. }
  242. }
  243. public func removeAllConfig() {
  244. Defaults.removeAll()
  245. }
  246. static let shared:AppConfigSettings = {
  247. return AppConfigSettings()
  248. }()
  249. }
  250. // MARK:- OOCustomImageManager
  251. enum OOCustomImageKey:NSString {
  252. case launch_logo = "launch_logo"
  253. case login_avatar = "login_avatar"
  254. case index_bottom_menu_logo_blur = "index_bottom_menu_logo_blur"
  255. case index_bottom_menu_logo_focus = "index_bottom_menu_logo_focus"
  256. case people_avatar_default = "people_avatar_default"
  257. case process_default = "process_default"
  258. case setup_about_logo = "setup_about_logo"
  259. }
  260. class OOCustomImageManager {
  261. static let `default`:OOCustomImageManager = {
  262. return OOCustomImageManager()
  263. }()
  264. private var imageCache = NSCache<NSString,UIImage>()
  265. private init() {
  266. self.loadCache()
  267. }
  268. private func loadCache() {
  269. if let configInfo = O2AuthSDK.shared.customStyle() {
  270. configInfo.images?.forEach({ (ooImage) in
  271. let value = ooImage.value!
  272. let data = Data(base64Encoded: value)
  273. let image = UIImage(data: data!)
  274. let scaleImage = image?.scaledImageFrom3x()
  275. imageCache.setObject(scaleImage!, forKey: ooImage.name! as NSString)
  276. })
  277. }
  278. }
  279. func loadImage(_ key:OOCustomImageKey) -> UIImage? {
  280. if let image = imageCache.object(forKey: key.rawValue) {
  281. return image
  282. }else {
  283. self.loadCache()
  284. if let image = imageCache.object(forKey: key.rawValue) {
  285. return image
  286. }else {
  287. return O2ThemeManager.image(for: "Icon.icon_zhuye_pre")
  288. }
  289. }
  290. }
  291. //异步获取图片
  292. func loadImageAsync(key:OOCustomImageKey, block:@escaping (UIImage?)->Void) {
  293. let item = DispatchWorkItem {
  294. if let configInfo = O2AuthSDK.shared.customStyle() {
  295. configInfo.images?.forEach({ (ooImage) in
  296. let name = ooImage.name! as NSString
  297. if name == key.rawValue {
  298. DDLogDebug("name:\(name)")
  299. let value = ooImage.value!
  300. let data = Data(base64Encoded: value)
  301. let image = UIImage(data: data!)
  302. block(image)
  303. }
  304. })
  305. }
  306. }
  307. DispatchQueue.main.async(execute: item)
  308. }
  309. }