personCard.js 17.1 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 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
'use strict'
YX.fn.personCard = function () {
	// 初始化节点事件
	this.myInfoEvt()
    this.personInfoEvt()
    //日期控件
    $("#datepicker").datepicker({dateFormat: "yy-mm-dd"})
}
/*****************************
 * 个人信息相关
 ******************************/
YX.fn.myInfoEvt = function () {
	//个人信息
    this.$myInfo = $('#myInfo')

    //修改头像
    this.$modifyAvatar = $('#modifyAvatar')
    //弹出我的信息
    $('#showMyInfo').on('click',this.showMyInfo.bind(this))
    this.$myInfo.delegate('.j-close', 'click', this.hideMyInfoBox.bind(this))
    this.$myInfo.delegate('.operate .j-edit', 'click', this.showEditMyInfo.bind(this))
    this.$myInfo.delegate('.operate .j-cancel', 'click', this.hideEditMyInfo.bind(this))
    this.$myInfo.delegate('.operate .j-save', 'click', this.saveEditMyInfo.bind(this))
    this.$myInfo.delegate('.j-modifyAvatar', 'click', this.showModifyAvatar.bind(this,"my"))
    //修改头像
    this.$modifyAvatar.delegate('.j-close', 'click', this.hideModifyAvatar.bind(this))
    this.$modifyAvatar.delegate('.j-choseFile, .j-reupload', 'click', this.doClickModifyAvatar.bind(this))
    this.$modifyAvatar.delegate('.j-save', 'click', this.doSaveAvatar.bind(this))
    this.$modifyAvatar.delegate('.j-upload','change', this.viewAvatar.bind(this))
    
}
YX.fn.showMyInfo = function () {
    var user = this.cache.getUserById(userUID)
    var $node = this.$myInfo.data({info:user})
    $node.find(".u-icon").attr('src', getAvatar(user.avatar))
    $node.find(".j-nick").text(user.nick)
    $node.find(".j-nickname").text(user.nick)
    var avatarSrc=""
    if(user.gender&&user.gender!=="unknown"){
        avatarSrc = 'images/'+user.gender+'.png'
        $node.find(".j-gender").removeClass("hide")
    }else{
        $node.find(".j-gender").addClass("hide")
    }
    $node.find(".j-gender").attr('src',avatarSrc)
    $node.find(".j-username").text("帐号:"+user.account)
    $node.find(".j-birth").text(user.birth ===undefined?"--":user.birth||"--")
    $node.find(".j-tel").text(user.tel ===undefined?"--":user.tel||"--")
    $node.find(".j-email").text(user.email ===undefined?"--":user.email||"--")
    $node.find(".j-sign").text(user.sign ===undefined?"--":user.sign||"--")
    $node.removeClass('hide')
    this.$mask.removeClass('hide')
}
YX.fn.hideMyInfoBox = function(){
    this.$myInfo.addClass('hide')
    this.$myInfo.removeClass('edit')
    this.$mask.addClass('hide')
}
YX.fn.showEditMyInfo = function(){
    var $node = this.$myInfo,
        user = $node.data("info")
    $node.find(".e-nick").val(user.nick)
    $node.find(".e-gender").val(user.gender)
    if(user.birth !==undefined){
        $node.find(".e-birth").val(user.birth)
    }
    if(user.tel !==undefined){
        $node.find(".e-tel").val(user.tel)
    }
    if(user.email !==undefined){
        $node.find(".e-email").val(user.email)
    }
    if(user.sign !==undefined){
        $node.find(".e-sign").val(user.sign)
    }
    this.$myInfo.addClass('edit')
}
YX.fn.hideEditMyInfo = function(){
    this.$myInfo.removeClass('edit')
}

YX.fn.saveEditMyInfo = function(){
    var $node = this.$myInfo
    var nick = $node.find(".e-nick").val().trim()
    if(!nick){
        alert("昵称不能为空")
        return
    }
    var gender = $node.find(".e-gender").val()
    var birth = $node.find(".e-birth").val().trim()
    var tel = $node.find(".e-tel").val().trim()
    var email = $node.find(".e-email").val().trim()
    if(email&&!/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/.test(email)){
         alert("email格式不正确")
        return
    }
    var sign  = $node.find(".e-sign").val().trim()
    this.mysdk.updateMyInfo(nick,gender,birth,tel,email,sign,this.cbSaveMyInfo.bind(this))
}

YX.fn.cbSaveMyInfo = function(err,data){
    if(err){
        alert(err)
    }else{
        data.account = userUID
        this.cache.updatePersonlist(data)
        this.showMe()
        this.$myInfo.removeClass("edit")
        this.showMyInfo()

    }
}
/*****************************
 * 用户信息
 ******************************/
