HomeViewController.swift 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. //
  2. // HomeViewController.swift
  3. // xingchuangke
  4. //
  5. // Created by Virgil on 2019/3/22.
  6. // Copyright © 2019 Virgil. All rights reserved.
  7. //
  8. import UIKit
  9. class HomeViewController: BaseViewController, UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate {
  10. @IBOutlet weak var imgUserType: UIImageView!
  11. @IBOutlet weak var lblRealName: UILabel!
  12. @IBOutlet weak var btnPersonal: UIButton!
  13. @IBOutlet weak var viewTop: UIView!
  14. var headerView: HomeHeaderView!
  15. @IBOutlet weak var tableView: UITableView!
  16. override func viewDidLoad() {
  17. super.viewDidLoad()
  18. tableView.register(UINib(nibName: "HomeTableViewCell", bundle: nil), forCellReuseIdentifier: "HomeTableViewCell")
  19. tableView.delegate = self
  20. tableView.dataSource = self
  21. tableView.separatorStyle = .none
  22. tableView.showsVerticalScrollIndicator = false
  23. tableView.estimatedRowHeight = 100
  24. tableView.keyboardDismissMode = .onDrag
  25. tableView.mj_header = MJRefreshNormalHeader(refreshingBlock: {[weak self] in
  26. self!.currentPage = 1
  27. self!.loadData()
  28. })
  29. tableView.mj_header?.lastUpdatedTimeKey = "ShowTableViewCell"
  30. tableView.mj_footer = MJRefreshBackNormalFooter(refreshingBlock: {[weak self] in
  31. self!.currentPage += 1
  32. self!.loadData()
  33. })
  34. // tableView.mj_header.backgroundColor = CommonUntils.getUIColorFromRGB(rgbValue: 0x37A6FF, alpha: 1.0)
  35. headerView = (CommonViewUntils.getViewForXIB(xibName: "HomeHeaderView") as! HomeHeaderView)
  36. headerView.isHid = true
  37. headerView.initView()
  38. tableView.tableHeaderView = headerView
  39. tableView.reloadData()
  40. let imgUrl = ["https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1553911731&di=204e2bce757c3d97ec7a459bced6d226&imgtype=jpg&er=1&src=http%3A%2F%2Fpic31.nipic.com%2F20130804%2F7487939_090818211000_2.jpg", "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1553317230347&di=ded31b12c8a8d3ad31d4b88003e1dab7&imgtype=0&src=http%3A%2F%2Fpic2.ooopic.com%2F12%2F07%2F12%2F07bOOOPICbc_1024.jpg", "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1553317230345&di=a119f97664d26ba897e692a50554c35e&imgtype=0&src=http%3A%2F%2Fpic20.huitu.com%2Fres%2F20140728%2F239586_20140728224850215303_1.jpg"]
  41. //轮播图片
  42. let viewHeader = Virgil_PagedView(frame: CGRect(x: 0, y: 0, width: ScreenWidth, height: headerView.viewContent1.frame.height), imageURLStrings: imgUrl, placeholderImage: UIImage(named: "img_banner_01")!, timeInterval: 6) {
  43. (_) -> Void in
  44. }
  45. viewHeader.backgroundColor = UIColor.clear
  46. viewHeader.titles = ["1111", "2222", "3333", "4444"]
  47. headerView.viewContent1.addSubview(viewHeader)
  48. if CommonUntils.isIphonex() {
  49. viewTop.setSizeHeight(height: 88)
  50. } else {
  51. viewTop.setSizeHeight(height: 64)
  52. }
  53. headerView.btnStartJS.addTarget(self, action: #selector(btnMenuClick), for: .touchUpInside)
  54. loadadverData()
  55. }
  56. // MARK: =============加载数据===============
  57. func loadData() {
  58. let url = RequestURL.hotActivityPageList
  59. let params = NSMutableDictionary()
  60. loadDataList(url: url, params: params, tableView: self.tableView, tag: 1001)
  61. }
  62. let arrHeader = NSMutableArray()
  63. func loadHeaderData() {
  64. let url = RequestURL.findAdvertisingWheel
  65. let params = NSMutableDictionary()
  66. params.setValue(0, forKey: "position")
  67. arrHeader.removeAllObjects()
  68. loadDataList(url: url, params: params, tag: 1003, array: arrHeader)
  69. }
  70. let dicReRevenueCalculator = NSMutableDictionary()
  71. func revenueCalculator() {
  72. if headerView.txtCTPOSPersonal.text!.length() < 1 {
  73. SVProgressHUD.showError(withStatus: "请输入传统POS(个人)月激活商户数")
  74. return
  75. }
  76. if headerView.txtCTPOS.text!.length() < 1 {
  77. SVProgressHUD.showError(withStatus: "请输入传统POS(商户)月激活商户数")
  78. return
  79. }
  80. if headerView.txtMPOS.text!.length() < 1 {
  81. SVProgressHUD.showError(withStatus: "请输入MPOS月激活商户数")
  82. return
  83. }
  84. if headerView.txtMonth.text!.length() < 1 {
  85. SVProgressHUD.showError(withStatus: "请输入累计月数")
  86. return
  87. }
  88. headerView.txtMonth.resignFirstResponder()
  89. headerView.txtCTPOSPersonal.resignFirstResponder()
  90. headerView.txtCTPOS.resignFirstResponder()
  91. headerView.txtMPOS.resignFirstResponder()
  92. let url = RequestURL.revenueCalculator
  93. let params = NSMutableDictionary()
  94. params.setValue(headerView.txtCTPOSPersonal.text!, forKey: "ctPosGRNum")
  95. params.setValue(headerView.txtCTPOS.text!, forKey: "ctPosSHNum")
  96. params.setValue(headerView.txtMPOS.text!, forKey: "MPosNum")
  97. params.setValue(headerView.txtMonth.text!, forKey: "monthNum")
  98. loadDataInfo(url: url, params: params, tag: 1002, reDic: dicReRevenueCalculator)
  99. }
  100. override func returnData(tag: Int) {
  101. if tag == 1001 {
  102. tableView.reloadData()
  103. } else if tag == 1002 {
  104. let viewMoney = (CommonViewUntils.getViewForXIB(xibName: "CalcResultView") as! CalcResultView)
  105. viewMoney.initView(money: "\(dicReRevenueCalculator.getString(key: "total"))") {[weak self] (_, _) in
  106. self!.headerView.txtCTPOSPersonal.text = ""
  107. self!.headerView.txtCTPOS.text = ""
  108. self!.headerView.txtMPOS.text = ""
  109. self!.headerView.txtMonth.text = ""
  110. }
  111. appDelegate.navController.showBackgroudView(viewContent: viewMoney, isCenter: true, clickOtherIsClose: false)
  112. } else if tag == 1003 {
  113. if arrHeader.count > 0 {
  114. var imgUrl = [String]()
  115. if arrHeader.count == 3 {
  116. headerView.isHid = true
  117. headerView.initView()
  118. }else {
  119. headerView.isHid = false
  120. headerView.initView()
  121. }
  122. for i in 0 ..< arrHeader.count {
  123. imgUrl.append(getStringForArray(key: "img_path", arr: arrHeader, current: i))
  124. }
  125. let viewHeader = Virgil_PagedView(frame: CGRect(x: 0, y: 0, width: ScreenWidth, height: headerView.viewContent1.frame.height), imageURLStrings: imgUrl, placeholderImage: UIImage(named: "img_banner_01")!, timeInterval: 6) {[weak self]
  126. (item) -> Void in
  127. if CommonValue.getUserId() == "" {
  128. appDelegate.navController.pushViewController(LoginViewController(), animated: true)
  129. return
  130. }
  131. let type = (self!.arrHeader[item] as! NSDictionary).getInt(key: "type")
  132. ///0:内部web 1:外部web 111:邀请好友 222开通商户
  133. if type == 0 || type == 1 {
  134. let web = CommonWebViewViewController()
  135. web.strTitle = "详情"
  136. web.url = (self!.arrHeader[item] as! NSDictionary).getString(key: "web_url")
  137. appDelegate.navController.pushViewController(web, animated: true)
  138. } else if type == 111 {
  139. let vc = InviteViewController()
  140. appDelegate.navController.pushViewController(vc, animated: true)
  141. } else if type == 222 {
  142. let vc = SelectPayBrandViewController()
  143. appDelegate.navController.pushViewController(vc, animated: true)
  144. }
  145. }
  146. viewHeader.backgroundColor = UIColor.clear
  147. viewHeader.titles = ["1111", "2222", "3333", "4444"]
  148. headerView.viewContent1.removeAll()
  149. headerView.viewContent1.addSubview(viewHeader)
  150. }
  151. loadData()
  152. }
  153. }
  154. override func returnError(tag: Int, type: String) {
  155. if tag == 1003 {
  156. loadData()
  157. }
  158. }
  159. //btnAddTarget(view: self.view, selector: #selector(self.btnMenuClick))
  160. @IBAction func btnMenuClick(_ sender: AnyObject) {
  161. if CommonValue.getUserId() == "" && (sender as! UIButton).tag != 1013 {
  162. appDelegate.navController.pushViewController(LoginViewController(), animated: true)
  163. return
  164. }
  165. switch (sender as! UIButton).tag {
  166. case 1001: // 荣誉资质
  167. // let vc = TestCommon();
  168. // vc.uploadHaiKe();
  169. appDelegate.navController.pushViewController(QualificationsViewController(), animated: true)
  170. break
  171. case 1002: // 消息
  172. appDelegate.navController.pushViewController(NewsListViewController(), animated: true)
  173. break
  174. case 1003: // 个人信息
  175. appDelegate.navController.pushViewController(PersonalInfoViewController(), animated: true)
  176. break
  177. case 1004: //
  178. break
  179. case 1005: //
  180. break
  181. case 1006: //
  182. break
  183. case 1013: //
  184. revenueCalculator()
  185. break
  186. default:
  187. break
  188. }
  189. }
  190. // MARK: TABLEVLEW 实现
  191. func numberOfSections(in tableView: UITableView) -> Int {
  192. return 1
  193. }
  194. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  195. return self.arrData.count
  196. }
  197. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  198. let cell = tableView.dequeueReusableCell(withIdentifier: "HomeTableViewCell", for: indexPath as IndexPath) as! HomeTableViewCell
  199. cell.img.loadImage(imgUrl: getString(current: indexPath.row, key: "image"), defaultImage: "")
  200. cell.img.setCornerRadius(size: 8)
  201. return cell
  202. }
  203. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  204. if indexPath.row >= arrData.count {
  205. return
  206. }
  207. let viewWeb = CommonWebViewViewController()
  208. viewWeb.strTitle = "详情"
  209. viewWeb.url = getString(current: indexPath.row, key: "webUrl")
  210. toViewController(viewController: viewWeb)
  211. }
  212. func scrollViewDidScroll(_ scrollView: UIScrollView) {
  213. if scrollView.contentOffset.y < 10 {
  214. viewTop.backgroundColor = CommonUntils.getUIColorFromRGB(rgbValue: 0x37A6FF, alpha: 0.15)
  215. } else {
  216. var alpha = 0.15 + 0.002 * scrollView.contentOffset.y
  217. if alpha > 1 {
  218. alpha = 1
  219. }
  220. viewTop.backgroundColor = CommonUntils.getUIColorFromRGB(rgbValue: 0x37A6FF, alpha: alpha)
  221. }
  222. }
  223. override func notiReloadUser(noti: NSNotification) {
  224. reloadUserInfo()
  225. headerView.reloadMoney()
  226. }
  227. func reloadUserInfo() {
  228. lblRealName.setSizeWidth(width: 120)
  229. lblRealName.text = CommonValue.getUserNickName()
  230. lblRealName.sizeToFitWidth()
  231. if CommonValue.getUserNickName() == "未实名" || CommonValue.getUserNickName() == "未登录" || CommonValue.getUserNickName() == "" {
  232. imgUserType.isHidden = true
  233. } else {
  234. if CommonValue.checkIsAS() {
  235. imgUserType.isHidden = true
  236. } else {
  237. imgUserType.isHidden = false
  238. }
  239. imgUserType.marginLeft(left: 2, view: lblRealName)
  240. imgUserType.image = UIImage(named: "member\(CommonValue.getUserLevel())")
  241. }
  242. }
  243. override func viewWillAppear(_ animated: Bool) {
  244. super.viewWillAppear(animated)
  245. appDelegate.setNavigationBarHidden(isHidden: true)
  246. reloadUserInfo()
  247. headerView.reloadMoney()
  248. tableView.setSizeHeight(height: ScreenHeight - common_bottom_height - 48)
  249. loadHeaderData()
  250. tableView.reloadData()
  251. }
  252. override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
  253. super.init(nibName: "HomeViewController", bundle: nil)
  254. }
  255. required init?(coder aDecoder: NSCoder) {
  256. fatalError("init(coder:) has not been implemented")
  257. }
  258. let bgView = UIView.init(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: kScreenHeight))
  259. let advertView = UIView.init(frame: CGRect(x: 0, y: 0, width: 300, height: 400))
  260. var advertArray = NSArray.init()
  261. func loadadverData(){
  262. let http = AFHTTPSessionManager()
  263. http.post("http://app.xingchuangke.net/app/screenList", parameters: nil, progress: { (_) in
  264. }, success: { (operation, json) in
  265. print(json as Any)
  266. let success = (json as! NSDictionary).object(forKey: "statusCode") as! Int
  267. if success == 200 {
  268. let datat = (json as! NSDictionary).object(forKey: "data") as! NSArray
  269. self.advertArray = datat
  270. let dic = datat[0]as!NSDictionary
  271. self.bgView.backgroundColor = .black
  272. self.bgView.alpha = 0.5
  273. self.view.addSubview(self.bgView)
  274. self.view.bringSubviewToFront(self.bgView)
  275. self.view.bringSubviewToFront(self.advertView)
  276. self.advertView.center = self.bgView.center
  277. self.advertView.backgroundColor = .white
  278. self.advertView.layer.cornerRadius = 20
  279. self.advertView.alpha = 1
  280. self.view.addSubview(self.advertView)
  281. var strHtml = self.getString(key: "content", dic: dic)
  282. let label:UITextView = UITextView()
  283. label.isEditable = false
  284. label.frame = CGRect(x:10,
  285. y:10,
  286. width:280,
  287. height:380);
  288. let width:CGFloat = ( 270);
  289. let str:NSString = "<html><meta name=\"viewport\" content=\"width=device-width,initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no\"/><head><style type=\"text/css\">body{font-size:18px;}</style></head><body>%@<style>*{ width: %fpx; margin: 0; padding: 0 0; box-sizing: border-box;} img{ width: %fpx;}</style></body></html>" as NSString;
  290. strHtml = NSString(format: str,strHtml,width,width) as String
  291. let data:Data = strHtml.data(using: String.Encoding(rawValue: String.Encoding.unicode.rawValue))!
  292. do {
  293. let attrinbuteString: NSAttributedString = try! NSAttributedString.init(data: data, options: [ NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html ], documentAttributes: nil)
  294. DispatchQueue.main.async {
  295. label.attributedText = attrinbuteString
  296. }
  297. }
  298. label.isUserInteractionEnabled = true
  299. let tapGes = UITapGestureRecognizer(target: self,
  300. action: #selector(self.titleLabelClick(tapGes: )))
  301. label.addGestureRecognizer(tapGes)
  302. let closeBtn = UIButton.init(frame: CGRect(x: kScreenWidth/2-20, y: self.advertView.bottom()+20, width: 60, height: 60))
  303. closeBtn.addTarget(self, action: #selector(self.closeadvert), for: .touchUpInside)
  304. closeBtn.setImage(UIImage.init(named: "ion_delete"), for: .normal)
  305. self.bgView.addSubview(closeBtn)
  306. self.advertView.addSubview(label)
  307. let btnTap = UIButton.init(frame: CGRect(x: 250, y: 20, width: 40, height: 40))
  308. btnTap.setImage(UIImage.init(named: "ico_04"), for:.normal )
  309. btnTap.addTarget(self, action: #selector(self.btnTapTouch), for: .touchUpInside)
  310. self.advertView.addSubview(btnTap)
  311. }
  312. }) { (_, _) in
  313. }
  314. }
  315. @objc func btnTapTouch(){
  316. let dic = advertArray[0]as!NSDictionary
  317. if getString(key: "webUrl", dic: dic) == "" {
  318. }else{
  319. let web = CommonWebViewViewController()
  320. web.strTitle = dic.getString(key: "title")
  321. web.url = dic.getString(key: "webUrl")
  322. appDelegate.navController.pushViewController(web, animated: true)
  323. print("dianji")
  324. }
  325. }
  326. @objc func closeadvert(){
  327. self.bgView.removeFromSuperview()
  328. self.advertView.removeFromSuperview()
  329. }
  330. // 点击事件 需要添加 @objc
  331. @objc func titleLabelClick(tapGes:UITapGestureRecognizer){
  332. // 1、获取当前label
  333. let dic = advertArray[0]as!NSDictionary
  334. if getString(key: "webUrl", dic: dic) == "" {
  335. }else{
  336. let web = CommonWebViewViewController()
  337. web.strTitle = dic.getString(key: "title")
  338. web.url = dic.getString(key: "webUrl")
  339. appDelegate.navController.pushViewController(web, animated: true)
  340. print("dianji")
  341. }
  342. }
  343. }