Skip to content
  • P
    Projects
  • G
    Groups
  • S
    Snippets
  • Help

libai / guodong

  • This project
    • Loading...
  • Sign in
Go to a project
  • Project
  • Repository
  • Issues 0
  • Merge Requests 0
  • Pipelines
  • Wiki
  • Snippets
  • Members
  • Activity
  • Graph
  • Charts
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
  • Files
  • Commits
  • Branches
  • Tags
  • Contributors
  • Graph
  • Compare
  • Charts
Switch branch/tag
  • guodong
  • gzh
  • customer
  • index.html
Find file
Normal viewHistoryPermalink
index.html 22 KB
Newer Older
libai's avatar
测试
3d59d510
 
libai committed 4 years ago
1 2 3 4
<!doctype html>
<html>
<head>
<meta charset="utf-8">
libai's avatar
测试
0a71d2b3
 
libai committed 4 years ago
5
<title> </title>
libai's avatar
测试
3d59d510
 
libai committed 4 years ago
6 7 8 9 10 11 12 13 14 15 16 17
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
<meta name="format-detection" content="telephone=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<style>
    html,body{
        width: 100%;
        height: 100%;
        margin: 0;
        padding: 0;
    }
libai's avatar
测试
261cf160
 
libai committed 4 years ago
18 19 20
    input[type="button"],button{
        -webkit-appearance: none;
    }
libai's avatar
测试
846f4120
 
libai committed 4 years ago
21 22 23 24
    a{
        color: #428bca;
        text-decoration:none;
    }
libai's avatar
测试
3d59d510
 
libai committed 4 years ago
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
    .cs-box{
        width: 100%;
        height: 100%;
        margin: 0;
        padding: 0;
        display: flex;
        flex-direction: column;
        position: relative;
        overflow: hidden;
    }
    .cs-box h4{
        text-align: center;
        height: 30px;
        line-height: 30px;
        width: 100%;
        background: #229ffd;
        color: rgb(255, 255, 255);
        margin: 0;
    }
    .cs-chat{
        overflow-y: auto;
        padding: 10px;
        height: 380px;
        word-wrap: break-word;
        background-color: #f3f5f7;
        flex: 1 1 auto;
    }
    .cs-submit{
        width: 100%;
        background-color:gainsboro;
        flex: 0 0 auto;
        display: flex;
        justify-content: space-around;
        padding: 0.266667rem;
        position: relative;
    }
    .txt{
        display:flex;
        -webkit-box-sizing: border-box;
        box-sizing: border-box;
        border: 1px solid #d7d7d7;
        border-radius: 0.16rem;
        overflow: hidden;
        position: relative;
        background:#fcfafa;
        width: 80%;
    }
    .btn{
        display: flex;
        position: relative;
    }
    .txt .content{
        outline: none;
        border: none;
        background: none;
libai's avatar
测试
846f4120
 
libai committed 4 years ago
80
        width: 95%;
libai's avatar
测试
3d59d510
 
libai committed 4 years ago
81 82 83 84 85 86 87 88 89
        min-height: 0.9rem;
        padding: 0.266667rem 0 0.133333rem 0;
        line-height: 0.9rem;
        white-space: pre-wrap;
        overflow-x: hidden;
        overflow-y: scroll;
        font-size: 18px;
        resize: none;
        color: #353535;
libai's avatar
测试
846f4120
 
libai committed 4 years ago
90
        display: inline-block;
libai's avatar
测试
3d59d510
 
libai committed 4 years ago
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
    }
    .btn input{
         padding: 0.25rem 1rem;
        border-radius: 5px;
        box-sizing: border-box;
        background-color: #428bca;  
        color: #fff; 
        text-align: center;  
        vertical-align: middle;  
        border: 1px solid transparent;  
        font-weight: 500;  
        margin-right: 8px;
        outline: none;
    }
    .txt .uploadimg{
        width: 2rem;
        height: 1.9rem;
        margin:0.1rem;
        border-radius: 10%;
        background: url(img/img.png) no-repeat;
        background-size: contain;
        text-align: center;
        box-sizing: border-box;
        border: 1px solid transparent;
libai's avatar
测试
846f4120
 
libai committed 4 years ago
115
        display: inline-block;
libai's avatar
测试
3d59d510
 
libai committed 4 years ago
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
        background-color: #eee;  
    }
    .txt .uploadimg input{
        opacity: 0;
        width: 100%;
        height: 100%;
    }
    .chat-box{
        position: relative;
        width: 100%;
        margin: 5px 0;
    }
    .info{
        display: inline-block;
        width:2.2rem;
        height:2.2rem;
        line-height: 2.2rem;
        overflow: hidden;
        vertical-align: top;
        font-size: 13px;
        background-color: #229ffd;
        border-radius: 50%;
        text-align: center;
        color: #fff;
        padding: 0.1rem;
        margin-right:6px;
    }
    .chat-text{
        display: inline-block;
        vertical-align: top;
        position: relative;
        padding: 0.4rem 0.3rem;
        max-width: 80%;
        white-space: pre-wrap;
        border: 1px solid #DEDEDE;
        background-color: #fff;
        border-radius: 4px;
        box-sizing: border-box;
        color: #252525;
    }
    .service{
        text-align: left;
    }
    .custom{
        text-align: right;
      
    }
    .custom .chat-text{
        margin-right:6px;
        background-color: #A5DE37;
        color: #fff;
    }
    .service-text:before {
        content: '';
        position: absolute;
        top: 50%;
        left: -4px;
        width: 5px;
        height: 5px;
        background: #fff;
        border-top: 1px solid #DEDEDE;
        border-left: 1px solid #DEDEDE;
        -webkit-transform: rotate(-45deg);
        transform: rotate(-45deg);
    }
    #custom .chat-text:after {
        content: '';
        position: absolute;
        top: 50%;
        right: -10px;
        width: 0;
        height: 0;
        border-top: 6px solid transparent;
        border-bottom: 6px solid transparent;
        border-left: 10px solid #A5DE37;
        -webkit-transform: translate(0, -50%);
        transform: translate(0, -50%);
    }
