friend_manager.js
12 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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
//定义我的好友表格每行的操作按钮
function gmfOperateFormatter(value, row, index) {
return [
'<a class="envelope" href="javascript:void(0)" title="发消息">',
'<i class="glyphicon glyphicon-envelope"></i>',
'</a>',
'<a class="ban-circle" href="javascript:void(0)" title="拉黑">',
'<i class="glyphicon glyphicon-ban-circle"></i>',
'</a>',
'<a class="remove ml10" href="javascript:void(0)" title="删除">',
'<i class="glyphicon glyphicon-remove"></i>',
'</a>'
].join('');
}
//我的好友表格每行的操作按钮点击事件
window.gmfOperateEvents = {
'click .envelope': function (e, value, row, index) {
$('#scm_to_account').val(row.Info_Account);
$('#send_c2c_msg_dialog').modal('show');
},
'click .ban-circle': function (e, value, row, index) {
if (confirm("确定将该好友拉黑吗?")) {
addBlackList(row.Info_Account);
}
},
'click .remove': function (e, value, row, index) {
$('#df_to_account').val(row.Info_Account);
$('#delete_friend_dialog').modal('show');
}
};
//初始化我的好友表格
function initGetMyFriendTable(data) {
$('#get_my_friend_table').bootstrapTable({
method: 'get',
cache: false,
height: 500,
striped: true,
pagination: true,
sidePagination: 'client',
pageSize: pageSize,
pageNumber: 1,
pageList: [10, 20, 50, 100],
search: true,
showColumns: true,
clickToSelect: true,
columns: [
{field: "Info_Account", title: "账号", align: "center", valign: "middle", sortable: "true"},
{field: "Nick", title: "昵称", align: "center", valign: "middle", sortable: "true"},
{field: "Gender", title: "性别", align: "center", valign: "middle", sortable: "true"},
{field: "gmfOperate",title: "操作",align: "center",valign: "middle",formatter: "gmfOperateFormatter",events: "gmfOperateEvents"}
],
data: data,
formatNoMatches: function () {
return '无符合条件的记录';
}
});
}
//申请加好友
var applyAddFriend = function () {
var len = webim.Tool.getStrBytes($("#af_add_wording").val());
if (len > 120) {
alert('您输入的附言超过字数限制(最长40个汉字)');
return;
}
var add_friend_item = [
{
'To_Account': $("#af_to_account").val(),
"AddSource": "AddSource_Type_Unknow",
"AddWording": $("#af_add_wording").val() //加好友附言,可为空
}
];
var options = {
'From_Account': loginInfo.identifier,
'AddFriendItem': add_friend_item
};
webim.applyAddFriend(
options,
function (resp) {
if (resp.Fail_Account && resp.Fail_Account.length > 0) {
for (var i in resp.ResultItem) {
alert(resp.ResultItem[i].ResultInfo);
break;
}
} else {
if ($('#af_allow_type').val() == '允许任何人') {
//重新加载好友列表
//getAllFriend(getAllFriendsCallbackOK);
alert('添加好友成功');
} else {
$('#add_friend_dialog').modal('hide');
alert('申请添加好友成功');
}
}
},
function (err) {
alert(err.ErrorInfo);
}
);
};
//删除好友
var deleteFriend = function () {
if (!confirm("确定删除该好友吗?")) {
return;
}
var to_account = [];
to_account = [
$("#df_to_account").val()
];
if (to_account.length <= 0) {
return;
}
var options = {
'From_Account': loginInfo.identifier,
'To_Account': to_account,
//Delete_Type_Both'//单向删除:"Delete_Type_Single", 双向删除:"Delete_Type_Both".
'DeleteType': $('input[name="df_type_radio"]:checked').val()
};
webim.deleteFriend(
options,
function (resp) {
//在表格中删除对应的行
$('#get_my_friend_table').bootstrapTable('remove', {
field: 'Info_Account',
values: [$("#df_to_account").val()]
});
//重新加载好友列表
//getAllFriend(getAllFriendsCallbackOK);
deleteSessDiv(webim.SESSION_TYPE.C2C,$("#df_to_account").val());//在最近联系人列表中删除好友(如果存在的话)
$('#delete_friend_dialog').modal('hide');
alert('删除好友成功');
},
function (err) {
alert(err.ErrorInfo);
}
);
};
//获取我的好友
var getMyFriend = function () {
//清空
initGetMyFriendTable([]);
var options = {
'From_Account': loginInfo.identifier,
'TimeStamp': 0,
'StartIndex': 0,
'GetCount': totalCount,
'LastStandardSequence': 0,
"TagList": [
"Tag_Profile_IM_Nick",
"Tag_SNS_IM_Remark",
"Tag_Profile_IM_Gender"
]
};
webim.getAllFriend(
options,
function (resp) {
if (resp.FriendNum > 0) {
var table_friends_data = [];
var friends = resp.InfoItem;
if (!friends || friends.length == 0) {
alert('你目前还没有好友');
return;
}
var count = friends.length;
for (var i = 0; i < count; i++) {
var friend_name = friends[i].Info_Account;
var gender = "未知";
if (friends[i].SnsProfileItem) {
for (var j in friends[i].SnsProfileItem) {
switch (friends[i].SnsProfileItem[j].Tag) {
case 'Tag_Profile_IM_Nick':
friend_name = friends[i].SnsProfileItem[j].Value;
break;
case 'Tag_Profile_IM_Gender':
switch (friends[i].SnsProfileItem[j].Value) {
case 'Gender_Type_Male':
gender = '男';
break;
case 'Gender_Type_Female':
gender = '女';
break;
case 'Gender_Type_Unknown':
gender = '未知';
break;
}
break;
}
}
}
table_friends_data.push({
'Info_Account': friends[i].Info_Account,
'Nick': webim.Tool.formatText2Html(friend_name),
'Gender': gender
});
}
//打开我的好友列表对话框
$('#get_my_friend_table').bootstrapTable('load', table_friends_data);
$('#get_my_friend_dialog').modal('show');
} else {
alert('您目前还没有好友');
}
},
function (err) {
alert(err.ErrorInfo);
}
);
};
//从我的好友列表中给好友发消息
function sendC2cMsg(){
toAccount=$("#scm_to_account").val();
msgtosend=$("#scm_content").val();
var msgLen = webim.Tool.getStrBytes(msgtosend);
var maxLen, errInfo;
maxLen = webim.MSG_MAX_LENGTH.C2C;
errInfo = "消息长度超出限制(最多" + Math.round(maxLen / 3) + "汉字)";
if (msgtosend.length < 1) {
alert("发送的消息不能为空!");
$("#send_msg_text").val('');
return;
}
if (msgLen > maxLen) {
alert(errInfo);
return;
}
var sess=webim.MsgStore.sessByTypeId(webim.SESSION_TYPE.C2C, toAccount);
if (!sess) {
sess = new webim.Session(webim.SESSION_TYPE.C2C, toAccount, toAccount, friendHeadUrl, Math.round(new Date().getTime() / 1000));
}
var isSend = true;//是否为自己发送
var seq = -1;//消息序列,-1表示sdk自动生成,用于去重
var random = Math.round(Math.random() * 4294967296);//消息随机数,用于去重
var msgTime = Math.round(new Date().getTime() / 1000);//消息时间戳
var subType;//消息子类型
subType = webim.C2C_MSG_SUB_TYPE.COMMON;
var msg = new webim.Msg(sess, isSend, seq, random, msgTime, loginInfo.identifier, subType, loginInfo.identifierNick);
var text_obj;
text_obj = new webim.Msg.Elem.Text(msgtosend);
msg.addText(text_obj);
webim.sendMsg(msg, function (resp) {
if (!selToID) {//没有聊天会话
selType=webim.SESSION_TYPE.C2C;
selToID=toAccount;
selSess=sess;
addSess(selType,toAccount, toAccount, friendHeadUrl, 0, 'sesslist');
setSelSessStyleOn(toAccount);
//私聊时,在聊天窗口手动添加一条发的消息,群聊时,长轮询接口会返回自己发的消息
addMsg(msg);
}else{//有聊天会话
if(selToID==toAccount){//聊天对象不变
addMsg(msg);
}else{//聊天对象发生改变
var tempSessDiv = document.getElementById("sessDiv_" + toAccount);
if(!tempSessDiv){//不存在这个会话
addSess(webim.SESSION_TYPE.C2C,toAccount, toAccount, friendHeadUrl, 0, 'sesslist');//增加一个会话
}
onSelSess(webim.SESSION_TYPE.C2C,toAccount);//再进行切换
}
}
webim.Tool.setCookie("tmpmsg_" + toAccount, '', 0);
$("#scm_content").val('');
$('#send_c2c_msg_dialog').modal('hide');
$('#get_my_friend_dialog').modal('hide');
}, function (err) {
alert(err.ErrorInfo);
$("#scm_content").val('');
});
}
//将我的好友资料(昵称和头像)保存在infoMap
var initInfoMapByMyFriends = function (cbOK) {
var options = {
'From_Account': loginInfo.identifier,
'TimeStamp': 0,
'StartIndex': 0,
'GetCount': totalCount,
'LastStandardSequence': 0,
"TagList": [
"Tag_Profile_IM_Nick",
"Tag_Profile_IM_Image"
]
};
webim.getAllFriend(
options,
function (resp) {
if (resp.FriendNum > 0) {
var friends = resp.InfoItem;
if (!friends || friends.length == 0) {
if (cbOK)
cbOK();
return;
}
var count = friends.length;
for (var i = 0; i < count; i++) {
var friend=friends[i];
var friend_account = friend.Info_Account;
var friend_name=friend_image='';
for (var j in friend.SnsProfileItem) {
switch (friend.SnsProfileItem[j].Tag) {
case 'Tag_Profile_IM_Nick':
friend_name = friend.SnsProfileItem[j].Value;
break;
case 'Tag_Profile_IM_Image':
friend_image = friend.SnsProfileItem[j].Value;
break;
}
}
var key=webim.SESSION_TYPE.C2C+"_"+friend_account;
infoMap[key]={
'name':friend_name,
'image':friend_image
};
}
if (cbOK)
cbOK();
}
},
function (err) {
alert(err.ErrorInfo);
}
);
};