// // MineViewController.swift // Meterage // // Created by 203 on 2023/2/1. // import Alamofire import UIKit import DefaultsKit import SwiftyJSON class MineViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { @IBOutlet weak var userAvatarView: UIImageView! @IBOutlet weak var userNameView: UILabel! @IBOutlet weak var userDeptView: UILabel! @IBOutlet weak var noticeView: UIImageView! @IBOutlet weak var noticeCountView: UILabel! @IBOutlet weak var tableView: UITableView! private let defaults = Defaults.shared override func viewDidLoad() { super.viewDidLoad() // 我的 Tab页不显示导航标题 navigationController?.setNavigationBarHidden(true, animated: false) // Do any additional setup after loading the view. userAvatarView.layer.cornerRadius = 40 userAvatarView.layer.masksToBounds = true userAvatarView.layer.borderColor = UIColor.white.cgColor userAvatarView.layer.borderWidth = 1 noticeCountView.layer.cornerRadius = 7 noticeCountView.layer.masksToBounds = true /// 给UIImageView添加点击事件 let singleTapGesture = UITapGestureRecognizer(target: self, action: #selector(imageViewClick)) noticeView.addGestureRecognizer(singleTapGesture) noticeView.isUserInteractionEnabled = true tableView.register(UINib(nibName: "MineTableViewCell", bundle: nil), forCellReuseIdentifier: "mineTableViewCell") tableView.dataSource = self tableView.delegate = self tableView.separatorColor = .hintColor tableView.tableFooterView = UIView(frame: CGRect.zero) //数据请求 let baseURL = defaults.get(for: serverConfigKey)! let userInfoURL = baseURL + Constant.userInfo.rawValue let token = defaults.get(for: tokenKey) ?? "" let account = defaults.get(for: userNameKey) ?? "" Alamofire.request(userInfoURL, method: .get, parameters: ["account": account], headers: ["token": token]).responseJSON { [self] response in switch response.result { case let .success(value): let user = UserInfoModel(respJson: JSON(value)).data userNameView.text = user.name userDeptView.text = user.deptName case .failure: AlertHub.shared.showWaringAlert(controller: self, message: "请求失败,请检查网络") } } let noticeListURL = baseURL + Constant.noticeList.rawValue + "?limit=20&offset=1" let paramDic: [String: Any] = ["noticeNo": "", "noticeTitle": "", "noticePublisher": "", "noticeStartTime": "", "noticeEndTime": ""] Alamofire.request(HttpRequestCreator.shared.createPostRequest(url: noticeListURL, dic: paramDic, token: token)).responseJSON { [self] response in switch response.result { case let .success(value): let notice = NoticeListModel(respJson: JSON(value)).data if (notice.total > 9) { noticeCountView.text = "9+" } else { noticeCountView.text = String(notice.total) } case .failure: AlertHub.shared.showWaringAlert(controller: self, message: "请求失败,请检查网络") } } } /// 消息图标点击事件 @objc func imageViewClick(){ let destinationController = NoticeListViewController(nibName: "NoticeListViewController", bundle: nil) let navigation = UINavigationController(rootViewController: destinationController) navigation.modalPresentationStyle = UIModalPresentationStyle.fullScreen present(navigation, animated: true, completion: nil) } func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 60 } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { print("点击了\(MINE_TITLE_ARRAY[indexPath.row])") // switch indexPath.row { // case 0: // let loginModeController = LoginModeViewController() // // 由底部带有item的页面跳转之后隐藏底部bar // loginModeController.hidesBottomBarWhenPushed = true // navigationController?.pushViewController(loginModeController, animated: true) // break // case 1: // print("导出数据") // break // case 2: // print("导入数据") // break // case 3: // break // default: // break // } } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { MINE_TITLE_ARRAY.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell: MineTableViewCell = tableView.dequeueReusableCell(withIdentifier: "mineTableViewCell", for: indexPath) as! MineTableViewCell cell.leftImageView.image = UIImage(named: MINE_IMAGE_ARRAY[indexPath.row]) cell.titleLableView.text = MINE_TITLE_ARRAY[indexPath.row] cell.selectionStyle = .none return cell } func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) { } }