YX.fn.personInfoEvt = function () {
    //用户信息
    this.$personCard = $('#personCard')
    //聊天内容 点击头像
    this.$chatContent.delegate('.j-img','click',this.showInfoInChat)
    //聊天面板外头像
    $("#headImg").on('click',this.showInfo2.bind(this))
    // 帐号信息面板事件
    this.$personCard.delegate('.j-close', 'click', this.hideInfoBox.bind(this))
    this.$personCard.delegate('.j-saveAlias', 'click', this.addFriendAlias.bind(this))
    this.$personCard.delegate('.j-add', 'click', this.addFriendInBox.bind(this))
    this.$personCard.delegate('.j-del', 'click', this.removeFriend.bind(this))
    this.$personCard.delegate('.j-chat', 'click', this.doChat.bind(this))
    this.$personCard.delegate('.mutelist>.u-switch', 'click', this.doMutelist.bind(this))
    this.$personCard.delegate('.blacklist>.u-switch', 'click', this.doBlacklist.bind(this))
    this.$personCard.delegate('.mute>.u-switch', 'click', this.doMute.bind(this))
}
 // 好友备注
YX.fn.addFriendAlias = function () {
    var account = this.$personCard.data("account")
    var alias = this.$personCard.find(".e-alias").val().trim()
    this.mysdk.updateFriend(account,alias,this.cbAddFriendAlias.bind(this))
}
YX.fn.cbAddFriendAlias = function(err,data){
    if(!err){
        alert("修改备注成功")
        this.$personCard.find(".e-alias").val(data.alias)
        this.cache.updateFriendAlias(data.account,data.alias)
        this.$personCard.find(".j-nick").text(this.getNick(data.account))
        if (this.crtSessionAccount === data.account) { 
            this.$nickName.text(this.getNick(data.account))
        }
        // 左边版本重绘下
        if(this.buildSessions){
            this.buildSessions()    
        } 
        if(this.buildFriends){
            this.buildFriends()    
        }
    }else{
        alert("修改备注失败")
    }
}
YX.fn.addFriendInBox = function(){
    // if(this.$personCard.is(".blacklist")){
    //     return
    // }
    var account = this.$personCard.data("account")
    this.mysdk.addFriend(account,this.cbAddFriendInBox.bind(this))
}
YX.fn.cbAddFriendInBox = function(error, params){
    if(!error){
       this.hideInfoBox()
       this.cache.addFriend(params.friend)
       if(this.buildFriends){
            this.buildFriends()    
        }
   }else{
        alert("添加好友失败")
   }
}
YX.fn.removeFriend = function(){
    if(window.confirm("确定要删除")){
        var account = this.$personCard.data("account")
        this.mysdk.deleteFriend(account,this.cbRemoveFriend.bind(this))
    }
    
}
YX.fn.cbRemoveFriend = function(error, params){
   if(!error){
       this.hideInfoBox()
       this.cache.removeFriend(params.account)
        if (this.crtSessionAccount === params.account) { 
            this.$nickName.text(this.getNick(params.account))
        }   
        if(this.buildFriends){
            this.buildFriends()    
        }
   }else{
    alert("删除好友失败")
   }
}
YX.fn.doChat = function(){
    var account = this.$personCard.data("account")
    this.hideInfoBox()
    var $container
    if(!this.$sessionsWrap.is('.hide')){
        $container = this.$sessionsWrap
    }else if(!this.$friendsWrap.is('.hide')){
        $container = this.$friendsWrap
    }else{
       this.openChatBox(account,"p2p")
       return
    }
    var $li = $container.find('.m-panel li[data-account="'+account+'"]')
    if($li.length>0){
        $li.find(".count").addClass("hide")
        $li.find(".count").text(0)
    }

    this.openChatBox(account,"p2p")
}
YX.fn.doMutelist = function () {
    if(this.$personCard.is(".blacklist")){
        return
    }
    var account = this.$personCard.data("account"),
        status = !this.$personCard.data("inMutelist")
    this.mysdk.markInMutelist(account,status,this.cbDoMutelist.bind(this))

}
YX.fn.cbDoMutelist = function(err,data){
    if(!err){
        if(data.isAdd){
            this.cache.addToMutelist(data.record)
            this.$personCard.data("inMutelist",true)
        }else{
            this.cache.removeFromMutelist(data.account)
            this.$personCard.data("inMutelist",false)
        }
        this.$personCard.find(".mutelist .u-switch").toggleClass("off")          
    }else{
        alert("操作失败")
    }
}
YX.fn.doBlacklist = function(){
    var account = this.$personCard.data("account"),
        status = !this.$personCard.data("inBlacklist")
        this.mysdk.markInBlacklist(account,status,this.cbDoBlacklist.bind(this))
}
YX.fn.cbDoBlacklist = function(err,data){
    if(!err){
        if(data.isAdd){
            this.cache.addToBlacklist(data.record)
            this.$personCard.data("inBlacklist",true)
            this.$personCard.addClass("blacklist")
        }else{
            this.cache.removeFromBlacklist(data.account)
            this.$personCard.data("inBlacklist",false)
            this.$personCard.removeClass("blacklist")
        }
        this.$personCard.find(".blacklist .u-switch").toggleClass("off")
        // 左边版本重绘下黑名单要藏起来
        if(this.buildSessions){
            this.buildSessions()    
        } 
        if(this.buildFriends){
            this.buildFriends()    
        }       
    }else{
        alert("操作失败")
    }
}
//群静音
YX.fn.doMute = function(){
    var account = this.$personCard.data("account"),
        status = !this.$personCard.data("teamMute")
    this.mysdk.updateMuteStateInTeam(this.crtSessionAccount,account,status,this.cbDoMute.bind(this))

}
YX.fn.cbDoMute = function(err,data){
    if(!err){
        var status = data.mute?true:false
        this.$personCard.data('teamMute',status)
        this.$personCard.find(".mute .u-switch").toggleClass("off")          
    }else{
        alert("操作失败")
    }
}
/*******************
 * 修改头像相关
 ********************/