libai's avatar
测试
261cf160
 
libai committed 4 years ago
194 195 196 197 198 199 200 201 202 203 204 205
    .copyright{
        width: 100%;
        height:1rem;
        line-height: 1rem;
        background-color:gainsboro;
        flex: 0 0 auto;
        display: flex;
        position: relative;
        text-align: center;
        font-size: 10px;
        color:#353535;
    }
libai's avatar
测试
3d59d510
 
libai committed 4 years ago
206 207 208
    .system{
        text-align: center;
        color:gray;
libai's avatar
测试
846f4120
 
libai committed 4 years ago
209
        font-size: 13px;
libai's avatar
测试
3d59d510
 
libai committed 4 years ago
210 211 212 213 214 215 216 217 218
        margin: 10px 0;
    }
    .funbtn{
        text-align: center;
        width: 50%;
        height: 1.5rem;
        line-height: 1.5rem;
        font-size: 15px;
        margin-left: 25%;
libai's avatar
测试
846f4120
 
libai committed 4 years ago
219 220
        text-decoration: underline;
        color:#229ffd;
libai's avatar
测试
3d59d510
 
libai committed 4 years ago
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
    }

    .tips{
        position: absolute;
        width: 100%;
        height: 1.5rem;
        line-height: 1.5rem;
        top: -1.5rem;
        left: 0;
        font-size: 12px;
        padding-left: 0.4rem;
    }
    .warn{
        background:rgba(254,174,27, 0.8);
        color:#fff;
    }
    .error{
        background:rgba( 255,67,81, 0.8);
        color:#fff;
    }
    .normal{
        background:rgba(27,154,247, 0.8);
        color:#fff;
    }
