mobileChat.js 66.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 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 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124
function Chat() {
                this.socket = null
                this.salesid = G_salesid
                this.fansid = G_fansid
                this.isvank = G_isvank
                this.firstOpenChat = true
                this.host = (location.host.indexOf("192") > -1 || location.host.indexOf("localhost") > -1) ? location.host + "/zzhnc" : location.host
                this.url = "ws://" + this.host + "/web/socket/" + G_salesid + "/" + G_isvank         //  mini.weiyisz.com
                this.fansListPage = 1
                this.fansInfo = {}  //当前的粉丝信息
                this.pages = {}     //所有的粉丝记录所在的当前的
                this.faceCode = ["/::)", "/::~", "/::B", "/::|", "/:8-)", "/::<", "/::$", "/::X", "/::Z", "/::'(", "/::-|", "/::@", "/::P", "/::D", "/::O", "/::(", "/::+", "/:–b", "/::Q", "/::T", "/:,@P", "/:,@-D", "/::d", "/:,@o", "/::g", "/:|-)", "/::!", "/::L", "/::>", "/::,@", "/:,@f", "/::-S", "/:?", "/:,@x", "/:,@@", "/::8", "/:,@!", "/:!!!", "/:xx", "/:bye", "/:wipe", "/:dig", "/:handclap", "/:&-(", "/:B-)", "/:<@", "/:@>", "/::-O", "/:>-|", "/:P-(", "/::'|", "/:X-)", "/::*", "/:@x", "/:8*", "/:pd", "/:<W>", "/:beer", "/:basketb", "/:oo", "/:coffee", "/:eat", "/:pig", "/:rose", "/:fade", "/:showlove", "/:heart", "/:break", "/:cake", "/:li", "/:bome", "/:kn", "/:footb", "/:ladybug", "/:shit", "/:moon", "/:sun", "/:gift", "/:hug", "/:strong", "/:weak", "/:share", "/:v", "/:@)", "/:jj", "/:@@", "/:bad", "/:lvu", "/:no", "/:ok", "/:love", "/:<L>", "/:jump", "/:shake", "/:<O>", "/:circle", "/:kotow", "/:turn", "/:skip", "/:oy", "/:#-0", "/:oy", "/:kiss", "/:<&", "/:&>"]
                this.faceText = ["微笑", "撇嘴", "色", "发呆", "得意", "流泪", "害羞", "闭嘴", "睡", "大哭", "尴尬", "发怒", "调皮", "呲牙", "惊讶", "难过", "酷", "冷汗", "抓狂", "吐", "偷笑", "愉快", "白银", "傲慢", "饥饿", "困", "恐慌", "流汗", "憨笑", "悠闲", "奋斗", "咒骂", "疑问", "嘘", "晕", "疯了", "哀", "骷髅", "敲打", "再见", "擦汗", "抠鼻", "鼓掌", "糗大了", "坏笑", "左哼哼", "右哼哼", "哈欠", "鄙视", "委屈", "快哭了", "阴险", "亲亲", "吓", "可怜", "菜刀", "西瓜", "啤酒", "篮球", "乒乓", "咖啡", "饭", "猪头", "玫瑰", "凋谢", "嘴唇", "爱心", "心碎", "蛋糕", "闪电", "炸弹", "刀", "足球", "瓢虫", "便便", "月亮", "太阳", "礼物", "拥抱", "强", "弱", "握手", "胜利", "抱拳", "勾引", "拳头", "差劲", "爱你", "NO", "OK", "爱情", "飞吻", "跳跳", "发抖", "怄火", "转圈", "磕头", "回头", "跳绳", "投降", "激动", "乱舞", "献吻", "左太极", "右太极"]
                this.faceText2 = ["/微笑","/撇嘴","/色","/发呆","/得意","/流泪","/害羞","/闭嘴","/睡","/大哭","/尴尬","/发怒","/调皮","/呲牙","/惊讶","/难过","/酷","/冷汗","/抓狂","/吐","/偷笑","/愉快","/白银","/傲慢","/饥饿","/困","/恐慌","/流汗","/憨笑","/悠闲","/奋斗","/咒骂","/疑问","/嘘","/晕","/疯了","/哀","/骷髅","/敲打","/再见","/擦汗","/抠鼻","/鼓掌","/糗大了","/坏笑","/左哼哼","/右哼哼","/哈欠","/鄙视","/委屈","/快哭了","/阴险","/亲亲","/吓","/可怜","/菜刀","/西瓜","/啤酒","/篮球","/乒乓","/咖啡","/饭","/猪头","/玫瑰","/凋谢","/嘴唇","/爱心","/心碎","/蛋糕","/闪电","/炸弹","/刀","/足球","/瓢虫","/便便","/月亮","/太阳","/礼物","/拥抱","/强","/弱","/握手","/胜利","/抱拳","/勾引","/拳头","/差劲","/爱你","/NO","/OK","/爱情","/飞吻","/跳跳","/发抖","/怄火","/转圈","/磕头","/回头","/跳绳","/投降","/激动","/乱舞","/献吻","/左太极","/右太极"]
                this.faceImg = ["http://www.onegreen.net/QQ/UploadFiles/201404/20140427102755304.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427102806118.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427102811204.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427102816272.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427102821779.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427102826616.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427102831909.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427102836860.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427102841446.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427102846605.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427102851155.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427102856815.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427102901326.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427102906485.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427102911867.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427102916775.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427102921112.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427102926579.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427102931107.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427102936174.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427102941562.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427102946241.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427102951305.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427102956983.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427103001341.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427103006230.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427103011620.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427103016770.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427103021180.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427103026333.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427103031826.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427103036856.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427103041851.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427103046204.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427103051515.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427103056368.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427103101584.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427103106639.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427103111714.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427103116995.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427103121307.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427103126628.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427103131537.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427103136922.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427103141708.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427103146409.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427103151786.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427103156150.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427103201493.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427103206968.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427103211938.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427103216563.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427103221494.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427103226447.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427103231181.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427103236908.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427103241610.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427103246942.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427103251889.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427103256822.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427103301730.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427103306315.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427103311631.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427103316218.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427103321727.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427103326466.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427103331391.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427103336293.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427103341727.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427103346519.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427103351983.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427103356315.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427103401393.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427103406284.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427103411342.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427103416889.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427103421513.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427103426896.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427103431815.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427103436286.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427103441411.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427103446541.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427103451461.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427103456248.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427103501861.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427103506188.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427103511436.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427103516890.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427103521415.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427103526248.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427103531694.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427103536789.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427103541535.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427103546800.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427103551956.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427103556647.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427103601489.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427103606571.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427103611997.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427103616555.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427103621939.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427103626894.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427103636123.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427103641405.png", "http://www.onegreen.net/QQ/UploadFiles/201404/20140427103646322.png"]
            }
            Chat.prototype = {
                init: function () {                                                                                 //---获取聊天记录
                    var that = this
                    if(this._getUrl_G('ccc') != 'ccc'){
                        if(this._getUrl_G('code') == 'nmamtf18565803458' && this.salesid != 224 ){
                            $("body").html('非法访问,请在微信端打开')
                        }
                    }   
                    
                    $.post("http://" + this.host + "/sale/chatlogList?salesId=" + this.salesid + "&page=" + this.fansListPage + "&pageSize=100", function (data) {
                         
                        if (data.code == 0) {
                            var total = data.data.length
                            var uniqueArr = that._unique(data.data)
                            uniqueArr.forEach(function(e){
                                var msg = (e.replytype < 5 ? e.reply : e.ask)
                                if(!msg){
                                    msg = e.reply || e.ask
                                }
                                
                                that.create_newChat_list({
                                    fansid: e.fansid,
                                    logo:e.logo,
                                    nickname:e.nickname,
                                    ask:msg,
                                    asktime:e.asktime
                                })
                                that.isLook({ fansid: e.fansid }, e.num)
                                that.create_newChat_info({fansid:e.fansid})
                                
                            })
                            console.log(total)
                            if(total ==100){
                                that.fansListPage = that.fansListPage + 1
                                $("#chatListBox").append("<li class='moreFansBox'><span data-page='"+ that.fansListPage +"'>获取更多</span></li>")
                            }
                        }
                        if (!data.data || data.data.length<1 && that.fansListPage == 1) {
                            $(".model_box").css("display", "flex")
                            $("#main").hide()
                        }
                        if(that.firstOpenChat && !!that.fansid && parseFloat(that.fansid) > 0){
                            that.fansInfo.id = that.fansid
                            that.fansInfo.name = $("[data-index='"+ that.fansid +"']").find(".name").text()
                            that.fansInfo.logo = $("[data-index='"+ that.fansid +"']").find('div>img').attr('src')
                            
                            that.openFansChat()
                        }
                    })

                    
                },
                connect: function () {                                                                              //---socket链接
                    var that = this
                    this.socket = new WebSocket(this.url)
                    this.socket.onopen = function () {
                        console.log("socket run..")
                    }
                    this.socket.onmessage = function (msg) {

                        var data = null
                        try {
                            data = JSON.parse(msg.data)
                            that.process(data)
                        } catch (error) {

                        }
                    }
                    this.socket.onclose = function () {
                        console.log("socket close..")
                    }
                },
                process: function (msg) {                                                                           //---处理socket发来的消息(逻辑控制层)
                    
                    
                    if (!!msg.imgurl2 || !!msg.reply && !!!msg.ask) {            //是否是自己发送的
                        this.create_newChat_(msg,"me")
                        this.updataChatList(msg)
                    } else {
                        var _isNew = this._isNewChat(msg)        //是否新会话
                        if (_isNew) {
                            // this.create_newChat_list(msg ,"sort")
                            // this.create_newChat_info(msg)
                            // this.create_newChat_(msg)
                            // this.isLook(msg)
                        } else {
                            this.create_newChat_(msg)
                            this.updataChatList(msg)
                            if (!this._isInThisPage(msg)) {
                                this.isLook(msg)
                            }
                        }
                    }
                    if (false) {
                        this.create_warn(msg)
                    }
                },
                isLook: function (msg, reset) {                                                                     //---处理未读标识( 用到了 localStorage )

                    var localData = localStorage.getItem("look")
                    var obj = {}
                    if (!!localData) {
                        obj = JSON.parse(localStorage.getItem("look"))
                    }

                    var count = obj["_" + msg.fansid] || 0
                    count++
                    if (reset != undefined) {
                        if (reset == "reset") {
                            count = 0
                        } else {

                            count = reset
                        }
                    }

                    obj["_" + msg.fansid] = count
                    for (x in obj) {

                        var a = x.substring(1, x.length)
                        if (a == msg.fansid) {
                            var _num = (obj[x] == 1? " " : obj[x])
                            $("[data-index='" + a + "']").find(".dot").text(_num)
                        }


                    }
                    var _json = JSON.stringify(obj)
                    localStorage.setItem("look", _json)
                    $("[data-index='" + a + "'] .dot").text()

                    if ($("[data-index='" + msg.fansid + "'] .dot").text() === "0") {
                        $("[data-index='" + msg.fansid + "'] .dot").hide()
                    } else {
                        $("[data-index='" + msg.fansid + "'] .dot").show()
                    }

                },
                create_newChat_list: function (msg,sort) {                                                          //---创建一个列表会话
                    if($("[data-index=" + msg.fansid + "]").length == 0){
                        var template = '<li layim-event="chat" data-type="history" onclick="aa()" data-index="' + msg.fansid + '" >' +
                            '<div>' +
                            '<img src=' + (msg.logo || "../res/images/default_user.png") + '>' +
                            '<span class="dot"></span>' +
                            '</div>' +
                            '<span class="name">' + (msg.nickname || "客户 : " + msg.fansid) + '</span>' +
                            '<span class="time" style="float:right;color:#999;font-size:14px;">' + this._toTimeText_G(msg.asktime) + '</span>' +
                            '<p class="contInfo">' + this.toFaceImg(msg.ask)  + '</p>' +
                            '<span class="layim-msg-status">new</span>' +
                            '</li>'
                        if(sort =="sort"){
                            $("#chatListBox").prepend(template)
                        }else{
                            $("#chatListBox").append(template)
                        }
                    }
                    
                    $(".model_box").hide()
                },
                create_newChat_list_query:function(msg){
                    var template = '<li class="queryFansItemBox" layim-event="chat" data-type="history" onclick="aa()" data-index="'+ msg.id +'">'+
                                        '<div><img class="userLogo" src="'+ (msg.logo || "../res/images/default_user.png") +'" alt=""></div>'+
                                        '<div class="content">'+
                                            '<p class="name">'+ (msg.nickname || "客户 : " + msg.fansid) +'</p>'+
                                            '<p>'+ this.toFaceImg(msg.lastAskMsg) +'</p>'+
                                        '</div>'+
                                        '<div>'+
                                            '<p class="lastTime">'+ this._toTimeText_G(msg.lastAskTime) +'</p>'+
                                        '</div>'+
                                    '</li>'
                    return template
                },
                create_newChat_info: function (msg) {                                                               //---创建一个对话会话    
                    if($('[data-fansid="' + msg.fansid + '"]').length == 0){
                        var template = '<div class="layim-chat-main layui-hide" data-fansid="' + msg.fansid + '" style="overflow-y:initial;bottom:50px;top:30px">' +
                            '<div style="width:100%;height:100%;overflow-y:scroll">'+
                            '<ul> ' +
                            '</ul>' +
                            '</div>'+
                            '</div>'
                        $("#chatInfoBox").append(template)
                    }            
                },
                create_warn: function (msg) {                                                                       //---创建一个警告提示消息
                    var template = '<li class="layim-chat-system">' +
                        '<span>' + msg.message + '</span>' +
                        '</li>'
                    
                    var _height = $("[data-fansid='" + msg.fansid + "']").find("ul").height()
                    if(msg.sort){
                        $("[data-fansid='" + msg.fansid + "']").find("ul").prepend(template)
                    }else{
                        $("[data-fansid='" + msg.fansid + "']").find("ul").append(template)
                        $("[data-fansid='" + msg.fansid + "']").find("div").scrollTop(_height)

                    }
                },
                create_getMore:function(msg){                                                                       //---创建一个拉取更多的按钮
                    var that = this
                    var template = '<li class="layim-chat-system postMore"  data-page="'+msg.page+'">' +
                        '<span>' + msg.message + '</span>' +
                        '</li>'
                    $("[data-fansid='" + msg.fansid + "']").find("ul").prepend(template)
                    
                },
                postMore:function(fansid,page){                                                                     //---拉取聊天数据
                    var that = this
                    $.post("http://" + this.host + "/sale/chatlogList?salesId=" + this.salesid + "&page=" + page + "&fansId=" + fansid +"&pageSize=50"  , function (data) {
                        var page_ = page+1
                        if(data.data.data){
                            var page_item = Math.ceil(data.data.count / 20)          //当前粉丝的聊天记录的总页数
                            
                            data.data.data.forEach(function (e, i) {
                                that.create_newChat_(e,"sort") 
                            });
                            if(page_item >= page_){
                                that.create_getMore({fansid:fansid,message:"查看更多",page:page_})
                            }else{
                                that.create_warn({fansid:fansid,message:"没有更多记录",sort:"sort"})
                            }
                        }
                    })
                },
                create_Img:function(msg){                                                                           //---创建一个图片标签并返回
                    var randomVal = Math.random().toString(36).substr(2);
                    var str = '<image src="'+ msg +'?'+ randomVal +'" class="userimg" alt="">'
                    return str
                },
                create_newChat_: function (msg,option) {                                                            //---插入一条消息
                    if(option == "me"){    // 服务器返回的销售的消息体(不用创建会话 , 而是改变状态)
                        this.clearState(msg)
                        
                        return false   
                    }
                    var template = this.msgTemplate(msg)           
                    if(option == "sort"){
                        $("[data-fansid='" + msg.fansid + "']").find("ul").prepend(template)
                    }else{
                        $("[data-fansid='" + msg.fansid + "']").find("ul").append(template)
                    }
                    showimg.bind()
                    var b_isMe = !(msg.replytype == 9 || msg.replytype == 11)       //是否是销售本人(Boolean )
                    if(!msg.replytype){
                        b_isMe = (!!msg.reply && !!!msg.ask)
                    }
                    if (b_isMe) {
                        
                        if (!$("#send").is(".layui-disabled")) {
                            $("#send").addClass("layui-disabled")
                        }
                    }
                    if(option != 'sort'){
                        var _height = $("[data-fansid='" + msg.fansid + "']").find("ul").height()
                        $("[data-fansid='" + msg.fansid + "']").find("div").scrollTop(_height)
                    }
                    

                },
                clearState:function(msg){                                                                           //---改变当条消息的状态
                    $("[data-msgid="+ msg.id +"]").html(this._toTime_G(msg.asktime))
                    if(msg.replytype == -1){
                        this.create_warn({
                            fansid:msg.fansid,
                            message: msg.reply || '消息处理异常,请刷新重试'
                        })      
                        $("[data-msgid="+ msg.id +"]").html('<i class="iconfont icon-weibiaoti-"></i>')
                        return false 
                    } 
                    if(msg.replytype == 45015){
                        this.create_warn({
                            fansid:msg.fansid,
                            message: "发送失败: 回应已超过48小时"
                        })      
                        $("[data-msgid="+ msg.id +"]").html('<i class="iconfont icon-weibiaoti-"></i>')
                        return false 
                    } 
                    if(msg.replytype == 45047){
                        this.create_warn({
                            fansid:msg.fansid,
                            message:"发送失败: 连续下发超过5条"    
                        })
                        $("[data-msgid="+ msg.id +"]").html('<i class="iconfont icon-weibiaoti-"></i>')      
                        return false 
                    }
                    if(msg.replytype == 40001 || msg.replytype == 40002 || msg.replytype == 40003 || msg.replytype == 48001 ){
                        this.create_warn({
                            fansid:msg.fansid,
                            message:"参数错误,请截图联系管理员--> " + msg.replytype
                        })     
                        $("[data-msgid="+ msg.id +"]").html('<i class="iconfont icon-weibiaoti-"></i>')
                        return false 
                    }
                },
                sendTimeOut:function(msg){                                                                          //---定时器判断是否发送超时 ( 10s ) 超时改变状态
                    setTimeout(function(){
                        if($("[data-msgid="+ msg.id +"]").find(".icon-loading").length >0){
                            $("[data-msgid="+ msg.id +"]").html('<i class="iconfont icon-weibiaoti-"></i>')
                        }
                        
                    },10000)
                },
                msgTemplate:function(msg){                                                                          //---创建一条消息模版
                    var that = this
                   

                    var b_isMe = !(msg.replytype == 9 || msg.replytype == 11)       //是否是销售本人(Boolean )    f废弃 (至判断是否有消息内容)
                    
                        b_isMe = (!!msg.reply && !!!msg.ask)
                    

                    var isMeClass = b_isMe ? "layim-chat-mine" : ""                 //是否是销售本人(ClassName)
                    var msg__ = b_isMe ? msg.reply : msg.ask                        //对应的选择消息
                    var logo = (function(){
                        var _logo = ""
                        if(msg.replytype == 1 || msg.replytype == 2 || msg.replytype == 3){
                            _logo = "../res/images/robot_logo.png"
                        }else{
                            _logo = (b_isMe ? '../res/images/vanke_logo.png' : (that.fansInfo.logo || "../res/images/default_user.png"))
                        }
                        return _logo
                    })()
                    
                    var name = (function(){
                        var _name = ""
                        if(msg.replytype == 1 || msg.replytype == 2 || msg.replytype == 3 ){
                            _name = "机器人"
                        }else{
                            _name = (b_isMe ? (msg.salename || "vanke" ): (that.fansInfo.name || "客户 : " + msg.fansid))
                        }
                        return _name
                    })()
                    if(msg.replytype == 9){
                        b_isMe = false
                        msg__ = this.create_Img(msg)
                    }else{
                        msg__ = this.toFaceImg(msg__)
                    }
                    var template = ""
                    if(!!msg.ask || !!msg.imgurl){
                        template += '<li class="layim-chat-li ">' +
                                '<div class="layim-chat-user" >' +
                                '<img src="' + (that.fansInfo.logo || "../res/images/default_user.png") + '" onclick="">' +
                                '<cite class="layim-user-box"><span class="username_box">' + (that.fansInfo.name || "客户 : " + msg.fansid) + '</span><span class="time_box">' + this._toTime_G(msg.asktime) + '</span></cite>' +
                                '</div>' +
                                '<div class="layim-chat-text">' + (!!msg.imgurl ? this.create_Img(msg.imgurl) : this.toFaceImg(msg.ask))  + '</div>' +
                                '</li>'
                        
                    }
                    if(!!msg.reply || !!msg.imgurl2){
                        template += '<li class="layim-chat-li layim-chat-mine">' +
                                '<div class="layim-chat-user">' +
                                '<img src="'+ (msg.replytype < 4 ? "../res/images/robot_logo.png" : "../res/images/vanke_logo.png") +'" onclick="">' +
                                '<cite class="layim-user-box"><span class="time_box" '+ (msg.id ? 'data-msgid='+ msg.id  : "") +'>' + (!!msg.asktime ? this._toTime_G(msg.asktime) : '<span class="rotateZAnimated"><i class="iconfont icon-loading"></i></span>') + '</span><span class="username_box">'+ (msg.replytype < 4 ? "机器人" : (msg.salename || "vanke" )) +'</span></span></cite>' +
                                '</div>' +
                                '<div class="layim-chat-text">' + (!!msg.imgurl2 ? this.create_Img("http://" + msg.imgurl2) : this.toFaceImg(msg.reply))  + '</div>' +
                                '</li>'
                    }
                    
                    
                    
                    
                    return template
                },
                updataChatList: function (msg) {                                                                    //---更新聊天列表( 时间 , 消息体)
                    var msg_ = (!!msg.reply && !!!msg.ask) ? msg.reply : msg.ask
                    $("[data-index='" + msg.fansid + "']").find(".contInfo").html(this.toFaceImg(msg_))
                    $("[data-index='" + msg.fansid + "']").find(".time").text(this._toTimeText_G(msg.asktime))
                    $("#chatListBox").prepend($("#chatListBox").find("[data-index='" + msg.fansid + "']").detach());
                },
                _isNewChat: function (msg) {                                                                        //---是否是新的会话
                    var _isNew = true
                    $("[data-fansid]").each(function () {
                        if ($(this).data("fansid") == msg.fansid) {
                            _isNew = false
                        }
                    })
                    return _isNew
                },
                _isInThisPage: function (msg) {                                                                     //---是否在当前聊天页
                    var _isThis = !$("[data-fansid='" + msg.fansid + "']").is(".layui-hide")
                    return _isThis
                },
                _unique:function(arr){                                                                              //---数组ID排重
                    var _arr = []
                    var _obj = {}
                    arr.forEach(function(e){
                        if(!_obj[e.fansid]){
                            _obj[e.fansid] = true
                            _arr.push(e)
                        }
                    })
                    return _arr
                },
                _toTimeText_G: function (str) {                                                                     //---时间转换函数
                    if (typeof (str) != "string" && typeof (str) != "number") {
                        console.log(str + ":No is a String")
                        return str
                    }
                    try {
                        if (new Date(str) == "Invalid Date") {
                            str = str.replace(/-/g, "/")
                        }
                        var timeStamp = new Date(str).getTime()

                    } catch (error) {
                        console.log("请传入正确的事件格式")
                        return str
                    }
                    var new_timeStamp = new Date().getTime()
                    var diff = new_timeStamp - timeStamp

                    diff = diff / 1000
                    if (diff < 0) {
                        console.log("超出当前日期")
                        // return str
                        return "刚刚"
                    }
                    if (diff < 3600) {
                        if (diff < 60) {
                            return "刚刚"
                        } else {
                            return Math.floor(diff / 60) + "分钟前"
                        }
                    } else if (diff < 86400) {

                        return Math.floor(diff / 3600) + "小时前"
                    } else if (diff < 864000) {
                        return Math.floor(diff / 86400) + "天前"
                    } else {
                        var __time = this._toTime_G(str)
                        return __time
                    }
                },  
                _toTime_G: function (v) {                                                                           //--- 转时间
                    var time = new Date(v)
                    var year = time.getFullYear()
                    var mon = time.getMonth() + 1;
                    var day = time.getDate();
                    var hour = time.getHours();
                    var min = time.getMinutes();
                    if (mon < 10) mon = "0" + mon
                    if (day < 10) day = "0" + day
                    if (hour < 10) hour = "0" + hour
                    if (min < 10) min = "0" + min
                    return mon + "-" + day + " " + hour + ":" + min
                },
                _getUrl_G: function(name) {                                                                         //---获取Url参数
                    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");  
                    var r = window.location.search.substr(1).match(reg);  
                    if (r != null) return unescape(r[2]); return null;  
                },
                _toLabelTag:function(_val){
                    _val = _val.replace(/ /g,"")
                    var str = ''
                    if(!!_val){
                        var _arr = _val.split('|')
                        _val.split('|').forEach(function(e){
                            str += '<span class="labelItem">'+ e +'</span>'
                        })
                        
                    }
                    return str
                },
                toFaceCode: function (msg) {                                                                        //---表情文字转代码
                    var _msg = msg
                    var reg = /\[.*?\]/g;
                    var some = reg.exec(_msg);                //匹配到的字段

                    while (some) {
                        var _some = some[0].replace(/\[/, "")
                        _some = _some.replace(/\]/, "")
                        var index = this.faceText.indexOf(_some)
                        msg = msg.replace(new RegExp(_some, 'g'), this.faceCode[index])
                        some = reg.exec(_msg);
                    }
                    msg = msg.replace(/\[/g, "")
                    msg = msg.replace(/\]/g, "")
                    return msg
                },
                toFaceImg: function (msg) {                                                                         //---表情文字转图片
                    var _msg = msg
                    var reg = /\[.*?\]/g;
                    var some = reg.exec(_msg);                //匹配到的字段
                    var isReset = false

                    while (some) {
                        var _some = some[0].replace(/\[/, "")
                        _some = _some.replace(/\]/, "")
                        var index = this.faceText.indexOf(_some)
                        if(index >-1){
                            msg = msg.replace(new RegExp(_some, 'g'), '<image src="'+ this.faceImg[index] +'" alt="">') 

                        }else{
                            isReset = "[" + _some + "]"
                        }
                        some = reg.exec(_msg);
                    }
                    var that = this
                    this.faceText2.forEach(function(e,i){
                        var reg2 = new RegExp(e,"g")
                        if(reg2.test(msg)){
                            msg = msg.replace(reg2,'<image src="'+ that.faceImg[i] +'" alt="">'   )

                        }
                    })
                    if(!!msg){
                        msg = msg.replace(/\[/g, "")
                        msg = msg.replace(/\]/g, "")
                        msg = msg.replace(/</g,"&lt;")
                        msg = msg.replace(/&lt;image/g,"<img")
                    }
                    
                    if(isReset){
                        return isReset
                    }else{
                        return msg
                    }
                    
                },
                resetCountMsg:function(id){                                                                         //---重置未读条数
                    $.post("../sale/cleanCatlogList?salesId=" + this.salesid + "&fansId=" + id, function (data) {
                        console.log(data)
                    })
                },
                openFansChat:function(){                                                                            //---是否直接跳转某粉丝对话
                    var that = this
                    if(!!this.fansid && parseFloat(this.fansid) > 0){
                        var id = this.fansid
                        that.firstOpenChat = false
                        that.create_newChat_info({fansid:id})
                        that.resetCountMsg(id)
                        that.isLook({ fansid: id }, "reset")

                        $("#chatBox").removeClass("layui-hide")               //显示对话
                        
                        $("#chatName").text(name)
                        $("#content").data("id", id)
                        var _height = $("[data-fansid='" + id + "']").find("ul").height()
                        $("[data-fansid='" + id + "']").find("div").scrollTop(_height)
                        if($("#chatBox").find("[data-fansid='" + id + "']").find("li").length < 1){
                            $.post("http://" + that.host + "/sale/chatlogList?salesId=" + that.salesid + "&page=1" + "&fansId=" + id + "&pageSize=50", function (data) {
                                if(data.code == 0){
                                    
                                    $("#send").data("salename",data.data.data[0].salename)
                                    var page_item = Math.ceil(data.data.count / 20)          //当前粉丝的聊天记录的总页数
                                    if(page_item > 1){
                                        that.create_getMore({fansid:id,message:"查看更多",page:2})
                                    }else{
                                        that.create_warn({fansid:id,message:"没有更多记录"})
                                    }

                                    data.data.data.reverse().forEach(function (e, i) {
                                        that.create_newChat_(e)
                                        if (i == data.data.data.length - 1) {
                                            that.updataChatList(e)
                                        }
                                    });
                                    
                                }
                            })
                        }
                        $("#chatBox").find("[data-fansid='" + id + "']").removeClass("layui-hide")
                        $.post("http://" + that.host + "/sale/checkChatStatus?salesId="+ that.salesid +"&fansId=" + id ,function(data){
                            if(data){
                                $(".noRobot").show()
                                $(".isRobot").hide()
                                $("#activationRobot").removeClass('on').attr('disabled',false).text('点击机器人托管')
                            }else{
                                $(".noRobot").hide()
                                $(".isRobot").show()
                                $("#activationRobot").addClass('on').attr('disabled',true).text('机器人托管中')
                            }
                        })

                    }
                },
                bind: function () {                                                                                 //---用户操作相关
                    var that = this

                    $(document).ready(function(){
                        setTimeout(function(){
                            $('.G_model').hide()
                        },500)
                    })

                    //点击消息列表
                    $(document).on("click", "[layim-event='chat']", function () {
                        var id = $(this).data("index")
                        var name = $(this).find(".name").text()
                        var logo = $(this).find('div>img').attr('src')
                        $("#queryFansBox").hide()
                        that.fansInfo = {
                            id:id,
                            name:name,
                            logo:logo
                        }
                        that.create_newChat_info({fansid:id})
                        that.resetCountMsg(id)
                        that.isLook({ fansid: id }, "reset")

                        $("#chatBox").removeClass("layui-hide")               //显示对话
                        
                        $("#chatName").text(name)
                        $("#content").data("id", id)
                        var _height = $("[data-fansid='" + id + "']").find("ul").height()
                        $("[data-fansid='" + id + "']").find("div").scrollTop(_height)
                        if($("#chatBox").find("[data-fansid='" + id + "']").find("li").length < 1){
                            that.show()
                            $.post("http://" + that.host + "/sale/chatlogList?salesId=" + that.salesid + "&page=1" + "&fansId=" + id + "&pageSize=50", function (data) {
                                if(data.code == 0){
                                    
                                    $("#send").data("salename",data.data.data[0].salename)
                                    var page_item = Math.ceil(data.data.count / 20)          //当前粉丝的聊天记录的总页数
                                    if(page_item > 1){
                                        that.create_getMore({fansid:id,message:"查看更多",page:2})
                                    }else{
                                        that.create_warn({fansid:id,message:"没有更多记录"})
                                    }

                                    data.data.data.reverse().forEach(function (e, i) {
                                        that.create_newChat_(e)
                                        if (i == data.data.data.length - 1) {
                                            that.updataChatList(e)
                                        }
                                    });
                                    
                                }
                                that.hide()
                            })
                        }
                        $("#chatBox").find("[data-fansid='" + id + "']").removeClass("layui-hide")
                        $.post("http://" + that.host + "/sale/checkChatStatus?salesId="+ that.salesid +"&fansId=" + id ,function(data){
                            if(data){
                                $(".noRobot").show()
                                $(".isRobot").hide()
                                $("#activationRobot").removeClass('on').attr('disabled',false).text('点击机器人托管')
                            }else{
                                $(".noRobot").hide()
                                $(".isRobot").show()
                                $("#activationRobot").addClass('on').attr('disabled',true).text('机器人托管中')
                            }
                        })
                    })
                    //点击返回按钮
                    $("#backChat").click(function () {
                        $("#chatBox").addClass("layui-hide")
                        $("#chatBox").find("[data-fansid]").removeClass("layui-hide").addClass("layui-hide")
                        $("#content").val("")
                        var id = $("#content").data("id")
                        that.resetCountMsg(id)
                        that.init()
                    })

                    //点击发送按钮
                    $("#send").click(function () {
                        if ($(this).is(".layui-disabled")) {
                            return false
                        }
                        
                        var msg = $("#content").val()
                        $("#faceBox").slideUp(100)
                        var msgLocal = msg
                        msg = that.toFaceCode(msg)
                        var id = $("#content").data("id")
                        var _ask = G_isAsk
                        var _salename = $(this).data("salename") || "vanke"
                        var timeId = ""+new Date().getTime()
                        timeId = timeId.substring(4,timeId.length)
                        timeId = parseFloat(timeId)
                        var _obj = {
                            "fansid": id,
                            "ask": "",
                            "reply": msgLocal,
                            "salesid": that.salesid,//销售id
                            "salename": _salename,
                            "id": timeId,
                            "askfrom": _ask,
                            "readed": true,
                            "replytype":4
                        }
                        that.create_newChat_(_obj)
                        that.sendTimeOut(_obj)
                        _obj.reply = msg
                        _obj = JSON.stringify(_obj)
                        that.socket.send(_obj)
                        $("#content").val("")
                    })

                    //监听按键事件
                    $("#content").keyup(function (e) {
                        var msg = $(this).val()
                        msg = msg.replace(/ /g, "")
                        if (msg.length < 1) {
                            if (!$("#send").is(".layui-disabled")) {
                                $("#send").addClass("layui-disabled")
                            }
                        } else {
                            $("#send").removeClass("layui-disabled")
                        }
                        if (e.which == 13) {
                            $("#send").click()
                        }
                    })

                    //点击输入框事件
                    $("#content").click(function(){
                        var that = this
                        setTimeout(function () {
                            that.scrollIntoView(true);
                            var id = $(that).data("id")
                            var _height = $("[data-fansid='" + id + "']").find("ul").height()
                            $("[data-fansid='" + id + "']").find("div").scrollTop(_height)
                            document.body.scrollTop = document.body.scrollHeight;
                        }, 300);
                        $("#faceBox").slideUp(100)
                    })
                    //点击呼出表情事件
                    $(".icon-xiaolian").click(function (e) {
                        e.stopPropagation()
                        $("#faceBox").slideDown(100)
                    })
                    //点击表情model隐藏
                    $("#faceBox").click(function () {
                        $("#faceBox").slideUp(100)
                    })
                    //点击单个表情
                    $("ul.layui-layim-face li").click(function (e) {
                        e.stopPropagation()
                        var oldVal = $("#content").val()
                        oldVal += $(this).attr("title")
                        $("#content").val(oldVal)
                        console.log($(this))

                        if (oldVal.length < 1) {
                            if (!$("#send").is(".layui-disabled")) {
                                $("#send").addClass("layui-disabled")
                            }
                        } else {
                            $("#send").removeClass("layui-disabled")
                        }
                    })

                    //人工接入
                    $("#killRobot").click(function(){
                        var id = $("#content").data("id")
                        $.post("http://" + that.host + "/sale/checkoutStatus?salesId="+ that.salesid +"&fansId=" + id ,function(data){
                            if(data){
                                that.create_warn({fansid:id,message:"接入成功"})
                                $("#killRobot").hide()
                                $(".noRobot").show()
                                $('#activationRobot').attr('disabled',false).removeClass('on').text('点击机器人托管')
                            }
                        })
                        
                    })

                    //机器人托管
                    $('#activationRobot').click(function(){
                        var id = $("#content").data("id")
                        $.post("http://" + that.host + "/sale/checkoutStatus?salesId="+ that.salesid +"&fansId=" + id ,function(data){
                            if(!data){
                                that.create_warn({fansid:id,message:"万小二接入成功"})
                                $("#killRobot").show()
                                $(".noRobot").hide()
                                $('#activationRobot').attr('disabled',true).addClass('on').text('机器人托管中')
                            }
                        })
                    })

                    //查看更多粉丝列表
                    $(document).on('click','.moreFansBox span',function(){
                        that.init()
                        $(this).parent().remove()
                    })

                    //查看更多聊天记录
                    $(document).on("click",".postMore",function(){
                        var page = $(this).data("page")
                        var fansid = $(this).parents("[data-fansid]").data("fansid")
                        that.postMore(fansid,page)
                        $(this).remove()
                        
                    })

                    //展开功能盒子
                    $(".icon-guanbi").click(function(){
                        var isOpen = $(this).is('.on')
                        if(isOpen){
                            $(this).removeClass('on')
                            $(this).parent().find(".posStyle").css("display","none")
                        }else{
                            $(this).addClass('on')
                            $(this).parent().find(".posStyle").css("display","block")
                        }
                    })
                    //关闭功能盒子
                    $(".posStyle").click(function(){
                        $(".posStyle").css("display","none")
                        $(".icon-guanbi").removeClass("on")
                    })

                    //发送小程序卡片的盒子视图控制 - 显示
                    $(".icon-fasonghongbaocopy").click(function(){
                        $("#floorListCard").css("display","flex")
                    })
                    //发送小程序卡片的盒子视图控制 - 隐藏
                    $("#floorListCard .closeBox .icon-guanbi1").click(function(){
                        $("#floorListCard").hide()
                    })
                    //发送小程序卡片事件
                    $("#floorListCard li span").click(function(){
                        var fansid = $("#content").data("id")
                        var floorid = $(this).data("id")
                        var floorName = $(this).parent().text().replace(/发送/,"")
                        $("#floorListCard").hide()
                        $.get("http://" + that.host + "/sale/sendProudct",{"productId":floorid,"fansId":fansid },function(data){
                            if(data.code == 0){
                                that.create_warn({
                                    fansid:fansid,
                                    message:"已发送卡片:" + floorName
                                })
                            }else if(data.code == 45015){
                                that.create_warn({
                                    fansid:fansid,
                                    message:"发送卡片失败: 回应超时"
                                })
                            }else if(data.code == 45047){
                                that.create_warn({
                                    fansid:fansid,
                                    message:"发送卡片失败: 连续下发条数超限"
                                })
                            }else{
                                that.create_warn({
                                    fansid:fansid,
                                    message:"发送卡片失败: " + data.code
                                })
                            }
                            
                        })
                    })

                    //上传照片
                    $.up({
                        el:".icon-xiangji",
                        url:"../upload/UploadImg",
                        success:function(data){   
                            var id = $("#content").data("id")
                            var _ask = G_isAsk
                            var _salename = $("#send").data("salename") || "vanke"
                            var timeId = ""+new Date().getTime()
                            timeId = timeId.substring(4,timeId.length)
                            timeId = parseFloat(timeId)
                            var _obj = {
                                "fansid": id,
                                "ask": "",
                                "reply": "",
                                "id": timeId,
                                "imgurl2": location.host + data.data ,
                                "salesid": that.salesid,//销售id
                                "salename": _salename,
                                "askfrom": _ask,
                                "readed": true,
                                "replytype":10
                            }
                            that.create_newChat_(_obj)
                            _obj = JSON.stringify(_obj)
                            that.socket.send(_obj)
                        }
                    })

                    //触发搜索粉丝
                    $('#queryFansBox .icon-duihao').click(function(){
                        var val = $('#queryFansBox input').val()
                        that.show()
                        $.post('http://' + that.host + '/sale/portrait/search',{
                            saleId:that.salesid,
                            keywork:val
                        },function(data){
                            try {
                                if (data.code == 0) {
                                    var str = ""
                                    data.data.forEach(function(e){
                                        str += that.create_newChat_list_query(e)
                                    })
                                    $(".queryFansResult ul").html(str)
                                    $(".queryFansResult div").show()
                                }
                                if (!data.data || data.data.length<1 && that.fansListPage == 1) {
                                    alert('没有记录')
                                }
                            } catch (error) {
                                that.hide()
                            }
                            that.hide()
                        })
                    })
                    $(document).on('click','#queryFansBox .queryLabelsBox .labelsList span',function(){
                        var val = $(this).text()
                        that.show()
                        $.post('http://' + that.host + '/sale/portrait/search',{
                            saleId:that.salesid,
                            keywork:val
                        },function(data){
                            try {
                                if (data.code == 0) {
                                    var str = ""
                                    data.data.forEach(function(e){
                                        str += that.create_newChat_list_query(e)
                                    })
                                    $(".queryFansResult ul").html(str)
                                    $(".queryFansResult div").show()
                                }
                                if (!data.data || data.data.length<1 && that.fansListPage == 1) {
                                    alert('没有记录')
                                }
                            } catch (error) {
                                that.hide()
                            }
                            that.hide()
                        })
                    })
                    
                    //进入搜索
                    $(".layui-title-query").click(function(){
                        $("#queryFansBox").fadeIn()
                        //初始话搜索界面的用户标签
                        $.post('http://' + that.host + '/sale/tags/'+ that.salesid,function(data){
                            if(data.code == 0){
                                var str = ''
                                data.data.forEach(function(e){
                                    str += '<span onclick="">'+ e +'</span>'
                                })
                                    
                                
                                $('#queryFansBox .queryLabelsBox .labelsList').html(str)
                            }
                        })
                    })

                    //进入粉丝编辑界面
                    $(document).on('click','.layim-chat-user img',function(){
                        $("#fansInfoBox").show()
                        that.show()
                        $.get('http://' + that.host + '/sale/fansPortrait/'+ that.fansInfo.id +'?saleId=' + that.salesid,function(data){
                            try {
                                if(data.code == 0){
                                
                                    var _data = data.data.fans
                                    $('#fansInfo_id').text(_data.id)
                                    $('#fansInfo_logo').attr("src",_data.logo)
                                    $('#fansInfo_add').text(_data.formattedAddress)
                                    $('#fansInfo_nickname').text(_data.nickname)
                                    
                                    $('#fansInfo_parentId').text(data.data.saleId)
                                    $("#fansInfo_upDate").text(that._toTime_G(data.data.updateDate || data.data.createDate))
                                    $('#fansInfo_note').text(!!data.data.remarkInfo ? data.data.remarkInfo : "")
                                    $('#fansInfo_labels').html(that._toLabelTag(data.data.tag))
                                    $('#fansInfo_name').val(data.data.remarkName)
                                    $('#fansInfo_tel').val(data.data.phone)
                                }
                                that.hide()
                            } catch (error) {
                                that.hide()
                            }
                        })
                    })

                    //点击编辑标签(传值给标签编辑页)
                    $('#editLabelsFn').click(function () {
                        $('#fansInfoBox').find('.fansInfoBox_label').fadeIn()
                        var str = ''
                        $(this).find('span.labelItem').each(function(){
                            str += '<span>' + $(this).text()  + '</span>'
                            
                        })
                        $('#fansInfoBox .userTags').html(str)

                        $.post('http://' + that.host + '/sale/tags/'+ that.salesid,function(data){
                            if(data.code == 0){
                                var _str = ''
                                data.data.forEach(function(e){
                                        var isClass = ""
                                        $('#editLabelsFn span.labelItem').each(function(){
                                            var _val = $(this).text()
                                            if(_val == e  ){
                                                isClass = "unSelect"
                                            } 
                                        })
                                        _str += '<span class="'+ isClass +'" onclick="">'+ e +'</span>'
                                    })
                                    
                                
                                $('#fansInfoBox .tagHistory .tagListBox').html(_str)
                            }
                        })
                    })

                    //保存粉丝信息
                    $("#saveLabelFn").click(function () {
                        var labels = ''
                        $("#editLabelsFn").find('span.labelItem').each(function () {
                            labels += $(this).text() + '|'
                        })
                        labels = labels.substring(0, labels.length - 1)
                        var tel = $(".telValue").val()
                        var note = $("#fansInfoBox").find('textarea').val()
                        var name = $('#fansInfoBox').find('.nameValue').val()
                        console.log(labels + "\n" + tel + "\n" + note + '\n' + name)
                    
                        
                        if(!!tel){
                            if(!(/^1[34578]\d{9}$/.test(tel))) {
                                alert("手机号码有误,请重填");
                                return false;
                            }
                        }
                        $.post('http://' + that.host + '/sale/updatePortrait/'+ that.fansInfo.id,{
                            saleId:that.salesid,
                            phone:tel,
                            remarkName:name,
                            remarkInfo:note,
                            tag:labels
                        },function(data){
                            console.log(data)
                            that.hide()
                        })
                        $("#fansInfoBox").hide()
                    })
                    
                    $("#closeLabelFn").click(function(){
                        $("#fansInfoBox").hide()
                    })

                    


                    


                   
                    





                    

                    
                },
                show:function(){
                    $('.G_model').css('display','flex')
                },
                hide:function(){
                    $('.G_model').hide()
                }
            }

            function Showimg(el) {
                this.el = el
            }
			
			Showimg.prototype = {
				getitems: function(el) { //获取items数组对象
					var list = document.querySelectorAll(el)
					var items = []
					for(i = 0; i < list.length; i++) {
						var obj = {}
						obj.src = list[i].src
						obj.h = list[i].naturalHeight
						obj.w = list[i].naturalWidth
						items.push(obj)
					}
					return items
				},
				ceratePhoto: function(i) { //初始化新的相册框
					var pswpElement = document.querySelectorAll('.pswp')[0];
					var items = this.getitems(this.el);
					var options = {
						history: false,
						focus: false,
						index: i,
						showAnimationDuration: 0,
						hideAnimationDuration: 0
					};
					gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, options);
					gallery.init()
				},
				bind: function() { //绑定click事件
					var that = this
					$(document).off("click", this.el)
					$(document).on("click", this.el, function() {
						var index = $(this).index(that.el)
						that.ceratePhoto(index)
					})
					$(this.el).click(function() {})
				}
			}
			
			var showimg = new Showimg(".userimg")
			showimg.bind()

            var chat = new Chat()
            chat.init()
            chat.bind()
            chat.connect()
            // chat.openFansChat()
            function aa(){ }