O2JsApiBizUtil.swift 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. //
  2. // O2JsApiBizUtil.swift
  3. // O2Platform
  4. //
  5. // Created by FancyLou on 2019/8/19.
  6. // Copyright © 2019 zoneland. All rights reserved.
  7. //
  8. import UIKit
  9. import WebKit
  10. import CocoaLumberjack
  11. class O2JsApiBizUtil: 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 "contact.departmentPicker":
  25. departmentsPicker(json: String(json))
  26. break
  27. case "contact.identityPicker":
  28. identityPicker(json: String(json))
  29. break
  30. case "contact.groupPicker":
  31. groupPicker(json: String(json))
  32. break
  33. case "contact.personPicker":
  34. personPicker(json: String(json))
  35. break
  36. case "contact.complexPicker":
  37. complexPicker(json: String(json))
  38. break
  39. default :
  40. DDLogError("notification类型不正确, type: \(type)")
  41. }
  42. }else {
  43. DDLogError("util类型不存在 json解析异常。。。。。")
  44. }
  45. }else {
  46. DDLogError("消息json解析异常。。。")
  47. }
  48. }else {
  49. DDLogError("message 消息 body 类型不正确。。。")
  50. }
  51. }
  52. private func departmentsPicker(json: String) {
  53. if let alert = O2WebViewBaseMessage<O2BizUnitPickerMessage>.deserialize(from: json) {
  54. let maxNumber = alert.data?.maxNumber ?? 0
  55. let pickedDepartments = alert.data?.pickedDepartments ?? []
  56. let multiple = alert.data?.multiple ?? true
  57. let orgType = alert.data?.orgType ?? ""
  58. let topList = alert.data?.topList ?? []
  59. let callback = alert.callback ?? ""
  60. self.showPicker(callback: callback,
  61. pickMode: ["departmentPicker"],
  62. maxNumber: maxNumber,
  63. multiple: multiple,
  64. orgType: orgType,
  65. topList: topList,
  66. deptPickedList: pickedDepartments)
  67. }else {
  68. DDLogError("departmentsPicker, 解析json失败")
  69. }
  70. }
  71. private func identityPicker(json: String) {
  72. if let alert = O2WebViewBaseMessage<O2BizIdentityPickerMessage>.deserialize(from: json) {
  73. let maxNumber = alert.data?.maxNumber ?? 0
  74. let pickedIdentities = alert.data?.pickedIdentities ?? []
  75. let multiple = alert.data?.multiple ?? true
  76. let dutyList = alert.data?.duty ?? []
  77. let topList = alert.data?.topList ?? []
  78. let callback = alert.callback ?? ""
  79. self.showPicker(callback: callback,
  80. pickMode: ["identityPicker"],
  81. maxNumber: maxNumber,
  82. multiple: multiple,
  83. dutyList: dutyList,
  84. topList: topList,
  85. idPickedList: pickedIdentities)
  86. }else {
  87. DDLogError("identityPicker, 解析json失败")
  88. }
  89. }
  90. private func groupPicker(json: String) {
  91. if let alert = O2WebViewBaseMessage<O2BizGroupPickerMessage>.deserialize(from: json) {
  92. let maxNumber = alert.data?.maxNumber ?? 0
  93. let pickedGroups = alert.data?.pickedGroups ?? []
  94. let multiple = alert.data?.multiple ?? true
  95. let callback = alert.callback ?? ""
  96. self.showPicker(callback: callback,
  97. pickMode: ["groupPicker"],
  98. maxNumber: maxNumber,
  99. multiple: multiple,
  100. groupPickedList: pickedGroups)
  101. }else {
  102. DDLogError("groupPicker, 解析json失败")
  103. }
  104. }
  105. private func personPicker(json: String) {
  106. if let alert = O2WebViewBaseMessage<O2BizPersonPickerMessage>.deserialize(from: json) {
  107. let maxNumber = alert.data?.maxNumber ?? 0
  108. let pickedUsers = alert.data?.pickedUsers ?? []
  109. let multiple = alert.data?.multiple ?? true
  110. let callback = alert.callback ?? ""
  111. self.showPicker(callback: callback,
  112. pickMode: ["personPicker"],
  113. maxNumber: maxNumber,
  114. multiple: multiple,
  115. userPickedList: pickedUsers)
  116. }else {
  117. DDLogError("personPicker, 解析json失败")
  118. }
  119. }
  120. private func complexPicker(json: String) {
  121. if let alert = O2WebViewBaseMessage<O2BizComplexPickerMessage>.deserialize(from: json) {
  122. let pickMode = alert.data?.pickMode ?? []
  123. let maxNumber = alert.data?.maxNumber ?? 0
  124. let pickedDepartments = alert.data?.pickedDepartments ?? []
  125. let pickedIdentities = alert.data?.pickedIdentities ?? []
  126. let pickedGroups = alert.data?.pickedGroups ?? []
  127. let pickedUsers = alert.data?.pickedUsers ?? []
  128. let multiple = alert.data?.multiple ?? true
  129. let orgType = alert.data?.orgType ?? ""
  130. let dutyList = alert.data?.duty ?? []
  131. let topList = alert.data?.topList ?? []
  132. let callback = alert.callback ?? ""
  133. self.showPicker(callback: callback,
  134. pickMode: pickMode,
  135. maxNumber: maxNumber,
  136. multiple: multiple,
  137. orgType: orgType,
  138. dutyList: dutyList,
  139. topList: topList,
  140. deptPickedList: pickedDepartments,
  141. idPickedList: pickedIdentities,
  142. groupPickedList: pickedGroups,
  143. userPickedList: pickedUsers
  144. )
  145. }else {
  146. DDLogError("complexPicker, 解析json失败")
  147. }
  148. }
  149. private func showPicker(callback: String, pickMode:[String], maxNumber: Int = 0, multiple:Bool = true, orgType: String = "", dutyList:[String] = [], topList:[String] = [], deptPickedList:[String] = [], idPickedList:[String] = [], groupPickedList:[String] = [], userPickedList:[String] = []) {
  150. var modes:[ContactPickerType] = []
  151. if pickMode.count > 0 {
  152. pickMode.forEach { (str) in
  153. switch str {
  154. case "departmentPicker":
  155. modes.append(ContactPickerType.unit)
  156. break
  157. case "identityPicker":
  158. modes.append(ContactPickerType.identity)
  159. break
  160. case "groupPicker":
  161. modes.append(ContactPickerType.group)
  162. break
  163. case "personPicker":
  164. modes.append(ContactPickerType.person)
  165. break
  166. default:
  167. break
  168. }
  169. }
  170. }else {
  171. modes = [ContactPickerType.unit, ContactPickerType.identity, ContactPickerType.group, ContactPickerType.person]
  172. }
  173. if let v = ContactPickerViewController.providePickerVC(
  174. pickerModes:modes,
  175. topUnitList: topList,
  176. unitType: orgType,
  177. maxNumber: maxNumber,
  178. multiple: multiple,
  179. dutyList: dutyList,
  180. initDeptPickedArray: deptPickedList,
  181. initIdPickedArray: idPickedList,
  182. initGroupPickedArray: groupPickedList,
  183. initUserPickedArray: userPickedList,
  184. pickedDelegate: { (result: O2BizContactPickerResult) in
  185. let json = result.toJSONString() ?? "{}"
  186. DDLogDebug("返回选择结果:\(json)")
  187. self.evaluateJs(callBackJs: "\(callback)('\(json)')")
  188. }
  189. ) {
  190. self.viewController.navigationController?.pushViewController(v, animated: true)
  191. }else {
  192. self.viewController.showError(title: "选择器生成错误。。。。")
  193. }
  194. }
  195. private func evaluateJs(callBackJs: String) {
  196. DDLogDebug("执行回调js:"+callBackJs)
  197. self.viewController.webView.evaluateJavaScript(callBackJs, completionHandler: { (result, err) in
  198. DDLogDebug("回调js执行完成!")
  199. })
  200. }
  201. }