libai's avatar
测试
846f4120
 
libai committed 4 years ago
245 246 247 248
    .des{
        background:rgb(81, 206, 255);
        color:#fff;
    }
libai's avatar
测试
3d59d510
 
libai committed 4 years ago
249 250 251 252 253 254 255
</style>
</head>
<body>
<div class="cs-box">
        <h4>客服聊天<span id="state"></span></h4>
        <div id="cs-chat" class="cs-chat">
            <div class="chat-box service" id="help">
libai's avatar
测试
2d4e7275
 
libai committed 4 years ago
256
                <div class="info des">客服</div>
libai's avatar
测试
3d59d510
 
libai committed 4 years ago
257 258 259 260 261 262 263 264 265 266 267 268 269
                <span class="chat-text service-text">您好,有什么需要帮助的吗?</span>
            </div>
        </div>
        <div class="cs-submit">
             <div class="txt">
                <textarea id="content" class="content" placeholder="请输入您的问题"></textarea>  
                <div class="uploadimg">
                    <input accept="image/*" type="file"  id="img" onchange="uploadimg()" />
                </div>
             </div>
             <div class="btn">
                <input type="button" value="发送" id="send"/>
             </div>
libai's avatar
测试
846f4120
 
libai committed 4 years ago
270
             <div class="tips" id="tips" hidden="hidden"></div>
libai's avatar
测试
3d59d510
 
libai committed 4 years ago
271
        </div>
libai's avatar
测试
261cf160
 
libai committed 4 years ago
272 273 274
        <div class="copyright">
            
       </div>
libai's avatar
测试
3d59d510
 
libai committed 4 years ago
275 276 277
    <input type="text" hidden="hidden" id="guestid">
</div>
<script>
libai's avatar
测试
95c99acc
 
libai committed 4 years ago
278
    var server="http://liveauth.ishuaji.cn";
libai's avatar
测试
3d59d510
 
libai committed 4 years ago
279 280 281 282
    var cdn="https://app.ixiulive.com/";
    var ws;
    var checkTimer;
    var sendtime=0;
libai's avatar
测试
846f4120
 
libai committed 4 years ago
283
    var map = new Map();
libai's avatar
测试
0a71d2b3
 
libai committed 4 years ago
284
    var recOk=true;
libai's avatar
测试
3d59d510
 
libai committed 4 years ago
285 286
    $(function(){	
		btninit();
libai's avatar
测试
846f4120
 
libai committed 4 years ago
287
        params();
libai's avatar
测试
3d59d510
 
libai committed 4 years ago
288
	});
libai's avatar
测试
846f4120
 
libai committed 4 years ago
289 290
    function params(){
        var url = window.location.search;
libai's avatar
测试
4a546331
 
libai committed 4 years ago
291
        var ua = window.navigator.userAgent.toLowerCase();
libai's avatar
测试
a483459f
 
libai committed 4 years ago
292 293 294 295 296
        if(ua.match(/MicroMessenger/i) == 'micromessenger'){
            map.set('ua','wechat');
        }else{
            map.set('ua','other');
        }
libai's avatar
测试
4b10a8cf
 
libai committed 4 years ago
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
        map.set('pkg','wechat');
        map.set('cid','wechat');
        map.set('gzh','yabo');
        // if (url.indexOf("?") != -1) {
        //     var p = url.substr(1);
        //     keys = p.split("&");
        //     for (i = 0; i < keys.length; i++) {
        //         map.set(keys[i].split("=")[0],keys[i].split("=")[1]);
        //         if("guestid"===keys[i].split("=")[0]){
        //             setCookie('cs-guestid',keys[i].split("=")[1],3);
        //         }
        //     }
        // }       
    }
  
libai's avatar
测试
3d59d510
 
