ContactPersonInfoV2ViewController.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. import O2OA_Auth_SDK
  17. class ContactPersonInfoV2ViewController: UITableViewController {
  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. OOContactsInfoDB.shareInstance.deleteData(contact!, (me?.id)!)
  31. }else{
  32. //增加
  33. OOContactsInfoDB.shareInstance.insertData(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. override func viewDidLoad() {
  56. super.viewDidLoad()
  57. self.personImg.layer.cornerRadius = self.personImg.frame.size.width / 2
  58. self.personImg.clipsToBounds = true
  59. loadPersonInfo(nil)
  60. let startChatButton = OOBaseUIButton(x: (kScreenW - 260)/2, y: 5, w: 260, h: 30, target: self, action: #selector(_startChat))
  61. startChatButton.setTitle("发起聊天", for: .normal)
  62. let btnContainerView = UIView(x: 0, y: 0, w: kScreenW, h: 40)
  63. btnContainerView.addSubview(startChatButton)
  64. tableView.tableFooterView = btnContainerView
  65. }
  66. override func viewWillAppear(_ animated: Bool) {
  67. super.viewWillAppear(animated)
  68. //去掉nav底部分割线
  69. self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
  70. self.navigationController?.navigationBar.shadowImage = UIImage()
  71. }
  72. override func didReceiveMemoryWarning() {
  73. super.didReceiveMemoryWarning()
  74. // Dispose of any resources that can be recreated.
  75. }
  76. @objc private func _startChat() {
  77. MBProgressHUD_JChat.showMessage(message: "创建中...", toView: view)
  78. var username = ""
  79. if self.person != nil {
  80. username = self.person?.id ?? ""
  81. }else if self.identity != nil {
  82. username = self.identity?.id ?? ""
  83. }
  84. if username == "" {
  85. return
  86. }
  87. JMSGConversation.createSingleConversation(withUsername: username) { (result, error) in
  88. MBProgressHUD_JChat.hide(forView: self.view, animated: true)
  89. if error == nil {
  90. let conv = result as! JMSGConversation
  91. let vc = JCChatViewController(conversation: conv)
  92. NotificationCenter.default.post(name: NSNotification.Name(rawValue: kUpdateConversation), object: nil, userInfo: nil)
  93. self.navigationController?.pushViewController(vc, animated: true)
  94. }else{
  95. O2Logger.error(error.debugDescription)
  96. MBProgressHUD_JChat.show(text: "创建会话失败,请重试", view: self.view)
  97. }
  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.setImage(UIImage(named: "icon_email"), for: .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. ProgressHUD.showError("发邮件失败")
  168. }
  169. })
  170. let copyAction = UIAlertAction(title: "复制", style: .default, handler: { _ in
  171. UIPasteboard.general.string = mail
  172. ProgressHUD.showSuccess("复制成功")
  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. ProgressHUD.showError("发短信失败")
  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. ProgressHUD.showError("打电话失败")
  198. }
  199. })
  200. let copyAction = UIAlertAction(title: "复制", style: .default, handler: { _ in
  201. UIPasteboard.general.string = phone
  202. ProgressHUD.showSuccess("复制成功")
  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. ProgressHUD.show("加载中...")
  214. Alamofire.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 = OOContactsInfoDB.shareInstance.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. ProgressHUD.dismiss()
  244. self.tableView.reloadData()
  245. }
  246. case .failure(let err):
  247. DDLogError(err.localizedDescription)
  248. DispatchQueue.main.async {
  249. ProgressHUD.showError("加载失败")
  250. }
  251. }
  252. }
  253. }
  254. }