| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906 |
- //
- // QSLHomeController.swift
- // QuickSearchLocation
- //
- // Created by mac on 2024/4/10.
- //
- import UIKit
- import SwiftyJSON
- import MAMapKit
- import AMapFoundationKit
- import AMapLocationKit
- import YYText
- class QSLHomeController: QSLBaseController {
-
- lazy var viewModel: QSLHomeViewModel = {
- return QSLHomeViewModel()
- }()
-
- var personalModel: QSLUserModel = QSLUserModel()
-
- ///判断是不是第一次定位成功
- var isFirstLocatScuess : Bool = true
-
- var isFirstRequestLoc : Bool = false
-
- var isEnterSearchFriend : Bool = false
-
- // var friendList: [QSLUserModel] = [QSLUserModel]()
-
- override func viewDidLoad() {
-
- super.viewDidLoad()
-
- checkDialog()
-
- QSLTools.shared.requestShortcutItem()
- }
-
- func checkDialog(){
- if UserDefaults.standard.string(forKey: "FIRST_ENTER_LOCATION_HOMEPAGE") == nil{
- isFirstRequestLoc = true
- }
-
- dealWithMap();
- QSLTools.shared.isOpen = true
-
- if(QSLTools.shared.urlScheme.count > 0){
-
- if(isFirstRequestLoc){
- isEnterSearchFriend = true
- }
-
- QSLTools.shared.dealwithScheme(QSLTools.shared.urlScheme)
-
- return
- }
- //会员而且没有好友才弹出来
- if QSLBaseManager.shared.isVip() {
- QSLNetwork().request(.friendList(dict: [:])) { response in
- let list = response.mapArray(QSLUserModel.self, modelKey: "data>list")
- if(list.count == 0){
- self.showAddFriendView()
- }
- } fail: { code, error in
-
- }
- }else{
- let vc = QSLActivityVipVC()
- vc.modalPresentationStyle = .fullScreen
- self.present(vc, animated: false)
- vc.dismissHandler = {[weak self] isCancel in
-
- QSLGuideusersToCommentManager.commentShare.manageWhetherTriggerPopUpWindow(.nonMember){[weak self] in
- self?.showAddFriendView(hideBtnClosure: {[weak self] in
- DispatchQueue.main.async {
- self?.checkLocAgain();
- }
- })
- }
-
- }
- }
-
-
- //试用过期弹窗
- // QSLTrialExpireAlertView.alert(view: currentWindow, isOneBtn: true, oneBtnText: "继续支付", oneBtnClosure: { [weak self] in
- // QSLCountdownManager.shared.selectGood = yearGood
- // self?.unlockBtnAction()
- // QSEventHandle.eventPush(eventName: QSLGravityConst.new_vip_retention_click, eventProps: ["button":"continue_payment"])
- // },closeBtnClosure: {
- // QSEventHandle.eventPush(eventName: QSLGravityConst.new_vip_retention_click, eventProps: ["button":"close"])
- // })
- }
-
- func checkLocAgain(){
- homeMapLocationM.startUpdatingLocation()
-
- homeMapView.showsUserLocation = true
- homeMapView.userTrackingMode = .follow
-
- self.onceLocation()
- firstEnterLocationHomepage()
- }
-
- override func viewWillAppear(_ animated: Bool) {
- super.viewWillAppear(animated)
- if(self.isFirstRequestLoc && self.isEnterSearchFriend){
- self.isEnterSearchFriend = false
- self.checkLocAgain();
- }
- }
-
- func showAddFriendView(hideBtnClosure: @escaping () -> () = {}){
- if let currentWindow = UIApplication.keyWindow {
- QSLHomeAddFriendView.alert(view: currentWindow, clickBtnClosure: {[weak self] in
- QSLJumpManager.shared.pushToAdd(type: .homealert)
- self?.isEnterSearchFriend = true
- },hideBtnClosure: {
- hideBtnClosure()
- })
- }
- }
-
- func dealWithMap(){
- viewModel.initUIData()
- setUpMap()
- setUpUI()
-
- loadData()
-
- NotificationCenter.default.addObserver(self, selector: #selector(loadData), name: QSLNotification.QSLLogin, object: nil)
- NotificationCenter.default.addObserver(self, selector: #selector(loadData), name: QSLNotification.QSLLogout, object: nil)
- NotificationCenter.default.addObserver(self, selector: #selector(loadData), name: QSLNotification.QSLRefreshFriend, object: nil)
- NotificationCenter.default.addObserver(self, selector: #selector(loadData), name: QSLNotification.QSLRefreshRequest, object: nil)
- NotificationCenter.default.addObserver(self, selector: #selector(requestVip), name: QSLNotification.QSLRefreshMember, object: nil)
-
- //处理第一次进入首页定位
- if(isFirstRequestLoc){
- return
- }
-
- firstEnterLocationHomepage()
- }
-
- //处理第一次进入首页定位
- func firstEnterLocationHomepage() {
- guard UserDefaults.standard.string(forKey: "FIRST_ENTER_LOCATION_HOMEPAGE") != nil else {
- UserDefaults.standard.set("YES", forKey: "FIRST_ENTER_LOCATION_HOMEPAGE")
- UserDefaults.standard.synchronize()
- ///第一次进入埋点
- QSEventHandle.gravityPush(eventName: QSLGravityConst.location_enter_first_homepage, eventProps: ["id": 03005])
- return
- }
- }
-
- /// 高德地图
- lazy var homeMapView = {
-
- let _mapView = MAMapView()
- _mapView.delegate = self
-
- _mapView.minZoomLevel = 7;
- _mapView.maxZoomLevel = 19;
- _mapView.zoomLevel = 17;
-
- _mapView.showsCompass = false
- _mapView.showsScale = false
- _mapView.isRotateCameraEnabled = false
-
- if(!isFirstRequestLoc){
- _mapView.showsUserLocation = true
- _mapView.userTrackingMode = .follow
- }
- // 关闭显示精度圈
- let represent = MAUserLocationRepresentation()
- represent.showsAccuracyRing = false
- _mapView.update(represent)
-
- let tap = UITapGestureRecognizer(target: self, action: #selector(mapTapAction))
- _mapView.addGestureRecognizer(tap)
-
- return _mapView
- }()
-
- /// 高德地图定位
- lazy var homeMapLocationM = {
-
- let _homeMapLocationM = AMapLocationManager()
- _homeMapLocationM.delegate = self
-
- _homeMapLocationM.distanceFilter = 100
- _homeMapLocationM.locatingWithReGeocode = true
-
- _homeMapLocationM.desiredAccuracy = kCLLocationAccuracyHundredMeters
- _homeMapLocationM.locationTimeout = 2
- _homeMapLocationM.reGeocodeTimeout = 2
-
- _homeMapLocationM.pausesLocationUpdatesAutomatically = false
- _homeMapLocationM.allowsBackgroundLocationUpdates = true
-
- return _homeMapLocationM
- }()
- /// 头部定位权限弹窗
- lazy var homeAuthHeaderView = {
-
- let _homeAuthHeaderView = QSLHomeAuthHeaderView()
- _homeAuthHeaderView.addRadius(radius: 6)
- return _homeAuthHeaderView
- }()
-
- @objc func homeFriendBtnAction(){
- QSLJumpManager.shared.pushToAdd(type: .homesmall)
- }
-
- lazy var homeFriendBtn: UIView = {
- let _homeResortBtn = UIView(frame: CGRect(x: QSLConst.qsl_kScreenW - 40.rpx, y: QSLConst.qsl_kStatusBarFrameH + 28.rpx, width: 34.rpx, height: 44.rpx))
-
- let imgView = UIImageView.init(image: UIImage(named: "home_find_friend"))
- imgView.frame = CGRectMake(0, 0, 34.rpx, 40.rpx)
- imgView.isUserInteractionEnabled = true
- _homeResortBtn.addSubview(imgView)
-
- let label = YYLabel.init(frame: CGRectMake(0, 29.rpx, 34.rpx, 15.rpx))
- // 创建文字属性
- let attributedString = NSMutableAttributedString(string: QSLConfig.addFriendIconText)
- attributedString.yy_font = UIFont.boldSystemFont(ofSize: 13)
- attributedString.yy_color = UIColor.black // 文字颜色为黑色
- // 创建描边阴影(模拟边框效果)
- let shadow = YYTextShadow()
- shadow.color = UIColor.white // 边框颜色
- shadow.radius = 4 // 边框粗细
- shadow.offset = CGSize.zero // 偏移量为0,形成描边效果
- // 设置文字描边
- attributedString.yy_setTextShadow(shadow, range: NSRange(location: 0, length: attributedString.length))
- label.attributedText = attributedString
- label.textAlignment = .center
- _homeResortBtn.addSubview(label)
-
- let tapG = UITapGestureRecognizer.init(target: self, action: #selector(homeFriendBtnAction))
- _homeResortBtn.addGestureRecognizer(tapG)
-
- return _homeResortBtn
- }()
-
- /// 定位按钮
- lazy var homeLocateBtnView = {
-
- let _homeLocateBtnView = UIView()
- _homeLocateBtnView.addRadius(radius: 6)
- _homeLocateBtnView.effectViewWithAlpha(alpha: 1, size: CGSize(width: 40, height: 40), style: .light)
- return _homeLocateBtnView
- }()
-
- lazy var homeLocateBtn = {
-
- let _homeLocateBtn = UIButton(type: .custom)
- _homeLocateBtn.image(UIImage(named: "home_btn_locate"))
- return _homeLocateBtn
- }()
-
- /// 无好友和未登录时的状态
- lazy var homeEmptyView: QSLHomeEmptyView = {
-
- let _homeEmptyView = QSLHomeEmptyView()
- _homeEmptyView.delegate = self
- _homeEmptyView.isHidden = true
- return _homeEmptyView
- }()
-
- lazy var homeFriendView: QSLHomeFriendView = {
-
- let topHeight = QSLConst.qsl_kStatusBarFrameH
- let view = QSLHomeFriendView(frame: CGRect(x: 0, y: QSLConst.qsl_kScreenH - viewModel.friendViewHeight - QSLConst.qsl_kTabbarFrameH, width: QSLConst.qsl_kScreenW, height: QSLConst.qsl_kScreenH - topHeight))
- view.scrollTopHeight = topHeight - 40
- view.delegate = self
- return view
- }()
- }
- // MARK: - 网络请求等
- extension QSLHomeController {
-
- @objc func loadData() {
- // viewModel.refreshDataSoure(complete: { [unowned self] in
- // self.homeFriendView.viewModel = self.viewModel
- // let friViewTopY = QSLConst.qsl_kScreenH - viewModel.friendViewHeight - QSLConst.qsl_kTabbarFrameH
- // homeFriendView.scrollCenterHeight = friViewTopY
- // homeFriendView.snp.updateConstraints { make in
- // make.top.equalTo(friViewTopY)
- // }
- // })
- if(!isFirstRequestLoc){
- self.onceLocation()
- }
-
- if QSLBaseManager.shared.isLogin() {
- self.initSocket()
- } else {
- self.deposeSocket()
- }
-
- requestFriendList()
- requestVip()
- }
-
- // 请求好友列表
- @objc func requestFriendList() {
-
- self.homeMapView.removeAnnotations(self.homeMapView.annotations)
-
- viewModel.friendList.removeAll()
- viewModel.friendLocationList.removeAll()
- viewModel.friendList.append(QSLBaseManager.shared.userModel)
-
- QSLNetwork().request(.friendList(dict: [:])) { response in
- let list = response.mapArray(QSLUserModel.self, modelKey: "data>list")
- self.viewModel.friendList.append(contentsOf: list)
- self.homeFriendView.viewModel = self.viewModel
-
- for model in list {
- let pointAnnotation = MAPointAnnotation()
- let loc = CLLocationCoordinate2D(latitude: model.location.lat, longitude: model.location.lng)
- pointAnnotation.coordinate = loc
-
- if model.remark.count > 0 {
- pointAnnotation.title = model.remark
- } else {
- pointAnnotation.title = model.phone
- }
- pointAnnotation.subtitle = "300"
-
- let pointModel = QSLMapPointModel()
- pointModel.userId = model.userId
- pointModel.pointAnnotation = pointAnnotation
- self.viewModel.friendLocationList.append(pointModel)
-
- if QSLBaseManager.shared.isVip() && !model.blockedMe {
- self.homeMapView.addAnnotation(pointAnnotation)
- }
- }
- } fail: { code, error in
-
- self.homeFriendView.viewModel = self.viewModel
- }
- }
-
- // 请求vip信息
- @objc func requestVip() {
-
- QSLNetwork().request(.userMember(dict: [:])) { response in
-
- let memberModel = response.mapObject(QSLMemberModel.self, modelKey: "data")
- QSLBaseManager.shared.userModel.memberModel = memberModel
-
- if memberModel.expired {
- QSLBaseManager.shared.saveVipExpiredTime(time: 0)
- } else {
- QSLBaseManager.shared.saveVipExpiredTime(time: memberModel.endTimestamp)
- }
-
- QSLBaseManager.shared.saveUserId(id: memberModel.userId)
-
- QSLTools.shared.requestShortcutItem()
- // NotificationCenter.default.post(name: QSLNotification.QSLRefreshMember, object: nil)
-
- } fail: { code, error in
-
- }
- }
- }
- // MARK: - 设置地图相关方法
- extension QSLHomeController: MAMapViewDelegate, AMapLocationManagerDelegate {
-
- // 初始化高德地图设置
- func setUpMap() {
-
- AMapServices.shared().enableHTTPS = true
- AMapLocationManager.updatePrivacyShow(.didShow, privacyInfo: .didContain)
- AMapLocationManager.updatePrivacyAgree(.didAgree)
-
-
- homeMapLocationM.locatingWithReGeocode = true
- if(!isFirstRequestLoc){
- homeMapLocationM.startUpdatingLocation()
- }
- }
-
- // 申请地图定位权限
- func mapViewRequireLocationAuth(_ locationManager: CLLocationManager!) {
-
- locationManager.requestWhenInUseAuthorization()
- }
-
- // 地图触摸事件
- @objc func mapTapAction() {
-
- if self.homeFriendView.scrollLocation != .ScrollBottom {
- self.homeFriendView.scrollToLocation(type: .ScrollBottom)
- }
- }
-
- // 获取一次性定位
- func onceLocation() {
-
- // 先停止持续定位
- self.homeMapLocationM.stopUpdatingLocation()
-
- self.homeMapLocationM.requestLocation(withReGeocode: true) { location, regeocode, error in
-
- if error != nil {
-
- self.personalModel = QSLBaseManager.shared.userModel
- // 获取电话和名字
- // self.personalModel.phone = QSLBaseManager.shared.userModel.phone
- var name = "用户\(QSLBaseManager.shared.userModel.phone.suffix(4))"
- if !QSLBaseManager.shared.isLogin() {
- name = "自己"
- }
- name = "自己"
- self.personalModel.remark = name
- // 地址
- self.personalModel.location.addr = "请先开启位置权限"
- QSLBaseManager.shared.updateUser(model: self.personalModel)
- if self.viewModel.friendList.isEmpty {
- self.viewModel.friendList.append(self.personalModel)
- } else {
- self.viewModel.friendList[0] = self.personalModel
- }
- self.homeFriendView.viewModel = self.viewModel
- self.homeMapLocationM.startUpdatingLocation()
-
- return
- }
-
-
- self.personalModel = QSLBaseManager.shared.userModel
- var name = "用户\(QSLBaseManager.shared.userModel.phone.suffix(4))"
- if !QSLBaseManager.shared.isLogin() {
- name = "自己"
- }
- name = "自己"
- self.personalModel.remark = name
-
- self.personalModel.location.addr = regeocode?.formattedAddress ?? "未知"
-
- self.personalModel.location.timestamp = Int(Date.secondStamp) ?? 0
-
- if let location = location {
- // 地图居中
- self.homeMapView.setCenter(location.coordinate, animated: true)
- self.homeFriendView.scrollLocation = .ScrollCenter
-
- self.personalModel.location.lat = location.coordinate.latitude
- self.personalModel.location.lng = location.coordinate.longitude
-
- // 速度
- self.personalModel.location.speed = location.speed >= 0 ? location.speed : 0
-
- // 朝向
- self.personalModel.location.bearing = location.course >= 0 ? location.course : 0
- }
-
- QSLBaseManager.shared.updateUser(model: self.personalModel)
-
-
- if QSLBaseManager.shared.isLogin() {
- let locationData = self.personalModel.location
- let addr = locationData.addr
- if addr != "请先开启位置权限" {
- self.sendSocket()
- }
- }
-
- if self.viewModel.friendList.isEmpty {
- self.viewModel.friendList.append(self.personalModel)
- } else {
- self.viewModel.friendList[0] = self.personalModel
- }
-
- print("location: \(String(describing: location))")
- if let regeocode = regeocode {
- print("reGeocode: \(regeocode)")
- }
-
- self.homeFriendView.viewModel = self.viewModel
- self.homeMapLocationM.startUpdatingLocation()
- }
- }
-
- // 持续定位
- func amapLocationManager(_ manager: AMapLocationManager!, didUpdate location: CLLocation!, reGeocode: AMapLocationReGeocode!) {
-
- if self.isFirstLocatScuess {
- self.isFirstLocatScuess = false
- //始终允许定位成功
- QSEventHandle.gravityPush(eventName: QSLGravityConst.location_success_always, eventProps: ["id": 03003])
- }
-
- // 获取电话和名字
- self.personalModel = QSLBaseManager.shared.userModel
-
- var name = "用户\(QSLBaseManager.shared.userModel.phone.suffix(4))"
- if !QSLBaseManager.shared.isLogin() {
- name = "自己"
- }
- name = "自己"
- self.personalModel.remark = name
- // 时间戳
- self.personalModel.location.timestamp = Int(Date.secondStamp) ?? 0
- // 经纬度
- self.personalModel.location.lat = location.coordinate.latitude
- self.personalModel.location.lng = location.coordinate.longitude
- // 速度
- if location.speed >= 0 {
- self.personalModel.location.speed = location.speed
- } else {
- self.personalModel.location.speed = 0
- }
- // 朝向
- if location.course >= 0 {
- self.personalModel.location.bearing = location.course
- } else {
- self.personalModel.location.bearing = 0
- }
- // 打印位置信息
- print("location: {lat: \(location.coordinate.latitude); lon: \(location.coordinate.longitude); accuracy: \(location.horizontalAccuracy)}")
- // 逆地理编码处理
- if let reGeocode = reGeocode {
- print("reGeocode: \(reGeocode)")
- self.personalModel.location.addr = reGeocode.formattedAddress
- } else {
- self.personalModel.location.addr = "请先开启位置权限"
- }
- // 保存到模块
- QSLBaseManager.shared.updateUser(model: self.personalModel)
- // 更新好友列表
- if self.viewModel.friendList.isEmpty {
- self.viewModel.friendList.append(self.personalModel)
- } else {
- self.viewModel.friendList[0] = self.personalModel
- }
- self.homeFriendView.viewModel = self.viewModel
- // 更新UI
- // self.updateHomeUI()
- // 如果已登录,发送定位信息
- if QSLBaseManager.shared.isLogin() {
- let locationData = self.personalModel.location
- let addr = locationData.addr
- if addr != "请先开启位置权限" {
- self.sendSocket()
- }
- }
- }
-
- func mapView(_ mapView: MAMapView!, viewFor annotation: (any MAAnnotation)!) -> MAAnnotationView! {
-
- // 处理用户位置的标注
- if annotation is MAUserLocation {
- let reuseIdentifier = "UserAnotation"
-
- var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseIdentifier) as? QSLHomeAnnotatinView
- if annotationView == nil {
- annotationView = QSLHomeAnnotatinView(annotation: annotation, reuseIdentifier: reuseIdentifier)
- }
-
- let name = "我"
- annotationView?.title = name
-
- annotationView?.isEnabled = false
- annotationView?.image = UIImage(named: "home_mine_anno")
- annotationView?.canShowCallout = false
- annotationView?.centerOffset = CGPoint(x: 0, y: -18)
- annotationView?.calloutOffset = CGPoint(x: 0, y: 0)
- annotationView?.zIndex = 360
- annotationView?.isSelected = true
-
- return annotationView
- }
-
- // 处理普通点标注
- if annotation is MAPointAnnotation {
- let reuseIdentifier = "OtherAnnotation"
-
- var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseIdentifier) as? QSLHomeAnnotatinView
- if annotationView == nil {
- annotationView = QSLHomeAnnotatinView(annotation: annotation, reuseIdentifier: reuseIdentifier)
- annotationView?.title = annotation.title
- }
-
- if let subtitle = annotation.subtitle as? Int {
- annotationView?.zIndex = subtitle
- }
-
- annotationView?.isEnabled = false
- annotationView?.image = UIImage(named: "home_mine_anno")
- annotationView?.canShowCallout = false
- annotationView?.centerOffset = CGPoint(x: 0, y: -18)
- annotationView?.calloutOffset = CGPoint(x: 0, y: 0)
- annotationView?.isSelected = true
-
- return annotationView
- }
-
- return nil
- }
- //@brief 定位权限状态改变时回调函数。注意:iOS13及之前版本回调
- // 高德地图权限变化回调
- func amapLocationManager(_ manager: AMapLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
- //print("高德地图定位权限变化: \(status.rawValue)")
-
- switch status {
- case .authorizedAlways, .authorizedWhenInUse:
- //print("定位权限已授权")
- //支付成功埋点
- QSEventHandle.gravityPush(eventName: QSLGravityConst.location_allow_permission, eventProps: ["id": 03001])
- case .denied:
- //print("定位权限被拒绝")
- //拒绝定位权限
- QSEventHandle.gravityPush(eventName: QSLGravityConst.location_allow_permission, eventProps: ["id": 03002])
- default:
- break
- }
- }
-
- // 系统定位权限变化回调
- func amapLocationManager(_ manager: AMapLocationManager, locationManagerDidChangeAuthorization locationManager: CLLocationManager) {
- if #available(iOS 14.0, *) {
- let status = locationManager.authorizationStatus
- // 统一处理权限变化
- amapLocationManager(manager, didChangeAuthorization: status)
-
- if(status != .notDetermined){
- QSLTools.shared.requestNotificationPermission()
- }
- //print("系统定位权限变化: \(status.rawValue)")
- } else {
- // Fallback on earlier versions
- }
-
-
- }
-
- // 定位结果回调
- func amapLocationManager(_ manager: AMapLocationManager, didFailWithError error: Error) {
- print("定位失败: \(error.localizedDescription)")
- QSEventHandle.gravityPush(eventName: QSLGravityConst.location_fail_always, eventProps: ["id": 03004])
- }
-
- }
- // MARK: - 设置WebSocket
- extension QSLHomeController: QSLSocketManagerDelegate {
-
- // 初始化
- func initSocket() {
-
- QSLSocketManager.shared.urlString = "\(QSLApi.prodWSUrl)/websocket/\(QSLBaseManager.shared.userModel.authToken)"
- QSLSocketManager.shared.connect()
- QSLSocketManager.shared.delegate = self
- }
-
- // 关闭socket
- func deposeSocket() {
-
- QSLSocketManager.shared.urlString = ""
- QSLSocketManager.shared.close()
- QSLSocketManager.shared.delegate = nil
- }
-
- // 发送socket信息
- func sendSocket() {
-
- var message = QSLMapMessageModel()
- message.cmd = "u.location"
-
- var location = QSLMapTrackModel()
- location.lat = self.personalModel.location.lat
- location.lng = self.personalModel.location.lng
- location.addr = self.personalModel.location.addr
- location.speed = self.personalModel.location.speed
- location.bearing = self.personalModel.location.bearing
- location.timestamp = Int(Date.milliStamp) ?? 0
-
- message.body = location.toJSONString()
- QSLSocketManager.shared.sendMessage(message.toJSONString())
- }
-
- // 接收到消息
- func socketDidReceiveMessage(with string: String) {
-
- let locationModel = QSLMapMessageModel.mapModel(from: string)
- if let data = locationModel.body.data(using: .utf8), let list = try? JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [QSLMapTrackModel] {
-
-
- if locationModel.cmd == "d.location.batch" {
-
- for model in list {
-
- var isBlockedMe = false
- for var user in viewModel.friendList {
- if model.userId == user.friendId {
- user.location = model
- isBlockedMe = user.blockedMe
- }
- }
-
- if !isBlockedMe && QSLBaseManager.shared.isVip() {
-
- for location in viewModel.friendLocationList {
-
- if model.userId == location.userId {
-
- let annotation = MAPointAnnotation()
- let cll2d = CLLocationCoordinate2D(latitude: model.lat, longitude: model.lng)
- annotation.coordinate = cll2d
- location.pointAnnotation = annotation
- self.homeMapView.addAnnotation(location.pointAnnotation)
- }
- }
- }
- }
-
- self.homeFriendView.viewModel = self.viewModel
- }
- }
- }
- }
- // MARK: - 设置界面代理方法
- extension QSLHomeController: QSLHomeEmptyViewDelegate, QSLHomeFriendViewDelegate {
-
- func viewDidScroll() {
- switch self.homeFriendView.scrollLocation {
- case .ScrollTop:
- self.homeFriendView.snp.updateConstraints { make in
- make.top.equalTo(QSLConst.qsl_kStatusBarFrameH - 40.rpx)
- }
- break
- case .ScrollCenter:
- self.homeFriendView.snp.updateConstraints { make in
- make.top.equalTo(QSLConst.qsl_kScreenH - viewModel.friendViewHeight - QSLConst.qsl_kStatusBarFrameH)
- }
- break
- case .ScrollBottom:
- self.homeFriendView.snp.updateConstraints { make in
- if(QSLBaseManager.shared.isVip()){
- make.top.equalTo(QSLConst.qsl_kScreenH - QSLConst.qsl_kTabbarFrameH - 56.rpx - 40.rpx)
- }else{
- make.top.equalTo(QSLConst.qsl_kScreenH - QSLConst.qsl_kTabbarFrameH - 56.rpx - 40.rpx - 48.rpx)
- }
-
- }
- break
- default:
- break
- }
- }
-
- func addFriendAction(isSmall: Bool) {
-
- if isSmall {
- QSLJumpManager.shared.pushToAdd(type: .homesmall)
- } else {
- QSLJumpManager.shared.pushToAdd(type: .homebig)
- }
- }
-
- func refreshBtnAction() {
-
- QSEventHandle.eventPush(eventName: QSLGravityConst.home_locate)
-
- self.loadData()
- }
-
- // 轨迹
- func routeBtnAction(model: QSLUserModel) {
- if(!QSLConfig.loginPayEnable){
- if !QSLBaseManager.shared.isVip() {
-
- QSLJumpManager.shared.pushToVip(type: .homeRoad)
- return
- }
- }else{
- if !QSLBaseManager.shared.isLogin() {
-
- if let view = self.tabBarController?.view {
- QSLAlertView.alert(view: view, title: "温馨提示", content: "登录即可体验查看轨迹记录", secondBtnClosure: {
-
- QSLJumpManager.shared.pushToLogin(type: .road)
- })
- }
-
- return
- }
-
- if !QSLBaseManager.shared.isVip() {
-
- QSLJumpManager.shared.pushToVip(type: .homeRoad)
- return
- }
- }
- QSLJumpManager.shared.pushToRoad(type: .home, model: model)
- }
-
- // 点击列表的定位按钮
- func locateBtnAction(model: QSLUserModel) {
-
- if !QSLBaseManager.shared.isVip() {
- self.view.toast(text: "请先开通会员")
- return
- }
-
- if model.blockedMe {
- return
- }
-
- let location = CLLocationCoordinate2D(latitude: model.location.lat, longitude: model.location.lng)
- self.homeMapView.setCenter(location, animated: true)
-
- for point in self.viewModel.friendLocationList {
-
- if point.userId == model.friendId {
- self.homeMapView.removeAnnotation(point.pointAnnotation)
- point.pointAnnotation?.subtitle = "350"
- self.homeMapView.addAnnotation(point.pointAnnotation)
- }
- }
- }
-
- func emptyFriPhoneViewClick() {
-
- if let vc = self.tabBarController {
- QSLFriendAddAlertView.show(vc: vc) {
-
- }
- }
- }
-
- func homeFriPhoneViewClick() {
-
- if let vc = self.tabBarController {
- QSLFriendAddAlertView.show(vc: vc) {
-
- }
- }
- }
- }
- // MARK: - 设置UI相关方法
- extension QSLHomeController {
-
- // 设置基础UI
- func setUpUI() {
-
- view.addSubview(homeMapView)
- homeMapView.snp.makeConstraints { make in
- make.edges.equalTo(0)
- }
-
- view.addSubview(homeFriendBtn)
- //
- // view.addSubview(homeLocateBtnView)
- // homeLocateBtnView.snp.makeConstraints { make in
- // make.size.equalTo(CGSize(width: 40, height: 40))
- // make.top.equalTo(self.homeButtonsView.snp.bottom).offset(16)
- // make.right.equalTo(QSLHomeViewModel.UX.viewRight)
- // }
-
- view.addSubview(homeFriendView)
- homeFriendView.snp.makeConstraints { make in
- make.left.right.equalTo(0)
- make.top.equalTo(QSLConst.qsl_kScreenH - viewModel.friendViewHeight - QSLConst.qsl_kTabbarFrameH)
- make.height.equalTo(QSLConst.qsl_kScreenH - QSLConst.qsl_kTabbarFrameH)
- }
-
- let friViewTopY = QSLConst.qsl_kScreenH - viewModel.friendViewHeight - QSLConst.qsl_kTabbarFrameH
- homeFriendView.scrollCenterHeight = friViewTopY
- homeFriendView.snp.updateConstraints { make in
- make.top.equalTo(friViewTopY)
- }
- }
- }
|