O2JsApiUtil.swift 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. //
  2. // O2JsApiUtil.swift
  3. // O2Platform
  4. //
  5. // Created by FancyLou on 2019/5/7.
  6. // Copyright © 2019 zoneland. All rights reserved.
  7. //
  8. import UIKit
  9. import WebKit
  10. import CocoaLumberjack
  11. class O2JsApiUtil: O2WKScriptMessageHandlerImplement {
  12. let viewController: BaseWebViewUIViewController
  13. init(viewController: BaseWebViewUIViewController) {
  14. self.viewController = viewController
  15. }
  16. func userController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
  17. if message.body is NSString {
  18. let json = message.body as! NSString
  19. DDLogDebug("message json:\(json)")
  20. if let jsonData = String(json).data(using: .utf8) {
  21. let dicArr = try! JSONSerialization.jsonObject(with: jsonData, options: .allowFragments) as! [String:AnyObject]
  22. if let type = dicArr["type"] as? String {
  23. switch type {
  24. case "date.datePicker":
  25. datePicker(json: String(json))
  26. break
  27. case "date.timePicker":
  28. timePicker(json: String(json))
  29. break
  30. case "date.dateTimePicker":
  31. dateTimePicker(json: String(json))
  32. break
  33. case "calendar.chooseOneDay":
  34. calendarPickDay(json: String(json))
  35. break
  36. case "calendar.chooseDateTime":
  37. calendarPickerDateTime(json: String(json))
  38. break
  39. case "calendar.chooseInterval":
  40. calendarPickerDateInterval(json: String(json))
  41. break
  42. case "device.rotate":
  43. rotateToggle(json: String(json))
  44. break
  45. case "device.getPhoneInfo":
  46. getPhoneInfo(json: String(json))
  47. break
  48. case "device.scan":
  49. scan(json: String(json))
  50. break
  51. case "device.location":
  52. locationSingle(json: String(json))
  53. break
  54. case "device.openMap":
  55. openMap(json: String(json))
  56. break
  57. case "navigation.setTitle":
  58. navigationSetTitle(json: String(json))
  59. break
  60. case "navigation.close":
  61. navigationClose(json: String(json))
  62. break
  63. case "navigation.goBack":
  64. navigationGoBack(json: String(json))
  65. break
  66. case "navigation.openOtherApp":
  67. navigationOpenOtherApp(json: String(json))
  68. break
  69. default:
  70. DDLogError("notification类型不正确, type: \(type)")
  71. }
  72. }else {
  73. DDLogError("util类型不存在 json解析异常。。。。。")
  74. }
  75. }else {
  76. DDLogError("消息json解析异常。。。")
  77. }
  78. }else {
  79. DDLogError("message 消息 body 类型不正确。。。")
  80. }
  81. }
  82. private func datePicker(json: String) {
  83. if let alert = O2WebViewBaseMessage<O2UtilPicker>.deserialize(from: json) {
  84. let title = alert.data?.value ?? ""
  85. DDLogDebug("value:\(title)")
  86. let defaultDate: Date!
  87. if title.isBlank {
  88. defaultDate = Date()
  89. }else {
  90. defaultDate = title.toDate(formatter: "yyyy-MM-dd")
  91. }
  92. let picker = QDatePicker{ (date: String) in
  93. print(date)
  94. if alert.callback != nil {
  95. let callJs = "\(alert.callback!)('{\"value\":\"\(date)\"}')"
  96. self.evaluateJs(callBackJs: callJs)
  97. }
  98. }
  99. picker.themeColor = O2ThemeManager.color(for: "Base.base_color")!
  100. picker.datePickerStyle = .YMD
  101. picker.pickerStyle = .datePicker
  102. picker.showDatePicker(defaultDate: defaultDate)
  103. }else {
  104. DDLogError("datePicker, 解析json失败")
  105. }
  106. }
  107. private func timePicker(json: String) {
  108. if let alert = O2WebViewBaseMessage<O2UtilPicker>.deserialize(from: json) {
  109. let title = alert.data?.value ?? ""
  110. let defaultDate: Date!
  111. if title.isBlank {
  112. defaultDate = Date()
  113. }else {
  114. let ymd = Date().formatterDate(formatter: "yyyy-MM-dd")
  115. defaultDate = (ymd+" "+title).toDate(formatter: "yyyy-MM-dd HH:mm")
  116. }
  117. let picker = QDatePicker{ (date: String) in
  118. print(date)
  119. if alert.callback != nil {
  120. let callJs = "\(alert.callback!)('{\"value\":\"\(date)\"}')"
  121. self.evaluateJs(callBackJs: callJs)
  122. }
  123. }
  124. picker.themeColor = O2ThemeManager.color(for: "Base.base_color")!
  125. picker.datePickerStyle = .HM
  126. picker.pickerStyle = .datePicker
  127. picker.showDatePicker(defaultDate: defaultDate)
  128. }else {
  129. DDLogError("datePicker, 解析json失败")
  130. }
  131. }
  132. private func dateTimePicker(json: String) {
  133. if let alert = O2WebViewBaseMessage<O2UtilPicker>.deserialize(from: json) {
  134. let title = alert.data?.value ?? ""
  135. let defaultDate: Date!
  136. if title.isBlank {
  137. defaultDate = Date()
  138. }else {
  139. defaultDate = title.toDate(formatter: "yyyy-MM-dd HH:mm")
  140. }
  141. let picker = QDatePicker{ (date: String) in
  142. print(date)
  143. if alert.callback != nil {
  144. let callJs = "\(alert.callback!)('{\"value\":\"\(date)\"}')"
  145. self.evaluateJs(callBackJs: callJs)
  146. }
  147. }
  148. picker.themeColor = O2ThemeManager.color(for: "Base.base_color")!
  149. picker.datePickerStyle = .YMDHM
  150. picker.pickerStyle = .datePicker
  151. picker.showDatePicker(defaultDate: defaultDate)
  152. }else {
  153. DDLogError("datePicker, 解析json失败")
  154. }
  155. }
  156. private func calendarPickDay(json: String) {
  157. if let alert = O2WebViewBaseMessage<O2UtilPicker>.deserialize(from: json) {
  158. let title = alert.data?.value ?? ""
  159. DDLogDebug("value:\(title)")
  160. let defaultDate: Date!
  161. if title.isBlank {
  162. defaultDate = Date()
  163. }else {
  164. defaultDate = title.toDate(formatter: "yyyy-MM-dd")
  165. }
  166. let calendarPicker = QCalendarPicker{ (date: String) in
  167. if alert.callback != nil {
  168. let callJs = "\(alert.callback!)('{\"value\":\"\(date)\"}')"
  169. self.evaluateJs(callBackJs: callJs)
  170. }
  171. }
  172. calendarPicker.calendarPickerStyle = .datePicker
  173. calendarPicker.showPickerWithDefault(defaultDate: defaultDate)
  174. }else {
  175. DDLogError("datePicker, 解析json失败")
  176. }
  177. }
  178. private func calendarPickerDateTime(json: String) {
  179. if let alert = O2WebViewBaseMessage<O2UtilPicker>.deserialize(from: json) {
  180. let title = alert.data?.value ?? ""
  181. DDLogDebug("value:\(title)")
  182. let defaultDate: Date!
  183. if title.isBlank {
  184. defaultDate = Date()
  185. }else {
  186. defaultDate = title.toDate(formatter: "yyyy-MM-dd HH:mm")
  187. }
  188. let calendarPicker = QCalendarPicker{ (date: String) in
  189. if alert.callback != nil {
  190. let callJs = "\(alert.callback!)('{\"value\":\"\(date)\"}')"
  191. self.evaluateJs(callBackJs: callJs)
  192. }
  193. }
  194. calendarPicker.calendarPickerStyle = .dateTimePicker
  195. calendarPicker.showPickerWithDefault(defaultDate: defaultDate)
  196. }else {
  197. DDLogError("datePicker, 解析json失败")
  198. }
  199. }
  200. private func calendarPickerDateInterval(json: String) {
  201. if let alert = O2WebViewBaseMessage<O2UtilPicker>.deserialize(from: json) {
  202. let start = alert.data?.startDate ?? ""
  203. let end = alert.data?.endDate ?? ""
  204. DDLogDebug("start:\(start) , end:\(end)")
  205. let startDate: Date!
  206. if start.isBlank {
  207. startDate = Date()
  208. }else {
  209. startDate = start.toDate(formatter: "yyyy-MM-dd")
  210. }
  211. let endDate: Date!
  212. if end.isBlank {
  213. endDate = Date()
  214. }else {
  215. endDate = end.toDate(formatter: "yyyy-MM-dd")
  216. }
  217. let calendarPicker = QCalendarPicker{ (date: String) in
  218. if alert.callback != nil {
  219. let result = date.split(" ")
  220. let callJs = "\(alert.callback!)('{\"startDate\":\"\(result[0])\", \"endDate\":\"\(result[1])\"}')"
  221. self.evaluateJs(callBackJs: callJs)
  222. }
  223. }
  224. calendarPicker.calendarPickerStyle = .dateIntervalPicker
  225. calendarPicker.showPickerWithDefault(defaultDate: startDate, endDate: endDate)
  226. }else {
  227. DDLogError("calendarPickerDateInterval, 解析json失败")
  228. }
  229. }
  230. // 手机屏幕旋转 横屏转竖屏 竖屏转横屏
  231. private func rotateToggle(json: String) {
  232. if let alert = O2WebViewBaseMessage<O2UtilNavigation>.deserialize(from: json) {
  233. self.viewController.onChangeOrientationBtnTapped(nil)
  234. if alert.callback != nil {
  235. let callJs = "\(alert.callback!)('{}')"
  236. self.evaluateJs(callBackJs: callJs)
  237. }
  238. }else {
  239. DDLogError("rotate 屏幕旋转, 解析json失败")
  240. }
  241. }
  242. //获取手机信息
  243. private func getPhoneInfo(json: String) {
  244. if let alert = O2WebViewBaseMessage<O2UtilNavigation>.deserialize(from: json) {
  245. if alert.callback != nil {
  246. DeviceUtil.shared.getDeviceInfoForJsApi { (info) in
  247. let backData = info.toJSONString(prettyPrint: false) ?? ""
  248. let callJs = "\(alert.callback!)('\(backData)')"
  249. self.evaluateJs(callBackJs: callJs)
  250. }
  251. }
  252. }else {
  253. DDLogError("getPhoneInfo, 解析json失败")
  254. }
  255. }
  256. //扫二维码返回结果
  257. private func scan(json: String) {
  258. if let alert = O2WebViewBaseMessage<O2UtilNavigation>.deserialize(from: json) {
  259. if alert.callback != nil {
  260. //扫一扫 。。。返回结果
  261. ScanHelper.openScan(vc: self.viewController, callbackResult: { (result) in
  262. let callJs = "\(alert.callback!)('{\"text\": \"\(result)\"}')"
  263. self.evaluateJs(callBackJs: callJs)
  264. })
  265. }
  266. }else {
  267. DDLogError("getPhoneInfo, 解析json失败")
  268. }
  269. }
  270. //单次定位
  271. private func locationSingle(json: String) {
  272. if let alert = O2WebViewBaseMessage<O2UtilNavigation>.deserialize(from: json) {
  273. if alert.callback != nil {
  274. DispatchQueue.main.async {
  275. self.viewController.locationCallBack = { result in
  276. guard let r = result else {
  277. DDLogError("没有获取到位置信息")
  278. return
  279. }
  280. let callJs = "\(alert.callback!)('\(r)')"
  281. self.evaluateJs(callBackJs: callJs)
  282. }
  283. self.viewController.startLocation()
  284. }
  285. }
  286. }else {
  287. DDLogError("locationSingle, 解析json失败")
  288. }
  289. }
  290. //打开地图位置
  291. private func openMap(json: String) {
  292. if let map = O2WebViewBaseMessage<O2UtilOpenMap>.deserialize(from: json) {
  293. if let callback = map.callback, let data = map.data {
  294. DispatchQueue.main.async {
  295. IMShowLocationViewController.pushShowLocation(vc: self.viewController, latitude: data.latitude,
  296. longitude: data.longitude, address: data.address, addressDetail: data.addressDetail)
  297. let callJs = "\(callback)('{}')"
  298. self.evaluateJs(callBackJs: callJs)
  299. }
  300. }
  301. }else {
  302. DDLogError("openMap, 解析json失败")
  303. }
  304. }
  305. //设置t标题
  306. private func navigationSetTitle(json: String) {
  307. if let alert = O2WebViewBaseMessage<O2UtilNavigation>.deserialize(from: json) {
  308. let title = alert.data?.title ?? ""
  309. if title != "" {
  310. self.viewController.title = title
  311. }
  312. if alert.callback != nil {
  313. let callJs = "\(alert.callback!)('{}')"
  314. self.evaluateJs(callBackJs: callJs)
  315. }
  316. }else {
  317. DDLogError("navigationSetTitle, 解析json失败")
  318. }
  319. }
  320. //关闭窗口
  321. private func navigationClose(json: String) {
  322. if let alert = O2WebViewBaseMessage<O2UtilNavigation>.deserialize(from: json) {
  323. self.closeSelf()
  324. if alert.callback != nil {
  325. let callJs = "\(alert.callback!)('{}')"
  326. self.evaluateJs(callBackJs: callJs)
  327. }
  328. }else {
  329. DDLogError("navigationClose, 解析json失败")
  330. }
  331. }
  332. //返回上级 html history
  333. private func navigationGoBack(json: String) {
  334. if let alert = O2WebViewBaseMessage<O2UtilNavigation>.deserialize(from: json) {
  335. if self.viewController.webView.canGoBack {
  336. self.viewController.webView.goBack()
  337. }else {
  338. self.closeSelf()
  339. }
  340. if alert.callback != nil {
  341. let callJs = "\(alert.callback!)('{}')"
  342. self.evaluateJs(callBackJs: callJs)
  343. }
  344. }else {
  345. DDLogError("navigationGoBack, 解析json失败")
  346. }
  347. }
  348. // 打开其他app schema方式
  349. private func navigationOpenOtherApp(json: String) {
  350. if let alert = O2WebViewBaseMessage<O2UtilNavigationOpenOtherApp>.deserialize(from: json) {
  351. var backresult = "{\"result\": true, \"message\": \"\"}"
  352. if let schema = alert.data?.schema {
  353. DDLogDebug("打开app schema:\(schema)")
  354. if let url = URL(string: "\(schema)") {
  355. if #available(iOS 10, *) {
  356. if UIApplication.shared.canOpenURL(url) {
  357. UIApplication.shared.open(url, options: [:], completionHandler: nil)
  358. } else {
  359. DDLogError("无法打开url,\(schema)")
  360. backresult = "{\"result\": false, \"message\": \"ios 当前不支持这个app打开,schema:\(schema)\"}"
  361. }
  362. } else {
  363. UIApplication.shared.openURL(url)
  364. }
  365. } else {
  366. DDLogError("url 为空。。。。。")
  367. }
  368. }
  369. if alert.callback != nil {
  370. let callJs = "\(alert.callback!)('\(backresult)')"
  371. self.evaluateJs(callBackJs: callJs)
  372. }
  373. }else {
  374. DDLogError("navigationOpenOtherApp, 解析json失败")
  375. }
  376. }
  377. private func closeSelf() {
  378. guard let vcs = self.viewController.navigationController?.viewControllers else {
  379. self.viewController.navigationController?.dismiss(animated: false, completion: nil)
  380. return
  381. }
  382. if vcs.count > 0 {
  383. if vcs[vcs.count - 1] == self.viewController {
  384. self.viewController.navigationController?.popViewController(animated: false)
  385. }
  386. }else {
  387. self.viewController.navigationController?.dismiss(animated: false, completion: nil)
  388. }
  389. }
  390. private func evaluateJs(callBackJs: String) {
  391. DDLogDebug("执行回调js:"+callBackJs)
  392. self.viewController.webView.evaluateJavaScript(callBackJs, completionHandler: { (result, err) in
  393. DDLogDebug("回调js执行完成!")
  394. })
  395. }
  396. }