libai committed 4 years ago
312
    function btninit(){
libai's avatar
测试
4b10a8cf
 
libai committed 4 years ago
313
        connect(false);
libai's avatar
测试
3d59d510
 
libai committed 4 years ago
314 315 316
        $("#send").click(function(){
            sendmsg();
        });
libai's avatar
测试
4b10a8cf
 
libai committed 4 years ago
317 318 319 320 321 322 323 324
        // $("#img").click(function(){
        //     var guestid=$("#guestid").val();
        //     if(null==guestid||guestid==''){
        //         //连接客服
        //         system('正在转接客服,请稍等');
        //         connect(false);
        //     }
        // });
libai's avatar
测试
3d59d510
 
libai committed 4 years ago
325 326 327 328 329 330 331 332 333
    }

    function sendmsg(){
        var content=$("#content").val();
        if(null==content||content==''){
            tips("不可以发送空消息","error",5000);
            return;
        }
        if(content.length>120){
libai's avatar
测试
0a71d2b3
 
libai committed 4 years ago
334 335
            tips("内容2超过最大限制","warn",5000);
            return;2
libai's avatar
测试
2b841706
 
libai committed 4 years ago
336
        }
libai's avatar
测试
3d59d510
 
libai committed 4 years ago
337 338 339 340
        var guestid=$("#guestid").val();
        if(null==guestid||guestid==''){
            //连接客服
            system('正在转接客服,请稍等');
libai's avatar
测试
261cf160
 
libai committed 4 years ago
341
            connect(true);
libai's avatar
测试
3d59d510
 
libai committed 4 years ago
342 343 344 345 346
        }else{
            send(content,2001) 
        }
    }

libai's avatar
测试
261cf160
 
