宽窄优行-由【嘉易行】项目成品而来
younger_times
2023-07-05 0d8f5fc8a516bfd07e425909e4a4432600572ee7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
//
//  SocketManager.swift
//  TicketDriverProject
//
//  Created by alvin_y on 2018/8/6.
//  Copyright © 2018年 yang-wang. All rights reserved.
//
 
import UIKit
import RHSocketKit
import RxSwift
import RxCocoa
import HandyJSON
import MBProgressHUD
 
/// 正式分享
let share_url = "https://zhentonggongsi.com:8443"
 
/// 测试网分享
//let share_url = "39.108.148.228:8080"
/// 测试外网
//let Host = "39.108.148.228"
 
/// Sockt管理类
class YYSocketManager: NSObject {
 
    //仿OC写法
    static let instance: YYSocketManager = YYSocketManager()
    
    /// MBProgressHUD
    var hud : MBProgressHUD?
    
    /// DisposeBag
    private var timeDisposeBag = DisposeBag()
    
    class func shared() -> YYSocketManager {
        return instance
    }
    
    /// 订单信息
    let orderInfosubject = PublishSubject<SocketModel>()
    
    /// 司机取消订单【推送】用户
    let cancel = PublishSubject<SocketModel>()
    
    /// 司机位置
    let position = PublishSubject<SocketModel>()
    
    /// viewModel
    let viewModel = TravelServiceViewModel()
    
    /// 弹出框
    weak var popupView: YYPopupView?
    
    override init() {
        super.init()
        initNotificationCenter()
        bindRx()
    }
    
    
    func initNotificationCenter()  {
        NotificationCenter.default.addObserver(self, selector: #selector(detectSocketServiceState(notif:)), name: NSNotification.Name(rawValue: kNotificationSocketServiceState), object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(detectSocketPacketResponse(notif:)), name: NSNotification.Name(rawValue: kNotificationSocketPacketResponse), object: nil)
    }
    
    /// 开始
    func startSocket()  {
        if !RHSocketService.sharedInstance().isRunning{
            //方便多次观察,先停止之前的连接
            YYSocketManager.shared().stopSocket()
            let encoder = RHSocketVariableLengthEncoder()
            let decoder = RHSocketVariableLengthDecoder()
            encoder.countOfLengthByte = 4
            decoder.countOfLengthByte = 4
            RHSocketService.sharedInstance().encoder = encoder
            RHSocketService.sharedInstance().decoder = decoder
            let param = RHSocketConnectParam()
            param.host = SocketHost
            param.port = Int32(SocketPort)
            //心跳间隔
            param.heartbeatInterval = 5
            //设置短线后是否自动重连
            param.autoReconnect = true
            RHSocketService.sharedInstance().start(with: param)
        }
    }
    
    /// Socket是否在运行
    /// - Returns: BOOL
    func isRunning() -> Bool {
        return RHSocketService.sharedInstance().isRunning
    }
    
    /// 停止
    private func stopSocket()  {
        RHSocketService.sharedInstance().stop()
    }
    
    //MARK: - socket状态
    @objc func detectSocketServiceState(notif:NSNotification)  {
        if let state = notif.object {
            if state as? Bool == true{
                //链接成功
                Observable<Int>.interval(RxTimeInterval.seconds(5), scheduler: MainScheduler.instance)
                    .subscribe(onNext: { [unowned self](_) in
                        self.heartBeat()
                    }).disposed(by: timeDisposeBag)
                loggerswift.success(s: "socket链接成功")
            }else{
                //链接失败
                self.timeDisposeBag = DisposeBag()
                loggerswift.error(s: "socket链接失败")
            }
        }else{
            //链接失败
            self.timeDisposeBag = DisposeBag()
            loggerswift.error(s: "socket链接失败")
        }
    }
    
    //MARK: - 解析接收长连接信息
    func decodeResponse(notification:NSNotification) -> NSDictionary {
        if notification.object != nil {
            return (notification.object as! NSDictionary)
        }
        if let userInfo:Dictionary = notification.userInfo {
            let rsp:RHSocketPacketResponse = userInfo["RHSocketPacket"] as! RHSocketPacketResponse
//            let strEncode = CFStringConvertEncodingToNSStringEncoding(UInt32(CFStringEncodings.GB_18030_2000.rawValue))
 
            if let data:Data = rsp.object as? Data{
                let result = String.init(data: data, encoding: .utf8)
                let newData = result?.data(using: String.Encoding.utf8)
                guard let data = newData else{
                    return [:]
                }
                let dic = try? JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.allowFragments)
                if let dict:NSDictionary = dic as? NSDictionary{
                   
                    return dict
                }else{
                    return [:]
                }
 
            }else{
                return [:]
            }
 
        }else{
            return [:]
        }
    }
    
