O2JsApiNotification.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. //
  2. // O2JsApiNotification.swift
  3. // O2Platform
  4. //
  5. // Created by FancyLou on 2019/4/22.
  6. // Copyright © 2019 zoneland. All rights reserved.
  7. //
  8. import UIKit
  9. import WebKit
  10. import AudioToolbox
  11. import CocoaLumberjack
  12. /**
  13. * o2m.notification
  14. **/
  15. class O2JsApiNotification: O2WKScriptMessageHandlerImplement {
  16. let viewController: BaseWebViewUIViewController
  17. init(viewController: BaseWebViewUIViewController) {
  18. self.viewController = viewController
  19. }
  20. func userController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
  21. if message.body is NSString {
  22. let json = message.body as! NSString
  23. DDLogDebug("message json:\(json)")
  24. if let jsonData = String(json).data(using: .utf8) {
  25. let dicArr = try! JSONSerialization.jsonObject(with: jsonData, options: .allowFragments) as! [String:AnyObject]
  26. if let type = dicArr["type"] as? String {
  27. switch type {
  28. case "alert":
  29. alert(json: String(json))
  30. break
  31. case "confirm":
  32. confirm(json: String(json))
  33. break
  34. case "prompt":
  35. prompt(json: String(json))
  36. break
  37. case "vibrate":
  38. vibrate(json: String(json))
  39. break
  40. case "toast":
  41. toast(json: String(json))
  42. break
  43. case "actionSheet":
  44. actionSheet(json: String(json))
  45. break
  46. case "showLoading":
  47. showLoading(json: String(json))
  48. break
  49. case "hideLoading":
  50. hideLoading(json: String(json))
  51. break
  52. default:
  53. DDLogError("notification类型不正确, type: \(type)")
  54. }
  55. }else {
  56. DDLogError("notification类型 json解析异常。。。。。")
  57. }
  58. }else {
  59. DDLogError("消息json解析异常。。。")
  60. }
  61. }else {
  62. DDLogError("message 消息 body 类型不正确。。。")
  63. }
  64. }
  65. //o2m.notification.alert
  66. private func alert(json: String) {
  67. DDLogDebug("alert:\(json)")
  68. if let alert = O2NotificationMessage<O2NotificationAlertMessage>.deserialize(from: json) {
  69. var buttonName = alert.data?.buttonName ?? ""
  70. if buttonName == "" {
  71. buttonName = "确定"
  72. }
  73. let title = alert.data?.title ?? ""
  74. let message = alert.data?.message ?? "消息"
  75. self.viewController.showSystemAlertWithButtonName(title: title, message: message , buttonName: buttonName) { (action) in
  76. if alert.callback != nil {
  77. let callJs = "\(alert.callback!)()"
  78. self.evaluateJs(callBackJs: callJs)
  79. }
  80. }
  81. }else {
  82. DDLogError("alert, 解析json失败")
  83. }
  84. }
  85. //o2m.notification.confirm
  86. private func confirm(json: String) {
  87. DDLogDebug("confirm:\(json)")
  88. if let alert = O2NotificationMessage<O2NotificationConfirm>.deserialize(from: json) {
  89. let title = alert.data?.title ?? ""
  90. let message = alert.data?.message ?? ""
  91. let buttons = alert.data?.buttonLabels ?? ["确定", "取消"]
  92. if buttons.count != 2 {
  93. self.viewController.showError(title: "确认框按钮个数不正确!")
  94. return
  95. }
  96. let okAction = UIAlertAction(title: buttons[0], style: .default) { (ok) in
  97. if alert.callback != nil {
  98. let callJs = "\(alert.callback!)(0)"
  99. self.evaluateJs(callBackJs: callJs)
  100. }
  101. }
  102. let cancelAction = UIAlertAction(title: buttons[1], style: .cancel) { (cancel) in
  103. if alert.callback != nil {
  104. let callJs = "\(alert.callback!)(1)"
  105. self.evaluateJs(callBackJs: callJs)
  106. }
  107. }
  108. self.viewController.showDefaultConfirm(title: title, message: message, okAction: okAction, cancelAction: cancelAction)
  109. }else {
  110. DDLogError("confirm , 解析json失败")
  111. }
  112. }
  113. //o2m.notification.prompt
  114. private func prompt(json: String) {
  115. DDLogDebug("prompt:\(json)")
  116. if let alert = O2NotificationMessage<O2NotificationConfirm>.deserialize(from: json) {
  117. let title = alert.data?.title ?? ""
  118. let message = alert.data?.message ?? ""
  119. let buttons = alert.data?.buttonLabels ?? ["确定", "取消"]
  120. if buttons.count != 2 {
  121. self.viewController.showError(title: "回复框按钮个数不正确!")
  122. return
  123. }
  124. let promptController = UIAlertController(title: title, message: message, preferredStyle: .alert)
  125. promptController.addTextField { (textField) in
  126. textField.placeholder = "测试"
  127. }
  128. let okAction = UIAlertAction(title: buttons[0], style: .default) { (ok) in
  129. if alert.callback != nil {
  130. let value = promptController.textFields?.first?.text ?? ""
  131. let json = "{buttonIndex: 0, value: \"\(value)\"}"
  132. let callJs = "\(alert.callback!)('\(json)')"
  133. self.evaluateJs(callBackJs: callJs)
  134. }
  135. }
  136. let cancelAction = UIAlertAction(title: buttons[1], style: .cancel) { (cancel) in
  137. if alert.callback != nil {
  138. let value = promptController.textFields?[0].text ?? ""
  139. let json = "{buttonIndex: 1, value: \"\(value)\"}"
  140. let callJs = "\(alert.callback!)('\(json)')"
  141. self.evaluateJs(callBackJs: callJs)
  142. }
  143. }
  144. promptController.addAction(okAction)
  145. promptController.addAction(cancelAction)
  146. self.viewController.present(promptController, animated: true, completion: nil)
  147. }else {
  148. DDLogError("prompt , 解析json失败")
  149. }
  150. }
  151. //o2m.notification.vibrate
  152. private func vibrate(json: String) {
  153. DDLogDebug("vibrate:\(json)")
  154. if let alert = O2NotificationMessage<O2NotificationToast>.deserialize(from: json) {
  155. //这个代码好像对机器有要求 6s以上的手机才行?
  156. // if #available(iOS 10.0, *) {
  157. // DDLogDebug("vibrate after iOS 10.0")
  158. // UIImpactFeedbackGenerator.init(style: .medium).impactOccurred()
  159. // }else {
  160. DDLogDebug("vibrate before iOS 10.0")
  161. let soundID = SystemSoundID(kSystemSoundID_Vibrate)
  162. AudioServicesPlaySystemSound(soundID)
  163. // }
  164. if alert.callback != nil {
  165. let callJs = "\(alert.callback!)()"
  166. self.evaluateJs(callBackJs: callJs)
  167. }
  168. }else {
  169. DDLogError("vibrate , 解析json失败")
  170. }
  171. }
  172. //o2m.notification.toast
  173. private func toast(json: String) {
  174. DDLogDebug("toast:\(json)")
  175. if let alert = O2NotificationMessage<O2NotificationToast>.deserialize(from: json) {
  176. let message = alert.data?.message ?? ""
  177. self.viewController.showSuccess(title: message)
  178. if alert.callback != nil {
  179. let callJs = "\(alert.callback!)()"
  180. self.evaluateJs(callBackJs: callJs)
  181. }
  182. }else {
  183. DDLogError("toast , 解析json失败")
  184. }
  185. }
  186. //o2m.notification.actionSheet
  187. private func actionSheet(json: String) {
  188. DDLogDebug("actionSheet:\(json)")
  189. if let alert = O2NotificationMessage<O2NotificationActionSheet>.deserialize(from: json) {
  190. let title = alert.data?.title ?? ""
  191. let cancelButton = alert.data?.cancelButton ?? "取消"
  192. let otherButtons = alert.data?.otherButtons ?? []
  193. if otherButtons.count < 1 {
  194. self.viewController.showError(title: "列表按钮个数必须大于0!")
  195. return
  196. }
  197. var actions : [UIAlertAction] = []
  198. for (index, text) in otherButtons.enumerated() {
  199. let okAction = UIAlertAction(title: text, style: .default) { (ok) in
  200. if alert.callback != nil {
  201. let callJs = "\(alert.callback!)(\(index))"
  202. self.evaluateJs(callBackJs: callJs)
  203. }
  204. }
  205. actions.append(okAction)
  206. }
  207. let cancelAction = UIAlertAction(title: cancelButton, style: .cancel) { (cancel) in
  208. if alert.callback != nil {
  209. let callJs = "\(alert.callback!)(-1)"
  210. self.evaluateJs(callBackJs: callJs)
  211. }
  212. }
  213. actions.append(cancelAction)
  214. self.viewController.showActionSheetIncludeCancelBtn(title: "", message: title, actions: actions)
  215. }else {
  216. DDLogError("actionSheet , 解析json失败")
  217. }
  218. }
  219. //o2m.notification.showLoading
  220. private func showLoading(json: String) {
  221. DDLogDebug("showLoading:\(json)")
  222. if let alert = O2NotificationMessage<O2NotificationLoading>.deserialize(from: json) {
  223. let text = alert.data?.text ?? "Loading..."
  224. self.viewController.showLoading(title: text)
  225. if alert.callback != nil {
  226. let callJs = "\(alert.callback!)()"
  227. self.evaluateJs(callBackJs: callJs)
  228. }
  229. }else {
  230. DDLogError("toast , 解析json失败")
  231. }
  232. }
  233. //o2m.notification.hideLoading
  234. private func hideLoading(json: String) {
  235. DDLogDebug("hideLoading:\(json)")
  236. self.viewController.hideLoading()
  237. if let alert = O2NotificationMessage<O2NotificationLoading>.deserialize(from: json) {
  238. if alert.callback != nil {
  239. let callJs = "\(alert.callback!)()"
  240. self.evaluateJs(callBackJs: callJs)
  241. }
  242. }else {
  243. DDLogError("toast , 解析json失败")
  244. }
  245. }
  246. private func evaluateJs(callBackJs: String) {
  247. DDLogDebug("执行回调js:"+callBackJs)
  248. self.viewController.webView.evaluateJavaScript(callBackJs, completionHandler: { (result, err) in
  249. DDLogDebug("回调js执行完成!")
  250. })
  251. }
  252. }