ContactPersonInfoV2ViewController.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 = ["企业信息","姓名","员工号","唯一编码","联系电话","电子邮件","部门"]
  38. var myPersonURL:String?
  39. var identity:IdentityV2? {
  40. didSet {
  41. self.myPersonURL = AppDelegate.o2Collect.generateURLWithAppContextKey(ContactContext.contactsContextKeyV2, query: ContactContext.personInfoByNameQuery, parameter: ["##name##":identity!.person! as AnyObject])
  42. }
  43. }
  44. var person:PersonV2?{
  45. didSet {
  46. self.myPersonURL = AppDelegate.o2Collect.generateURLWithAppContextKey(ContactContext.contactsContextKeyV2, query: ContactContext.personInfoByNameQuery, parameter: ["##name##":person!.id! as AnyObject])
  47. }
  48. }
  49. var isLoadPerson:Bool = true
  50. var contact:PersonV2? {
  51. didSet {
  52. isLoadPerson = false
  53. }
  54. }
  55. //im 聊天
  56. private lazy var viewModel: IMViewModel = {
  57. return IMViewModel()
  58. }()
  59. override func viewDidLoad() {
  60. super.viewDidLoad()
  61. self.beijingImg.theme_image = ThemeImagePicker(keyPath: "Icon.pic_beijing1")
  62. self.personImg.layer.cornerRadius = self.personImg.frame.size.width / 2
  63. self.personImg.clipsToBounds = true
  64. loadPersonInfo(nil)
  65. let startChatButton = OOBaseUIButton(x: (kScreenW - 260)/2, y: 5, w: 260, h: 30, target: self, action: #selector(_startChat))
  66. startChatButton.setTitle("发起聊天", for: .normal)
  67. let btnContainerView = UIView(frame: CGRect(x: 0, y: 0, width: kScreenW, height: 40))
  68. btnContainerView.addSubview(startChatButton)
  69. tableView.tableFooterView = btnContainerView
  70. }
  71. override func viewWillAppear(_ animated: Bool) {
  72. super.viewWillAppear(animated)
  73. //去掉nav底部分割线
  74. self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
  75. self.navigationController?.navigationBar.shadowImage = UIImage()
  76. }
  77. override func didReceiveMemoryWarning() {
  78. super.didReceiveMemoryWarning()
  79. // Dispose of any resources that can be recreated.
  80. }
  81. @objc private func _startChat() {
  82. var username = ""
  83. if self.contact != nil {
  84. username = self.contact?.distinguishedName ?? ""
  85. }else if self.person != nil {
  86. username = self.person?.distinguishedName ?? ""
  87. }
  88. if username == "" {
  89. self.showError(title: "无法创建聊天!")
  90. return
  91. }
  92. self.viewModel.createConversation(type: o2_im_conversation_type_single, users: [username]).then { (conv) in
  93. let chatView = IMChatViewController()
  94. chatView.conversation = conv
  95. self.navigationController?.pushViewController(chatView, animated: true)
  96. }.catch { (err) in
  97. self.showError(title: "创建单聊失败, \(err.localizedDescription)")
  98. }
  99. }
  100. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  101. // #warning Incomplete implementation, return the number of rows
  102. return nameLabs.count
  103. }
  104. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  105. let cell = tableView.dequeueReusableCell(withIdentifier: "personInfoCell", for: indexPath) as! ContactPersonInfoCell
  106. cell.nameLab.text = self.nameLabs[indexPath.row]
  107. switch self.nameLabs[indexPath.row] {
  108. case "企业信息":
  109. cell.nameLab.font = UIFont.systemFont(ofSize: 17)
  110. cell.nameLab.textColor = UIColor.black
  111. cell.valueLab.isHidden = true
  112. cell.eventBut.isHidden = true
  113. case "姓名":
  114. cell.valueLab.text = self.contact?.name
  115. cell.eventBut.isHidden = true
  116. case "员工号":
  117. cell.valueLab.text = self.contact?.employee
  118. cell.eventBut.isHidden = true
  119. case "唯一编码":
  120. cell.valueLab.text = self.contact?.unique
  121. cell.eventBut.isHidden = true
  122. case "联系电话":
  123. cell.valueLab.text = self.contact?.mobile
  124. cell.eventBut.addTarget(self, action: #selector(self.call), for: UIControl.Event.touchUpInside)
  125. case "电子邮件":
  126. cell.valueLab.text = self.contact?.mail
  127. cell.eventBut.theme_setImage(ThemeImagePicker(keyPath:"Icon.icon_email"), forState: .normal)
  128. cell.eventBut.addTarget(self, action: #selector(self.sendMail), for: UIControl.Event.touchUpInside)
  129. case "部门":
  130. var unitName = ""
  131. if let idenList = self.contact?.woIdentityList {
  132. for iden in idenList {
  133. if let unit = iden.woUnit {
  134. if unitName != "" {
  135. unitName.append(";")
  136. }
  137. unitName.append(unit.name ?? "")
  138. }
  139. }
  140. }
  141. cell.valueLab.text = unitName
  142. cell.eventBut.isHidden = true
  143. default:
  144. break
  145. }
  146. return cell
  147. }
  148. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  149. switch self.nameLabs[indexPath.row] {
  150. case "联系电话":
  151. self.call()
  152. case "电子邮件":
  153. self.sendMail()
  154. default:
  155. break
  156. }
  157. self.tableView.deselectRow(at: indexPath, animated: true)
  158. }
  159. @objc func sendMail() {
  160. if let mail = self.contact?.mail, mail != "" {
  161. let alertController = UIAlertController(title: "", message: nil,preferredStyle: .actionSheet)
  162. let mailAction = UIAlertAction(title: "发邮件", style: .default, handler: { _ in
  163. let mailURL = URL(string: "mailto://\(mail)")
  164. if UIApplication.shared.canOpenURL(mailURL!) {
  165. UIApplication.shared.openURL(mailURL!)
  166. }else{
  167. self.showError(title: "发邮件失败")
  168. }
  169. })
  170. let copyAction = UIAlertAction(title: "复制", style: .default, handler: { _ in
  171. UIPasteboard.general.string = mail
  172. self.showSuccess(title: "复制成功")
  173. })
  174. let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: nil)
  175. alertController.addAction(mailAction)
  176. alertController.addAction(copyAction)
  177. alertController.addAction(cancelAction)
  178. self.present(alertController, animated: true, completion: nil)
  179. }
  180. }
  181. @objc func call(){
  182. if let phone = self.contact?.mobile, phone != "" {
  183. let alertController = UIAlertController(title: "", message: nil,preferredStyle: .actionSheet)
  184. let smsAction = UIAlertAction(title: "发短信", style: .default, handler: { _ in
  185. let smsURL = URL(string: "sms://\(phone)")
  186. if UIApplication.shared.canOpenURL(smsURL!) {
  187. UIApplication.shared.openURL(smsURL!)
  188. }else{
  189. self.showError(title: "发短信失败")
  190. }
  191. })
  192. let phoneAction = UIAlertAction(title: "打电话", style: .default, handler: { _ in
  193. let phoneURL = URL(string: "tel://\(phone)")
  194. if UIApplication.shared.canOpenURL(phoneURL!) {
  195. UIApplication.shared.openURL(phoneURL!)
  196. }else{
  197. self.showError(title: "打电话失败")
  198. }
  199. })
  200. let copyAction = UIAlertAction(title: "复制", style: .default, handler: { _ in
  201. UIPasteboard.general.string = phone
  202. self.showSuccess(title: "复制成功")
  203. })
  204. let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: nil)
  205. alertController.addAction(phoneAction)
  206. alertController.addAction(smsAction)
  207. alertController.addAction(copyAction)
  208. alertController.addAction(cancelAction)
  209. self.present(alertController, animated: true, completion: nil)
  210. }
  211. }
  212. func loadPersonInfo(_ sender: AnyObject?){
  213. self.showLoading(title: "加载中...")
  214. AF.request(myPersonURL!).responseJSON {
  215. response in
  216. switch response.result {
  217. case .success( let val):
  218. let json = JSON(val)["data"]
  219. self.contact = Mapper<PersonV2>().map(JSONString:json.description)!
  220. //OOCon
  221. let me = O2AuthSDK.shared.myInfo()
  222. self.isCollect = DBManager.shared.isCollect(self.contact!, (me?.id)!)
  223. if self.isCollect == true {
  224. self.personCollect.isSelected = true
  225. }else{
  226. self.personCollect.isSelected = false
  227. }
  228. self.personName.text = self.contact?.name
  229. if let qq = self.contact?.qq, qq != "" {
  230. self.personQQ.text = "QQ \(qq)"
  231. }else{
  232. self.personQQ.text = ""
  233. }
  234. if let gt = self.contact?.genderType, gt == "f" {
  235. self.personGirl.setImage(UIImage(named: "icon_girl_2"), for: .normal)
  236. }else{
  237. self.personMan.setImage(UIImage(named: "icon_boy_2"), for: .normal)
  238. }
  239. let urlstr = AppDelegate.o2Collect.generateURLWithAppContextKey(ContactContext.contactsContextKeyV2, query: ContactContext.personIconByNameQueryV2, parameter: ["##name##":self.contact?.unique as AnyObject], generateTime: false)
  240. let url = URL(string: urlstr!)
  241. self.personImg.hnk_setImageFromURL(url!)
  242. DispatchQueue.main.async {
  243. self.hideLoading()
  244. self.tableView.reloadData()
  245. }
  246. case .failure(let err):
  247. DDLogError(err.localizedDescription)
  248. DispatchQueue.main.async {
  249. self.showError(title: "加载失败")
  250. }
  251. }
  252. }
  253. }
  254. }