QSLHomeController.swift 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  1. //
  2. // QSLHomeController.swift
  3. // QuickSearchLocation
  4. //
  5. // Created by mac on 2024/4/10.
  6. //
  7. import UIKit
  8. import SwiftyJSON
  9. import MAMapKit
  10. import AMapFoundationKit
  11. import AMapLocationKit
  12. class QSLHomeController: QSLBaseController {
  13. lazy var viewModel: QSLHomeViewModel = {
  14. return QSLHomeViewModel()
  15. }()
  16. var personalModel: QSLUserModel = QSLUserModel()
  17. ///判断是不是第一次定位成功
  18. var isFirstLocatScuess : Bool = true
  19. // var friendList: [QSLUserModel] = [QSLUserModel]()
  20. override func viewDidLoad() {
  21. super.viewDidLoad()
  22. viewModel.initUIData()
  23. setUpMap()
  24. setUpUI()
  25. loadData()
  26. NotificationCenter.default.addObserver(self, selector: #selector(loadData), name: QSLNotification.QSLLogin, object: nil)
  27. NotificationCenter.default.addObserver(self, selector: #selector(loadData), name: QSLNotification.QSLLogout, object: nil)
  28. NotificationCenter.default.addObserver(self, selector: #selector(loadData), name: QSLNotification.QSLRefreshFriend, object: nil)
  29. NotificationCenter.default.addObserver(self, selector: #selector(loadData), name: QSLNotification.QSLRefreshRequest, object: nil)
  30. NotificationCenter.default.addObserver(self, selector: #selector(requestVip), name: QSLNotification.QSLRefreshMember, object: nil)
  31. //处理第一次进入首页定位
  32. firstEnterLocationHomepage()
  33. }
  34. //处理第一次进入首页定位
  35. func firstEnterLocationHomepage() {
  36. guard UserDefaults.standard.string(forKey: "FIRST_ENTER_LOCATION_HOMEPAGE") != nil else {
  37. UserDefaults.standard.set("YES", forKey: "FIRST_ENTER_LOCATION_HOMEPAGE")
  38. UserDefaults.standard.synchronize()
  39. ///第一次进入埋点
  40. gravityInstance?.track(QSLGravityConst.location_enter_first_homepage, properties: ["id": 03005])
  41. return
  42. }
  43. }
  44. /// 高德地图
  45. lazy var homeMapView = {
  46. let _mapView = MAMapView()
  47. _mapView.delegate = self
  48. _mapView.minZoomLevel = 7;
  49. _mapView.maxZoomLevel = 19;
  50. _mapView.zoomLevel = 17;
  51. _mapView.showsCompass = false
  52. _mapView.showsScale = false
  53. _mapView.isRotateCameraEnabled = false
  54. _mapView.showsUserLocation = true
  55. _mapView.userTrackingMode = .follow
  56. // 关闭显示精度圈
  57. let represent = MAUserLocationRepresentation()
  58. represent.showsAccuracyRing = false
  59. _mapView.update(represent)
  60. let tap = UITapGestureRecognizer(target: self, action: #selector(mapTapAction))
  61. _mapView.addGestureRecognizer(tap)
  62. return _mapView
  63. }()
  64. /// 高德地图定位
  65. lazy var homeMapLocationM = {
  66. let _homeMapLocationM = AMapLocationManager()
  67. _homeMapLocationM.delegate = self
  68. _homeMapLocationM.distanceFilter = 100
  69. _homeMapLocationM.locatingWithReGeocode = true
  70. _homeMapLocationM.desiredAccuracy = kCLLocationAccuracyHundredMeters
  71. _homeMapLocationM.locationTimeout = 2
  72. _homeMapLocationM.reGeocodeTimeout = 2
  73. _homeMapLocationM.pausesLocationUpdatesAutomatically = false
  74. _homeMapLocationM.allowsBackgroundLocationUpdates = true
  75. return _homeMapLocationM
  76. }()
  77. /// 头部定位权限弹窗
  78. lazy var homeAuthHeaderView = {
  79. let _homeAuthHeaderView = QSLHomeAuthHeaderView()
  80. _homeAuthHeaderView.addRadius(radius: 6)
  81. return _homeAuthHeaderView
  82. }()
  83. lazy var homeButtonsView = {
  84. let _homeButtonsView = QSLHomeButtonView()
  85. return _homeButtonsView
  86. }()
  87. /// 定位按钮
  88. lazy var homeLocateBtnView = {
  89. let _homeLocateBtnView = UIView()
  90. _homeLocateBtnView.addRadius(radius: 6)
  91. _homeLocateBtnView.effectViewWithAlpha(alpha: 1, size: CGSize(width: 40, height: 40), style: .light)
  92. return _homeLocateBtnView
  93. }()
  94. lazy var homeLocateBtn = {
  95. let _homeLocateBtn = UIButton(type: .custom)
  96. _homeLocateBtn.image(UIImage(named: "home_btn_locate"))
  97. return _homeLocateBtn
  98. }()
  99. /// 无好友和未登录时的状态
  100. lazy var homeEmptyView: QSLHomeEmptyView = {
  101. let _homeEmptyView = QSLHomeEmptyView()
  102. _homeEmptyView.delegate = self
  103. _homeEmptyView.isHidden = true
  104. return _homeEmptyView
  105. }()
  106. lazy var homeFriendView: QSLHomeFriendView = {
  107. let topHeight = QSLConst.qsl_kStatusBarFrameH
  108. 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))
  109. view.scrollTopHeight = topHeight - 40
  110. view.delegate = self
  111. return view
  112. }()
  113. }
  114. // MARK: - 网络请求等
  115. extension QSLHomeController {
  116. @objc func loadData() {
  117. // viewModel.refreshDataSoure(complete: { [unowned self] in
  118. // self.homeFriendView.viewModel = self.viewModel
  119. // let friViewTopY = QSLConst.qsl_kScreenH - viewModel.friendViewHeight - QSLConst.qsl_kTabbarFrameH
  120. // homeFriendView.scrollCenterHeight = friViewTopY
  121. // homeFriendView.snp.updateConstraints { make in
  122. // make.top.equalTo(friViewTopY)
  123. // }
  124. // })
  125. self.onceLocation()
  126. if QSLBaseManager.shared.isLogin() {
  127. self.initSocket()
  128. } else {
  129. self.deposeSocket()
  130. }
  131. requestFriendList()
  132. requestVip()
  133. }
  134. // 请求好友列表
  135. @objc func requestFriendList() {
  136. self.homeMapView.removeAnnotations(self.homeMapView.annotations)
  137. viewModel.friendList.removeAll()
  138. viewModel.friendLocationList.removeAll()
  139. viewModel.friendList.append(QSLBaseManager.shared.userModel)
  140. QSLNetwork().request(.friendList(dict: [:])) { response in
  141. let list = response.mapArray(QSLUserModel.self, modelKey: "data>list")
  142. self.viewModel.friendList.append(contentsOf: list)
  143. self.homeFriendView.viewModel = self.viewModel
  144. for model in list {
  145. let pointAnnotation = MAPointAnnotation()
  146. let loc = CLLocationCoordinate2D(latitude: model.location.lat, longitude: model.location.lng)
  147. pointAnnotation.coordinate = loc
  148. if model.remark.count > 0 {
  149. pointAnnotation.title = model.remark
  150. } else {
  151. pointAnnotation.title = model.phone
  152. }
  153. pointAnnotation.subtitle = "300"
  154. let pointModel = QSLMapPointModel()
  155. pointModel.userId = model.userId
  156. pointModel.pointAnnotation = pointAnnotation
  157. self.viewModel.friendLocationList.append(pointModel)
  158. if QSLBaseManager.shared.isVip() && !model.blockedMe {
  159. self.homeMapView.addAnnotation(pointAnnotation)
  160. }
  161. }
  162. } fail: { code, error in
  163. self.homeFriendView.viewModel = self.viewModel
  164. }
  165. }
  166. // 请求vip信息
  167. @objc func requestVip() {
  168. QSLNetwork().request(.userMember(dict: [:])) { response in
  169. let memberModel = response.mapObject(QSLMemberModel.self, modelKey: "data")
  170. QSLBaseManager.shared.userModel.memberModel = memberModel
  171. if memberModel.expired {
  172. QSLBaseManager.shared.saveVipExpiredTime(time: 0)
  173. } else {
  174. QSLBaseManager.shared.saveVipExpiredTime(time: memberModel.endTimestamp)
  175. }
  176. QSLBaseManager.shared.saveUserId(id: memberModel.userId)
  177. // NotificationCenter.default.post(name: QSLNotification.QSLRefreshMember, object: nil)
  178. } fail: { code, error in
  179. }
  180. }
  181. }
  182. // MARK: - 设置地图相关方法
  183. extension QSLHomeController: MAMapViewDelegate, AMapLocationManagerDelegate {
  184. // 初始化高德地图设置
  185. func setUpMap() {
  186. AMapServices.shared().enableHTTPS = true
  187. AMapLocationManager.updatePrivacyShow(.didShow, privacyInfo: .didContain)
  188. AMapLocationManager.updatePrivacyAgree(.didAgree)
  189. homeMapLocationM.locatingWithReGeocode = true
  190. homeMapLocationM.startUpdatingLocation()
  191. }
  192. // 申请地图定位权限
  193. func mapViewRequireLocationAuth(_ locationManager: CLLocationManager!) {
  194. locationManager.requestWhenInUseAuthorization()
  195. }
  196. // 地图触摸事件
  197. @objc func mapTapAction() {
  198. if self.homeFriendView.scrollLocation != .ScrollBottom {
  199. self.homeFriendView.scrollToLocation(type: .ScrollBottom)
  200. }
  201. }
  202. // 获取一次性定位
  203. func onceLocation() {
  204. // 先停止持续定位
  205. self.homeMapLocationM.stopUpdatingLocation()
  206. self.homeMapLocationM.requestLocation(withReGeocode: true) { location, regeocode, error in
  207. if error != nil {
  208. self.personalModel = QSLBaseManager.shared.userModel
  209. // 获取电话和名字
  210. // self.personalModel.phone = QSLBaseManager.shared.userModel.phone
  211. var name = "用户\(QSLBaseManager.shared.userModel.phone.suffix(4))"
  212. if !QSLBaseManager.shared.isLogin() {
  213. name = "自己"
  214. }
  215. name = "自己"
  216. self.personalModel.remark = name
  217. // 地址
  218. self.personalModel.location.addr = "请先开启位置权限"
  219. QSLBaseManager.shared.updateUser(model: self.personalModel)
  220. if self.viewModel.friendList.isEmpty {
  221. self.viewModel.friendList.append(self.personalModel)
  222. } else {
  223. self.viewModel.friendList[0] = self.personalModel
  224. }
  225. self.homeFriendView.viewModel = self.viewModel
  226. self.homeMapLocationM.startUpdatingLocation()
  227. return
  228. }
  229. self.personalModel = QSLBaseManager.shared.userModel
  230. var name = "用户\(QSLBaseManager.shared.userModel.phone.suffix(4))"
  231. if !QSLBaseManager.shared.isLogin() {
  232. name = "自己"
  233. }
  234. name = "自己"
  235. self.personalModel.remark = name
  236. self.personalModel.location.addr = regeocode?.formattedAddress ?? "未知"
  237. self.personalModel.location.timestamp = Int(Date.secondStamp) ?? 0
  238. if let location = location {
  239. // 地图居中
  240. self.homeMapView.setCenter(location.coordinate, animated: true)
  241. self.homeFriendView.scrollLocation = .ScrollCenter
  242. self.personalModel.location.lat = location.coordinate.latitude
  243. self.personalModel.location.lng = location.coordinate.longitude
  244. // 速度
  245. self.personalModel.location.speed = location.speed >= 0 ? location.speed : 0
  246. // 朝向
  247. self.personalModel.location.bearing = location.course >= 0 ? location.course : 0
  248. }
  249. QSLBaseManager.shared.updateUser(model: self.personalModel)
  250. if QSLBaseManager.shared.isLogin() {
  251. let locationData = self.personalModel.location
  252. let addr = locationData.addr
  253. if addr != "请先开启位置权限" {
  254. self.sendSocket()
  255. }
  256. }
  257. if self.viewModel.friendList.isEmpty {
  258. self.viewModel.friendList.append(self.personalModel)
  259. } else {
  260. self.viewModel.friendList[0] = self.personalModel
  261. }
  262. print("location: \(String(describing: location))")
  263. if let regeocode = regeocode {
  264. print("reGeocode: \(regeocode)")
  265. }
  266. self.homeFriendView.viewModel = self.viewModel
  267. self.homeMapLocationM.startUpdatingLocation()
  268. }
  269. }
  270. // 持续定位
  271. func amapLocationManager(_ manager: AMapLocationManager!, didUpdate location: CLLocation!, reGeocode: AMapLocationReGeocode!) {
  272. if self.isFirstLocatScuess {
  273. self.isFirstLocatScuess = false
  274. //始终允许定位成功
  275. gravityInstance?.track(QSLGravityConst.location_success_always, properties: ["id": 03003])
  276. }
  277. // 获取电话和名字
  278. self.personalModel = QSLBaseManager.shared.userModel
  279. var name = "用户\(QSLBaseManager.shared.userModel.phone.suffix(4))"
  280. if !QSLBaseManager.shared.isLogin() {
  281. name = "自己"
  282. }
  283. name = "自己"
  284. self.personalModel.remark = name
  285. // 时间戳
  286. self.personalModel.location.timestamp = Int(Date.secondStamp) ?? 0
  287. // 经纬度
  288. self.personalModel.location.lat = location.coordinate.latitude
  289. self.personalModel.location.lng = location.coordinate.longitude
  290. // 速度
  291. if location.speed >= 0 {
  292. self.personalModel.location.speed = location.speed
  293. } else {
  294. self.personalModel.location.speed = 0
  295. }
  296. // 朝向
  297. if location.course >= 0 {
  298. self.personalModel.location.bearing = location.course
  299. } else {
  300. self.personalModel.location.bearing = 0
  301. }
  302. // 打印位置信息
  303. print("location: {lat: \(location.coordinate.latitude); lon: \(location.coordinate.longitude); accuracy: \(location.horizontalAccuracy)}")
  304. // 逆地理编码处理
  305. if let reGeocode = reGeocode {
  306. print("reGeocode: \(reGeocode)")
  307. self.personalModel.location.addr = reGeocode.formattedAddress
  308. } else {
  309. self.personalModel.location.addr = "请先开启位置权限"
  310. }
  311. // 保存到模块
  312. QSLBaseManager.shared.updateUser(model: self.personalModel)
  313. // 更新好友列表
  314. if self.viewModel.friendList.isEmpty {
  315. self.viewModel.friendList.append(self.personalModel)
  316. } else {
  317. self.viewModel.friendList[0] = self.personalModel
  318. }
  319. self.homeFriendView.viewModel = self.viewModel
  320. // 更新UI
  321. // self.updateHomeUI()
  322. // 如果已登录,发送定位信息
  323. if QSLBaseManager.shared.isLogin() {
  324. let locationData = self.personalModel.location
  325. let addr = locationData.addr
  326. if addr != "请先开启位置权限" {
  327. self.sendSocket()
  328. }
  329. }
  330. }
  331. func mapView(_ mapView: MAMapView!, viewFor annotation: (any MAAnnotation)!) -> MAAnnotationView! {
  332. // 处理用户位置的标注
  333. if annotation is MAUserLocation {
  334. let reuseIdentifier = "UserAnotation"
  335. var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseIdentifier) as? QSLHomeAnnotatinView
  336. if annotationView == nil {
  337. annotationView = QSLHomeAnnotatinView(annotation: annotation, reuseIdentifier: reuseIdentifier)
  338. }
  339. let name = "我"
  340. annotationView?.title = name
  341. annotationView?.isEnabled = false
  342. annotationView?.image = UIImage(named: "home_mine_anno")
  343. annotationView?.canShowCallout = false
  344. annotationView?.centerOffset = CGPoint(x: 0, y: -18)
  345. annotationView?.calloutOffset = CGPoint(x: 0, y: 0)
  346. annotationView?.zIndex = 360
  347. annotationView?.isSelected = true
  348. return annotationView
  349. }
  350. // 处理普通点标注
  351. if annotation is MAPointAnnotation {
  352. let reuseIdentifier = "OtherAnnotation"
  353. var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseIdentifier) as? QSLHomeAnnotatinView
  354. if annotationView == nil {
  355. annotationView = QSLHomeAnnotatinView(annotation: annotation, reuseIdentifier: reuseIdentifier)
  356. annotationView?.title = annotation.title
  357. }
  358. if let subtitle = annotation.subtitle as? Int {
  359. annotationView?.zIndex = subtitle
  360. }
  361. annotationView?.isEnabled = false
  362. annotationView?.image = UIImage(named: "home_mine_anno")
  363. annotationView?.canShowCallout = false
  364. annotationView?.centerOffset = CGPoint(x: 0, y: -18)
  365. annotationView?.calloutOffset = CGPoint(x: 0, y: 0)
  366. annotationView?.isSelected = true
  367. return annotationView
  368. }
  369. return nil
  370. }
  371. //@brief 定位权限状态改变时回调函数。注意:iOS13及之前版本回调
  372. // 高德地图权限变化回调
  373. func amapLocationManager(_ manager: AMapLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
  374. //print("高德地图定位权限变化: \(status.rawValue)")
  375. switch status {
  376. case .authorizedAlways, .authorizedWhenInUse:
  377. //print("定位权限已授权")
  378. //支付成功埋点
  379. gravityInstance?.track(QSLGravityConst.location_allow_permission, properties: ["id": 03001])
  380. case .denied:
  381. //print("定位权限被拒绝")
  382. //拒绝定位权限
  383. gravityInstance?.track(QSLGravityConst.location_deny_permission, properties: ["id": 03002])
  384. default:
  385. break
  386. }
  387. }
  388. // 系统定位权限变化回调
  389. func amapLocationManager(_ manager: AMapLocationManager, locationManagerDidChangeAuthorization locationManager: CLLocationManager) {
  390. if #available(iOS 14.0, *) {
  391. let status = locationManager.authorizationStatus
  392. // 统一处理权限变化
  393. amapLocationManager(manager, didChangeAuthorization: status)
  394. //print("系统定位权限变化: \(status.rawValue)")
  395. } else {
  396. // Fallback on earlier versions
  397. }
  398. }
  399. // 定位结果回调
  400. func amapLocationManager(_ manager: AMapLocationManager, didFailWithError error: Error) {
  401. print("定位失败: \(error.localizedDescription)")
  402. gravityInstance?.track(QSLGravityConst.location_fail_always, properties: ["id": 03004])
  403. }
  404. }
  405. // MARK: - 设置WebSocket
  406. extension QSLHomeController: QSLSocketManagerDelegate {
  407. // 初始化
  408. func initSocket() {
  409. QSLSocketManager.shared.urlString = "\(QSLApi.prodWSUrl)/websocket/\(QSLBaseManager.shared.userModel.authToken)"
  410. QSLSocketManager.shared.connect()
  411. QSLSocketManager.shared.delegate = self
  412. }
  413. // 关闭socket
  414. func deposeSocket() {
  415. QSLSocketManager.shared.urlString = ""
  416. QSLSocketManager.shared.close()
  417. QSLSocketManager.shared.delegate = nil
  418. }
  419. // 发送socket信息
  420. func sendSocket() {
  421. var message = QSLMapMessageModel()
  422. message.cmd = "u.location"
  423. var location = QSLMapTrackModel()
  424. location.lat = self.personalModel.location.lat
  425. location.lng = self.personalModel.location.lng
  426. location.addr = self.personalModel.location.addr
  427. location.speed = self.personalModel.location.speed
  428. location.bearing = self.personalModel.location.bearing
  429. location.timestamp = Int(Date.milliStamp) ?? 0
  430. message.body = location.toJSONString()
  431. QSLSocketManager.shared.sendMessage(message.toJSONString())
  432. }
  433. // 接收到消息
  434. func socketDidReceiveMessage(with string: String) {
  435. let locationModel = QSLMapMessageModel.mapModel(from: string)
  436. if let data = locationModel.body.data(using: .utf8), let list = try? JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [QSLMapTrackModel] {
  437. if locationModel.cmd == "d.location.batch" {
  438. for model in list {
  439. var isBlockedMe = false
  440. for var user in viewModel.friendList {
  441. if model.userId == user.friendId {
  442. user.location = model
  443. isBlockedMe = user.blockedMe
  444. }
  445. }
  446. if !isBlockedMe && QSLBaseManager.shared.isVip() {
  447. for location in viewModel.friendLocationList {
  448. if model.userId == location.userId {
  449. let annotation = MAPointAnnotation()
  450. let cll2d = CLLocationCoordinate2D(latitude: model.lat, longitude: model.lng)
  451. annotation.coordinate = cll2d
  452. location.pointAnnotation = annotation
  453. self.homeMapView.addAnnotation(location.pointAnnotation)
  454. }
  455. }
  456. }
  457. }
  458. self.homeFriendView.viewModel = self.viewModel
  459. }
  460. }
  461. }
  462. }
  463. // MARK: - 设置界面代理方法
  464. extension QSLHomeController: QSLHomeEmptyViewDelegate, QSLHomeFriendViewDelegate {
  465. func viewDidScroll() {
  466. switch self.homeFriendView.scrollLocation {
  467. case .ScrollTop:
  468. self.homeFriendView.snp.updateConstraints { make in
  469. make.top.equalTo(QSLConst.qsl_kStatusBarFrameH - 40.rpx)
  470. }
  471. break
  472. case .ScrollCenter:
  473. self.homeFriendView.snp.updateConstraints { make in
  474. make.top.equalTo(QSLConst.qsl_kScreenH - viewModel.friendViewHeight - QSLConst.qsl_kStatusBarFrameH)
  475. }
  476. break
  477. case .ScrollBottom:
  478. self.homeFriendView.snp.updateConstraints { make in
  479. make.top.equalTo(QSLConst.qsl_kScreenH - QSLConst.qsl_kTabbarFrameH - 56.rpx - 40.rpx)
  480. }
  481. break
  482. default:
  483. break
  484. }
  485. }
  486. func addFriendAction(isSmall: Bool) {
  487. if isSmall {
  488. QSLJumpManager.shared.pushToAdd(type: .homesmall)
  489. } else {
  490. QSLJumpManager.shared.pushToAdd(type: .homebig)
  491. }
  492. }
  493. func refreshBtnAction() {
  494. gravityInstance?.track(QSLGravityConst.home_locate)
  495. self.loadData()
  496. }
  497. // 轨迹
  498. func routeBtnAction(model: QSLUserModel) {
  499. if !QSLBaseManager.shared.isLogin() {
  500. if let view = self.tabBarController?.view {
  501. QSLAlertView.alert(view: view, title: "温馨提示", content: "登录即可体验查看轨迹记录", secondBtnClosure: {
  502. QSLJumpManager.shared.pushToLogin(type: .road)
  503. })
  504. }
  505. return
  506. }
  507. if !QSLBaseManager.shared.isVip() {
  508. QSLJumpManager.shared.pushToVip(type: .homeRoad)
  509. return
  510. }
  511. QSLJumpManager.shared.pushToRoad(type: .home, model: model)
  512. }
  513. // 点击列表的定位按钮
  514. func locateBtnAction(model: QSLUserModel) {
  515. if !QSLBaseManager.shared.isVip() {
  516. self.view.toast(text: "请先开通会员")
  517. return
  518. }
  519. if model.blockedMe {
  520. return
  521. }
  522. let location = CLLocationCoordinate2D(latitude: model.location.lat, longitude: model.location.lng)
  523. self.homeMapView.setCenter(location, animated: true)
  524. for point in self.viewModel.friendLocationList {
  525. if point.userId == model.friendId {
  526. self.homeMapView.removeAnnotation(point.pointAnnotation)
  527. point.pointAnnotation?.subtitle = "350"
  528. self.homeMapView.addAnnotation(point.pointAnnotation)
  529. }
  530. }
  531. }
  532. func emptyFriPhoneViewClick() {
  533. if let vc = self.tabBarController {
  534. QSLFriendAddAlertView.show(vc: vc) {
  535. }
  536. }
  537. }
  538. func homeFriPhoneViewClick() {
  539. if let vc = self.tabBarController {
  540. QSLFriendAddAlertView.show(vc: vc) {
  541. }
  542. }
  543. }
  544. }
  545. // MARK: - 设置UI相关方法
  546. extension QSLHomeController {
  547. // 设置基础UI
  548. func setUpUI() {
  549. view.addSubview(homeMapView)
  550. homeMapView.snp.makeConstraints { make in
  551. make.edges.equalTo(0)
  552. }
  553. // view.addSubview(homeButtonsView)
  554. // homeButtonsView.snp.makeConstraints { make in
  555. // make.size.equalTo(CGSize(width: 40, height: 104))
  556. // make.top.equalTo(QSLConst.qsl_kStatusBarFrameH + 164)
  557. // make.right.equalTo(QSLHomeViewModel.UX.viewRight)
  558. // }
  559. //
  560. // view.addSubview(homeLocateBtnView)
  561. // homeLocateBtnView.snp.makeConstraints { make in
  562. // make.size.equalTo(CGSize(width: 40, height: 40))
  563. // make.top.equalTo(self.homeButtonsView.snp.bottom).offset(16)
  564. // make.right.equalTo(QSLHomeViewModel.UX.viewRight)
  565. // }
  566. view.addSubview(homeFriendView)
  567. homeFriendView.snp.makeConstraints { make in
  568. make.left.right.equalTo(0)
  569. make.top.equalTo(QSLConst.qsl_kScreenH - viewModel.friendViewHeight - QSLConst.qsl_kTabbarFrameH)
  570. make.height.equalTo(QSLConst.qsl_kScreenH - QSLConst.qsl_kTabbarFrameH)
  571. }
  572. let friViewTopY = QSLConst.qsl_kScreenH - viewModel.friendViewHeight - QSLConst.qsl_kTabbarFrameH
  573. homeFriendView.scrollCenterHeight = friViewTopY
  574. homeFriendView.snp.updateConstraints { make in
  575. make.top.equalTo(friViewTopY)
  576. }
  577. }
  578. }