QSLHomeController.swift 28 KB

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