O2JsApiNotification.swift 11 KB

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