QSLHomeController.swift 28 KB

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