﻿$(".LayImAhrefNode").click(function () {
    var id = $("#imid").val();
    var name = $("#imname").val();
    javascript: LoadLayIM.OpenLayIM('0', '', '', id, name, '/Content/kcim/images/avatar.png', '');
});
var LoadLayIM = {
    B2BLayIMUrl: "",
    PlatFrom: "",
    YQBLayIMUrl: "",
    yqb_visitor_email: "",
    Pimg: "",
    Pname: "",
    Purl: "",
    cip: "",
    cCountry: "",
    ChatSign: "",
    cUname: "",
    cIco: "",
    reg: /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4})$/,
    isMin: false,
    LoadJS: function (name, flag) {
        var Url = "/Content/kcim";

        if (flag) {

            Url = "";
        }
        var _element = document.createElement("script");
        _element.setAttribute("type", "text/javascript");
        _element.setAttribute("src", Url + name);
        document.body.appendChild(_element);
    },
    LoadCSS: function (name) {
        var _element = document.createElement("link");
        _element.setAttribute("rel", "stylesheet");
        _element.setAttribute("ttpe", "text/css");
        _element.setAttribute("href", "/Content/kcim" + name);
        document.getElementsByTagName("head")[0].appendChild(_element);
    },
    Init: function (platfrom) {
        var img = new Image();
        img.src = "/Content/kcim/lay/css/modules/layer/default/icon.png";
        this.PlatFrom = platfrom;
        if (platfrom === "mobile") {
            this.LoadJS("/js/mobile/emoji.js", false);
            this.LoadJS("/js/mobile/NIM_Web_NIM_v4.8.0.js", false);
            this.LoadJS("/lay/layui.js", false);
            this.LoadJS("/lay/lay/modules/jquery.js", false);
            this.LoadJS("/js/mobile/Super_im_lay.js?t=20180730981", false);
            this.LoadCSS("/lay/css/layui.mobile.css");
        }
        else {
            this.LoadJS("/js/emoji.js", false);
            this.LoadJS("/js/NIM_Web_NIM_v4.8.0.js", false);
            this.LoadJS("/lay/layui.js", false);
            this.LoadJS("/lay/lay/modules/jquery.js", false);
            this.LoadJS("/js/Super_im_lay.js?t=20180730981", false);
            this.LoadCSS("/lay/css/layui.css");
        }

        $.ajax({
            type: "get",
            url: this.B2BLayIMUrl + "/OutOpen/GetRealIP",
            data: null,
            success: function (data) {
                var jsonobj = eval(data);
                LoadLayIM.cip = jsonobj.cip;
                LoadLayIM.cCountry = jsonobj.cname;
            },
            error: function () {
                LoadLayIM.cip = "";
                LoadLayIM.cCountry = "";
            }
        });

    },
    RandomWord: function (randomFlag, min, max) {
        var str = "",
            range = min,
            arr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
        if (randomFlag) {
            range = Math.round(Math.random() * (max - min)) + min;
        }
        for (var i = 0; i < range; i++) {
            pos = Math.round(Math.random() * (arr.length - 1));
            str += arr[pos];
        }
        return str;
    },
    //全局聊天，暂时没有用到
    gobalChat: function (userId, loginName, headImage) {
        var id = "yqb_" + userId;
        var local = layui.data('layim')[id] || {};
        var friends = local.chatlog = local.chatlog || {};
        layui.each(friends, function (index, item) {
            var index = item.length - 1;
            var friend = item[index];
            if (friend.chatname)
                LoadLayIM.OpenLayIM(userId + '*', loginName, headImage, friend.id.split('_')[1], friend.chatname, friend.chatimg);
        });
    },
    OpenLayIM: function (UserId, UserName, UserImage, ChatId, ChatName, ChatImage, ChatSign) {
        thisObj = this;
        var func = function () {

            //用于全局聊天
            if (UserId.toString().indexOf('*') >= 1) {
                UserId = UserId.split('*')[0];
                thisObj.isMin = true;
            }

            LoadLayIM.cUname = ChatName;
            LoadLayIM.cIco = ChatImage;
            LoadLayIM.ChatSign = ChatSign;

            var _ip = LoadLayIM.cip;

            if (UserId == 0) {
                UserId = localStorage.getItem("YQB_Visitor" + ChatId);
                var isupdate = false;

                if (UserId == null) {
                    var reg = new RegExp("\\.", "g");

                    if ("" == _ip || null == _ip || undefined == _ip)
                        UserId = LoadLayIM.RandomWord(true, 4, 12) + ChatId;
                    else
                        UserId = _ip.replace(reg, "_") + ChatId;

                    isupdate = true;
                }
                else {
                    if (UserId == ChatId) {
                        UserId = LoadLayIM.RandomWord(true, 4, 12) + ChatId;
                        isupdate = true;
                    }
                }

                var setemail = localStorage.getItem("YQB_Visitor_Eamil" + ChatId);
                if (setemail == null) {
                    if ("" == _ip || null == _ip || undefined == _ip)
                        UserName = "UN" + UserId;
                    else
                        UserName = LoadLayIM.cCountry + "(" + _ip + ")";
                }
                else {
                    var index = setemail.indexOf('@');
                    var email_slice = "";
                    if (index > -1) {
                        email_slice = setemail.slice(0, index);
                    }
                    if ("" == _ip || null == _ip || undefined == _ip)
                        UserName = "UN" + UserId + email_slice;
                    else
                        UserName = email_slice + "," + LoadLayIM.cCountry;
                }

                if (isupdate) {
                    localStorage.setItem("YQB_Visitor" + ChatId, UserId);
                }

                LoadLayIM.openChatBox(UserId, UserName, UserImage, ChatId, ChatName, ChatImage);
            }
            else {
                LoadLayIM.openChatBox(UserId, UserName, UserImage, ChatId, ChatName, ChatImage);
            }
        };
        if($.fn.jquery>'3'){ 
            $.getScript("https://cdn.bootcdn.net/ajax/libs/jquery-migrate/3.4.0/jquery-migrate.min.js", function(){
                func();

            }).fail(function(){func()});
        }else{
            func();
        }
    },
    openChatBox: function (UserId, UserName, UserImage, ChatId, ChatName, ChatImage) {
        var _ip = LoadLayIM.cip;
        var _region = LoadLayIM.cCountry;

        $.ajax({
            type: "get",
            url: this.B2BLayIMUrl + "/OutOpen/GetImSign",
            data: { UserId: UserId },
            success: function (data) {
                SuperIm.Init({
                    appKey: data.AppKey,
                    uid: UserId,
                    unixTime: data.UnixTime,
                    checkSum: data.CheckSum,
                    lang: "zh-cn",
                    name: UserName,
                    avatar: UserImage,
                    ip: _ip,
                    region: _region,
                    isMin: this.isMin,
                    platfrom: LoadLayIM.PlatFrom,
                    chat: {
                        name: ChatName,
                        open_id: "yqb_" + ChatId,
                        avatar: ChatImage,
                        sign: LoadLayIM.ChatSign
                    },
                    onImConnected: function (e) {
                        if (null == localStorage.getItem("YQB_Visitor_First" + ChatId)) {
                            var info = "网站用户来自于: " + _region + ",IP为: " + _ip;
                            SuperIm.sendMsg("yqb_" + ChatId, info);

                            localStorage.setItem("YQB_Visitor_First" + ChatId, true);
                            if (localStorage.getItem("YQB_Visitor_Eamil" + ChatId) != "" && localStorage.getItem("YQB_Visitor_Eamil" + ChatId) != null && localStorage.getItem("YQB_Visitor_Eamil" + ChatId) != "null") {
                                SuperIm.sendMsg("yqb_" + ChatId, localStorage.getItem("YQB_Visitor_Eamil" + ChatId));
                            }
                        }
                    }
                });
            }
        });
    }
}