QSLHomeController.swift 27 KB

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