IHAddressService.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. //
  2. // IHAddressService.swift
  3. // Inhealth
  4. //
  5. // Created by weclouds on 2020/1/7.
  6. // Copyright © 2020 weclouds. All rights reserved.
  7. //
  8. import UIKit
  9. class IHAddressService: NSObject {
  10. static let share = IHAddressService()
  11. ///国家列表
  12. lazy var countryList: [DropDownData] = {
  13. let contrylist = [DropDownData]()
  14. return contrylist
  15. }()
  16. //省份列表
  17. lazy var provinceList: [DropDownData] = {
  18. let provinceList = [DropDownData]()
  19. return provinceList
  20. }()
  21. //城市列表
  22. lazy var cityList: [DropDownData] = {
  23. let cityList = [DropDownData]()
  24. return cityList
  25. }()
  26. //酒店下拉列表
  27. lazy var hotelList: [DropDownData] = {
  28. let hotelList = [DropDownData]()
  29. return hotelList
  30. }()
  31. ///获取国家列表
  32. func getContryList(requestSuccess:@escaping ([DropDownData])->Void,requestFail:@escaping (()->Void)){
  33. let username = AppShare.username
  34. let client_key = AppShare.client_key
  35. let os = AppShare.os
  36. let version = AppShare.version
  37. let token = AppShare.token
  38. g_area_country_listHttpRequest(username, client_key: client_key, os: os, version: version, token: token, success: { (json) -> (Void) in
  39. self.countryList.removeAll()
  40. let mCountryInfo = DropDown_Info.fromJSON(json)
  41. let result = mCountryInfo?.result
  42. guard let list = result?.list else { return}
  43. for country in list{
  44. // let mdata =
  45. self.countryList.append(country)
  46. }
  47. requestSuccess(self.countryList)
  48. }, fail: requestFail)
  49. }
  50. ///获取省份列表
  51. func getProvinceList(_ countryId:String?,requestSuccess:@escaping ([DropDownData]?)->Void,requestFail:@escaping (()->Void)){
  52. let username = AppShare.username
  53. let client_key = AppShare.client_key
  54. let os = AppShare.os
  55. let version = AppShare.version
  56. let token = AppShare.token
  57. g_area_province_listHttpRequest(username, client_key: client_key, os: os, version: version, token: token, countryId: countryId, success: { (json) -> (Void) in
  58. self.provinceList.removeAll()
  59. let mProvinceInfo = DropDown_Info.fromJSON(json)
  60. let result = mProvinceInfo?.result
  61. guard let list = result?.list else { return}
  62. for country in list{
  63. self.provinceList.append(country)
  64. }
  65. requestSuccess(self.provinceList)
  66. }, fail: requestFail)
  67. }
  68. //获取城市列表
  69. func getCityeList(_ provinceId:String?,requestSuccess:@escaping ([DropDownData]?)->Void,requestFail:@escaping (()->Void)){
  70. let username = AppShare.username
  71. let client_key = AppShare.client_key
  72. let os = AppShare.os
  73. let version = AppShare.version
  74. let token = AppShare.token
  75. g_area_city_listHttpRequest(username, client_key: client_key, os: os, version: version, token: token, provinceId: provinceId, success: { (json) -> (Void) in
  76. self.cityList.removeAll()
  77. let mCityInfo = DropDown_Info.fromJSON(json)
  78. let result = mCityInfo?.result
  79. guard let list = result?.list else { return}
  80. for city in list{
  81. self.cityList.append(city)
  82. }
  83. requestSuccess(self.cityList)
  84. }, fail: requestFail)
  85. }
  86. ///获取酒店下拉列表
  87. func getHotelList(_ cityId:String?,requestSuccess:@escaping ([DropDownData])->Void,requestFail:@escaping (()->Void)){
  88. let username = AppShare.username
  89. let client_key = AppShare.client_key
  90. let os = AppShare.os
  91. let version = AppShare.version
  92. let token = AppShare.token
  93. g_area_hotel_listHttpRequest(username, client_key: client_key, os: os, version: version, token: token, cityId: cityId, success: { (json) -> (Void) in
  94. log.debug("hotel json - \(json)")
  95. self.hotelList.removeAll()
  96. let mCityInfo = DropDown_Info.fromJSON(json)
  97. let result = mCityInfo?.result
  98. if result?.list?.count == 0{
  99. AppShare.noHotel = true
  100. }else{
  101. AppShare.noHotel = false
  102. }
  103. guard let list = result?.list else { return}
  104. for hotel in list{
  105. self.hotelList.append(hotel)
  106. }
  107. requestSuccess(self.hotelList)
  108. }, fail: requestFail)
  109. }
  110. ///获取酒店下拉列表
  111. func getHotelWithBuildlist(_ cityId:String?,requestSuccess:@escaping ([Hotel])->Void,requestFail:@escaping (()->Void)){
  112. let username = AppShare.username
  113. let client_key = AppShare.client_key
  114. let os = AppShare.os
  115. let version = AppShare.version
  116. let token = AppShare.token
  117. g_area_hotel_listHttpRequest(username, client_key: client_key, os: os, version: version, token: token, cityId: cityId, success: { (json) -> (Void) in
  118. log.debug("hotel json - \(json)")
  119. let mCityInfo = Hotel_List_Info.fromJSON(json)
  120. let result = mCityInfo?.result
  121. if result?.list?.count == 0{
  122. AppShare.noHotel = true
  123. }else{
  124. AppShare.noHotel = false
  125. }
  126. if result?.code == .Success{
  127. requestSuccess(result!.list!)
  128. }else{
  129. requestFail()
  130. }
  131. }, fail: requestFail)
  132. }
  133. //获取第一个酒店
  134. func getFirstHotel(complete:@escaping ((String?,String?)->Void)) {
  135. IHAddressService.share.getContryList(requestSuccess: { (countrys) in
  136. if countrys.count == 0 {
  137. return
  138. }
  139. let firstCountry = countrys.first
  140. Intermediate.countryName = (firstCountry?.name)!
  141. Intermediate.countryId = (firstCountry?.id)!
  142. //1、有省份
  143. IHAddressService.share.getProvinceList(firstCountry?.id, requestSuccess: { (provinces) in
  144. //没有省份
  145. if provinces == nil || provinces!.count == 0{
  146. //请求酒店列表
  147. IHAddressService.share.getHotelList(firstCountry?.id, requestSuccess: { (hotelList) in
  148. let firstHotel = hotelList.first
  149. self.getFirstBuild(firstHotel!.id!)
  150. complete(firstHotel?.id,firstHotel?.name)
  151. let info = StorageInfo(countryId: Intermediate.countryId, countryName: Intermediate.countryName, provinceId: "", provinceName: "", cityId: "", cityName: "", hotelId: firstHotel?.id, hotelName: firstHotel!.name,buildId:"",buildName:"")
  152. IHHotelStorage.shareInstance.storageHotelInfo(info)
  153. }) {
  154. log.debug("失败了")
  155. }
  156. }else{//有省份
  157. let firstProvince = provinces?.first
  158. //请求城市
  159. IHAddressService.share.getCityeList(firstProvince?.id, requestSuccess: { (citylist) in
  160. if citylist == nil || citylist!.count == 0 { //没有城市
  161. //请求酒店列表 ->省份id
  162. IHAddressService.share.getHotelList(firstProvince?.id, requestSuccess: { (hotelList) in
  163. let firstHotel = hotelList.first
  164. self.getFirstBuild(firstHotel!.id!)
  165. complete(firstHotel?.id,firstHotel?.name)
  166. let info = StorageInfo(countryId: Intermediate.countryId, countryName: Intermediate.countryName, provinceId: firstProvince?.id, provinceName: firstProvince?.name, cityId: "", cityName: "", hotelId: firstHotel?.id, hotelName: firstHotel!.name,buildId:"",buildName:"")
  167. IHHotelStorage.shareInstance.storageHotelInfo(info)
  168. }) {
  169. log.debug("失败了")
  170. }
  171. }else{
  172. //请求酒店列表 ->城市id
  173. let firstCity = citylist?.first
  174. IHAddressService.share.getHotelList(firstCity?.id, requestSuccess: { (hotelList) in
  175. let firstHotel = hotelList.first
  176. complete(firstHotel?.id,firstHotel?.name)
  177. self.getFirstBuild(firstHotel!.id!)
  178. let info = StorageInfo(countryId: Intermediate.countryId, countryName: Intermediate.countryName, provinceId: firstProvince?.id, provinceName: firstProvince?.name, cityId: firstCity?.id, cityName: firstCity?.name, hotelId: firstHotel?.id, hotelName: firstHotel!.name,buildId:"",buildName:"")
  179. IHHotelStorage.shareInstance.storageHotelInfo(info)
  180. }) {
  181. log.debug("失败了")
  182. }
  183. }
  184. }) {
  185. log.debug("失败了")
  186. }
  187. }
  188. }) {
  189. }
  190. //1.1 有城市
  191. //1.2 无城市
  192. //2、无省份
  193. }) {
  194. }
  195. }
  196. func getFirstBuild(_ hotelId:String) {
  197. IHAreaService.share.getBuildNavData(hotelId: hotelId, requestSuccess: { (buildList) in
  198. if buildList.count != 0{
  199. let build = buildList.first
  200. Intermediate.buildId = build!.id!
  201. Intermediate.buildName = build!.name!
  202. }
  203. }) {
  204. }
  205. }
  206. }