ContactPickerViewController.swift 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. //
  2. // ContactPickerViewController.swift
  3. // O2Platform
  4. //
  5. // Created by FancyLou on 2019/8/12.
  6. // Copyright © 2019 zoneland. All rights reserved.
  7. //
  8. import UIKit
  9. import CocoaLumberjack
  10. enum ContactPickerType {
  11. case unit
  12. case identity
  13. case group
  14. case person
  15. }
  16. typealias DidPickedContact = (_ result: O2BizContactPickerResult) -> Void ///< 定义确认回调
  17. class ContactPickerViewController: UIViewController {
  18. static func providePickerVC(
  19. pickerModes:[ContactPickerType],
  20. topUnitList: [String] = [],
  21. unitType: String = "",
  22. maxNumber: Int = 0,
  23. multiple: Bool = true,
  24. dutyList:[String] = [],
  25. initDeptPickedArray:[String] = [],
  26. initIdPickedArray:[String] = [],
  27. initGroupPickedArray:[String] = [],
  28. initUserPickedArray:[String] = [],
  29. pickedDelegate: @escaping DidPickedContact
  30. ) -> ContactPickerViewController? {
  31. if pickerModes.count < 1 {
  32. DDLogError("没有选择器类型")
  33. return nil
  34. }
  35. let storyBoard = UIStoryboard(name: "Contacts_new", bundle: nil)
  36. let destVC = storyBoard.instantiateViewController(withIdentifier: "contactPicker") as? ContactPickerViewController
  37. destVC?.selectorList = pickerModes
  38. if topUnitList.count > 0 {
  39. destVC?.topUnitList = topUnitList
  40. }
  41. if !unitType.isEmpty {
  42. destVC?.unitType = unitType
  43. }
  44. if maxNumber > 0 {
  45. destVC?.maxNumber = maxNumber
  46. }
  47. destVC?.multiple = multiple
  48. if dutyList.count > 0 {
  49. destVC?.dutyList = dutyList
  50. }
  51. if initDeptPickedArray.count > 0 {
  52. destVC?.initDeptPickedArray = initDeptPickedArray
  53. }
  54. if initIdPickedArray.count > 0 {
  55. destVC?.initIdPickedArray = initIdPickedArray
  56. }
  57. if initGroupPickedArray.count > 0 {
  58. destVC?.initGroupPickedArray = initGroupPickedArray
  59. }
  60. if initUserPickedArray.count > 0 {
  61. destVC?.initUserPickedArray = initUserPickedArray
  62. }
  63. destVC?.pickedDelegate = pickedDelegate
  64. return destVC
  65. }
  66. @IBOutlet weak var topBarStackView: UIStackView!
  67. @IBOutlet weak var pickerContainerView: UIView!
  68. @IBOutlet weak var topBarStackViewHeightConstraint: NSLayoutConstraint!
  69. //各个初始化参数
  70. var selectorList:[ContactPickerType] = [] //选择器 多值
  71. var topUnitList: [String] = [] //顶级组织
  72. var unitType: String = "" //组织类型 查询组织用的
  73. var maxNumber = 0 //可选择的最大数量
  74. var multiple = true //是否多选
  75. var dutyList:[String] = [] //身份查询的时候的限制的职务列表
  76. var initDeptPickedArray:[String] = [] //初始 已选择的数据
  77. var initIdPickedArray:[String] = [] //初始 已选择的数据
  78. var initGroupPickedArray:[String] = [] //初始 已选择的数据
  79. var initUserPickedArray:[String] = [] //初始 已选择的数据
  80. var pickedDelegate: DidPickedContact?
  81. //已经选中的值
  82. private var selectedDeptSet:[O2UnitPickerItem] = []
  83. private var selectedIdSet:[O2IdentityPickerItem] = []
  84. private var selectedGroupSet:[O2GroupPickerItem] = []
  85. private var selectedUserSet:[O2PersonPickerItem] = []
  86. //选择按钮文字
  87. private var pickBtnTitle = ""
  88. private let viewModel: ContactPickerViewModel = {
  89. return ContactPickerViewModel()
  90. }()
  91. override func viewDidLoad() {
  92. super.viewDidLoad()
  93. if initDeptPickedArray.count > 0 {
  94. initDeptPickedArray.forEach { (s) in
  95. var name = ""
  96. if s.contains("@") {
  97. name = s.split("@")[0]
  98. }else {
  99. name = s
  100. }
  101. let unit = O2UnitPickerItem()
  102. unit.distinguishedName = s
  103. unit.name = name
  104. selectedDeptSet.append(unit)
  105. }
  106. }
  107. if initIdPickedArray.count > 0 {
  108. initIdPickedArray.forEach { (s) in
  109. var name = ""
  110. if s.contains("@") {
  111. name = s.split("@")[0]
  112. }else {
  113. name = s
  114. }
  115. let id = O2IdentityPickerItem()
  116. id.distinguishedName = s
  117. id.name = name
  118. selectedIdSet.append(id)
  119. }
  120. }
  121. if initGroupPickedArray.count > 0 {
  122. initGroupPickedArray.forEach { (s) in
  123. var name = ""
  124. if s.contains("@") {
  125. name = s.split("@")[0]
  126. }else {
  127. name = s
  128. }
  129. let group = O2GroupPickerItem()
  130. group.distinguishedName = s
  131. group.name = name
  132. selectedGroupSet.append(group)
  133. }
  134. }
  135. if initUserPickedArray.count > 0 {
  136. initUserPickedArray.forEach { (s) in
  137. var name = ""
  138. if s.contains("@") {
  139. name = s.split("@")[0]
  140. }else {
  141. name = s
  142. }
  143. let person = O2PersonPickerItem()
  144. person.distinguishedName = s
  145. person.name = name
  146. selectedUserSet.append(person)
  147. }
  148. }
  149. let c = selectedDeptSet.count + selectedIdSet.count + selectedGroupSet.count + selectedUserSet.count
  150. pickBtnTitle = "选择(\(c))"
  151. if maxNumber > 0 {
  152. pickBtnTitle = "选择(\(c)/\(maxNumber))"
  153. }
  154. navigationItem.rightBarButtonItem = UIBarButtonItem(title: pickBtnTitle, style: .plain, target: self, action: #selector(selected))
  155. if selectorList.count == 1 {
  156. self.topBarStackView.isHidden = true
  157. self.topBarStackViewHeightConstraint.constant = 0.0
  158. showPicker(tag: selectorList[0].hashValue)
  159. }else {
  160. self.topBarStackView.isHidden = false
  161. self.topBarStackViewHeightConstraint.constant = 48.0
  162. topBarStackView.axis = .horizontal
  163. topBarStackView.alignment = .fill
  164. topBarStackView.spacing = 5
  165. topBarStackView.distribution = .fillEqually
  166. topBarStackView.removeSubviews()
  167. selectorList.forEach { (s) in
  168. switch(s) {
  169. case .unit:
  170. let unitBtn = generatePickerTypeBtn(title: "组织选择", type: .unit)
  171. topBarStackView.addArrangedSubview(unitBtn)
  172. case .identity:
  173. let identityBtn = generatePickerTypeBtn(title: "身份选择", type: .identity)
  174. topBarStackView.addArrangedSubview(identityBtn)
  175. case .group:
  176. let groupBtn = generatePickerTypeBtn(title: "群组选择", type: .group)
  177. topBarStackView.addArrangedSubview(groupBtn)
  178. case .person:
  179. let personBtn = generatePickerTypeBtn(title: "人员选择", type: .person)
  180. topBarStackView.addArrangedSubview(personBtn)
  181. }
  182. }
  183. if topBarStackView.subviews.count > 0 {
  184. if let button = (topBarStackView.subviews[0] as? UIButton){
  185. button.isSelected = true
  186. showPicker(tag: button.tag)
  187. }
  188. }
  189. }
  190. }
  191. // MARK: - public method 提供给外部是一哦那个
  192. // 检查值是否已经包含在选中的列表中
  193. func isSelectedValue(type: ContactPickerType, value: String) -> Bool {
  194. switch type {
  195. case .unit:
  196. var f = false
  197. self.selectedDeptSet.forEach { (item) in
  198. if item.distinguishedName == value {
  199. f = true
  200. }
  201. }
  202. return f
  203. case .identity:
  204. var f = false
  205. self.selectedIdSet.forEach { (item) in
  206. if item.distinguishedName == value {
  207. f = true
  208. }
  209. }
  210. return f
  211. case .group:
  212. var f = false
  213. self.selectedGroupSet.forEach { (item) in
  214. if item.distinguishedName == value {
  215. f = true
  216. }
  217. }
  218. return f
  219. case .person:
  220. var f = false
  221. self.selectedUserSet.forEach { (item) in
  222. if item.distinguishedName == value {
  223. f = true
  224. }
  225. }
  226. return f
  227. }
  228. }
  229. // 删除一个选中的值
  230. func removeSelectedValue(type: ContactPickerType, value: String) {
  231. switch type {
  232. case .unit:
  233. self.selectedDeptSet.removeAll { (item) -> Bool in
  234. return item.distinguishedName == value
  235. }
  236. break
  237. case .identity:
  238. self.selectedIdSet.removeAll { (item) -> Bool in
  239. return item.distinguishedName == value
  240. }
  241. break
  242. case .group:
  243. self.selectedGroupSet.removeAll { (item) -> Bool in
  244. return item.distinguishedName == value
  245. }
  246. break
  247. case .person:
  248. self.selectedUserSet.removeAll { (item) -> Bool in
  249. return item.distinguishedName == value
  250. }
  251. break
  252. }
  253. self.refreshPickButton()
  254. }
  255. // 选择一个组织
  256. func addSelectedDept(dept: OOUnitModel) {
  257. let c = selectedDeptSet.count + selectedIdSet.count + selectedGroupSet.count + selectedUserSet.count
  258. if maxNumber > 0 && c >= maxNumber {
  259. self.showError(title: "不能添加更多了!")
  260. return
  261. }
  262. let item = O2UnitPickerItem()
  263. item.copyFromUnitModel(dept: dept)
  264. self.selectedDeptSet.append(item)
  265. self.refreshPickButton()
  266. }
  267. // 选择一个身份
  268. func addSelectedIdentity(id: OOIdentityModel) {
  269. let c = selectedDeptSet.count + selectedIdSet.count + selectedGroupSet.count + selectedUserSet.count
  270. if maxNumber > 0 && c >= maxNumber {
  271. self.showError(title: "不能添加更多了!")
  272. return
  273. }
  274. let item = O2IdentityPickerItem()
  275. item.copyFromIdentityModel(identity: id)
  276. self.selectedIdSet.append(item)
  277. //异步获取用户信息 然后填充进去
  278. self.getPersonInfoFor(forType: "0", dn: id.person!)//这里的person是人员的id
  279. self.refreshPickButton()
  280. }
  281. // 选择一个群组
  282. func addSelectedGroup(group: OOGroupModel) {
  283. let c = selectedDeptSet.count + selectedIdSet.count + selectedGroupSet.count + selectedUserSet.count
  284. if maxNumber > 0 && c >= maxNumber {
  285. self.showError(title: "不能添加更多了!")
  286. return
  287. }
  288. let item = O2GroupPickerItem()
  289. item.copyFromGroupModel(group: group)
  290. self.selectedGroupSet.append(item)
  291. self.refreshPickButton()
  292. }
  293. func addSelectedPerson(id: OOIdentityModel) {
  294. let c = selectedDeptSet.count + selectedIdSet.count + selectedGroupSet.count + selectedUserSet.count
  295. if maxNumber > 0 && c >= maxNumber {
  296. self.showError(title: "不能添加更多了!")
  297. return
  298. }
  299. let item = O2PersonPickerItem()
  300. item.distinguishedName = id.person
  301. self.selectedUserSet.append(item)
  302. //异步获取用户信息 然后填充进去
  303. self.getPersonInfoFor(forType: "1", dn: id.person!)//这里的person是人员的dn
  304. self.refreshPickButton()
  305. }
  306. // MARK: - private method 当前类私有方法
  307. @objc private func selected() {
  308. let c = selectedDeptSet.count + selectedIdSet.count + selectedGroupSet.count + selectedUserSet.count
  309. DDLogDebug("选中了:\(c) 个数据")
  310. if c < 1 {
  311. self.showError(title: "请至少选择一条数据!")
  312. return
  313. }else {
  314. let result = O2BizContactPickerResult(departments: selectedDeptSet,
  315. identities: selectedIdSet,
  316. groups: selectedGroupSet,
  317. users: selectedUserSet)
  318. self.pickedDelegate?(result)
  319. self.popVC()
  320. }
  321. }
  322. //刷新选择按钮文字内容
  323. private func refreshPickButton() {
  324. let c = selectedDeptSet.count + selectedIdSet.count + selectedGroupSet.count + selectedUserSet.count
  325. pickBtnTitle = "选择(\(c))"
  326. if maxNumber > 0 {
  327. pickBtnTitle = "选择(\(c) / \(maxNumber))"
  328. }
  329. navigationItem.rightBarButtonItem?.title = pickBtnTitle
  330. }
  331. //生成选择器Tab按钮
  332. private func generatePickerTypeBtn(title: String, type: ContactPickerType) -> UIButton {
  333. let button = UIButton(type: .system)
  334. button.setTitle(title, for: .normal)
  335. button.tag = type.hashValue
  336. button.setTitleColor(toolbar_text_color, for: .normal)
  337. button.theme_setTitleColor(ThemeColorPicker(keyPath: "Base.base_color"), forState: .selected)
  338. button.tintColor = UIColor.clear
  339. button.addTarget(self, action: #selector(clickBtn(btn:)), for: .touchUpInside)
  340. return button
  341. }
  342. //点击选择器Tab按钮
  343. @objc private func clickBtn(btn: UIButton) {
  344. topBarStackView.subviews.forEach { (v) in
  345. if let b = v as? UIButton {
  346. b.isSelected = false
  347. if b.tag == btn.tag {
  348. b.isSelected = true
  349. showPicker(tag: b.tag)
  350. }
  351. }
  352. }
  353. }
  354. //显示对应的选择器内容页面
  355. private func showPicker(tag: Int) {
  356. self.pickerContainerView.removeSubviews()
  357. switch(tag) {
  358. case ContactPickerType.unit.hashValue:
  359. self.title = "组织选择"
  360. if let pickerViewController = self.storyboard?.instantiateViewController(withIdentifier: "unitPicker") as? ContactUnitPickerViewController {
  361. pickerViewController.topUnitList = self.topUnitList
  362. pickerViewController.unitType = self.unitType
  363. if self.children.contains(pickerViewController) {
  364. self.pickerContainerView.addSubview(pickerViewController.view)
  365. }else {
  366. pickerViewController.view.frame = CGRect(x: CGFloat.zero, y: CGFloat.zero, width: self.pickerContainerView.frame.width, height: self.pickerContainerView.frame.height)
  367. self.addChild(pickerViewController)
  368. self.pickerContainerView.addSubview(pickerViewController.view)
  369. }
  370. }
  371. break
  372. case ContactPickerType.identity.hashValue:
  373. self.title = "身份选择"
  374. if let pickerViewController = self.storyboard?.instantiateViewController(withIdentifier: "identityPicker") as? ContactIdentityPickerViewController {
  375. pickerViewController.dutyList = self.dutyList
  376. pickerViewController.topUnitList = self.topUnitList
  377. pickerViewController.backResultIsUser = false
  378. if self.children.contains(pickerViewController) {
  379. self.pickerContainerView.addSubview(pickerViewController.view)
  380. }else {
  381. pickerViewController.view.frame = CGRect(x: CGFloat.zero, y: CGFloat.zero, width: self.pickerContainerView.frame.width, height: self.pickerContainerView.frame.height)
  382. self.addChild(pickerViewController)
  383. self.pickerContainerView.addSubview(pickerViewController.view)
  384. }
  385. }
  386. break
  387. case ContactPickerType.group.hashValue:
  388. self.title = "群组选择"
  389. if let pickerViewController = self.storyboard?.instantiateViewController(withIdentifier: "groupPicker") as? ContactGroupPickerViewController {
  390. if self.children.contains(pickerViewController) {
  391. self.pickerContainerView.addSubview(pickerViewController.view)
  392. }else {
  393. pickerViewController.view.frame = CGRect(x: CGFloat.zero, y: CGFloat.zero, width: self.pickerContainerView.frame.width, height: self.pickerContainerView.frame.height)
  394. self.addChild(pickerViewController)
  395. self.pickerContainerView.addSubview(pickerViewController.view)
  396. }
  397. }
  398. break
  399. case ContactPickerType.person.hashValue:
  400. self.title = "人员选择"
  401. if let pickerViewController = self.storyboard?.instantiateViewController(withIdentifier: "identityPicker") as? ContactIdentityPickerViewController {
  402. pickerViewController.dutyList = self.dutyList
  403. pickerViewController.topUnitList = self.topUnitList
  404. pickerViewController.backResultIsUser = true
  405. if self.children.contains(pickerViewController) {
  406. self.pickerContainerView.addSubview(pickerViewController.view)
  407. }else {
  408. pickerViewController.view.frame = CGRect(x: CGFloat.zero, y: CGFloat.zero, width: self.pickerContainerView.frame.width, height: self.pickerContainerView.frame.height)
  409. self.addChild(pickerViewController)
  410. self.pickerContainerView.addSubview(pickerViewController.view)
  411. }
  412. }
  413. //
  414. //
  415. // if let pickerViewController = self.storyboard?.instantiateViewController(withIdentifier: "personPicker") as? ContactPersonPickerViewController {
  416. // if self.children.contains(pickerViewController) {
  417. // self.pickerContainerView.addSubview(pickerViewController.view)
  418. // }else {
  419. // pickerViewController.view.frame = CGRect(x: 0, y: 0, w: self.pickerContainerView.frame.width, h: self.pickerContainerView.frame.height)
  420. // self.addChild(pickerViewController)
  421. // self.pickerContainerView.addSubview(pickerViewController.view)
  422. // }
  423. // }
  424. break
  425. default:
  426. DDLogDebug("click unkown")
  427. }
  428. }
  429. // forType 0:身份填充 1:用户填充
  430. private func getPersonInfoFor(forType: String, dn: String) {
  431. viewModel.getPersonInfo(dn: dn).then { (person) in
  432. if (forType == "0") {
  433. self.selectedIdSet.first(where: { $0.person == person.distinguishedName })?.updatePersonInfo(person: person)
  434. }else {
  435. self.selectedUserSet.first(where: { $0.distinguishedName == person.distinguishedName })?.copyFromPersonModel(person: person)
  436. }
  437. }
  438. }
  439. /*
  440. // MARK: - Navigation
  441. // In a storyboard-based application, you will often want to do a little preparation before navigation
  442. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  443. // Get the new view controller using segue.destination.
  444. // Pass the selected object to the new view controller.
  445. }
  446. */
  447. }