YX.fn.showModifyAvatar = function (type) {
    if(type==="my"){
        this.$myInfo.addClass("hide")
        this.$modifyAvatar.removeClass("hide")
        this.$modifyAvatar.addClass("j-my")
    }else if(type==="team"){
        if($(".j-teamAvatar").hasClass("active")&&this.$teamInfo.data('gtype')==='advanced'){
            this.$modifyAvatar.removeClass("hide")
            this.$modifyAvatar.addClass("j-team")
            this.$mask.removeClass('hide')
        }
    }
}
YX.fn.hideModifyAvatar = function () {
    this.resetCorpAvatar()
    if(this.$modifyAvatar.hasClass('j-my')){
        this.$modifyAvatar.addClass("hide")
        this.$myInfo.removeClass("hide") 
        this.$modifyAvatar.removeClass("j-my")     
    }else{
        this.$modifyAvatar.addClass("hide")
        this.$mask.addClass('hide')
        this.$modifyAvatar.removeClass("j-team")  
    }
}
YX.fn.resetCorpAvatar = function () {
    this.corpParameters = null
    this.avatarUrl = null
    if(!!this.modifyAvatar){
        this.modifyAvatar.disable()     
    }
    this.$modifyAvatar.find(".big img").attr("src","").addClass("hide")
    this.$modifyAvatar.find(".small img").attr("src","").addClass("hide")
    $('#cropImg img').attr("src","").addClass("hide")
    this.$modifyAvatar.find(".choseFileCtn").removeClass("hide")
}
YX.fn.viewAvatar = function () {
    var fileInput = this.$modifyAvatar.find(".j-upload").get(0)
    if(fileInput.files.length === 0){
        return
    }
    this.mysdk.previewImage({fileInput:fileInput,callback:this.cbUploadAvatar.bind(this)})
}
YX.fn.cbUploadAvatar = function (err,data) {
    if(err){
        alert(err)
        return
    }else{
        if(data.w<300||data.h<300){
            alert("图片长宽不能小于300")
            return
        }
        this.showPreAvatar(data.url)
    }
}
YX.fn.showPreAvatar = function (url) {
    var that = this,
        preUrl,
        $choseFileCtn = this.$modifyAvatar.find(".choseFileCtn"),
        $preBig = this.$modifyAvatar.find(".big img"),
        $preSmall = this.$modifyAvatar.find(".small img"),
        $cropImgContainer = $('#cropImg'),
        $cropImg = $cropImgContainer.find("img")
    $choseFileCtn.addClass("hide")
    this.avatarUrl =url
    preUrl = nim.viewImageSync({
        url: url,
        thumbnail: { // 生成缩略图, 可选填
            width: 300,
            height: 300,
            mode: 'cover'
        }
    })
    $preBig.attr("src",preUrl).removeClass("hide")
    $preSmall.attr("src",preUrl).removeClass("hide")
    $cropImg.attr("src",preUrl).removeClass("hide")
     $preBig.css({
        width:160,
        height:160
    })
    $preSmall.css({
        width:40,
        height:40
    })
}
YX.fn.doClickModifyAvatar = function () {
    //置空节点避免2次上传无响应
    this.$modifyAvatar.find(".j-uploadForm").get(0).reset()
    this.$modifyAvatar.find(".j-choseFile").val(null)
    this.$modifyAvatar.find(".j-upload").click()

}
YX.fn.doSaveAvatar = function () {
    var url
    var that = this
    if(!!this.avatarUrl){
        if(this.$modifyAvatar.hasClass('j-my')){
            this.mysdk.updateMyAvatar(this.avatarUrl,this.cbSaveMyAvatar.bind(this))
        }else{
            this.mysdk.updateTeam({
                teamId: this.crtSessionAccount,
                avatar: this.avatarUrl,
                done: function(error, data) {
                    if (!error) {
                        var url = data.avatar
                        $(".j-teamAvatar")[0].src = url+"?imageView&thumbnail=40y40"
                        $("#headImg")[0].src = url+"?imageView&thumbnail=56y56"
                        that.hideModifyAvatar()
                    } else {
                        alert('修改群头像失败')
                    }
                }
            })
        }
    }else{
        alert("请上传一张头像")
    }
}
YX.fn.cbSaveMyAvatar = function (err,data) {
    if(err){
        alert("修改头像失败")
        console.log(err)
    }else{
        this.cache.updateAvatar(data.avatar)
        this.hideModifyAvatar()
        this.showMyInfo()
        this.showMe()
    }
}
/**
 * 用户名片
 */
