MyViewController.swift
13.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
//
// MyViewController.swift
// ParentAssistant
//
// Created by 葛建军 on 2018/3/5.
// Copyright © 2018年 HANGZHOUTEAM. All rights reserved.
//
import UIKit
import CoreData
class MyViewController: UIViewController {
@IBOutlet var currentInfoTableView: UITableView!
@IBOutlet var titleViewHeight: NSLayoutConstraint!
@IBOutlet var headerBackGroundImageView: UIImageView!//整个header的背景图
@IBOutlet var personIconImageView: UIImageView!//用户头像
@IBOutlet var currentInfoView: UIView!
@IBOutlet var personNameLabel: UILabel!//用户名称
@IBOutlet var personNumberLabel: UILabel!//用户账号
@IBOutlet var loginBtn: UIButton!//登录按钮
@IBOutlet var currentInfoBtn: UIButton!//查看个人信息按钮
@IBOutlet var logoutBtn: UIButton!
var titleDataSet:[String]=["消息","订购详情","订购中心","我的孩子","空白","意见与反馈","清除缓存","关于"]
var iconDataSet:[String] = ["my_icon_orderMessage","my_icon_orderDetail","my_icon_order","my_icon_children","","my_icon_service","my_icon_cachingClear","my_icon_about"]
override func viewDidLoad() {
super.viewDidLoad()
layoutSubViews()
}
// MARK: - 设置控件属性
func layoutSubViews(){
let backImage = UIImage(named: "navigationBar_backgrounImage")
self.navigationController?.navigationBar.setBackgroundImage(backImage, for: UIBarMetrics.default)
// MARK: - 是否是iPhoneX顶部刘海
if UIApplication.shared.statusBarFrame.size.height>20 {
titleViewHeight.constant = 88
}else{
titleViewHeight.constant = 64
}
personIconImageView.layer.borderWidth = 1.0
personIconImageView.layer.borderColor = UIColor.white.cgColor
self.navigationController?.isNavigationBarHidden = true
let backButton = UIBarButtonItem()
backButton.title = "个人中心"
self.navigationItem.backBarButtonItem = backButton
self.configTheme()
//添加退出登录的通知,防止在本页面时账号被顶 不能及时刷新页面
NotificationCenter.default.addObserver(self, selector: #selector(MyViewController.loginOutNotificationAction), name: NSNotification.Name(rawValue: "loginOutSuccess"), object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(MyViewController.receivedNewNotificationAction), name: NSNotification.Name(rawValue: MessageNotification.receiveMessage), object: nil)
}
// MARK: - 页面将要显示时隐藏navigationbar显示自定义的顶部view
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.isNavigationBarHidden = true
// MARK: - 设置是否登录及个人信息
setupCurrentInfo()
}
// MARK: - 页面将要消失时显示navigationbar
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.navigationController?.isNavigationBarHidden = false
}
// MARK: - 设置是否登录及个人信息
func setupCurrentInfo() {
if AccountManager.shared.isOnline() {
logoutBtn.isHidden = false
personNameLabel.isHidden = false
personNumberLabel.isHidden = false
loginBtn.isHidden = true
currentInfoBtn.isHidden = false
//赋值
headerBackGroundImageView.image = UIImage(named: "my_headerView_backgroundimageView")
let info = AccountManager.shared
personNameLabel.text = info.name()
personNumberLabel.text = info.phone()
personIconImageView.sd_setImage(with: URL(string: AccountManager.shared.photo().addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!), placeholderImage: UIImage(named: "defphoto"))
}else{
logoutBtn.isHidden = true
personNameLabel.isHidden = true
personNumberLabel.isHidden = true
loginBtn.isHidden = false
currentInfoBtn.isHidden = true
headerBackGroundImageView.image = UIImage(named: "navigationBar_backgrounImage")
personIconImageView.image = UIImage(named: "my_defphoto")
}
currentInfoTableView.reloadData()
}
// MARK: - 登录按钮点击方法
@IBAction func showLoginAction(_ sender: UIButton) {
let vc = Story.instantiateViewControllerWithIdentifier("LoginViewController", storyName: "My") as! LoginViewController
self.navigationController?.pushViewController(vc, animated: true)
}
// MARK: - 个人信息按钮点击方法
@IBAction func showCurrentInfoAction(_ sender: UIButton) {
let vc = Story.instantiateViewControllerWithIdentifier("CurrentInfoViewController", storyName: "My") as! CurrentInfoViewController
self.navigationController?.pushViewController(vc, animated: true)
}
// MARK: - 退出登录按钮点击方法
@IBAction func loginOutAction(_ sender: UIButton) {
SVProgressHUD.show()
YXMessageManager.share.loginOut { (finish) in
SVProgressHUD.dismiss()
if finish {
AccountManager.shared.logOut()
}else{
appDelegate.window!.makeToast("云信退出登录失败", duration: 1, position: CSToastPositionCenter)
}
}
}
// MARK: - 退出登录时
@objc func loginOutNotificationAction(){
self.setupCurrentInfo()
self.currentInfoTableView.reloadData()
}
// MARK: - 收到新通知时
@objc func receivedNewNotificationAction(){
let array = NSManagedObject.fetchAllUnReadModel(identifier: "Message", ownid: AccountManager.shared.id()) as! [Message]
let cell = currentInfoTableView.cellForRow(at: IndexPath(row: 0, section: 0)) as! MyTableViewCell
if array.count>0 {
cell.newLabel.text = "\(array.count)"
}else{
cell.newLabel.text = ""
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
extension MyViewController: UITableViewDelegate,UITableViewDataSource{
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == 4 {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")
return cell!
}
let cell = tableView.dequeueReusableCell(withIdentifier: "MyTableViewCell") as! MyTableViewCell
cell.iconImageView.image = UIImage(named: iconDataSet[indexPath.row])
cell.nameLabel.text = titleDataSet[indexPath.row]
switch indexPath.row {
case 0:
cell.newLabel.isHidden = false
//如果有未读消息
if AccountManager.shared.isOnline() {
let array = NSManagedObject.fetchAllUnReadModel(identifier: "Message", ownid: AccountManager.shared.id()) as! [Message]
if array.count>0 {
cell.newLabel.text = "\(array.count)"
}else{
cell.newLabel.text = ""
}
}else{
cell.newLabel.text = ""
}
case 3:
cell.newLabel.isHidden = false
//如果家长未绑定孩子
if AccountManager.shared.isOnline() {
if AccountManager.shared.rawUserInfo!.contentData()["studentClass"].arrayValue.count == 0 {
cell.newLabel.text = "未绑定"
}else{
cell.newLabel.text = ""
}
}else{
cell.newLabel.text = ""
}
default:
cell.newLabel.isHidden = true
}
return cell
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return titleDataSet.count
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
switch indexPath.row {
case 5,6,7:
return 44
case 0,1,2,3:
if !AccountManager.shared.isOnline() {
return 0
}
if AccountManager.shared.isOnline() && AccountManager.shared.name()=="葛建军" && (indexPath.row == 1 || indexPath.row == 2){//隐藏支付相关功能
return 0
}else{
return 44
}
default:
return 16
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
switch indexPath.row {
case 0://消息
if AccountManager.shared.isOnline() {
let vc = Story.instantiateViewControllerWithIdentifier("MessageViewController", storyName: "My") as! MessageViewController
self.navigationController?.pushViewController(vc, animated: true)
}else{
appDelegate.window!.makeToast("请先登录", duration: 1, position: CSToastPositionCenter)
}
case 1://订购详情
if AccountManager.shared.isOnline() {
let vc = Story.instantiateViewControllerWithIdentifier("OrderDetailViewController", storyName: "My") as! OrderDetailViewController
self.navigationController?.pushViewController(vc, animated: true)
}else{
appDelegate.window!.makeToast("请先登录", duration: 1, position: CSToastPositionCenter)
}
case 2://家校通订购
if AccountManager.shared.isOnline() {
let vcs = Story.instantiateViewControllerWithIdentifier("JSViewController", storyName: "JS") as! JSViewController
vcs.borderline = true
vcs.webURL = "\(HTTPServer.HOSTWAP!)/ParentOrderCenter.aspx?userid=\(AccountManager.shared.id())"
self.navigationController?.present(vcs.wrapWithNavigationController(), animated: true, completion: nil)
}else{
appDelegate.window!.makeToast("请先登录", duration: 1, position: CSToastPositionCenter)
}
case 3://我的孩子
if AccountManager.shared.isOnline() {
let vc = Story.instantiateViewControllerWithIdentifier("MyChildrenViewController", storyName: "My") as! MyChildrenViewController
self.navigationController?.pushViewController(vc, animated: true)
}else{
appDelegate.window!.makeToast("请先登录", duration: 1, position: CSToastPositionCenter)
}
case 5://意见与反馈
let vcs = Story.instantiateViewControllerWithIdentifier("JSViewController", storyName: "JS") as! JSViewController
vcs.webURL = HTTPServer.HOSTWAP+"/FeedBack.aspx?userid=\(AccountManager.shared.userid)"
self.navigationController?.present(vcs.wrapWithNavigationController(), animated: true, completion: nil)
case 6://清除缓存
clearCache()
case 7://关于
let vcs = Story.instantiateViewControllerWithIdentifier("JSViewController", storyName: "JS") as! JSViewController
vcs.webURL = HTTPServer.HOSTWAP+"/About.html"
self.navigationController?.present(vcs.wrapWithNavigationController(), animated: true, completion: nil)
default:
break
}
}
//MARK: - 清除缓存
func clearCache(){
// 取出cache文件夹路径
let cachePath = NSSearchPathForDirectoriesInDomains(Foundation.FileManager.SearchPathDirectory.cachesDirectory, Foundation.FileManager.SearchPathDomainMask.userDomainMask, true).first
// 打印路径,需要测试的可以往这个路径下放东西
// print(cachePath)
// 取出文件夹下所有文件数组
let files = Foundation.FileManager.default.subpaths(atPath: cachePath!)
// 用于统计文件夹内所有文件大小
var big = Int();
// 快速枚举取出所有文件名
for p in files!{
// 把文件名拼接到路径中
let path = cachePath!.appendingFormat("/\(p)")
// 取出文件属性
let floder = try! Foundation.FileManager.default.attributesOfItem(atPath: path)
// 用元组取出文件大小属性
for (abc,bcd) in floder {
// 只去出文件大小进行拼接
if abc == FileAttributeKey.size{
big += (bcd as AnyObject).intValue
}
}
}
// 提示框
let message = "\(big/(1024*1024))M缓存"
let alert = UIAlertController(title: "清除缓存", message: message, preferredStyle: UIAlertControllerStyle.alert)
let alertConfirm = UIAlertAction(title: "确定", style: UIAlertActionStyle.default) { (alertConfirm) -> Void in
// 点击确定时开始删除
for p in files!{
// 拼接路径
let path = cachePath!.appendingFormat("/\(p)")
// 判断是否可以删除
if(Foundation.FileManager.default.fileExists(atPath: path)){
if path.contains("/Caches/Snapshots") {
return
}
// 删除
do {
try Foundation.FileManager.default.removeItem(atPath: path)
}
catch {
fatalError("不能删除:\(error)")
}
}
}
}
alert.addAction(alertConfirm)
let cancle = UIAlertAction(title: "取消", style: UIAlertActionStyle.cancel) { (cancle) -> Void in
}
alert.addAction(cancle)
// 提示框弹出
present(alert, animated: true) { () -> Void in
}
}
}