var html = $.map(msgList, function (val) {
            var _type = val.msgType;
            var _detail = val.detail;
            var _time = val.sentTime;
            var target = val.sendUserId;
            var _direction = "",
                _avatar,
                _html = "";

            if (target.substr(16) == user_id) {
                _direction = "my-";
                _avatar = $scope.sourceInfo.avatar;
            } else {
                _avatar = $scope.chatList[target].avatar || Util.getUserAvatar(target);
            }

            // 根据最后消息时间,是否需要添加聊天时间
            var _lastTime = chatLast[$scope.currentChatId];
            if (!_lastTime || Math.abs(_time - _lastTime) > showTimeInterval) {
                _html += '<div class="webim-chat-tips"><span>' + self.formatDate(_time) + '</span></div>';
            }
            chatLast[$scope.currentChatId] = _time;

            _html += baiduTpl.template(msg_item, {
                direction: _direction,
                avatar: _avatar,
                detail: _detail,
                type: _type
            });
            return _html;
        });
 getUserInfoByCookie: function () {
     var getCookie = cookie.get;
     var id = getCookie('userid');
     var userInfo = {
         id: id,
         brandName: getCookie('brandname') || getCookie('nickname'),
         avatar: utils.getUserAvatar( id )
     };
     return userInfo;
 },
 processData: function( data ){
     var goldTip = {
         1: '银牌',
         2: '金牌',
         3: '钻石',
         4: '皇冠'
     };
     if ( this.type == 'witkey' ) {
         data.avatar = utils.getUserAvatar( this.employId, 'middle' );
         if ( data.userMeta.isGolden != '0') {
             data.userMeta.goldTip = goldTip[ data.userMeta.isGolden ];
         }
         if ( data.userMeta.level ) {
             data.userMeta.levelImg = utils.getUserLevelImg( data.userMeta.level );
         }
         data.userMeta.is_mall = data.is_mall;
     }
 },
    tplUserItem: function (target, name, avatar, unread, callback, appendto) {

        if (getTargetUser(target).length > 0) {
            return false;
        }

        if (!name || !avatar) {
            var userId = target.substr(16);
            avatar = Util.getUserAvatar(userId);

            //获取聊天ID
            $.ajax({
                url: NAME_URL,
                data: {"user_id": userId},
                dataType: 'jsonp',
                jsonp: 'jsonpcallback',
                success: function (data) {

                    if (data.state == 1) {
                        name = data.msg;
                    } else {
                        name = "用户" + userId;
                    }

                    $scope.chatList[target] = {
                        id: target,
                        name: name,
                        avatar: avatar,
                        unread: unread
                    };

                    var user_item_tpl = baiduTpl.template(user_item, {
                        name: name,
                        id: target,
                        avatar: avatar,
                        unread: unread
                    });

                    if (appendto == "prepend") {
                        user_list.prepend(user_item_tpl);
                    } else {
                        user_list.append(user_item_tpl);
                    }
                    typeof callback === 'function' && callback();
                }
            });

        } else {

            var user_item_tpl = baiduTpl.template(user_item, {
                name: name,
                id: target,
                avatar: avatar,
                unread: unread
            });

            if (appendto == "prepend") {
                user_list.prepend(user_item_tpl);
            } else {
                user_list.append(user_item_tpl);
            }

            typeof callback === 'function' && callback();
        }
    },
    proxyIframe.onload = function () {
        proxy = proxyIframe.contentWindow.store;
    };

    proxyIframe.src = _proxyUrl;
    proxyIframe.style.display = 'none';
    document.body.appendChild(proxyIframe);
}

//// 聊天私有变量存$scope
var $scope = proxy.get("rongChat-" + user_id) || {chatHistory: {}, chatList: {}, chatSort: []};

// 登录用户信息
$scope.sourceInfo = {
    id: user_id,
    avatar: Util.getUserAvatar(user_id)
};
//var $scope = {
//    sourceInfo = {id: ,avatar: }
//    isMute:  false, // 是否禁音
//    currentChatId:  '', // 当前聊天Id
//    chatHistory:  {},//最近一次历史记录的消息时间 {"登录id-目标id":[{content:'',msgType:'',sentTime:"", sendUserId: 000000}]}
//    chatList:  {} //会话列表 {"登录id-目标id":{name:'',avatar:'',unread:""}}
//    chatSort: [目标id,目标id2]
//};

// 初始化html
var wrapTpl = require('./wrap.tmpl')({isMute: $scope.isMute});
$(document.body).append(wrapTpl);

// 会话列表tpl