YX.fn.showInfo = function (account,type) {
	if(type=="p2p"){
        var user = this.cache.getUserById(account)
        this.showInfoBox(user) 
    }
}
// 从聊天面板头部头像点进去
YX.fn.showInfo2 = function(){
    if(this.crtSessionType==="p2p"){
        var account = this.crtSessionAccount
        var user = this.cache.getUserById(account)
        this.showInfoBox(user) 
    }
}
// 从聊天面板头像点进去
YX.fn.showInfoInChat = function(account){
    var account = $(this).attr('data-account'),
        user = yunXin.cache.getUserById(account)
    if(account==userUID){
        return
    }
    if(yunXin.$teamInfo.data('gtype')==='advanced'){
        yunXin.showInfoBox(user,'team') 
    }else{
        yunXin.showInfoBox(user) 
    }
}

YX.fn.showInfoBox = function(user,type){
    if(user.account === userUID){
        this.showMyInfo()
        return
    }
    //高级群禁言功能
    var teamMute = false
    if(type==="team"){
        var isManager = this.cache.isTeamManager(userUID,this.crtSessionAccount)
        if(isManager){
            $('#setTeamMute').removeClass('hide')
            teamMute = this.cache.getTeamMemberInfo(user.account,this.crtSessionAccount).mute
        }else{
            $('#setTeamMute').addClass('hide')
        }
    }else{
        $('#setTeamMute').addClass('hide')
    }
    var isFriend = this.cache.isFriend(user.account)
    var inMutelist = this.cache.inMutelist(user.account)
    var inBlacklist = this.cache.inBlacklist(user.account)
    var $node = this.$personCard.data({account:user.account,inMutelist:inMutelist?true:false,inBlacklist:inBlacklist?true:false,teamMute:teamMute})
    $node.find(".u-icon").attr('src', getAvatar(user.avatar))
    $node.find(".j-nickname").text("昵称:"+user.nick)
    $node.find(".j-nick").text(this.getNick(user.account))
    var avatarSrc =""
    if(user.gender&&user.gender!=="unknown"){
        avatarSrc = 'images/'+user.gender+'.png'
        $node.find(".j-gender").removeClass("hide")
    }else{
        $node.find(".j-gender").addClass("hide")
    }
    $node.find(".j-gender").attr('src',avatarSrc)
    $node.find(".j-username").text("帐号:"+user.account)
    $node.find(".j-birth").text(user.birth ===undefined?"--":user.birth)
    $node.find(".j-tel").text(user.tel ===undefined?"--":user.tel)
    $node.find(".j-email").text(user.email ===undefined?"--":user.email)
    $node.find(".j-sign").text(user.sign ===undefined?"--":user.sign)
    if(inMutelist){
         $node.find(".mutelist>.u-switch").addClass('off')
    }
    if(!inBlacklist){
        $node.find(".blacklist>.u-switch").addClass('off')
    }else{
        $node.addClass('blacklist')
    }
    if(!isFriend){
        $node.addClass("notFriend")
    }else{
        var alias = this.cache.getFriendAlias(user.account)
        $node.find(".e-alias").val(alias)
    }
    if(teamMute){
        $node.find(".mute>.u-switch").removeClass('off')
    }else{
        $node.find(".mute>.u-switch").addClass('off')
    }
    $node.removeClass('hide')
    this.$mask.removeClass('hide')
}
YX.fn.hideInfoBox = function(){
    this.$personCard.addClass('hide')
    this.$mask.addClass('hide')
    this.$personCard.removeClass('notFriend')
    this.$personCard.removeClass('blacklist')
    this.$personCard.find(".mutelist .u-switch").removeClass('off')
    this.$personCard.find(".blacklist .u-switch").removeClass('off')        
}