ContactPersonInfoV2ViewController.swift 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. //
  2. // ContactPersonInfoV2ViewController.swift
  3. // O2Platform
  4. //
  5. // Created by 程剑 on 2017/7/11.
  6. // Copyright © 2017年 zoneland. All rights reserved.
  7. //
  8. import UIKit
  9. import Alamofire
  10. import AlamofireImage
  11. import AlamofireObjectMapper
  12. import SwiftyJSON
  13. import ObjectMapper
  14. import Eureka
  15. import CocoaLumberjack
  16. class ContactPersonInfoV2ViewController: UITableViewController {
  17. @IBOutlet weak var beijingImg: UIImageView!
  18. @IBOutlet weak var personImg: UIImageView!
  19. @IBOutlet weak var personName: UILabel!
  20. @IBOutlet weak var personQQ: UILabel!
  21. @IBOutlet weak var personCollect: UIButton!
  22. @IBOutlet weak var personGirl: UIButton!
  23. @IBOutlet weak var personMan: UIButton!
  24. @IBOutlet weak var personCollectLab: UILabel!
  25. var isCollect = false
  26. @IBAction func collectPerson(_ sender: UIButton) {
  27. let me = O2AuthSDK.shared.myInfo()
  28. if personCollect.isSelected == true {
  29. //删除
  30. DBManager.shared.deleteContactData(contact!, (me?.id)!)
  31. }else{
  32. //增加
  33. DBManager.shared.insertContactData(contact!, (me?.id)!)
  34. }
  35. personCollect.isSelected = !personCollect.isSelected
  36. }
  37. let nameLabs = [L10n.Contacts.enterpriseInformation, L10n.Contacts.personName, L10n.Contacts.employeeNumber,
  38. L10n.Contacts.uniqueCode, L10n.Contacts.contactNumber, L10n.Contacts.email, L10n.Contacts.dept
  39. , L10n.Contacts.officePhone, L10n.Contacts.superior, L10n.Contacts.boardDate, L10n.Contacts.description]
  40. var myPersonURL:String?
  41. var identity:IdentityV2? {
  42. didSet {
  43. self.myPersonURL = AppDelegate.o2Collect.generateURLWithAppContextKey(ContactContext.contactsContextKeyV2, query: ContactContext.personInfoByNameQuery, parameter: ["##name##":identity!.person! as AnyObject])
  44. }
  45. }
  46. var person:PersonV2?{
  47. didSet {
  48. self.myPersonURL = AppDelegate.o2Collect.generateURLWithAppContextKey(ContactContext.contactsContextKeyV2, query: ContactContext.personInfoByNameQuery, parameter: ["##name##":person!.id! as AnyObject])
  49. }
  50. }
  51. var isLoadPerson:Bool = true
  52. // 个人信息
  53. var contact:PersonV2? {
  54. didSet {
  55. isLoadPerson = false
  56. }
  57. }
  58. // 个人属性
  59. var attributes: [PersonAttribute] = []
  60. // 包含用户信息 和 个人属性的列表 展现
  61. var personInfoList: [PersonInfoWithAttributes] = []
  62. //im 聊天
  63. private lazy var viewModel: IMViewModel = {
  64. return IMViewModel()
  65. }()
  66. override func viewDidLoad() {
  67. super.viewDidLoad()
  68. self.beijingImg.theme_image = ThemeImagePicker(keyPath: "Icon.pic_beijing1")
  69. self.personImg.layer.cornerRadius = self.personImg.frame.size.width / 2
  70. self.personImg.clipsToBounds = true
  71. loadPersonInfo(nil)
  72. let startChatButton = OOBaseUIButton(x: (kScreenW - 260)/2, y: 5, w: 260, h: 30, target: self, action: #selector(_startChat))
  73. startChatButton.setTitle(L10n.Contacts.initiateChat, for: .normal)
  74. let btnContainerView = UIView(frame: CGRect(x: 0, y: 0, width: kScreenW, height: 40))
  75. btnContainerView.addSubview(startChatButton)
  76. tableView.tableFooterView = btnContainerView
  77. }
  78. override func viewWillAppear(_ animated: Bool) {
  79. super.viewWillAppear(animated)
  80. //去掉nav底部分割线
  81. self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
  82. self.navigationController?.navigationBar.shadowImage = UIImage()
  83. }
  84. override func didReceiveMemoryWarning() {
  85. super.didReceiveMemoryWarning()
  86. // Dispose of any resources that can be recreated.
  87. }
  88. @objc private func _startChat() {
  89. var username = ""
  90. if self.contact != nil {
  91. username = self.contact?.distinguishedName ?? ""
  92. }else if self.person != nil {
  93. username = self.person?.distinguishedName ?? ""
  94. }
  95. if username == "" {
  96. self.showError(title: L10n.Contacts.unableToCreatChat)
  97. return
  98. }
  99. self.viewModel.createConversation(type: o2_im_conversation_type_single, users: [username]).then { (conv) in
  100. let chatView = IMChatViewController()
  101. chatView.conversation = conv
  102. self.navigationController?.pushViewController(chatView, animated: true)
  103. }.catch { (err) in
  104. self.showError(title: L10n.errorWithMsg(err.localizedDescription))
  105. }
  106. }
  107. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  108. // #warning Incomplete implementation, return the number of rows
  109. return personInfoList.count
  110. }
  111. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  112. let cell = tableView.dequeueReusableCell(withIdentifier: "personInfoCell", for: indexPath) as! ContactPersonInfoCell
  113. //
  114. let data = self.personInfoList[indexPath.row]
  115. if data.infoType == 0 {
  116. cell.nameLab.text = data.name
  117. switch data.name {
  118. case L10n.Contacts.enterpriseInformation:
  119. cell.nameLab.font = UIFont.systemFont(ofSize: 17)
  120. cell.nameLab.textColor = UIColor.black
  121. cell.valueLab.isHidden = true
  122. cell.eventBut.isHidden = true
  123. case L10n.Contacts.personName:
  124. cell.valueLab.text = self.contact?.name
  125. cell.eventBut.isHidden = true
  126. case L10n.Contacts.employeeNumber:
  127. cell.valueLab.text = self.contact?.employee
  128. cell.eventBut.isHidden = true
  129. case L10n.Contacts.uniqueCode:
  130. cell.valueLab.text = self.contact?.unique
  131. cell.eventBut.isHidden = true
  132. case L10n.Contacts.contactNumber:
  133. if OrganizationPermissionManager.shared.isHiddenMobile(person: self.contact?.distinguishedName ?? "") {
  134. cell.valueLab.text = "***********"
  135. } else {
  136. cell.valueLab.text = self.contact?.mobile
  137. // cell.eventBut.addTarget(self, action: #selector(self.call), for: UIControl.Event.touchUpInside)
  138. }
  139. case L10n.Contacts.email:
  140. cell.valueLab.text = self.contact?.mail
  141. cell.eventBut.theme_setImage(ThemeImagePicker(keyPath:"Icon.icon_email"), forState: .normal)
  142. cell.eventBut.addTarget(self, action: #selector(self.sendMail), for: UIControl.Event.touchUpInside)
  143. case L10n.Contacts.dept:
  144. var unitName = ""
  145. if let idenList = self.contact?.woIdentityList {
  146. for iden in idenList {
  147. if let unit = iden.woUnit {
  148. if unitName != "" {
  149. unitName.append(";")
  150. }
  151. unitName.append(unit.name ?? "")
  152. }
  153. }
  154. }
  155. cell.valueLab.text = unitName
  156. cell.eventBut.isHidden = true
  157. case L10n.Contacts.officePhone:
  158. cell.valueLab.text = self.contact?.officePhone
  159. cell.eventBut.isHidden = true
  160. case L10n.Contacts.superior:
  161. cell.valueLab.text = self.contact?.superior
  162. cell.eventBut.isHidden = true
  163. case L10n.Contacts.boardDate:
  164. cell.valueLab.text = self.contact?.boardDate
  165. cell.eventBut.isHidden = true
  166. case L10n.Contacts.description:
  167. cell.valueLab.text = self.contact?.desc
  168. cell.eventBut.isHidden = true
  169. case L10n.Contacts.personAttributes:
  170. cell.nameLab.font = UIFont.systemFont(ofSize: 17)
  171. cell.nameLab.textColor = UIColor.black
  172. cell.nameLab.text = L10n.Contacts.personAttributes
  173. cell.valueLab.isHidden = true
  174. cell.eventBut.isHidden = true
  175. default:
  176. break
  177. }
  178. } else if data.infoType == 1 {
  179. if let attr = data.attr {
  180. cell.nameLab.text = attr.name
  181. // cell.nameLab.font = UIFont.systemFont(ofSize: 17)
  182. // cell.nameLab.textColor = UIColor.black
  183. cell.valueLab.text = attr.attributeList?.joined(separator: ", ")
  184. cell.eventBut.isHidden = true
  185. }
  186. }
  187. return cell
  188. }
  189. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  190. switch self.nameLabs[indexPath.row] {
  191. case L10n.Contacts.contactNumber:
  192. if !OrganizationPermissionManager.shared.isHiddenMobile(person: self.contact?.distinguishedName ?? "") {
  193. self.call()
  194. }
  195. case L10n.Contacts.email:
  196. self.sendMail()
  197. default:
  198. break
  199. }
  200. self.tableView.deselectRow(at: indexPath, animated: true)
  201. }
  202. @objc func sendMail() {
  203. if let mail = self.contact?.mail, mail != "" {
  204. let alertController = UIAlertController(title: "", message: nil,preferredStyle: .actionSheet)
  205. let mailAction = UIAlertAction(title:L10n.Contacts.sendEmail, style: .default, handler: { _ in
  206. let mailURL = URL(string: "mailto://\(mail)")
  207. if UIApplication.shared.canOpenURL(mailURL!) {
  208. UIApplication.shared.openURL(mailURL!)
  209. }else{
  210. self.showError(title: L10n.Contacts.sendEmailError)
  211. }
  212. })
  213. let copyAction = UIAlertAction(title:L10n.copy, style: .default, handler: { _ in
  214. UIPasteboard.general.string = mail
  215. self.showSuccess(title: L10n.copySuccess)
  216. })
  217. let cancelAction = UIAlertAction(title: L10n.cancel, style: .cancel, handler: nil)
  218. alertController.addAction(mailAction)
  219. alertController.addAction(copyAction)
  220. alertController.addAction(cancelAction)
  221. self.present(alertController, animated: true, completion: nil)
  222. }
  223. }
  224. @objc func call(){
  225. if let phone = self.contact?.mobile, phone != "" {
  226. let alertController = UIAlertController(title: "", message: nil,preferredStyle: .actionSheet)
  227. let smsAction = UIAlertAction(title: L10n.sms, style: .default, handler: { _ in
  228. let smsURL = URL(string: "sms://\(phone)")
  229. if UIApplication.shared.canOpenURL(smsURL!) {
  230. UIApplication.shared.openURL(smsURL!)
  231. }else{
  232. self.showError(title: L10n.smsFail)
  233. }
  234. })
  235. let phoneAction = UIAlertAction(title: L10n.call, style: .default, handler: { _ in
  236. let phoneURL = URL(string: "tel://\(phone)")
  237. if UIApplication.shared.canOpenURL(phoneURL!) {
  238. UIApplication.shared.openURL(phoneURL!)
  239. }else{
  240. self.showError(title: L10n.callFail)
  241. }
  242. })
  243. let copyAction = UIAlertAction(title: L10n.copy, style: .default, handler: { _ in
  244. UIPasteboard.general.string = phone
  245. self.showSuccess(title: L10n.copySuccess)
  246. })
  247. let cancelAction = UIAlertAction(title: L10n.cancel, style: .cancel, handler: nil)
  248. alertController.addAction(phoneAction)
  249. alertController.addAction(smsAction)
  250. alertController.addAction(copyAction)
  251. alertController.addAction(cancelAction)
  252. self.present(alertController, animated: true, completion: nil)
  253. }
  254. }
  255. func loadPersonInfo(_ sender: AnyObject?){
  256. self.showLoading()
  257. AF.request(myPersonURL!).responseJSON {
  258. response in
  259. switch response.result {
  260. case .success( let val):
  261. let json = JSON(val)["data"]
  262. self.contact = Mapper<PersonV2>().map(JSONString:json.description)!
  263. self.attributes = self.contact?.woPersonAttributeList ?? []
  264. // 默认的用户信息
  265. self.nameLabs.forEach { (label) in
  266. let attr = PersonInfoWithAttributes(infoType: 0, name: label, attr: nil)
  267. self.personInfoList.append(attr)
  268. }
  269. // 个人属性
  270. if !self.attributes.isEmpty {
  271. let attr = PersonInfoWithAttributes(infoType: 0, name: L10n.Contacts.personAttributes, attr: nil)
  272. self.personInfoList.append(attr)
  273. self.attributes.forEach { (pAttr) in
  274. if pAttr.name != "appBindDeviceList" {
  275. let p = PersonInfoWithAttributes(infoType: 1, name: pAttr.name, attr: pAttr)
  276. self.personInfoList.append(p)
  277. }
  278. }
  279. }
  280. //OOCon
  281. let me = O2AuthSDK.shared.myInfo()
  282. self.isCollect = DBManager.shared.isCollect(self.contact!, (me?.id)!)
  283. if self.isCollect == true {
  284. self.personCollect.isSelected = true
  285. }else{
  286. self.personCollect.isSelected = false
  287. }
  288. self.personName.text = self.contact?.name
  289. if let qq = self.contact?.qq, qq != "" {
  290. self.personQQ.text = "QQ \(qq)"
  291. }else{
  292. self.personQQ.text = ""
  293. }
  294. if let gt = self.contact?.genderType, gt == "f" {
  295. self.personGirl.setImage(UIImage(named: "icon_girl_2"), for: .normal)
  296. }else{
  297. self.personMan.setImage(UIImage(named: "icon_boy_2"), for: .normal)
  298. }
  299. let urlstr = AppDelegate.o2Collect.generateURLWithAppContextKey(ContactContext.contactsContextKeyV2, query: ContactContext.personIconByNameQueryV2, parameter: ["##name##":self.contact?.unique as AnyObject], generateTime: false)
  300. let url = URL(string: urlstr!)
  301. self.personImg.hnk_setImageFromURL(url!)
  302. DispatchQueue.main.async {
  303. self.hideLoading()
  304. self.tableView.reloadData()
  305. }
  306. case .failure(let err):
  307. DDLogError(err.localizedDescription)
  308. DispatchQueue.main.async {
  309. self.showError(title: L10n.errorWithMsg(err.localizedDescription))
  310. }
  311. }
  312. }
  313. }
  314. }