123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374 |
- //
- // Common.swift
- // o2app
- //
- // Created by 刘振兴 on 2017/8/18.
- // Copyright © 2017年 zone. All rights reserved.
- //
- import UIKit
- import SwiftValidator
- import SwiftyUserDefaults
- import CocoaLumberjack
- import CoreTelephony
- // 屏幕宽度
- let kScreenH = UIScreen.main.bounds.height
- // 屏幕高度
- let kScreenW = UIScreen.main.bounds.width
- public class DeviceUtil {
- static let shared: DeviceUtil = {
- return DeviceUtil()
- }()
- private init() {}
-
-
-
- //当前设备名称
- func getDeviceName() -> String {
- return UIDevice.current.name
- }
- //设备uuid
- func getDeviceUUId() -> String {
- return UIDevice.current.identifierForVendor?.uuidString ?? ""
- }
- //设备Model
- func getDeviceModel() -> String {
- return UIDevice.current.model
- }
- // 当前系统版本
- func getSystemVersion() -> String {
- return UIDevice.current.systemVersion
- }
- //运营商信息
- func getCarrier() -> String {
- let net = CTTelephonyNetworkInfo()
- return net.subscriberCellularProvider?.carrierName ?? ""
- }
- //获取当前网络情况
- // func getNetInfo(callback: @escaping (String)->Void ) {
- // let reachability = Reachability.init(hostName: "www.o2oa.net")
- // let status = reachability?.currentReachabilityStatus()
- // if status != nil {
- // switch status! {
- // case ReachableViaWiFi:
- // callback("wifi")
- // case ReachableViaWWAN:
- // callback(self.getNetType())
- // default:
- // callback("none")
- // }
- // }else {
- // callback("none")
- // }
- // }
-
- func getDeviceInfoForJsApi(callback: @escaping (O2UtilPhoneInfo)->Void ) {
- var info = O2UtilPhoneInfo()
- info.screenWidth = "\(kScreenW)"
- info.screenHeight = "\(kScreenH)"
- info.brand = self.getDeviceName()
- info.model = self.getDeviceModel()
- info.version = self.getSystemVersion()
- info.operatorType = self.getCarrier()
- // getNetInfo { (type) in
- // info.netInfo = type
- // callback(info)
- // }
- //TODO 网络情况
- callback(info)
- }
-
-
- private func getNetType() -> String {
- var netconnType = ""
- let info = CTTelephonyNetworkInfo()
- let status = info.currentRadioAccessTechnology
- if (status == "CTRadioAccessTechnologyGPRS") {
- netconnType = "GPRS"
- }else if (status == "CTRadioAccessTechnologyEdge" ) {
- netconnType = "2.75G EDGE";
- }else if (status == "CTRadioAccessTechnologyWCDMA"){
- netconnType = "3G"
- }else if (status == "CTRadioAccessTechnologyHSDPA"){
- netconnType = "3.5G HSDPA"
- }else if (status == "CTRadioAccessTechnologyHSUPA"){
- netconnType = "3.5G HSUPA"
- }else if (status == "CTRadioAccessTechnologyCDMA1x"){
- netconnType = "2G"
- }else if (status == "CTRadioAccessTechnologyCDMAEVDORev0"){
- netconnType = "3G"
- }else if (status == "CTRadioAccessTechnologyCDMAEVDORevA"){
- netconnType = "3G"
- }else if (status == "CTRadioAccessTechnologyCDMAEVDORevB"){
- netconnType = "3G"
- }else if (status == "CTRadioAccessTechnologyeHRPD"){
- netconnType = "HRPD"
- }else if (status == "CTRadioAccessTechnologyLTE"){
- netconnType = "4G"
- }
- return netconnType
- }
-
-
- }
- // MARK:- 自定义手机号码检校规则
- public class MobileNumberRule: RegexRule {
-
- static let regex = "^\\d{11}$"
-
- convenience init(message : String = "Not a valid Mobile Number"){
- self.init(regex: MobileNumberRule.regex, message : message)
- }
- }
- // MARK:- 所有配置定义,使用SwiftyUserDefaults实现存储
- protocol AppConfigEnable {
-
- //是否第一次使用应用,显示引导页使用
- var isFirstTime:Bool? { get set }
- // IM是否已经登录
- var openIMLoginStatus:Bool? { get set }
- // 跳转用的 从那个页面过来的
- var taskIndex: Int {get set}
- var appBackType: Int {get set}
- var notificationGranted: Bool {get set}
- var firstGranted: Bool {get set}
- // 生物识别登录用户
- var bioAuthUser: String {get set}
- var firstLoad: Bool {get set}
- //服务器 移动端配置hash值 判断是否更新了
- var customStyleHashLocal: String {get set}
- // 主题名称
- var themeName: String {get set}
- //demo服务器提示公告
- var demoAlertTag: Bool {get set}
- }
- // MARK:- 扩展定义的键
- extension DefaultsKeys {
- static let isFirstTime = DefaultsKey<Bool?>("isFirstTime")
- static let openIMLoginStatus = DefaultsKey<Bool?>("openIMLoginStatus")
- static let taskIndex = DefaultsKey<Int>("taskIndex")
- static let appBackType = DefaultsKey<Int>("appBackType")
- static let notificationGranted = DefaultsKey<Bool>("notificationGranted")
- static let firstGranted = DefaultsKey<Bool>("fristGranted")
-
- static let bioAuthUser = DefaultsKey<String>("bioAuthUser")
- static let firstLoad = DefaultsKey<Bool>("firstLoad")
- static let customStyleHashLocal = DefaultsKey<String>("customStyleHashLocal")
- static let themeName = DefaultsKey<String>("themeName")
- static let demoAlertTag = DefaultsKey<Bool>("demoAlertTag")
- }
- // MARK:- Default App Config
- public class AppConfigSettings: AppConfigEnable {
-
- var customStyleHashLocal: String {
- get {
- return Defaults[.customStyleHashLocal]
- }
- set {
- Defaults[.customStyleHashLocal] = newValue
- }
- }
-
- var themeName: String {
- get {
- return Defaults[.themeName]
- }
- set {
- Defaults[.themeName] = newValue
- }
- }
-
- var openIMLoginStatus: Bool?{
- get {
- guard let openIMLoginStatus = Defaults[.openIMLoginStatus] else {
- return false
- }
- return openIMLoginStatus
- }
- set {
- Defaults[.openIMLoginStatus] = newValue
- }
- }
-
- var isFirstTime: Bool? {
- get {
- guard let firstTime = Defaults[.isFirstTime] else {
- return true
- }
- return firstTime
- }
-
- set {
- Defaults[.isFirstTime] = newValue
- }
- }
-
- var notificationGranted: Bool {
- get {
- return Defaults[.notificationGranted]
- }
-
- set {
- Defaults[.notificationGranted] = newValue
- }
- }
- var firstGranted: Bool {
- get {
-
- return Defaults[.firstGranted]
- }
-
- set {
- Defaults[.firstGranted] = newValue
- }
- }
-
- var bioAuthUser: String {
- get {
- return Defaults[.bioAuthUser]
- }
- set {
- Defaults[.bioAuthUser] = newValue
- }
- }
-
- var firstLoad: Bool {
- get {
- return Defaults[.firstLoad]
- }
-
- set {
- Defaults[.firstLoad] = newValue
- }
- }
-
- var taskIndex: Int {
- get {
- return Defaults[.taskIndex]
- }
-
- set {
- Defaults[.taskIndex] = newValue
- }
- }
-
- var appBackType: Int {
- get {
- return Defaults[.appBackType]
- }
-
- set {
- Defaults[.appBackType] = newValue
- }
- }
-
- var demoAlertTag: Bool {
- get {
- return Defaults[.demoAlertTag]
- }
- set {
- Defaults[.demoAlertTag] = newValue
- }
- }
-
-
-
- public func removeAllConfig() {
- Defaults.removeAll()
- }
-
- static let shared:AppConfigSettings = {
- return AppConfigSettings()
- }()
- }
- // MARK:- OOCustomImageManager
- enum OOCustomImageKey:NSString {
- case launch_logo = "launch_logo"
- case login_avatar = "login_avatar"
- case index_bottom_menu_logo_blur = "index_bottom_menu_logo_blur"
- case index_bottom_menu_logo_focus = "index_bottom_menu_logo_focus"
- case people_avatar_default = "people_avatar_default"
- case process_default = "process_default"
- case setup_about_logo = "setup_about_logo"
- }
- class OOCustomImageManager {
-
- static let `default`:OOCustomImageManager = {
- return OOCustomImageManager()
- }()
-
- private var imageCache = NSCache<NSString,UIImage>()
-
- private init() {
- self.loadCache()
- }
-
- private func loadCache() {
- if let configInfo = O2AuthSDK.shared.customStyle() {
- configInfo.images?.forEach({ (ooImage) in
- let value = ooImage.value!
- let data = Data(base64Encoded: value)
- let image = UIImage(data: data!)
- let scaleImage = image?.scaledImageFrom3x()
- imageCache.setObject(scaleImage!, forKey: ooImage.name! as NSString)
- })
- }
- }
-
- func loadImage(_ key:OOCustomImageKey) -> UIImage? {
- if let image = imageCache.object(forKey: key.rawValue) {
- return image
- }else {
- self.loadCache()
- if let image = imageCache.object(forKey: key.rawValue) {
- return image
- }else {
- return O2ThemeManager.image(for: "Icon.icon_zhuye_pre")
- }
- }
- }
-
- //异步获取图片
- func loadImageAsync(key:OOCustomImageKey, block:@escaping (UIImage?)->Void) {
- let item = DispatchWorkItem {
- if let configInfo = O2AuthSDK.shared.customStyle() {
- configInfo.images?.forEach({ (ooImage) in
- let name = ooImage.name! as NSString
- if name == key.rawValue {
- DDLogDebug("name:\(name)")
- let value = ooImage.value!
- let data = Data(base64Encoded: value)
- let image = UIImage(data: data!)
- block(image)
- }
- })
- }
- }
- DispatchQueue.main.async(execute: item)
- }
-
-
-
-
-
- }
|