    //MARK: - socket接收信息
    @objc func detectSocketPacketResponse(notif:NSNotification)   {
        let dic = decodeResponse(notification: notif)
 
        let info = SocketModel.deserialize(from: dic)
 
        guard let model = info else {return}
        loggerswift.log(s: dic)
        if model.code == 200 && model.msg == "SUCCESS" {
            loggerswift.log(s: "长链接方式:\(model.method)")
            if model.method != "OK"{
//                loggerswift.log(s: "长链接已连接")
            }
            if model.method == "OK"{
            }
                // 用户取消订单【推送】司机
            else if (model.method == "ORDER_CANCLE"){
                cancel.onNext(model)
            }
                // 服务端下发订单状态格式
            else if (model.method == "ORDER_STATUS"){
                //订单推送
                orderInfosubject.onNext(model)
                if (model.data?.orderType == 4 || model.data?.orderType == 5) && model.data?.state == 6{
                    ObtainCouponView.giveAwayMerchantCoupon(orderId: model.data!.orderId, orderType: OrderType(rawValue: model.data!.orderType)!, vc: (LD_currentViewController() as! YYViewController))
                }
            }
                // 司机位置
            else if (model.method == "DRIVER_POSITION"){
                //订单推送
                position.onNext(model)
            }else if (model.method == "FERRY"){
                // 跨城
                orderInfosubject.onNext(model)
            }else if (model.method == "REASSIGN"){
                // 改派
                orderInfosubject.onNext(model)
            }
                
                // 附近暂无司机接单
            else if (model.method == "END_PUSH"){
                guard let orderId = model.data?.orderId,let orderType = model.data?.orderType else {return}
                viewModel.orderId.accept(orderId)
                viewModel.orderType.accept(OrderType(rawValue: orderType) ?? .taxi)
                if popupView == nil{
                    popupView = YYPopupView.instance(type: .double)
                    popupView?.label_title.text = "提示"
                    popupView?.label_content.text = "附近暂无司机接单,您是否继续等待?"
                    popupView?.button_cancel.setTitle("取消订单", for: .normal)
                    popupView?.button_submit.setTitle("继续等待", for: .normal)
                    popupView?.button_close.isHidden = false
                    popupView?.pressSubmitBlock = {[unowned self] in
                        self.viewModel.pushOrderTaxi()
                        self.popupView = nil
                    }
                    popupView?.pressCancelBlock = {[unowned self] in
                        self.viewModel.addCancle()
                        self.popupView = nil
                    }
                    app.window?.addSubview(popupView!)
                    popupView?.show()
                }
            }
                // 多端登录下线
            else if (model.method == "OFFLINE"){
                NotificationCenter.default.post(Notification.init(name: Notification.Name(rawValue: TokenInvalid)))
                if popupView == nil{
                    app.loginInfo.clear()
                    app.userInfo.clear()
                    popupView = YYPopupView.instance(type: .single)
                    popupView?.label_title.text = "提示"
                    popupView?.label_content.text = "您的账号已在其他地方登录,请重新登录"
                    popupView?.button_cancel.setTitle("取消", for: .normal)
                    popupView?.button_submit.setTitle("确定", for: .normal)
                    popupView?.button_close.isHidden = true
                    app.window?.addSubview(popupView!)
                    popupView?.show()
                }
            }
            
        }
        
    }
    
    
    //MARK: - Rx
    func bindRx() {
        /// 添加取消记录
        viewModel.addCancleSubject
            .subscribeOn(MainScheduler.instance)
            .subscribe(onNext: { (status) in
                switch status{
                case .loading:
                    self.show()
                    break
                case .success(_ ):
                    self.hide()
                    alert(text: "取消成功")
                    UIViewController.base()?.navigationController?.popToRootViewController(animated: true)
                    break
                case .error(let error):
                    self.hide()
                    alert(text: error.localizedDescription)
                    break
                }
            }).disposed(by: rx.disposeBag)
        
        /// 继续等待订单
        viewModel.pushOrderTaxiTaxiSubject
            .subscribeOn(MainScheduler.instance)
            .subscribe(onNext: { (status) in
                switch status{
                case .loading:
                    self.show()
                    break
                case .success(_ ):
                    self.hide()
                    break
                case .error(let error):
                    self.hide()
                    alert(text: error.localizedDescription)
                    break
                }
            }).disposed(by: rx.disposeBag)
    }
    
    func heartBeat()  {
        if app.loginInfo.id == 0 || app.loginInfo.token == "" {
            return
        }
        let dict = ["code": 200,
                    "msg": "SUCCESS",
                    "method": "PING",
                    "data": ["type": 1,"userId": app.loginInfo.id,"token": app.loginInfo.token]] as [String : Any]
        let jsonString = dict.dictionaryToJson()
        guard let string = jsonString else {return}
        let request = RHSocketPacketRequest()
        request.object = string.data(using: String.Encoding.utf8)
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: kNotificationSocketPacketRequest), object: request)
    }
    
    /*
    func uploadLocation()  {
        let dict:JSON = ["con":["id":app.loginInfo.id,"orderId":app.loginInfo.orderId,"lon":app.loginInfo.lon,"lat":app.loginInfo.lat],"method":"LOCATION","code":"\(0)","msg":"SUCCESS"]
        let jsonString = dict.rawString([.castNilToNSNull: true])
        guard let string = jsonString else {return}
        let request = RHSocketPacketRequest()
        request.object = string.data(using: String.Encoding.utf8)
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: kNotificationSocketPacketRequest), object: request)
    }
    */
    
    //MARK: - MBProgressHUD
       /// 不带文字
       func show()  {
           hud = build()
       }
       
       func hide()  {
           hud?.hide(animated: true)
       }
       
       private func build() -> MBProgressHUD {
           let hud = MBProgressHUD.showAdded(to: UIApplication.shared.keyWindow!, animated: true)
           hud.mode = .indeterminate
           hud.bezelView.style = .solidColor
           hud.bezelView.color = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 0.7)
           hud.customView?.frame = CGRect(x: 0, y: 0, width: 50, height: 50)
           return hud
       }
}
// MARK: - UIViewControllerTransitioningDelegate
extension YYSocketManager: UIViewControllerTransitioningDelegate{
    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return DismissAnimation()
    }
    
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return PresentAnimation()
    }
}