libai committed 4 years ago
347
    function connect(send){
libai's avatar
测试
3d59d510
 
libai committed 4 years ago
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
        var guestid=getCookie('cs-guestid');
        $.ajax({
				url:server+"/guest/service",
				data:{guestid:guestid},
				dataType:"json",
				type:"POST",
				success:function(data){
					if(data.code==1){
						guestid=data.guestid;
                        $("#guestid").val(guestid);
                        setCookie('cs-guestid',guestid,3)
                        var list=data.history;
                        if(list!=null&&list.length>0&&sendtime==0){
                            $("#help").remove();
                            $.each(list,function(i,msg){
                                historybulid(msg.mtype,msg.content,msg.fid,msg.mid);
                                if(i==list.length-1){
libai's avatar
测试
846f4120
 
libai committed 4 years ago
365
                                    system('---上次聊到这里('+msg.sendtime+')---');
libai's avatar
测试
3d59d510
 
libai committed 4 years ago
366 367 368 369 370
                                    $("#cs-chat").append('<div class="system">---------------</div>');
                                }
                            });
                        }
                        if(data.more){
libai's avatar
测试
846f4120
 
libai committed 4 years ago
371
                            $("#cs-chat").prepend('<div class="funbtn" onclick="more()">更早的聊天记录</div>');
libai's avatar
测试
3d59d510
 
libai committed 4 years ago
372
                        }
libai's avatar
测试
261cf160
 
libai committed 4 years ago
373
                        WebSocketStart(guestid,send);
libai's avatar
测试
3d59d510
 
libai committed 4 years ago
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
					}else{
                        system("连接失败("+data.code+")");
					}
				},
				error:function(){
                    system("网络异常");
				}
	        });	
    }

    function more(){
        var guestid=$("#guestid").val();
        var index= $("#cs-chat").children(".chat-box").eq(0).attr("data-mid");
        $.ajax({
				url:server+"/guest/chathistory",
				data:{guestid:guestid,index:index},
				dataType:"json",
				type:"POST",
				success:function(data){
					if(data.code==1){
                        $("#cs-chat").find(".funbtn").remove();
                        var list=data.history;
                        if(list.length>0){
                            $.each(list,function(i,msg){
                                historybulid(msg.mtype,msg.content,msg.fid,msg.mid);
                                if(i==list.length-1){
libai's avatar
测试
846f4120
 
libai committed 4 years ago
400
                                    $("#cs-chat").prepend('<div class="funbtn" onclick="more()">更早的聊天记录</div>');
libai's avatar
测试
3d59d510
 
libai committed 4 years ago
401 402 403 404 405 406
                                }
                                if(i==0){
                                    $("#cs-chat").prepend('<div class="system">'+msg.sendtime+'</div>');
                                }
                            });
                        }else{
libai's avatar
测试
846f4120
 
libai committed 4 years ago
407
                            $("#cs-chat").prepend('<div class="system">哦豁,没有了</div>'); 
libai's avatar
测试
3d59d510
 
libai committed 4 years ago
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
                        }
					}else{
                        $("#cs-chat").prepend('<div class="system">获取失败('+data.code+')</div>');
					}
				},
				error:function(){
                    $("#cs-chat").prepend('<div class="system">服务出错</div>');
				}
	        });	
    }


    function system(msg){
       var sysmsg='<div class="system">'+msg+'</div>';
       var cname= $("#cs-chat").children("div:last-child").attr("class");
       if(cname=='system'){
          $("#cs-chat").children("div:last-child").text(msg);
       }else{
libai's avatar
测试
846f4120
 
libai committed 4 years ago
426 427 428
            var chat=$("#cs-chat");
            chat.append(sysmsg);
            var offset=chat[0].scrollHeight-chat[0].clientHeight;
libai's avatar
测试
0a71d2b3
 
libai committed 4 years ago
429
            if(offset-chat[0].scrollTop<(500)){
libai's avatar
测试
846f4120
 
libai committed 4 years ago
430 431 432 433 434
                chat.animate({scrollTop: chat.scrollHeight}, 500);
            }else{
                tips("有新的系统消息","normal",3000);
            }
        }
libai's avatar
测试
3d59d510
 
libai committed 4 years ago
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
    }
    function state(msg){
        $("#state").text("("+msg+")");
    }
    function tips(msg,type,time){
        if(time>0){
            $("#tips").removeClass("error warn normal").addClass(type).text(msg).show().delay(time).hide(300);
        }else{
            $("#tips").removeClass("error warn normal").addClass(type).text(msg).show();
        }
    }
    function removetips(){
        $("#tips").hide();
    }
    function msgbulid(type,msg,kf,fid){
        var content="";
libai's avatar
测试
846f4120
 
libai committed 4 years ago
451
        var size=200;
libai's avatar
测试
3d59d510
 
libai committed 4 years ago
452
        if(fid>0){
libai's avatar
测试
846f4120
 
libai committed 4 years ago
453
            if(fid==1){
libai's avatar
测试
2d4e7275
 
libai committed 4 years ago
454
                content+='<div class="chat-box service"><div class="info des">客服</div><span class="chat-text service-text">';
libai's avatar
测试
846f4120
 
libai committed 4 years ago
455
            }else{
libai's avatar
测试
2d4e7275
 
libai committed 4 years ago
456
                content+='<div class="chat-box service"><div class="info">客服</div><span class="chat-text service-text">';
libai's avatar
测试
846f4120
 
libai committed 4 years ago
457
            }
libai's avatar
测试
3d59d510
 
libai committed 4 years ago
458 459 460 461 462 463
        }else{
             content+='<div class="chat-box custom"><span class="chat-text ">';
        }
        if(type==1){
            content+=msg+'</span></div>';
        }else{
libai's avatar
测试
846f4120
 
libai committed 4 years ago
464 465 466 467 468
            if(fid>0){
                msg+='?x-oss-process=image/resize,p_95';
            }else{
                msg+='?x-oss-process=image/resize,l_'+size+',m_mfit';
            }
libai's avatar
测试
3d59d510
 
libai committed 4 years ago
469 470
            content+='<img width="200px" src="'+cdn+msg+'"></span></div>';
        }
libai's avatar
测试
846f4120
 
libai committed 4 years ago
471 472 473 474 475 476 477 478 479 480 481 482 483
        var chat=$("#cs-chat");
        chat.append(content);
        var offset=chat[0].scrollHeight-chat[0].clientHeight;
        if(offset-chat[0].scrollTop<(chat[0].clientHeight/2)){
            chat.animate({scrollTop: chat.scrollHeight}, 1000);
        }else{
            if(fid>0){
                tips("收到一条新消息","normal",3000);
            }
        }
        if(type==2){
            system('长按图片即可放大或保存到手机查看');
        }
libai's avatar
测试
3d59d510
 
libai committed 4 years ago
484 485 486 487
    }

    function historybulid(type,msg,fid,mid){
        var content="";
libai's avatar
测试
846f4120
 
libai committed 4 years ago
488
        var size=200;
libai's avatar
测试
3d59d510
 
libai committed 4 years ago
489
        if(fid>0){
libai's avatar
测试
846f4120
 
libai committed 4 years ago
490
             content+='<div class="chat-box service" data-mid="'+mid+'"><div class="info des">客服</div><span class="chat-text service-text">';
libai's avatar
测试
3d59d510
 
libai committed 4 years ago
491 492 493 494 495 496
        }else{
             content+='<div class="chat-box custom" data-mid="'+mid+'"><span class="chat-text ">';
        }
        if(type==1){
            content+=msg+'</span></div>';
        }else{
libai's avatar
测试
846f4120
 
libai committed 4 years ago
497 498 499 500 501
            if(fid>0){
                msg+='?x-oss-process=image/resize,p_95';
            }else{
                msg+='?x-oss-process=image/resize,l_'+size+',m_mfit';
            }
libai's avatar
测试
3d59d510
 
libai committed 4 years ago
502 503 504 505 506 507
            content+='<img width="200px" src="'+cdn+msg+'"></span></div>';
        }
        $("#cs-chat").prepend(content);
    }


