YXMessageManager.swift
6.71 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
//
// YXMessageManager.swift
// ParentAssistant
//
// Created by 葛建军 on 2018/3/15.
// Copyright © 2018年 HANGZHOUTEAM. All rights reserved.
//
import UIKit
import CoreData
class YXMessageManager: NSObject,NIMLoginManagerDelegate,NIMSystemNotificationManagerDelegate {
static let share=YXMessageManager()
private override init() {}
func addYXDelegate(){
//添加云信代理
NIMSDK.shared().loginManager.add(YXMessageManager.share as NIMLoginManagerDelegate)
NIMSDK.shared().systemNotificationManager.add(YXMessageManager.share as NIMSystemNotificationManagerDelegate)
}
//登录云信
func loginWithYX(account:String,token:String,completion:((Bool)->Void)?){
NIMSDK.shared().loginManager.login(account, token: token) { (error) in
var b:Bool
if error==nil {
//登录成功
b=true
}else{
//登录失败
b=false
}
completion?(b)
}
}
func autoLogin(account:String,token:String){
NIMSDK.shared().loginManager.autoLogin(account, token: token)
}
//退出登录
func loginOut(completion:((Bool)->Void)?){
NIMSDK.shared().loginManager.logout { (error) in
var b:Bool
if error==nil {
//退出成功
b=true
}else{
//退出失败
b=false
}
completion?(b)
}
}
/**
* 被踢(服务器/其他端)回调
*
* @param code 被踢原因
* @param clientType 发起踢出的客户端类型
*/
func onKick(_ code: NIMKickReason, clientType: NIMLoginClientType) {
var reason="您的帐号已在别处登录"
switch (code) {
case NIMKickReason.byClient:
var clientName=""
switch (clientType) {
case NIMLoginClientType.typeAOS:
clientName="Android";
case NIMLoginClientType.typeiOS:
clientName="iOS";
case NIMLoginClientType.typePC:
clientName="电脑";
case NIMLoginClientType.typeWeb:
clientName="网页";
default:
clientName="";
}
reason = clientName == "" ? "你的帐号在其他设备上登录,请注意帐号信息安全,若非本人操作请及时修改密码或联系客服人员" : "你的帐号在\(clientName)端登录,请注意帐号信息安全,若非本人操作请及时修改密码或联系客服人员"
break
case NIMKickReason.byClientManually:
break
default: //code=NIMKickReason.ByServer:
reason="你被服务器踢下线"
break;
}
appDelegate.window!.makeToast("下线通知:\(reason)", duration: 3, position: CSToastPositionCenter)
AccountManager.shared.logOut(auto: true)
}
/**
* 登录回调
*
* @param step 登录步骤
* @discussion 这个回调主要用于客户端UI的刷新
*/
func onLogin(_ step: NIMLoginStep) {
}
/**
* 自动登录失败回调
*
* @param error 失败原因
* @discussion 自动重连不需要上层开发关心,但是如果发生一些需要上层开发处理的错误,SDK 会通过这个方法回调
* 用户需要处理的情况包括:AppKey 未被设置,参数错误,密码错误,多端登录冲突,账号被封禁,操作过于频繁等
*/
func onAutoLoginFailed(_ error: Error) {
AccountManager.shared.logOut()
}
func onReceive(_ notification: NIMCustomSystemNotification) {
if let info=JSON.fromString(notification.content) {
if info["type"].stringValue=="attach"{//群发通知
let attachItem = NSManagedObject.creatWith(identifier: "Message") as! Message
// if info["fileInfos"].arrayValue != [] {
// var array:[String]=[]
// for j in info["fileInfos"].arrayValue {
// _ = File.newFileByAttach(j)
// array.append(j["guid"].stringValue)
// }
// var fileInfo=Dictionary<String,AnyObject>()
// fileInfo["fileIds"]=array as AnyObject
// attachItem.title=(JSON(fileInfo).rawString() ?? "{}")
// }
attachItem.date=Date(timeIntervalSince1970: notification.timestamp)
// attachItem.msgId=info["msgId"].stringValue
// attachItem.senderId=notification.sender
attachItem.content = info["content"].stringValue
// attachItem.files=info["files"].stringValue
// if info["imageHeight"] != "" {
// var sizeInfo=Dictionary<String,AnyObject>()
// sizeInfo["imageHeight"]=info["imageHeight"].rawValue as AnyObject
// sizeInfo["imageWidth"]=info["imageWidth"].rawValue as AnyObject
// attachItem.userPortrait=JSON(sizeInfo).rawString() ?? "{}"
// }
// attachItem.sessionId="attach"
// if info["isSelectedCityCard"]==true {
// attachItem.type=4
// }else{
// attachItem.type=3
// }
// attachItem.isReceive=1 //unread是否需要加一
// attachItem.isGroup=info["isGroup"].intValue as NSNumber //用来在消息首页是否需要拼合图片
// if let name=Contact.findTeacherByUserId(notification.sender!)?.name{
// attachItem.fromName=name
// }else if let name=Contact.findByUserid(notification.sender!)?.name{
// attachItem.fromName=name
// }else{
// attachItem.fromName="老师"
// }
attachItem.ownId = AccountManager.shared.userid
attachItem.save()
NotificationCenter.default.post(name: Notification.Name(rawValue: MessageNotification.receiveMessage), object: nil, userInfo: nil)
}else if info["type"].stringValue=="ordermess"{//订购通知
let attachItem = NSManagedObject.creatWith(identifier: "Message") as! Message
attachItem.date=Date(timeIntervalSince1970: notification.timestamp)
attachItem.content = info["content"].stringValue
attachItem.ownId = AccountManager.shared.userid
attachItem.save()
NotificationCenter.default.post(name: Notification.Name(rawValue: MessageNotification.receiveMessage), object: nil, userInfo: nil)
}
}
}
}