QSLHomeController.swift 27 KB

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