robotMobile.ejs 10.6 KB
<!DOCTYPE html>
<html>
  <head>
    <title> 小爱外呼机器人</title>
    <meta http-equiv="X-UA-Compatible" content="IE=10"> 
    <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"> 
    <script type="text/javascript" src="/js/jquery-1.9.1.js"></script> 
    <script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
    <script src="https://cdn.bootcss.com/moment.js/2.22.1/moment.min.js"></script>
    <style type="text/css"> 
      .bootStrapShadow{
        padding: 16px;
      }
      .row_wrap{
        margin: 8px 0;
      }
      .verticalAlign{
        vertical-align:top;
      }
    </style> 
  </head>
  <body class="bootStrapShadow">
     <h2>
     呼出历史
    </h2>
    <div class="text-right row_wrap">
        <button  class="btn btn-primary" data-toggle="modal" data-target="#addMobileModal">添加外呼号码</button>
        <button  class="btn btn-primary" data-toggle="modal" data-target="#myModal">新建呼出</button>
    </div>
     <div class="table-responsive" id="grid">
      <table class="table table-bordered">
        <thead>
          <tr>
            <th>呼出手机号</th>
            <th>呼出时间</th>
            <th>状态</th>
            <th>操作</th> 
          </tr>
        </thead>
        <tbody id="grid-body">
          <tr> 
            <td>15510382586</td>
            <td>2015-08-30 01:02:00</td>
            <td>呼出完成</td>
            <td>
              <button type="button" class="btn btn-link">回放</button>
              <button type="button" class="btn btn-link"  data-toggle="modal" data-target="#viewModal">查看</button>  
            </td> 
          </tr> 
        </tbody>
      </table>
    </div>
    <!--新建呼出-->
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"> 
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <h4 class="modal-title" id="myModalLabel">新建呼出</h4>
          </div>
          <div class="modal-body">
              <form>
                <div class="form-group" id="mobile-select">
                  <label for="mobile">手机号</label>
                  <input type="mobile" class="form-control" name="call_out_number" id="call_out_number" placeholder="手机号">
                </div>
                <div class="form-group">
                  <label for="scene">场景</label>
                  <select class="form-control">
                    <option>外呼场景</option> 
                  </select> 
                </div>
                  
              </form>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
            <button type="button" class="btn btn-primary" id="myModalOk">确认</button>
          </div>
        </div>
      </div>
    </div>
    <!--查看呼出-->
    <div class="modal fade" id="viewModal" tabindex="-1" role="dialog" > 
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <h4 class="modal-title" id="viewModalLabel">查看</h4>
          </div>
          <div class="modal-body" id="viewModal-body">
              <address>
                <strong>机器人:</strong> 1355 Market Street, Suite 900
                <br> 
              </address>
              <address>
                <strong>机器人:</strong> 1355 Market Street, Suite 900
                <br> 
              </address>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button> 
          </div>
        </div>
      </div>
    </div> 
    <!--添加外呼手机号-->
    <div class="modal fade" id="addMobileModal" tabindex="-1" role="dialog" > 
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <h4 class="modal-title" id="addMobileModalLabel">添加外呼号码</h4>
          </div>
          <div class="modal-body" id="addMobileModal-body">
              <form>
                <div class="form-group">
                  <label for="mobile">姓名</label>
                  <input type="mobile" class="form-control" name="call_name" id="call_name" placeholder="姓名">
                </div> 
                <div class="form-group">
                  <label for="mobile">手机号</label>
                  <input type="mobile" class="form-control" name="add_call_out_number" id="add_call_out_number" placeholder="手机号">
                </div>  
              </form>  
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">取消</button> 
            <button type="button" class="btn btn-primary" id="addMobileModalOk">提交</button>
          </div>
        </div>
      </div>
    </div> 
    <script> 
      //刷新列表
      function loadGrid(){
        $.ajax({
          type: "GET",
          url: "/demo/api/call_tasks?limit=10000&offset=0",
          dataType: "json",
          contentType :'application/json',
          data: JSON.stringify({}),
          success: function(data){
            var items = data.items||[],temp=[];
            var status ={
              '0':'未呼出',
              '1':'正在通话',
              '2':'通话失败',
              '3':'通话完成'
            }
            for(i=0;i<items.length;i++){
              var item= items[i]; 
              temp.push('<tr>');
              temp.push('<td>'+item.call_out_number+'</td>');
              temp.push('<td>'+(item.call_out_time>0?moment(item.call_out_time*1000).format('YYYY-MM-DD HH:mm'):'')+'</td>');     
              temp.push('<td>'+(status[item.status]?status[item.status]:' ')+'</td>');
              temp.push('<td>');
              if(item.record_url){      
                temp.push('<audio src="'+item.record_url+'" controls="controls">');    
                temp.push('你的游览器不支持播放格式');    
                temp.push('</audio>');    
              }  
              temp.push('<a class="btn btn-link verticalAlign show-detail" data-id="'+item.id+'"  >查看</a> ');   
              temp.push('</td>');
              temp.push('</tr>');  
            }
            $('#grid-body').html(temp.join(""));
          }
        });
      }
      //获取外呼号码列表
      function loadMobiles(){
        $.ajax({
          type: "GET",
          url: "/demo/api/contact?limit=10000&offset=0",
          dataType: "json",
          contentType :'application/json',
          data: JSON.stringify({}),
          success: function(data){
            var items = data.items||[],temp=[]; 
            temp.push('<label for="scene">手机号</label>');
            temp.push('<select class="form-control" name="call_out_number" id="call_out_number">');
            for(i=0;i<items.length;i++){
              var item= items[i];  
              temp.push('<option value="'+item.tel+'">'+item.tel+'</option> ');     
            }
            temp.push('</select>');
            $('#mobile-select').html(temp.join(""));
          }
        });
      }
      //新建外呼
      $('#myModalOk').click(function () { 
        var mobile =$('#call_out_number').val();
        if(mobile&&/^(19[0-9]|16[0-9]|17[0-9]|13[0-9]|14[0-9]|15[0-9]|18[0-9])\d{8}$/i.test(mobile)){
          $.ajax({
            type: "POST",
            url: "/demo/api/call_tasks",
            dataType: "json",
            contentType :'application/json',
            data: JSON.stringify({
              "call_out_number":mobile,
              "scene":"呼叫场景DEMO"
            }),
            success: function(data){ 
              if(data.id){
                $('#myModal').modal('hide');
                loadGrid();
              }else{
                 alert(data.message)
              }
            }
          });
        }else{
          alert('请输入正确的手机号!')
        } 
      });
      //查看详情
      $(document).on('click','.show-detail',function () { 
          var id=$(this).data("id");
          $.ajax({
            type: "GET",
            url: "/demo/api/call_tasks/"+id+"/record_texts",
            dataType: "json",
            contentType :'application/json',
            data: '',
            success: function(data){  
              var temp=[];
              var items = data.items||[];
              for(i=0;i<items.length;i++){
                var item= items[i]; 
                temp.push('<address>');
                temp.push('<strong>'+(item.speaker?item.speaker=='robot'?'机器人':'接听者':'')+'</strong>'); 
                temp.push( item.speaked_at>0?'<small style="margin-left: 8px;color: #999;">'+moment(item.speaked_at*1000).format('YYYY-MM-DD HH:mm')+'</small>':'' );
                temp.push('<br>');  
                temp.push( item.content?item.content:'' );
                temp.push('<br>');   
                temp.push('</address>');   
              }
              $('#viewModal-body').html(temp.join(""));
              $('#viewModal').modal('show');
            }
          });
      });
      //添加手机号码
      $('#addMobileModalOk').click(function(){
        var mobile =$('#add_call_out_number').val();
        var name =$('#call_name').val();
        if(mobile&&/^(19[0-9]|16[0-9]|17[0-9]|13[0-9]|14[0-9]|15[0-9]|18[0-9])\d{8}$/i.test(mobile)){
          $.ajax({
            type: "POST",
            url: "/demo/api/contact",
            dataType: "json",
            contentType :'application/json',
            data: JSON.stringify({
              "tel":mobile,
              "name": name
            }),
            success: function(data){ 
              if(data.id){
                $('#addMobileModal').modal('hide');
                loadMobiles();
              }else{
                 alert(data.message)
              }
            }
          });
        }else{
          alert('请输入正确的手机号!')
        } 
      });
      //
      loadGrid();
      loadMobiles();
    </script>
  </body>
</html>