account.swift
7.83 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
//
// account.swift
// ParentAssistant
//
// Created by 葛建军 on 2018/3/6.
// Copyright © 2018年 HANGZHOUTEAM. All rights reserved.
//
import Foundation
class AccountManager: NSObject {
var rawUserInfo:JSON?
var userid:String=""
var token:String?
var refreshToken:String=""
var address:String = "未知"
//单例
static let shared = AccountManager()
override init(){
rawUserInfo=Setting.getUserInfo()
if let info=rawUserInfo{
userid=info.contentData()["userid"].stringValue
}
}
//MARK: - 获取个人信息
/// 获取用户信息
///
/// - Returns: 返回 UserInfoModel
func getUserInfo() -> UserInfoModel {
var model = UserInfoModel()
let dic = Setting.getUserInfo()?.dictionary
let data = dic!["data"]?.dictionary
if let value = data {
model = UserInfoModel.deserialize(from: value)!
}
return model
}
//MARK: 获取孩子信息
/// 获取用户孩子信息
///
/// - Returns: 返回 StudentClassInfoModel
func getChildClassInfo()-> Array<StudentClassInfoModel> {
var array = Array<StudentClassInfoModel>()
let dic = Setting.getUserInfo()?.dictionary
let data = dic!["data"]?.dictionary
let studentArr = data!["studentClass"]?.arrayValue
for i in 0..<studentArr!.count {
let studentDic = studentArr![i].dictionary
if let value = studentDic {
let model = StudentClassInfoModel.deserialize(from: value)
array.append(model!)
}
}
return array
}
func setTokenInfo(tokenInfo:JSON?){
if let info=tokenInfo{
token="Bearer "+info["access_token"].stringValue
refreshToken=info["refresh_token"].stringValue
}else{
token=""
}
}
func loadToken()->JSON?{
if let token=Setting.getJson("_token"){
return token
}
return nil
}
func saveToken(tokenInfo:JSON){
_=Setting.saveJson(tokenInfo, forKey: "_token")
}
//是否登录
func isOnline() -> Bool {
if id() != "" {
return true
}else{
return false
}
}
func id()->String{
if let info=rawUserInfo{
return info.contentData()["userid"].stringValue
}
return ""
}
func name()->String{
if let info=rawUserInfo{
return info.contentData()["name"].stringValue
}
return ""
}
func photo()->String{
if let info=rawUserInfo{
return info.contentData()["image"].stringValue
}
return "defphoto"
}
func phone()->String{
if let info=rawUserInfo{
return info.contentData()["mobile"].stringValue
}
return ""
}
func sex()->Int{
if let info=rawUserInfo{
return info.contentData()["sex"].intValue
}
return 0
}
func parentId()->Int{
if let info=rawUserInfo{
return info.contentData()["parentId"].intValue
}
return 0
}
func studentClass()->[JSON]{
if let info=rawUserInfo{
return info.contentData()["studentClass"].arrayValue
}
return []
}
func isNew()->Int{
if let info=rawUserInfo{
return info.contentData()["isNew"].intValue
}
return 0
}
func isHeaderTeacher()->Bool{
if let info=rawUserInfo{
return info.contentData()["f_Identity"].intValue == 2 ? true : false
}
return false
}
func logOut(auto:Bool? = false){
//退出登录
cleanUserInfoAndPoptoRootVC(auto: auto)
}
func cleanUserInfoAndPoptoRootVC(auto:Bool? = false){
rawUserInfo=nil
userid=""
_=Setting.saveUserInfo("")
UserDefaults.standard.removeObject(forKey: "_token")
refreshToken = ""
token = ""
if auto! {
if self.appRootViewController().classForCoder==TabBarController.classForCoder(){
((self.appRootViewController() as! TabBarController).selectedViewController! as! UINavigationController).popToRootViewController(animated: true)
}else{
self.appRootViewController().dismiss(animated: false, completion: {
(self.appRootViewController() as! UINavigationController).popToRootViewController(animated: true)
})
}
}
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "loginOutSuccess"), object: nil)
//取消之前注册的所有本地推送通知
// UIApplication.shared.cancelAllLocalNotifications()
//发出刷新首页通知
// NotificationCenter.default.post(name: NSNotification.Name(rawValue: "refreshFirstVC"), object: nil)
}
func appRootViewController()->UIViewController {
let appRootVC=UIApplication.shared.keyWindow?.rootViewController
var topVC=appRootVC
while topVC!.presentedViewController != nil {
topVC=topVC!.presentedViewController
}
return topVC!
}
func refreshUserInfo(_ mobile:String,completionHandler:((Bool) -> Void)?){
HTTPServer.shared.getParentInfo(["mobile":mobile as AnyObject], completionHandler: { (str, error) -> Void in
if error == nil {
if JSON.fromString(str)!["status"].intValue==4 {
HTTPServer.shared.refreshToken(completionHandler: { (str, error) in
if error == nil && JSON.fromString(str)!["status"].intValue==1 {
self.setTokenInfo(tokenInfo: JSON.fromString(str))
if let call=completionHandler{
call(true)
}
}else{
self.logOut()
appDelegate.window!.makeToast("登录信息已过期,请重新登录", duration: 2, position: CSToastPositionBottom)
if let call=completionHandler{
call(false)
}
}
}, token: self.refreshToken)
}else{
httpJsonResule(jsonString: str, error: error, successHandler: { (json) -> Void in
_=Setting.saveUserInfo(str)
self.rawUserInfo=json
self.userid=json.contentData()["userid"].stringValue
if let call=completionHandler{
call(true)
}
}, failHandler: { (error) -> Void in
if let call=completionHandler{
call(false)
}
})
}
}else{
if let call=completionHandler{
call(false)
}
}
})
}
}
import HandyJSON
class UserInfoModel: HandyJSON {
var parentId = Int()
var name = String()
var bindSchoolFlag = Bool()
var mobile = String()
var image = String()
var userid = String()
var sex = Int()
required init(){}
}
class StudentClassInfoModel: HandyJSON {
var photo = String()
var cityName = String()
var cardNumber = String()
var classId = Int()
var grade = Int()
var studentName = String()
var studentCode = String()
var parentId = Int()
var className = String()
var areaName = String()
var gradename = String()
var count = Int()
var schollId = Int()
var schoolName = String()
var studentUserId = String()
var studentId = Int()
var parentMobile = Int()
var sex = Int()
required init(){}
}