libai's avatar
测试
0a71d2b3
 
libai committed 4 years ago
508

libai's avatar
测试
261cf160
 
libai committed 4 years ago
509
    function WebSocketStart(guestid,send) {
libai's avatar
测试
4b10a8cf
 
libai committed 4 years ago
510
          
libai's avatar
测试
3d59d510
 
libai committed 4 years ago
511 512 513 514 515 516 517 518
             state("连接中");
             if(ws!=null&&ws.readyState == WebSocket.OPEN){
                system("The socket is opening,please close first!");
                return;
             }
             if ("WebSocket" in window)
             {
                // 打开一个 web socket
libai's avatar
测试
95c99acc
 
libai committed 4 years ago
519
                var url="ws://118.178.112.116:21901/ws?guestid="+guestid+"&type=5";
libai's avatar
测试
3d59d510
 
libai committed 4 years ago
520 521 522 523 524 525 526 527
                try {
                     ws = new WebSocket(url);
                  }catch(err){
                     console.error("WebSocket connect error:", err);
                     system("连接失败");
                  }
                ws.onopen = function()
                {
libai's avatar
测试
4b10a8cf
 
libai committed 4 years ago
528
                    recOk=true;
libai's avatar
测试
3d59d510
 
libai committed 4 years ago
529 530
                   state("连接成功");
                   system("客服已接入");
libai's avatar
测试
261cf160
 
libai committed 4 years ago
531 532
                   if(send){
                    sendmsg();
libai's avatar
测试
2b841706
 
libai committed 4 years ago
533 534
                   }else{
                    $("#guestid").val(guestid);
libai's avatar
测试
261cf160
 
libai committed 4 years ago
535
                   }
libai's avatar
测试
3d59d510
 
libai committed 4 years ago
536 537 538 539 540 541 542
                   checkTimer=setInterval("check()",60000); 
                };
                ws.onmessage = function (evt) 
                { 
                     var msg=JSON.parse(evt.data);
                     if(msg.uid!='0'){
                        msgbulid(msg.mtype,msg.content,msg.kfName,msg.fid);
libai's avatar
测试
846f4120
 
libai committed 4 years ago
543
                        sendtime=new Date().getTime();
libai's avatar
测试
3d59d510
 
libai committed 4 years ago
544 545 546 547
                     }
                };
                ws.onclose = function(event)
                { 
libai's avatar
测试
4b10a8cf
 
libai committed 4 years ago
548
               
libai's avatar
测试
0a71d2b3
 
libai committed 4 years ago
549 550
                 
                  
libai's avatar
测试
3d59d510
 
libai committed 4 years ago
551
                   state("连接断开");
libai's avatar
测试
9fdde34c
 
libai committed 4 years ago
552 553
                   $("#guestid").val('');
                   tips("连接已断开","error",3000);
libai's avatar
测试
4b10a8cf
 
libai committed 4 years ago
554 555 556 557 558
                   if(recOk){
                        var guestid=$("#guestid").val();
                        WebSocketStart(guestid,false)
                        recOk=false;
                    }
libai's avatar
测试
0a71d2b3
 
libai committed 4 years ago
559
                 
libai's avatar
测试
3d59d510
 
libai committed 4 years ago
560 561 562 563 564
                   console.error("WebSocket close observed:", event);
                };
                ws.onerror = function(event)
                { 
                   // 关闭 websocket
libai's avatar
测试
0a71d2b3
 
libai committed 4 years ago
565
                   if(recOk){
libai's avatar
测试
4b10a8cf
 
libai committed 4 years ago
566 567 568
                        var guestid=$("#guestid").val();
                        WebSocketStart(guestid,false)
                        recOk=false;
libai's avatar
测试
0a71d2b3
 
libai committed 4 years ago
569
                    }
libai's avatar
测试
3d59d510
 
libai committed 4 years ago
570
                   state("连接错误");
libai's avatar
测试
846f4120
 
libai committed 4 years ago
571
                   msgbulid(1,"会话意外断开,发送消息即可联系客服","系统",1);
libai's avatar
测试
3d59d510
 
libai committed 4 years ago
572 573 574 575 576 577 578 579 580
                   console.error("WebSocket error observed:", event);
                };
             } else{
                // 浏览器不支持 WebSocket
                state("不支持");
             }
   }
   function check(){
        if(sendtime>0){
libai's avatar
测试
8f82bc56
 
libai committed 4 years ago
581 582 583 584
            var min=3;
            var cut=2;
            var time=new Date().getTime()-sendtime;
            if(time>1000*60*min&&time<1000*60*min+1){
libai's avatar
测试
3d59d510
 
libai committed 4 years ago
585 586
                var index= $(".chat-box").last().attr("class").indexOf("service");
                if(index>-1){
libai's avatar
测试
8f82bc56
 
libai committed 4 years ago
587
                       msgbulid(1,"客服还没有等到您的回复,系统将在"+cut+"分钟后关闭会话","系统",1);
libai's avatar
测试
3d59d510
 
libai committed 4 years ago
588 589 590 591 592 593 594 595
                }
            }
            if(new Date().getTime()-sendtime>1000*60*(min+cut)){
                var index= $(".chat-box").last().attr("class").indexOf("service");
                if(index>-1){
                       msgbulid(1,"因您长时间未回复,本次会话已经中止,如果还有问题需要咨询,请直接发送消息","系统",1);
                       checkTimer=null;
                       sendtime=-1;
libai's avatar
测试
0a71d2b3
 
libai committed 4 years ago
596
                       recOk=false
libai's avatar
测试
307168ec
 
libai committed 4 years ago
597 598 599 600
                       var guestid=$("#guestid").val();
                       map.set("guestid",guestid);
                       var msg=new WsMsg(2100,'',JSON.stringify(mapToObj(map)));
                       ws.send(JSON.stringify(msg));
libai's avatar
测试
3d59d510
 
libai committed 4 years ago
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
                       $("#guestid").val('');
                       ws.close();
                }
            }
        }
   }
   function send(message,cid) {
		  if (!window.WebSocket) { return; }
		  if (ws.readyState == WebSocket.OPEN) {
              var guestid=$("#guestid").val();
              map.set("guestid",guestid);
              var msg=new WsMsg(cid,message,JSON.stringify(mapToObj(map)));
              ws.send(JSON.stringify(msg));
              if(cid==2001){
                msgbulid(1,message,'',0)
              }else{
                msgbulid(2,message,'',0)
              }
              $("#content").val('');
              sendtime=new Date().getTime();
		  } else {
			tips("连接已断开","error",3000);
            state("连接断开");
		  }
	}
   function  mapToObj(strMap){
        let obj= Object.create(null);
        for (let[k,v] of strMap) {
        obj[k] = v;
        }
        return obj;
    }
    function WsMsg(cid,content,extend) {
            this.time = new Date().getTime();
            this.msgid=Math.random().toString(36).slice(-8);
            this.cid = cid;
            this.content = content;
            this.extend = extend;
            this.toUid=2000;
    }
    function uploadimg(){
        var file=$("#img")[0].files;
        if(file.length<1){
            return;
        }
        if(file.length>1){
            tips("每次只可选择一个图片","error",3000);
            return;
        }
        if(file[0].size>10485760){
            tips("超过限制,最大支持10M的图片","error",5000);
            return;
        }
        var guestid=$("#guestid").val();
        var formdata = new FormData();
        formdata.append("img",file[0]);
        formdata.append("guestid",guestid);
        var url=server+"/guest/upload/img";
        var xhr = new XMLHttpRequest();
        xhr.upload.addEventListener("progress", uploadProgress, false);
        xhr.open("post", url, true);
        xhr.onload = function () {
libai's avatar
测试
a483459f
 
libai committed 4 years ago
663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680
			if(xhr.status==200){
				var result = xhr.responseText;  
				var json = eval("(" + result + ")");  
				switch (json.code) {
				case 1:
				   // var path=cdn+;
					send(json.imgkey,2002);
					$("#img").val('');
					break;							
				default:
					system(json.msg);
					$("#img").val('');
					break;
				}
			}else{
				system(xhr.status);
				tips("上传发生错误"+xhr.status,"error",3000);
			}
libai's avatar
测试
3d59d510
 
libai committed 4 years ago
681
        }; 
libai's avatar
测试
a483459f
 
libai committed 4 years ago
682 683 684
		xhr.onerror = function(e) {
			tips("上传发生错误"+e,"error",3000);
		};
libai's avatar
测试
3d59d510
 
libai committed 4 years ago
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
        xhr.send(formdata);
    }


    function uploadProgress(evt) {
        if (evt.lengthComputable) {
            var percentComplete = Math.round(evt.loaded * 100 / evt.total);
            tips("上传中("+percentComplete+"%)","normal");
            if(percentComplete==100){
                tips("上传完成","normal",3000);
            }
        }
        else {
            tips("上传失败","error",3000);
        }
   }
	function setCookie(cname, cvalue, exdays) {
	    var d = new Date();
	    d.setTime(d.getTime() + (exdays*24*60*60*1000));
	    var expires = "expires="+d.toUTCString();
	    document.cookie = cname + "=" + cvalue + "; " + expires;
   }
	function getCookie(cname) {
	    var name = cname + "=";
	    var ca = document.cookie.split(';');
	    for(var i=0; i<ca.length; i++) {
	        var c = ca[i];
	        while (c.charAt(0)==' ') c = c.substring(1);
	        if (c.indexOf(name) != -1) return c.substring(name.length, c.length);
	    }
	    return "";
   }
   function clearCookie(name) {  
    setCookie(name, "", -1);  
   }  

</script>
</body>
</html>