friend.js
6.5 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
/*
* 好友相关
*/
'use strict'
YX.fn.friend = function () {
//添加好友
this.$addFriend = $('#addFriend')
this.$addFriendBox = $('#addFriendBox')
this.$addFriend.on('click',this.showAddFriend.bind(this))
this.$addFriendBox.delegate('.j-close', 'click', this.hideAddFriend.bind(this))
this.$addFriendBox.delegate('.j-search','click',this.searchFriend.bind(this))
this.$addFriendBox.delegate('.j-back','click',this.resetSearchFriend.bind(this))
this.$addFriendBox.delegate('.j-add','click',this.addFriend.bind(this))
this.$addFriendBox.delegate('.j-blacklist','click',this.rmBlacklist.bind(this))
this.$addFriendBox.delegate('.j-chat','click',this.beginChat.bind(this))
this.$addFriendBox.delegate('.j-account','keydown',this.inputAddFriend.bind(this))
//黑名单
this.$blacklist = $('#blacklist')
$("#showBlacklist").on('click',this.showBlacklist.bind(this))
this.$blacklist.delegate('.j-close', 'click', this.hideBlacklist.bind(this))
this.$blacklist.delegate('.items .j-rm', 'click', this.removeFromBlacklist.bind(this))
//我的手机
$("#myPhone").on('click',this.sendToMyPhone.bind(this))
}
/**
* 通讯录列表显示
* @param {object} 数据对象
* @return {void}
*/
YX.fn.buildFriends = function () {
var data = {
friends:this.cache.getFriendslistOnShow(),
personSubscribes: this.cache.getPersonSubscribes(),
account:userUID
}
if(!this.friends){
var options = {
data:data,
onclickavatar:this.showInfo.bind(this),
onclickitem:this.openChatBox.bind(this),
infoprovider:this.infoProvider.bind(this)
}
this.friends = new NIMUIKit.FriendList(options)
this.friends.inject($('#friends').get(0))
}else{
this.friends.update(data)
}
this.doPoint()
}
/**
* 添加好友相关
*
*/
YX.fn.showAddFriend = function(){
this.friendData = null
this.$addFriendBox.removeClass("hide")
this.$mask.removeClass('hide')
this.$addFriendBox.find(".j-account").focus()
}
YX.fn.hideAddFriend = function(){
this.resetSearchFriend()
this.$addFriendBox.addClass("hide")
this.$mask.addClass('hide')
}
YX.fn.searchFriend = function(){
var account = $.trim(this.$addFriendBox.find(".j-account").val().toLowerCase())
if(account!==""){
this.mysdk.getUser(account,this.cbGetUserInfo.bind(this))
}
}
YX.fn.beginChat = function(){
var account = $.trim(this.$addFriendBox.find(".j-account").val().toLowerCase())
this.hideAddFriend()
this.openChatBox(account,"p2p")
}
YX.fn.resetSearchFriend = function(){
this.$addFriendBox.attr('class',"m-dialog")
this.$addFriendBox.find(".j-account").val("")
}
YX.fn.addFriend = function(){
var id = $.trim(this.$addFriendBox.find(".j-account").val().toLowerCase())
this.mysdk.addFriend(id,this.cbAddFriend.bind(this))
}
YX.fn.rmBlacklist = function(){
var id = $.trim(this.$addFriendBox.find(".j-account").val().toLowerCase())
this.mysdk.markInBlacklist(id,false,this.cbRmBlacklist.bind(this))
}
YX.fn.cbRmBlacklist = function(err,data){
if(!err){
var account = data.account
this.cache.removeFromBlacklist(account)
this.buildFriends()
var isFriend = this.cache.isFriend(account)
if(!isFriend){
this.friendData = data
}
this.$addFriendBox.removeClass('blacklist')
this.$addFriendBox.addClass(isFriend ? "friend" : "noFriend")
}
}
YX.fn.inputAddFriend = function(evt){
if(evt.keyCode==13){
$("#addFriendBox .j-account").blur()
this.searchFriend()
}
}
YX.fn.cbAddFriend = function(error, params) {
if(!error){
this.$addFriendBox.find(".tip").html("添加好友成功!")
this.$addFriendBox.attr('class',"m-dialog done")
this.cache.addFriend(params.friend)
this.cache.updatePersonlist(this.friendData)
this.buildFriends()
// 订阅好友登录事件
this.mysdk.subscribeMultiPortEvent([params.account])
}else{
this.$addFriendBox.find(".tip").html("该帐号不存在,请检查你输入的帐号是否正确")
this.$addFriendBox.attr('class',"m-dialog done")
}
}
YX.fn.cbGetUserInfo = function(err,data){
if(err){
alert(err)
}
if(!!data){
var $info = this.$addFriendBox.find(".info"),
user = data,
account = data.account
$info.find("img").attr("src",getAvatar(user.avatar))
$info.find(".j-nickname").html(user.nick)
$info.find(".j-username").html("帐号:"+ account)
if(account == userUID){
this.$addFriendBox.find(".tip").html("别看了就是你自己")
this.$addFriendBox.attr('class',"m-dialog done")
}else{
var isFriend = this.cache.isFriend(account),
inBlacklist = this.cache.inBlacklist(account)
if(!isFriend){
this.friendData = data
}
if(inBlacklist){
this.$addFriendBox.addClass("blacklist")
}else{
this.$addFriendBox.addClass(isFriend?"friend":"noFriend")
}
}
}else{
this.$addFriendBox.find(".tip").html("该帐号不存在,请检查你输入的帐号是否正确")
this.$addFriendBox.attr('class',"m-dialog done")
}
}
/************************
* 黑名单列表相关
************************/
YX.fn.showBlacklist = function() {
var data = this.cache.getBlacklist()
var html = appUI.buildBlacklist(data,this.cache)
this.$blacklist.find('.list').html(html)
this.$blacklist.removeClass('hide')
this.$mask.removeClass("hide")
document.documentElement.style.overflow='hidden'
}
YX.fn.hideBlacklist = function() {
$("#blacklist").addClass('hide')
this.$mask.addClass("hide")
document.documentElement.style.overflow=''
},
YX.fn.removeFromBlacklist = function(evt){
var id = $(evt.target).attr("data-id")
this.mysdk.markInBlacklist(id,false, this.cbRemoveFromBlacklist.bind(this))
}
YX.fn.cbRemoveFromBlacklist = function (err,data) {
if(!err){
this.cache.removeFromBlacklist(data.account)
var html = appUI.buildBlacklist(this.cache.getBlacklist(),this.cache)
this.$blacklist.find('.list').html(html)
this.buildFriends()
}else{
alert("操作失败")
}
}
/**
* 我的手机
*/
YX.fn.sendToMyPhone = function () {
this.openChatBox(userUID,"p2p")
}